Roy
--
"The tragedy of life doesn't lie in not reaching your goal. The tragedy lies
in having no goal to reach." -- Benjamin Mays
>I'm trying to write a program to reorganize files on an Ubuntu Linux
>8.04 box. I have it mostly working but I can't find a function to
>move files. Since mv is a common function in both GUI and CLI there
>is probably a function to do this. Does anyone know what that
>function (or functions) is? It could probably be done with a call to
>the system, but you don't learn anything that way. Any help or
>pointers on where to look appreciated.
rename(2) ?
--
Chris.
It is somewhat more complicated than that: rename works only within a
particular filesystem/ block device. This means that the file has to
be copied and the original removed afterwards if rename fails with
EXDEV.
> I'm trying to write a program to reorganize files on an Ubuntu Linux
> 8.04 box. I have it mostly working but I can't find a function to
> move files. Since mv is a common function in both GUI and CLI there
> is probably a function to do this. Does anyone know what that
> function (or functions) is? It could probably be done with a call to
> the system, but you don't learn anything that way. Any help or
> pointers on where to look appreciated.
High-level abstraction: rename(2)/renameat(2) ("man 2 rename")
Low-level abstraction(s): link(2)/linkat(2), symlink(2)/symlinkat(2),
unlink(2), open(2)/creat(2), read(2), write(2),
close(2)
--
Lew Pitcher
Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
rename(2) is a system call which changes the name pointing to a
particular file with the limitation that it doesn't work accross file
system boundaries. It is not some type of 'abstraction' build onto
what you refer to as 'lower level' above.
> I'm trying to write a program to reorganize files on an Ubuntu Linux
> 8.04 box. I have it mostly working but I can't find a function to
> move files. Since mv is a common function in both GUI and CLI there
> is probably a function to do this. Does anyone know what that
> function (or functions) is? It could probably be done with a call to
> the system, but you don't learn anything that way. Any help or
> pointers on where to look appreciated.
rename(2) will work if the source and destination are on the same file
system. If they are not, you have to manually read the contents of the
source and write them to the destination, fix up permissions,
timestamps, etc, yourself (which can be tricky, especially if your
system has fancy features like ACLs), and finally delete (with
unlink(2)) the source file. If you are moving a directory, you have to
create the new one, recursively move its contents, and then remove the
old one. Non-regular files like FIFOs, devices, symlinks, etc, require
more manual labor.
mv(1) does all this. AFAIK there is no standard or commonly available C
function that does this, so if you want something that just works,
calling mv(1) via system(3) is probably the way to go.
On my system, this code is shared between mv and cp (mv calls cp in some
cases), and totals 1408 lines. So it's non-trivial.
Given that Unixish systems /started with/ link(2) and unlink(2), and did not
have a rename(2), the rename(2) syscall abstracts the syscall pattern of
{if (link(oldpath, newpath)) unlink(oldpath);} into a single syscall. Thus,
to me, rename(2) is a higher-level abstraction than link(2) and unlink(2).
> It is not some type of 'abstraction' build onto what you refer to
> as 'lower level' above.
See my comment above.
It doesn't. rename(2) is an atomic operation, a link/ unlink sequence
isn't. And we are not discussing 'functionality missing in ancient
versions of UNIX(*)' but 'Linux', which always had a rename system call.
> Thus, to me, rename(2) is a higher-level abstraction than link(2)
> and unlink(2).
Rename(2) operates at exactly the same level as link(2) and unlink(2)
do, and provides functionality (atomic rename) not available
otherwise. All three are independent operations and the 'usual'
definition of 'higher-level abstraction' is not 'somewhat similar but
newer'.
>I'm trying to write a program to reorganize files on an Ubuntu Linux
>8.04 box. I have it mostly working but I can't find a function to
>move files. Since mv is a common function in both GUI and CLI there
>is probably a function to do this. Does anyone know what that
>function (or functions) is? It could probably be done with a call to
>the system, but you don't learn anything that way. Any help or
>pointers on where to look appreciated.
>
> Roy
Thanks to all. I actually found rename() this morning after a bit of
a sleep. Shows you should quit this stuff when you're tired. :o)
I normally find man files somewhat obtuse, but the explanation there
in this case made more sense to me than the glibc docs.
--
"Suicide is man's way of telling God, 'You can't fire me - I quit.'"
-- Bill Maher
> I'm trying to write a program to reorganize files on an Ubuntu Linux
> 8.04 box. I have it mostly working but I can't find a function to
> move files.
Use Perl (or Python or ...) instead. It will pay off in the end, believe
me.
use File::Copy;
move ($file1, $file2);
Takes care of most problems discussed in this thread.
-- HASM