LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   puzzling error of RENAME (https://www.linuxquestions.org/questions/programming-9/puzzling-error-of-rename-280319/)

iclinux 01-20-2005 07:51 PM

puzzling error of RENAME
 
I use RENAME to remove a file of "/usr" to the directory "/tmp".
It works well on my pc, but reports error on another.

The error info is: Invalid cross-device link


Code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
        int rtn = rename(argv[1], argv[2]);
        if (rtn != 0) {
                perror("rename");
                return -1;
        }
        return 0;
}


jtshaw 01-20-2005 08:09 PM

The other PC must be using a file system or implementation of rename that doesn't allow symbolic links from one logical device to another.

iclinux 01-20-2005 08:19 PM

thanks jtshaw,

but on that pc, the command MV does work.

is there a portable way to do this?

jtshaw 01-20-2005 08:29 PM

Code:

Rename() causes the link named from to be renamed as to.  If to exists,
    it is first removed.  Both from and to must be of the same type (that is,
    both directories or both non-directories), and must reside on the same
    file system.

    Rename() guarantees that an instance of to will always exist, even if the
    system should crash in the middle of the operation.

    If the final component of from is a symbolic link, the symbolic link is
    renamed, not the file or directory to which it points.

The way rename works it has to be able to link. That is how mv works if the file or directory is moving with in the same file system. What mv does when it is going across logical devices is copy from destination to source and then unlink the original file.

iclinux 01-20-2005 11:25 PM

jtshaw,
thanks.

But how can I move file across logical devices?

Is there any system call as MV?

jlliagre 01-21-2005 01:17 AM

As jtshaw said, mv is using the rename system call only when possible, and if not, is copying the file then remove the source file if the copy succeeds. These operations aren't "atomic" enough to be considered for implementing a system call.

iclinux 01-21-2005 03:06 AM

thanks jlliagre,

It seems that I have to call MV to move file across logical devices?

Is there a better way?

Regards

jlliagre 01-21-2005 03:20 AM

That's depend on what you mean by better.
calling mv from your program is simpler, implementing your own move code in C can be faster.

bigearsbilly 01-21-2005 09:06 AM

are they on different partitions?

well, if mv is no good why not
just write it to the other location and
then unlink the original?

that's what rename or mv will do.


All times are GMT -5. The time now is 07:58 AM.