Is there a POSIX pathname that can't name a file?

5

1

Are there any legal paths in POSIX that cannot be associated with a file, regular or irregular? That is, for which test -e "$LEGITIMATEPOSIXPATHNAME" cannot succeed?

Clarification #1: pathnames

By "legal paths in POSIX", I mean ones that POSIX says are allowed, not ones that POSIX doesn't explicitly forbid. I've looked this up, and the are POSIX specification calls them character strings that:

  1. Use only characters from the portable filename character set [a-zA-Z0-9._-] (cf. http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_276);
  2. Do not begin with -; and
  3. Have length between 1 and NAME_MAX, a number unspecified for POSIX that is not less than 14.

POSIX also allows that filesystems will probably be more relaxed than this, but it forbids the characters NUL and / from appearing in filenames. Note that such a paradigmatically UNIX filename as lost+found isn't FPF, according to this def. There's another constant PATH_MAX, whose use needs no further explanation.

The ideal answer will use FPFs, but I'm interested in any example with filenames that POSIX doesn't expressly forbid.

Clarification #2: impossibility

Obviously, pathnames normally could be bound to a file. But UNIX semantics will tell you that there are special places that couldn't normally have arbitrary files created, like in the /dev directory. Are any such special places stipulated in POSIX? That is what the question is getting after.

Charles Stewart

Posted 2010-01-14T11:59:26.350

Reputation: 2 624

Just a note...

it's not \ it's /. In Unix \ is a fine character to have in a file. /user/chuck/tom\mary the filename=tom\mary. – DrFloyd5 – 2010-04-10T13:43:09.780

@DrFloyd: Right. Fixed. – Charles Stewart – 2010-04-13T10:01:21.713

1I like this question. It reminds me of "Der Mouse", when moving from VMS to UNIX, being annoyed because he could not find a way to create a filename with a slash or a null in it - where the slash was part of the filename within a directory, rather than a separator between directory and something in the directory. // The standard POSIX APIs do not produce an error on such filenames, they simply interpret the embedded special characters as either path separate or string terminator. – Krazy Glew – 2015-12-22T18:07:48.953

4Hmmm, I was wondering if that title couldn't be more descriptive, but whatever I come up with seems to yield a contradiction in terms... – Arjan – 2010-01-14T12:18:22.917

Answers

3

Since the final question is whether there are special places that couldn't normally have a file, like in the /dev directory stipulated in POSIX, then the andswer is YES.

The complete list of pre-determined files and directories is given in chapter 10, POSIX Directory Structure and Devices, of the IEEE Open Group Base Specifications Issue 6:

The following directories shall exist on conforming systems and conforming applications shall make use of them only as described. Strictly conforming applications shall not assume the ability to create files in any of these directories, unless specified below.

/
The root directory.
/dev
Contains /dev/console, /dev/null, and /dev/tty, described below.

The following directory shall exist on conforming systems and shall be used as described:

/tmp
A directory made available for applications that need a place to create temporary files. Applications shall be allowed to create files in this directory, but shall not assume that such files are preserved between invocations of the application.

The following files shall exist on conforming systems and shall be both readable and writable:

/dev/null
An infinite data source and data sink. Data written to /dev/null shall be discarded. Reads from /dev/null shall always return end-of-file (EOF).
/dev/tty
In each process, a synonym for the controlling terminal associated with the process group of that process, if any. It is useful for programs or shell procedures that wish to be sure of writing messages to or reading data from the terminal no matter how output has been redirected. It can also be used for applications that demand the name of a file for output, when typed output is desired and it is tiresome to find out what terminal is currently in use.

The following file shall exist on conforming systems and need not be readable or writable:

/dev/console
The /dev/console file is a generic name given to the system console (see System Console). It is usually linked to an implementation-defined special file. It shall provide an interface to the system console conforming to the requirements of the Base Definitions volume of IEEE Std 1003.1-2001, Chapter 11, General Terminal Interface.

harrymc

Posted 2010-01-14T11:59:26.350

Reputation: 306 093

I assume that the term "files" also includes directories, so in your example a POSIX program cannot also assume that it can create a directory named "impossible". The terminology used is a bit confusing, and I assume the the authors didn't imagine that /dev could contain directories. – harrymc – 2010-01-22T10:27:15.107

