UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-01-05 06:13 AM

Gowrisubha.R
Member
Registered: 2005-11-14
Posts: 9

Re: Any system call is available for copying a file in unix

Hi,

Is there any system call for copying a file from one directory to another in unix? I searched in unistd.h, there is no such system call, I can find system call for rename a file in unistd.h but there is no system call for copy a file.

I don't want to use system("cp -f file1 file2") or a c program to read line by line from one file and write it on another file.

Pls. let me know the system call or any other method to copy.
Thanks
Gowrisubha

Offline

#2 2006-01-05 09:41 AM

duncang
Member
From: UK
Registered: 2004-04-30
Posts: 30

Re: Any system call is available for copying a file in unix

Offline

#3 2006-01-05 09:45 AM

duncang
Member
From: UK
Registered: 2004-04-30
Posts: 30

Re: Any system call is available for copying a file in unix

Offline

#4 2006-01-05 02:01 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Any system call is available for copying a file in unix

If your particular OS has a sendfile() function, you should be able to use that to
perform a file copy... You'll have to do the opening of the respective files, but the
data transfer would be handled for you, at least...

However, even if you had to roll your own, it's not that difficult: just a simple loop,
doing fixed read()'s/fread()'s from one file, and write()'s/fwrite()'s to the other...

Offline

#5 2006-01-05 05:38 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Any system call is available for copying a file in unix

Offline

#6 2006-01-05 08:51 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: Any system call is available for copying a file in unix

That's weird that they changed sendfile() like that...  I've definitely used it on 2.4.x
to do simple file-to-file copies before, so it certainly works there...  Why on Earth
would they require the destination to be a socket now??  I can understand the
restriction on the source being a file (though, it'd be nice if that went away, too),
but I don't get the requirement for the destination to be a socket...  Anyone know
the reasoning behind that limitation??

But, yeah, sendfile() is certainly not portable, in any sense...  So, yeah, you'd
probably be better off with a simple read+write loop, if you care about portability
at all...  (Sometimes, I consciously choose not to care about portability, and
deliberately use nifty system-specific features, though... ;-))

And, yeah, I remember that previous thread...  I think it was Nope...  And, if I
recall, he managed to get superior performance from sendfile() if he switched to
doing it via multiple sendfile() calls with smaller chunks, rather than do the whole
file in a single call...  Which indicates to me a very screwy implementation; it should
do such optimizations internally, I'd think, if they're needed...  So, in the end, it
wound up being not much easier to code than a standard read+write loop; though,
you got the zero-copy benefits, at least...  Probably not really worth the hassle, in
general...  (Especially if they've really broken the 2.6.x sendfile() so you can't
output to a file... ;-/)

Offline

#7 2006-01-05 10:27 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: Any system call is available for copying a file in unix

Offline

Board footer

Powered by FluxBB