Why does rename() return false despite moving a file successully to an NFS mounted disk?
Asked Answered
M

4

8

I'm getting an Invalid argument warning when moving a file from a local disk to an NFS mounted disk. The file is moved successfully despite the error message:

Warning: rename(/tmp/image.jpg,/mnt/remote.server-disk1/image.jpg): Invalid argument

The mounted disk:

$ df
remote.server:/disk1 917G  269M  871G   1% /mnt/remote.server-disk1

The exported disk on the remote server:

$ cat /etc/exports
/disk1 remote.server(rw,sync,root_squash,secure,crossmnt,anonuid=504,anongid=504)

The file on the local disk before rename():

$ stat /tmp/image.jpg
File: `image.jpg'
Size: 2105          Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d  Inode: 33556339    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (  501/  apache)   Gid: (  501/  apache)
...

The file on the remote disk after rename():

$ stat /disk1/image.jpg
File: `image.jpg'
Size: 2105          Blocks: 8          IO Block: 4096   regular file
Device: 821h/2081d  Inode: 34603214    Links: 1
Access: (0777/-rwxrwxrwx)  Uid: (  501/  apache)   Gid: (  501/  apache)
...

Any ideas? Thanks

Merchantable answered 10/11, 2013 at 20:18 Comment(0)
T
9

In Unix you can't rename or move between filesystems, Instead you must copy the file from one source location to the destination location, then delete the source. This will explain the error message you're getting. However, it seems unclear as why it still does the move. This could be related to permissions or the NFS mounted disk is locally cached.

Toad answered 27/12, 2013 at 20:12 Comment(1)
I my case - using php rename() over nfs, same warning, returns false - the file wasnt moved, but copied; the original was still there. The op doesnt say anything about the file on the local disk after rename(). It would make sense for php to issue a warning, not an error, and return false because it didnt fully succeed.Cerys
T
3

Maybe a bit late, but i think the answer is probably because of permissions linked to the target file system.

We had the same issues, and a strace give us the proper diagnostic:

strace php -r "rename('SOURCE_FILE', 'DST_FILE');";
...
chown("SOURCE_FILE", 0, 0) = -1 EINVAL (Invalid argument)                                                                                                                                                       
write(2, "PHP Warning:  rename(SOURCE_FILE"..., 192PHP Warning:  rename(SOURCE_FILE): Invalid argument in Command line code on line 1
) = 192
write(1, "bool(false)\n", 12bool(false)

In our case, the target file system was an NFS with version 4, and idmap was enabled.

Even a simple

chown $(whoami) $DST_FILE

wasn't working.

It's the default behavior (under debian at least) to have idmap enabled with nfs-common utils. So even if you fix it by using a copy + unlink (which is the best approach by the way), it may still hide an issue in your underlying file system.

(to disable idmap : https://forums.aws.amazon.com/thread.jspa?threadID=235501)

Toot answered 7/6, 2021 at 14:10 Comment(0)
E
0

Maybe it's because you doing this with no quotes

rename(/tmp/image.jpg,/mnt/remote.server-disk1/image.jpg);

try adding quotes

rename('/tmp/image.jpg', '/mnt/remote.server-disk1/image.jpg');

Excurvate answered 10/11, 2013 at 20:21 Comment(1)
That's just how PHP reports the error. I'm using variables for source file and destination file in the code.Merchantable
M
0

I couldn't resolve this, but copy() and then unlink() worked without error.

Merchantable answered 12/11, 2013 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.