/dev can and typically does contain directories: SunOS has had a fsfs mounted at /dev/fd for ages, and its copied by most modern UNIXes. But POSIX forbids a file from being both a character special and a directory, so there can't be anything under /dev/tty. So chosen: I have my answer. – Charles Stewart – 2010-01-24T09:45:13.123

3

Testing for a filename with the null character in it should always fail.

POSIX reserves '/' and null from filenames. This is sensible: one is the directory separator, and one is a string terminator. To support that point, Wikipedia says ext2, ext3, and ext4 allow all bytes in filenames except null and the forward slash. NTFS, whether or not in POSIX-compatibility mode, disallows little more than that; and FAT variants also disallow null. In theory, it really depends on the file system. But I wouldn't hold my breath trying to find a case where null finds its way into a filename.

Anonymous

Posted 2010-01-14T11:59:26.350

Reputation: 1 214

+1 for an answer that made me look something up. POSIX forbids "/" and NUL in filenames. Cf. my questions postscript. – Charles Stewart – 2010-01-18T13:05:37.947

2

/dev/null/impossible cannot exist. This is because /dev/null must be a file, and so cannot be a directory.

Same for /dev/tty/impossible and /dev/console/impossible

Demi

Posted 2010-01-14T11:59:26.350

Reputation: 718

1

"Legal path in POSIX" already means that it can point to a filesystem object (file, directory, symlink, etc).

On the second thought, some filesystems (such as FAT) have restrictions on allowed characters in filenames. So in my computer, ~/fs/phone/This:is*a?file|name.txt would be rejected by the vfat filesystem driver.


To answer the second question, test -e "$LEGITIMATEPOSIXPATHNAME" fails when the file does not exist, obviously.

user1686

Posted 2010-01-14T11:59:26.350

Reputation: 283 655

Hehe, +1 for that last paragraph. There is an answer to any question, I guess. :-) – Arjan – 2010-01-14T14:34:50.553

s/fails/cannot succeed/ – Charles Stewart – 2010-01-14T20:27:15.443

1I am not familiar with any *nix where /dev/fd/this-is-not-a-file could be a file, but IIUC, POSIX doesn't say it couldn't be a file. So being legal path in POSIX doesn't mean a POSIX-compliant OS must permit a file to be created there. – Charles Stewart – 2010-01-14T20:30:18.307

-1: Completely misses the point of the question. – Charles Stewart – 2010-01-18T09:45:33.710

@Charles, it kind of strikes me as odd that you're downvoting while I think your question was not clear at all to start with. (And still has a very bad title, which is not a question at all -- http://superuser.com/faq)

– Arjan – 2010-01-18T13:56:27.890

@Arjan: Title changed. Since people are misunderstanding it, I guess you must be right that it is not clear, but I do not see where the problem lies. It takes the form "Is there an X that is Y", where the Xs are familiar, and the Y is precisely defined. – Charles Stewart – 2010-01-18T14:08:37.707

0

The test would fail if the file-name breaks the limitations of the local implementation of POSIX.

Every file-system in existence makes assumptions as regarding maximum lengths, directory recursion limits and more. So a POSIX name that's legal on one operating system might not be legal on another.

So my answer to the question is "Yes":
Even names that are legal when tested on one POSIX system, may be rejected by another, because of implementation restrictions.

harrymc

Posted 2010-01-14T11:59:26.350

Reputation: 306 093

Every pathname that POSIX says is legal is accepted on all POSIX-compliant filesystems, by definition. It is these that I care about. – Charles Stewart – 2010-01-18T13:36:23.583

Well, NAME_MAX and PATH_MAX are implementation-dependent. I think you're contradicting yourself when you ask (1) for unattainable POSIX file-names, and at the same time (2) accepted on all POSIX-compliant systems. By definition, acceptable file-names cannot be unattainable, and the least-common-denominator of all POSIX systems is always attainable on all of them. – harrymc – 2010-01-18T14:13:02.220

Aha! No, I want pathnames that would work, except that other assumptions in POSIX prevent the files from existing – Charles Stewart – 2010-01-18T15:10:43.010