0

Could there ever be a situation in which touch would work and mkdir would fail, within a directory.

I understand that both these calls involve writing to the inode of the parent directory. So if touch worked, doesn't it automatically mean that the inode is writable and hence, the mkdir couldn't possibly fail.

This was Android (I only heard a report, didn't actually see this case) and the commands were being run from the terminal emulator, in the /system directory (after requesting su), but that shouldn't make a difference IMO.


EDIT: mkdir failed with "permission denied"

Anirudh Ramanathan
  • 46,179
  • 22
  • 132
  • 191

2 Answers2

2

There are a couple of ways I can think of by which this could happen:

1) If you have reached the maximum number of sub-directories in a directory (typically around 32000), you will not be able to mkdir, but you will be able to touch a file.

2) If you are very low on disk space, you will not be able to mkdir because it requires 4K, but you will be able to touch a file.

dogbane
  • 266,786
  • 75
  • 396
  • 414
1

Perhaps you don't have write permissions on the parent directory? You would still be able to touch existing files in the directory if you own them or have write permissions on them. (However, calling touch on a file that does not exist will still fail since the directory is read-only.)

What's the output of stat on the parent directory and the touched files?

ceyko
  • 4,822
  • 1
  • 18
  • 23
  • I need to check that. But if I didn't have write permissions on the parent, wouldn't the mkdir have also failed? – Anirudh Ramanathan Dec 10 '12 at 21:12
  • But you said the mkdir *did* fail. Or, if you meant touch instead, no you do not need write permissions on the parent to touch a file inside it. I just tested it out, myself, too. – ceyko Dec 10 '12 at 21:17
  • Oops, I meant `touch`. I just tested it out, and when I remove write permission, from the parent, I **can't** touch a file within it, which makes sense, since the directory inode can't be written to. How is it letting you do that?! – Anirudh Ramanathan Dec 10 '12 at 21:23
  • Ah, I should've been clearer. You can touch *existing* files within the directory, since it only has to update their access times, but *not* create new files. I'll edit my answer to reflect this. – ceyko Dec 10 '12 at 21:57
  • +1 That makes sense. But in this case, there was no existing file of that name, so I was writing a new file. – Anirudh Ramanathan Dec 10 '12 at 22:02
  • In that case, if it also isn't because of a reason @dogbane mentioned, I'm drawing a blank at the moment - try to get the output of `stat` on the directory and file. Alternatively, I hesitate to mention this, but is it at all possible that something happened in between `touch` and `mkdir` that would change the directory/file? Basically, have you been able to duplicate the behavior? – ceyko Dec 10 '12 at 22:07
  • I'm trying to get the person who reported this to give me the output from stat. I have been unable to reproduce it so far, at my end though. – Anirudh Ramanathan Dec 10 '12 at 22:10