Copy file using io.Pipe

1,142 views
Skip to first unread message

Archos

unread,
Aug 2, 2012, 2:30:48 PM8/2/12
to golan...@googlegroups.com
I wanted to use io.Pipe to avoid to use a variable with all content of a file into memory.
I've tried this:

    pr, pw := io.Pipe()

    content, err := ioutil.ReadFile(FILE)
    _, err = pr.Read(content)

but the problem is that Read needs a []byte so I would have to use an intermediate variable to get it, avoiding the advantage that I was looking for.


Is possible copy a file using Pipe but without to use an intermediate variable with the content of the file?

Rémy Oudompheng

unread,
Aug 2, 2012, 2:34:37 PM8/2/12
to Archos, golan...@googlegroups.com
Why not:

src, err := os.Open("source")
frobnicate(err)
dest, err := os.Create("destination")
frobnicate(err)
err := io.Copy(dest, src)
frobnicate(err)

Rémy

Archos

unread,
Aug 2, 2012, 2:50:36 PM8/2/12
to golan...@googlegroups.com, Archos
perfect!

Or to copy it into a temporary file:

    dest, err := ioutil.TempFile("", "destination")

Kyle Lemons

unread,
Aug 2, 2012, 5:34:24 PM8/2/12
to Rémy Oudompheng, Archos, golan...@googlegroups.com
I'll point out that this isn't equivalent to something like the "cp" command, which attempts to also copy the permissions, owner, timestamps, etc.  Those are also doable with relative ease if you require them.

Archos

unread,
Aug 2, 2012, 5:42:36 PM8/2/12
to golan...@googlegroups.com, Rémy Oudompheng
I've forgotten that I've built a function to copy preserving permissions:

srcFile, err := os.Open("source")
handle(err)

srcInfo, err := os.Stat("source")
handle(err)

dstFile, err := os.OpenFile("dest", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode().Perm())
handle(err)

_, err = io.Copy(dstFile, srcFile)
handle(err)

Kyle Lemons

unread,
Aug 2, 2012, 6:12:01 PM8/2/12
to Archos, golan...@googlegroups.com, Rémy Oudompheng
On Thu, Aug 2, 2012 at 2:42 PM, Archos <raul...@sent.com> wrote:
I've forgotten that I've built a function to copy preserving permissions:

srcFile, err := os.Open("source")
handle(err)

srcInfo, err := os.Stat("source")
handle(err)

dstFile, err := os.OpenFile("dest", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode().Perm())
handle(err)

Opening a file with those permissions makes them subject to your process's umask.  Using syscall.Chmod seems like a more likely way to actually get the correct permissions.

Archos

unread,
Aug 2, 2012, 6:19:09 PM8/2/12
to golan...@googlegroups.com, Archos, Rémy Oudompheng

El jueves, 2 de agosto de 2012 23:12:01 UTC+1, Kyle Lemons escribió:
On Thu, Aug 2, 2012 at 2:42 PM, Archos <raul...@sent.com> wrote:
I've forgotten that I've built a function to copy preserving permissions:

srcFile, err := os.Open("source")
handle(err)

srcInfo, err := os.Stat("source")
handle(err)

dstFile, err := os.OpenFile("dest", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode().Perm())
handle(err)

Opening a file with those permissions makes them subject to your process's umask.  Using syscall.Chmod seems like a more likely way to actually get the correct permissions.
Right. Anyway, I could be wrong but I believe that syscall.Chmod also is affected by the user mask so the safest would be to use `syscall.Umask(0)` before of the function that depends of umask.
 

Kyle Lemons

unread,
Aug 2, 2012, 6:23:54 PM8/2/12
to Archos, golan...@googlegroups.com, Rémy Oudompheng
The umask only affects the file creation mode.  See man 2 umask
 

Archos

unread,
Aug 2, 2012, 6:31:10 PM8/2/12
to golan...@googlegroups.com

Ok, but it could be used before of the function that opens the file:

syscall.Umask(0)

dstFile, err := os.OpenFile("dest", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcInfo.Mode().Perm())

The bad point is that I'm supposed that to use syscall.Umask would be invalid in windows system, would not be?
Reply all
Reply to author
Forward
0 new messages