nintyfan
nintyfan

Reputation: 434

Realpath requires file exist

I would like check if a path is a subpath of another path, so I call realpath(file_path, NULL) and compare the result with strncmp.

The reason I wrote this topic is: realpath sets errno to access denied. I would like to create a file if part of this path is a subpath of some directory, so the file can't exist before calling realpath.

Is there a realpath equivalent in Linux/Unix/C, which doesn't check that the file exists? It can also don't follow by symbolic links.


Material from comment:

Imagine I have directory "/tmp/tao-client-aaaaaa/classes/" and I just want to create (a file) "/tmp/tao-client-aaaaaa/classes/parent/parent generating test", because of application(server) asks to create "/parent/parent generating test". And the problem was I cannot create the directory named "parent", because it could be ".." and realpath will returns error, while I test file, which will be created in non-existent directory.

I think, that I could implement realpath by myself, but I hear that Unicode have many characters or character connection meaning parent directory.

Material from 'answer':

I have idea. I can create function which adds to path next directory, uses realpath and test result of realpath is child directory of argument (called path). Next it returns null if condition don't pass or realpath result if condition passes. Wrapper will create that directory and get next directory from path to save file and pass it to this function in this way result_path = test_real_path(result_path, next_directory).

But it's not very good solution, because I create directories and I don't know what do if result will not be child of path. Just remove all created directories?

Upvotes: 2

Views: 4755

Answers (1)

alk
alk

Reputation: 70931

I'd say "access denied" (EACCES) does not indicate a missing file (ENOENT) but insufficient access rights to the path or parts of it.

From man realpath:

ERRORS

EACCES Read or search permission was denied for a component of the path prefix.

[...]

ENOENT The named file does not exist.


And to answer your question:

Is there a realpath equivalent in Linux/Unix/C, which doesn't check that the file exists?

No.

But this isn't necessary for your use case. Just use . instead of the file name you want to create.

Upvotes: 2

Related Questions