0

I have a win32 app and I'm starting a robocopy child process for some file transfers. If I terminate the process using TerminateProcess is there a possibility to end with corrupt files? If so how to avoid this?

Gregor Sattel
  • 352
  • 3
  • 12
  • Have you tried it? You could setup a test copying very large files and then practice killing robocopy a few times, see what the result is. That might not prove that it was safe, but it could easily demonstrate if it is NOT safe. – StayOnTarget Mar 25 '21 at 15:25
  • There are a few ways to end a process, and `TerminateProcess` should be a last-ditch effort to do so. – Andy Mar 25 '21 at 15:31
  • Once you shell out copying of files to an external process, there is no safe way to terminate that process. Whether or not you wind up with file system corruption largely depends on the file systems involved. If you want to safely cancel copying, implement the copying and make it cancelable. – IInspectable Mar 25 '21 at 18:21

1 Answers1

1

From TerminateProcess function,

The TerminateProcess function is used to unconditionally cause a process to exit. The state of global data maintained by dynamic-link libraries (DLLs) may be compromised if TerminateProcess is used rather than ExitProcess.

Keep in mind that TerminateProcess does not allow its target to clean up and exit in a valid state.

Try to close the process cleanly, refer the link below,

If the robocopy process is terminated, the file being copied will have a timestamp of (a) 1980-01-01 (b) the same size as the original file.

Then use robocopy to copy files with TimeStamp in command line again.

Refer: How to use Robocopy to copy files with TimeStamp in command line

Strive Sun
  • 5,988
  • 1
  • 9
  • 26