LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-20-2005, 07:51 PM   #1
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Rep: Reputation: 15
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;
}
 
Old 01-20-2005, 08:09 PM   #2
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-20-2005, 08:19 PM   #3
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
thanks jtshaw,

but on that pc, the command MV does work.

is there a portable way to do this?
 
Old 01-20-2005, 08:29 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
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.
 
Old 01-20-2005, 11:25 PM   #5
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
jtshaw,
thanks.

But how can I move file across logical devices?

Is there any system call as MV?
 
Old 01-21-2005, 01:17 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 01-21-2005, 03:06 AM   #7
iclinux
Member
 
Registered: Dec 2004
Posts: 69

Original Poster
Rep: Reputation: 15
thanks jlliagre,

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

Is there a better way?

Regards
 
Old 01-21-2005, 03:20 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 01-21-2005, 09:06 AM   #9
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Puzzling problem in shell script Robhogg Programming 5 11-22-2004 03:48 PM
Puzzling static instead of sound under KDE3.2.1 CCMCGeek Linux - Hardware 0 04-04-2004 08:34 PM
A few puzzling Questions Sweepnx Linux - Newbie 11 02-03-2004 03:26 PM
Puzzling nvidia performance hjles Slackware 3 11-14-2003 09:46 AM
puzzling slack 9 situation... mipia Slackware 4 06-28-2003 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration