2

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?

Community
  • 1
  • 1
nintyfan
  • 386
  • 3
  • 16
  • 1
    Can you clarify your last sentence? Do you mean something like "it does not have to clean up symbolic links in the path"? I think it would be helpful if you gave an example of what you're trying to do. – Jonathan Leffler Mar 29 '15 at 16:09
  • I don't wanna the function/procedure follow symbolic links by security problems, but that's not necessary for my program to work. It shouldn't remove symbolic links from filesystem, but should generate error once find symbolic link. – nintyfan Mar 30 '15 at 15:47
  • There's no standard function that will give an error on encountering a symlink. I have code analogous to `realpath()` that checks the security of all symlinks and components in symlinks — it could be adapted to give an error when it comes acrosss a symlink. But it is a non-trivial piece of code, with around around 1000 lines of code plus test materials, etc. And it does some checking that you probably don't need and doesn't do some things that you definitely need it to do, but that's because it has a different mission in life. – Jonathan Leffler Mar 30 '15 at 15:57
  • It would still help if you gave an example with 2-4 paths and the results you want from the function when used on those examples. Because it is not yet clear to me what you are really after — doubly not since you say you don't want symlinks followed, but the purpose of `realpath()` is to resolve symlinks. – Jonathan Leffler Mar 30 '15 at 15:58
  • Imagine I have directory "/tmp/tao-client-aaaaaa/classes/" and I just want to create "/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 of it could be ".." and realpath will returns error, while I test file, which will be created in non-existed directory. – nintyfan Mar 30 '15 at 18:38
  • Is the code in the answer to [How can I create directory tree in C++/Linux?](http://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux/) of any help, where the answer is actually C code that can also be compiled with a C++ compiler. – Jonathan Leffler Mar 30 '15 at 18:41
  • Avoiding creating files or directories outside your document root requires you to analyze the name to ensure that there are no `..` name components, or that they are stopped from propagating the name outside the root. In your example, if the server requested `/parent/hobgoblins/../elves/../orcs/../../child/../../somewhere`, then the `somewhere` component is outside the `parent` directory. But you can textually clean that by replacing `/name/../` sequences with `/`, bearing in mind that the 4th `..` deletes `/parent/`. And if you end up with any `..` left, then the path is invalid. – Jonathan Leffler Mar 30 '15 at 19:42

1 Answers1

2

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.

alk
  • 69,737
  • 10
  • 105
  • 255
  • Thanks for responding. The error was I create directory without needed permissions and file should belongs to that directory. But there's still problem occurs : when I will test not exist path, so parent folder of file is not exist too, there's error "not such file or directory". – nintyfan Mar 30 '15 at 18:27