File IO in Multi-threaded Environment

KeysCoder

Member
Joined
Apr 10, 2006
Messages
18
Location
Florida Keys
Programming Experience
3-5
Suppose I want to over-write one file with another but I want to backup the over-written file. Use io.file.replace, right?

Now what if I want to check to make sure there is adequate disk space first? Is it possible that another thread can write a file to the hard drive between my disk space check and my file.replace operation - invalidating my disk space check?

Is there any way to avoid this issue?
 
There's a way to prevent any of your user code writing another file to the hard drive but that doesn't mean that another process won't. The most you can do is put all your I/O output in associated SyncLock blocks.
 
You can do the three file operations that ReplaceFile does yourself. Total disk space requirements can be calculated before starting any of them, and before doing the first copy operation you can reserve disk space for the second copy operation with a temp FileStream.
 
Suppose I want to over-write one file with another but I want to backup the over-written file. Use io.file.replace, right?

Er.. I'd rename the old file to be a backup and then write a new file - it doesnt make sense to me to copy FileA -> File.bak then write a new FileA because that's a very long op.

If the user runs out of disk space, is it really a concern?
 
Er.. I'd rename the old file to be a backup and then write a new file - it doesnt make sense to me to copy FileA -> File.bak then write a new FileA because that's a very long op.
Right, but there could also be different volumes which would result in copies. If it's all on one volume there should only be a change in file table.
 
Good point.. Find the exception that occurs when a rename-across-devices is attempted, and Try the rename, catching that exception (which attempts a copy instead)
 
Back
Top