-
File control
So I'm having trouble doing this is a semi-standard fashion:
I have a file encryptor that does something special when prompted to decrypt a file.
You pass the encrypted file as an argument to the program. The program reads the original filetype from the file (which I stored there upon encoding), and creates a temporary in some folder somewhere, where a decoded copy of the file is made.
Now, after calculating a checksum, the file is launched via ShellExecute(), or whatever function is its equivalent on the system for which I'm compiling. Then the program waits for the user to be done. It does this:
Code:
while(!std::ofstream(path.c_str(), std::ios::app)) my::wait(5000);
//now the file is ready
Surprisingly, this method works for most filetypes on my Windows Vista system.
Once the user is done, I recalculate the checksum to see whether the user has made an changes. If they made changes, I reencrypt and overwrite the original encrypted file, then delete the temporary. If not, then I just delete the temporary.
Now -- my problems -- it's not working very consistently. For some files (i.e. Quicktime types), my program is allowed access to the file while the video is playing, and my program deletes the program form under quicktime's nose. Crash.
Other files work well, but when I go to recalculate my checksum, I get a crash (i.e. BMP).
And finally, some files (i.e. MSWord2007) are never overwritten because Word saves any changes in a temp and then does not update the original (compatibility mode?) .
So my question is, first: is there anything wrong with my concept? Can this be done is a nearly-standard manner?
-
I doubt there's any way to do it automatically. Some programs will probably just read the file into memory and then close the file handle, so what would stop your program from deleting it then?
Why can't you just leave a prompt up for the user indefinitely that says "Hit enter when done" or whatever and then do the checksum, delete, etc.
-
Yes, that is the obvious solution. I just wanted to make it fancy. Thanks.
-
You could wait until the program that is using the file exits. The easiest way to do this would be to pick programs that run in the foreground. Or you could somehow determine the program's process id and wait until that process quits. I'm not sure how you'd do this under Windows, but I'm sure it could be done.
-
Yeah, but it still probably wouldn't be foolproof (not to mention more difficult to make platform-independent). What about programs like visual studio that allow multiple documents? Maybe you open an encrypted cpp file, modify it, then close the tab in visual studio (but visual studio itself is still open)