Opened 11 years ago

Closed 10 years ago

#2886 closed bug (fixed)

cd file: no such file or directory

Reported by: Adek336 Owned by: axeld
Priority: normal Milestone: R1
Component: Applications/Command Line Tools Version: R1/pre-alpha1
Keywords: Cc: raghuram87@…
Blocked By: Blocking:
Has a Patch: no Platform: All

Description

When trying to cd a file (not a directory), an error message is

sh: cd: Variete.scala: No such file or directory

instead of, for example

sh: cd: Variete.scala: Is not a directory

which is how it works under, for example Linux.

Also, when I type "cd" and then use "Tab" to autocomplete, it shouldn't autocomplete with filenames of non-directories.

Attachments (1)

cd-not-a-dir.patch (705 bytes) - added by raghuram87 10 years ago.

Download all attachments as: .zip

Change History (7)

Changed 10 years ago by raghuram87

Attachment: cd-not-a-dir.patch added

comment:1 Changed 10 years ago by raghuram87

Cc: raghuram87@… added

I have attached a patch that fixes the first issue above. If we cd to a file and not a dir now we get 'Is not a directory' and not 'No such file or directory'.

comment:2 Changed 10 years ago by stippi

Hey Raghu, I am looking into your patch. Thanks a lot for submitting it! In your comment, you write that the failure of either sh_physpath() or canonpath() is based on an error returned by stat(). Could this also mean that stat() is returning the wrong error? You are writing that anything else than ENOENT must mean it's ENOTDIR for real. But maybe that's what stat() should return in the first place. We are using an old version of Bash, have you checked if the code in the newer bash was changed at the same place, like in your patch? In another words, how does the same version of bash behave on other systems (probably too much hassle to check)? If it returns ENOTDIR, then this would indicate a problem in our stat() implementation. It wouldn't be the first issue of this kind. Unfortunately, I am not very firm with these POSIX subjects, so it's hard for me to tell if your fix is at the right place or not. But usually, an error in Haiku itself is more likely with these types of problems.

comment:3 in reply to:  2 Changed 10 years ago by raghuram87

No, its not an error with Haiku. Let me explain:

We do sh_canonpath() or sh_physpath(). In both these functions its checked if every part of the path breaking it at each /, is a valid dir or not. So for that a stat of the partial path is done and the returned struct is checked if the stat'ed node represents a direcotry, something like: s=stat(path) && isdir(s). Now, stat() could return a ENOENT because the user tried cd'ing into a non-existent file/dir. But the user could have tried cd'ing into a file, which is also the case the above condition fails. If the above condition failed we bail out of sh_canonpath() and sh_physpath() and set errno = ENOENT. This is where we make the mistake, because the above condition may have failed because path was a file and not a dir. Also stat() can never return ENOTDIR in this case. stat() returns ENOTDIR when a component of the path is not a dir and is a file (It returns ENOENT if its not a dir and not a file). However here we check for each component of the path from the start, so the condition that catches this error is isdir() and not stat(). So the patch now does the following: If we tried stat()'ing a non-existent file, errno would have already been set to ENOENT by stat(). Only when stat() succeeded and isdir() failed (which is a case when canon_failed is true) we have errno != ENOENT and that is when we can infer the error is ENOTDIR. There are no other posix calls in sh_canonpath() and sh_physpath(), hence the assumption that only two errors ENOENT or ENOTDIR are possible is sound.

Raghu.

comment:4 Changed 10 years ago by stippi

Thanks a lot for the complete explanation! But I am wondering if then sh_canonpath() and/or sh_physpath() should be setting errno to ENOTDIR, if the is_dir() check fails. Maybe that would also fix the second part of this ticket? Have you compared newer versions of bash in this regard?

comment:5 Changed 10 years ago by korli

Applied patch for first part in hrev30232. I checked bash 3.2 and upper and the check is also there.

comment:6 Changed 10 years ago by korli

Resolution: fixed
Status: newclosed

bash_completion added in hrev30233. this includes completion for cd.

Note: See TracTickets for help on using tickets.