FAQ
Hi,

I try to solve the following problem and I cannot figure out the right
combination of OpenFile arguments.

I am writing a cp clone with a twist:

When I copy a directory hierarchy into another, I _never_ want to overwrite
files. If a file with the exact name already
exists, I want to rename the new file with a new suffix, incrementing a
number.

E.g.

fileA shall be copied to a folder, but fileA is already there, so the new
fileA is renamed to fileA.001 (of course
assuming that fileA.001 does not exist.)

So, how do I open the new fileA for writing, such that it will fail if it
already exists?

Kind regards
Stefan



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Konstantin Kulikov at Sep 19, 2014 at 9:15 am
    Use os.Stat() first.
    On Fri, Sep 19, 2014 at 1:13 PM, Ondekoza wrote:
    Hi,

    I try to solve the following problem and I cannot figure out the right
    combination of OpenFile arguments.

    I am writing a cp clone with a twist:

    When I copy a directory hierarchy into another, I _never_ want to overwrite
    files. If a file with the exact name already
    exists, I want to rename the new file with a new suffix, incrementing a
    number.

    E.g.

    fileA shall be copied to a folder, but fileA is already there, so the new
    fileA is renamed to fileA.001 (of course
    assuming that fileA.001 does not exist.)

    So, how do I open the new fileA for writing, such that it will fail if it
    already exists?

    Kind regards
    Stefan



    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Govert Versluis at Sep 19, 2014 at 10:44 am

    On Friday, September 19, 2014 11:15:50 AM UTC+2, Konstantin Kulikov wrote:
    Use os.Stat() first.
    That's inherently racy though. A file could be created between your os.Stat
    call and the OpenFile call.
    This will probably not matter in 99.99% of the cases but still worth
    pointing out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Sep 19, 2014 at 9:16 am
    On Fri, Sep 19, 2014 at 11:13 AM, Ondekoza wrote:

    http://golang.org/pkg/os/#pkg-constants: O_EXCL int = syscall.O_EXCL
       // used with O_CREATE, file must not exist

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 19, '14 at 9:13a
activeSep 19, '14 at 10:44a
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase