Getting absolute path of a file

Use realpath(). The realpath() function shall derive, from the pathname pointed to by file_name, an absolute pathname that names the same file, whose resolution does not involve ‘.‘, ‘..‘, or symbolic links. The generated pathname shall be stored as a null-terminated string, up to a maximum of {PATH_MAX} bytes, in the buffer pointed to by … Read more

Is there a safer way to create a directory if it does not exist?

You can actually skip the if, even though Apple’s docs say that the directory must not exist, that is only true if you are passing withIntermediateDirectories:NO That puts it down to one call. The next step is to capture any errors: NSError * error = nil; [[NSFileManager defaultManager] createDirectoryAtPath:bundlePath withIntermediateDirectories:YES attributes:nil error:&error]; if (error != … Read more

Unique file identifier in windows

Here’s sample code that returns a unique File Index. ApproachA() is what I came up with after a bit of research. ApproachB() is thanks to information in the links provided by Mattias and Rubens. Given a specific file, both approaches return the same file index (during my basic testing). Some caveats from MSDN: Support for … Read more

How to mount one partition from an image file that contains multiple partitions on Linux?

You might do it like this, without much hassle: # kpartx -v -a logging-test.img add map loop0p1 (251:0): 0 497664 linear /dev/loop0 2048 add map loop0p2 (251:1): 0 66605058 linear /dev/loop0 501758 add map loop0p5 (251:2): 0 66605056 251:1 2 # ls /dev/mapper/ control loop0p1 loop0p2 loop0p5 # mount /dev/mapper/loop0p1 /mnt/test # mount | grep … Read more

Determine via C# whether a string is a valid file path

You can use the FileInfo constructor. It will throw a ArgumentException if “The file name is empty, contains only white spaces, or contains invalid characters.” It can also throw SecurityException or UnauthorizedAccessException, which I think you can ignore if you’re only concerned about format. Another option is to check against Path.GetInvalidPathChars directly. E.g.: boolean possiblePath … Read more

How to overwrite a file with NSFileManager when copying?

If you can’t/don’t want to keep the file contents in memory but want an atomic rewrite as noted in the other suggestions, you can first copy the original file to a temp directory to a unique path (Apple’s documentation suggests using a temporary directory), then use NSFileManager’s -replaceItemAtURL:withItemAtURL:backupItemName:options:resultingItemURL:error: According to the reference documentation, this method … Read more