why can't I `mv` a directory without extra wri

2019-08-18 18:31发布

My understanding of permissions has been broken today.

evaben@evaben /t/test> ll
total 16K
drwxr-xr-x  4 evaben evaben 4.0K Mar 13 12:44 ./
drwxrwxrwt 19 root   root   4.0K Mar 13 12:43 ../
drwxr-xr-x  2 evaben evaben 4.0K Mar 13 12:44 mine/
drwxr-xr-x  2 root   root   4.0K Mar 13 12:44 theirs/

I own the CWD, and mine, and have write permission on both.

evaben@evaben /t/test> mv mine theirs/
mv: cannot move 'mine' to 'theirs/mine': Permission denied

Of course I cannot move my directory into theirs; I do not have write permission in theirs.

evaben@evaben /t/test [1]> mv theirs/ mine/
mv: cannot move 'theirs/' to 'mine/theirs': Permission denied

Why can't I move theirs into mine? I can write the CWD, AND the dest (mine). I am not modifying theirs in any way.

evaben@evaben /t/test [1]> sudo chmod o+w theirs/
evaben@evaben /t/test> mv theirs/ mine/
(works)

I can mv if I have write permission on theirs.

The wikipedia explanation states:

When set for a directory, this (write) permission grants the ability to modify entries in the directory, which includes creating files, deleting files, and renaming files.

Arch wiki states similar, which seems to reinforce my (clearly wrong) understanding.

To further cloud my brain, it works as I expect for a file:

evaben@evaben /t/test> ll
total 12K
drwxr-xr-x  3 evaben evaben 4.0K Mar 13 13:04 ./
drwxrwxrwt 19 root   root   4.0K Mar 13 12:43 ../
drwxr-xr-x  3 evaben evaben 4.0K Mar 13 13:03 mine/
-rw-r--r--  1 root   root      0 Mar 13 13:04 their_file
evaben@evaben /t/test> mv their_file mine/
(worked)

wiki archwiki

EDIT:

I have tried to use rename(2) directly.

rename("theirs", "mine");

Works IFF mine is empty. - effectively mine is replaced my theirs. If mine has files; ENOTEMPTY 39 Directory not empty.

rename("theirs", "mine/theirs");

Gives EACCES 13 Permission denied. Which is both what I want to do and what mv gives - so apparently not a bug in mv (still a bug in my understanding).

2条回答
Lonely孤独者°
2楼-- · 2019-08-18 19:04

"I am not modifying theirs in any way."

Not quite correct. Part of the mv process is deleting the file from its original location. Which means you need write permission for that original location, which you don't have. Try using cp instead.

查看更多
虎瘦雄心在
3楼-- · 2019-08-18 19:29

I think the rename(2) man page has the explanation:

ERRORS
       EACCES Write permission is denied for the directory containing oldpath or newpath, or, search per‐
              mission  is  denied for one of the directories in the path prefix of oldpath or newpath, or
              oldpath is a directory and does not  allow  write  permission  (needed  to  update  the  ..
              entry).  (See also path_resolution(7).)

So apparently in order to move theirs into mine, you need to have permissions to update the .. link on theirs.

查看更多
登录 后发表回答