winapifilesystemsntdll

shortnames and parallel file copy


I am dealing with code that:

(all this is simplified quite a bit).

Problem:

When file gets created it also gets a shortname which can be the same as another file in source directory (name collision) which leads to variety of effects (depending on the order of operations). For example, copying two files my file and MYFILE~1 can produce 2 or 1 files in destination (depending on your luck), probably with corrupted content (in latter case).

Question:

How to avoid problems that arise from such collisions? Would be nice to have a function that creates/opens a file ignoring shortnames...

Notes:

Edit: it occurred to me that in general case you can't do it even if everything happens on one thread -- simply because regardless of order you choose, there will be a shortname generation scheme and a set of filenames that is guaranteed to produce a collision during processing.

If this is correct -- how system utilities copy files? Do they assume something about shortnames or perform "validate and fix discrepancies" after copy is complete?


Solution

  • Problem basically boils down to following: dst should never be opened via shortname.

    For example (in case of NT API) this can be achieved like this:

    It is possible for parallel process to rename file right after you opened it, but I'd treat it as an error.

    Error can lead to couple of retries followed by hard failure (that needs human attention). Shouldn't be too hard to fix the issue manually (or even programmatically) since name of conflicting file is returned by NtQueryInformationFile().

    P.S. Additional steps can be taken to prevent/reduce collisions. For example, if shortname generation logic is known -- dst objects can be processed in certain order.