Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string has a valid file path or directory path format in unmanaged C++ code?

I want to know how to check (in unmanaged Visual C++ code) if a string has a valid format to represent file path or folder path. The physical file or folder path itself may or may not exist in this case. To check correct string format is major goal in my case. I need to know if a string has a the right format to be a valid file path or it doesn't have? There are many posts about it for C# but no one for unmanaged C++. How to do it in unmanaged C++ in Visual C++?

like image 563
user3769902 Avatar asked Dec 21 '14 08:12

user3769902


1 Answers

The ONLY 100% certain way is to "try it". You can write code to determine simple cases, but there are will always be wrinkles that you need to deal with, unless you ALSO find out what filesystem the drive is that the file lives on, which, since Windows allows "junctions", means parsing the entire path down to the final part.

It is much simpler to do :

  std::ifstream test(proposedName); 
  if (!test)
  {
      std::cout << "The file doesn't exist" << std::endl;
  }

If you really want to be fancy, you could take a look at errno to figure out if it was "invalid filename" or "file doesn't exist". But in general, it's pointless to try to second guess the validity of a filename and path, since there are WAY too many rules that may or may not apply to YOUR particular case.

It gets more complicated if you have directories "halfway" that doesn't exist. You'd simply have to parse the path and try creating the directory on the way [and deleting it again if the whole process fails]. However, this is further complicated by the fact that you have to deal with non-canonical paths. Is c:\blah\..\.\foo\..\bar\xxx.9\..\..\bleurgh\papa.txt a valid filename? To solve this you'd have to FIRST canonicalize the name.

PathCanonicalize or it's friends would help with this. But it's still full of nasty complicated bits that are VERY hard to solve.

Sure, you can try writing a regex or something to catch the simple cases, but you will still end up with cases which are hard to figure out.

Note that there are situations when a path may appear valid, but isn't. Imagine that we have this path: c:\directory\foo\bar.txt. Now, that looks perfectly valid, we may have to create c:\direoctry and foo inside this. But what if c:\directory\foo is an already existing file? Would you remove the file and create a directory in its place? Or would you say it's "fine" and then fail when you try to create the directory? I don't know the answer, and this sort of complication is why I say "the only way to make sure is to try it" - everything else is just chasing your tail, or approximating the correctness.

And of course, it gets even more complicated if we take file permissions into account.

[And erenon makes a good point - even if you check it now, between the point you check something, and you actually start using your path, you may well have had changes to the directory structure making a previously valid name invalid - or vice versa - this sort of problem is often called "TOCTOU" - "Time of Check to Time of Use"]

like image 180
Mats Petersson Avatar answered Sep 28 '22 07:09

Mats Petersson