Backing up a file to prevent its corruption during processing
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
My program is iterating over list of files and processing each of them using some library. It's possible that file might be corrupted in the process so I want to backup it to prevent potential corruption.
I implemented it this way:
private static void ComprocessFiles(List<string> files)
FileInfo file = null;
foreach (string filepath in files)
bool backedUp = false;
bool keepBackup = false;
try
file = new FileInfo(filepath);
if (!file.Exists) continue;
if (file.IsReadOnly) continue;
BackupHelper.Backup(file);
backedUp = true;
ProcessFile(file);
BackupHelper.RestoreTimestamps(file);
file.Refresh();
catch (Exception ex)
finally
if (!IsCorrupted(file)) keepBackup = true;
if (!keepBackup) BackupHelper.DeleteBackup(file);
BackupHelper
class:
static class BackupHelper
static private DateTime timestamps = new DateTime[3];
static internal void Backup(FileInfo originalFile)
timestamps[0] = originalFile.CreationTime;
timestamps[1] = originalFile.LastWriteTime;
timestamps[2] = originalFile.LastAccessTime;
File.Copy(originalFile.FullName, originalFile.FullName + "_backup", true);
static internal void Backup(string originalFile)
Backup(new FileInfo(originalFile));
static internal void RestoreFile(FileInfo originalFile)
File.Copy(originalFile.FullName + "_backup", originalFile.FullName, true);
static internal void RestoreFile(string originalFile)
RestoreFile(new FileInfo(originalFile));
static internal void RestoreTimestamps(FileInfo originalFile)
originalFile.CreationTime = timestamps[0];
originalFile.LastWriteTime = timestamps[1];
originalFile.LastAccessTime = timestamps[2];
static internal void RestoreTimestamps(string originalFile)
RestoreTimestamps(new FileInfo(originalFile));
static internal void DeleteBackup(FileInfo originalFile)
timestamps = new DateTime[3];
File.Delete(originalFile + "_backup");
static internal void DeleteBackup(string originalFile)
DeleteBackup(new FileInfo(originalFile));
I tried my best as I plan to publish source code online but still feel that there is room for improvement.
I guess methods ProcessFile()
and IsCorrupted()
are not important for that question as well as Logger
class.
Important note - StackOverflowException
and OutOfMemoryException
can't be thrown by this code as it don't do many method calls and don't use much memory.
c# error-handling
add a comment |Â
up vote
2
down vote
favorite
My program is iterating over list of files and processing each of them using some library. It's possible that file might be corrupted in the process so I want to backup it to prevent potential corruption.
I implemented it this way:
private static void ComprocessFiles(List<string> files)
FileInfo file = null;
foreach (string filepath in files)
bool backedUp = false;
bool keepBackup = false;
try
file = new FileInfo(filepath);
if (!file.Exists) continue;
if (file.IsReadOnly) continue;
BackupHelper.Backup(file);
backedUp = true;
ProcessFile(file);
BackupHelper.RestoreTimestamps(file);
file.Refresh();
catch (Exception ex)
finally
if (!IsCorrupted(file)) keepBackup = true;
if (!keepBackup) BackupHelper.DeleteBackup(file);
BackupHelper
class:
static class BackupHelper
static private DateTime timestamps = new DateTime[3];
static internal void Backup(FileInfo originalFile)
timestamps[0] = originalFile.CreationTime;
timestamps[1] = originalFile.LastWriteTime;
timestamps[2] = originalFile.LastAccessTime;
File.Copy(originalFile.FullName, originalFile.FullName + "_backup", true);
static internal void Backup(string originalFile)
Backup(new FileInfo(originalFile));
static internal void RestoreFile(FileInfo originalFile)
File.Copy(originalFile.FullName + "_backup", originalFile.FullName, true);
static internal void RestoreFile(string originalFile)
RestoreFile(new FileInfo(originalFile));
static internal void RestoreTimestamps(FileInfo originalFile)
originalFile.CreationTime = timestamps[0];
originalFile.LastWriteTime = timestamps[1];
originalFile.LastAccessTime = timestamps[2];
static internal void RestoreTimestamps(string originalFile)
RestoreTimestamps(new FileInfo(originalFile));
static internal void DeleteBackup(FileInfo originalFile)
timestamps = new DateTime[3];
File.Delete(originalFile + "_backup");
static internal void DeleteBackup(string originalFile)
DeleteBackup(new FileInfo(originalFile));
I tried my best as I plan to publish source code online but still feel that there is room for improvement.
I guess methods ProcessFile()
and IsCorrupted()
are not important for that question as well as Logger
class.
Important note - StackOverflowException
and OutOfMemoryException
can't be thrown by this code as it don't do many method calls and don't use much memory.
c# error-handling
2
How can the file be corrupted mid-processing? Are you updating the file on the disk as you go? Is it an option to simply process the file and only write to disk after processing has completed? Why not simply write to a different file and not touch the original one, to avoid losing the initial data?
â Flater
Feb 9 at 12:50
You are right. Great idea. Thank you!
â InfernumDeus
Feb 12 at 3:51
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
My program is iterating over list of files and processing each of them using some library. It's possible that file might be corrupted in the process so I want to backup it to prevent potential corruption.
I implemented it this way:
private static void ComprocessFiles(List<string> files)
FileInfo file = null;
foreach (string filepath in files)
bool backedUp = false;
bool keepBackup = false;
try
file = new FileInfo(filepath);
if (!file.Exists) continue;
if (file.IsReadOnly) continue;
BackupHelper.Backup(file);
backedUp = true;
ProcessFile(file);
BackupHelper.RestoreTimestamps(file);
file.Refresh();
catch (Exception ex)
finally
if (!IsCorrupted(file)) keepBackup = true;
if (!keepBackup) BackupHelper.DeleteBackup(file);
BackupHelper
class:
static class BackupHelper
static private DateTime timestamps = new DateTime[3];
static internal void Backup(FileInfo originalFile)
timestamps[0] = originalFile.CreationTime;
timestamps[1] = originalFile.LastWriteTime;
timestamps[2] = originalFile.LastAccessTime;
File.Copy(originalFile.FullName, originalFile.FullName + "_backup", true);
static internal void Backup(string originalFile)
Backup(new FileInfo(originalFile));
static internal void RestoreFile(FileInfo originalFile)
File.Copy(originalFile.FullName + "_backup", originalFile.FullName, true);
static internal void RestoreFile(string originalFile)
RestoreFile(new FileInfo(originalFile));
static internal void RestoreTimestamps(FileInfo originalFile)
originalFile.CreationTime = timestamps[0];
originalFile.LastWriteTime = timestamps[1];
originalFile.LastAccessTime = timestamps[2];
static internal void RestoreTimestamps(string originalFile)
RestoreTimestamps(new FileInfo(originalFile));
static internal void DeleteBackup(FileInfo originalFile)
timestamps = new DateTime[3];
File.Delete(originalFile + "_backup");
static internal void DeleteBackup(string originalFile)
DeleteBackup(new FileInfo(originalFile));
I tried my best as I plan to publish source code online but still feel that there is room for improvement.
I guess methods ProcessFile()
and IsCorrupted()
are not important for that question as well as Logger
class.
Important note - StackOverflowException
and OutOfMemoryException
can't be thrown by this code as it don't do many method calls and don't use much memory.
c# error-handling
My program is iterating over list of files and processing each of them using some library. It's possible that file might be corrupted in the process so I want to backup it to prevent potential corruption.
I implemented it this way:
private static void ComprocessFiles(List<string> files)
FileInfo file = null;
foreach (string filepath in files)
bool backedUp = false;
bool keepBackup = false;
try
file = new FileInfo(filepath);
if (!file.Exists) continue;
if (file.IsReadOnly) continue;
BackupHelper.Backup(file);
backedUp = true;
ProcessFile(file);
BackupHelper.RestoreTimestamps(file);
file.Refresh();
catch (Exception ex)
finally
if (!IsCorrupted(file)) keepBackup = true;
if (!keepBackup) BackupHelper.DeleteBackup(file);
BackupHelper
class:
static class BackupHelper
static private DateTime timestamps = new DateTime[3];
static internal void Backup(FileInfo originalFile)
timestamps[0] = originalFile.CreationTime;
timestamps[1] = originalFile.LastWriteTime;
timestamps[2] = originalFile.LastAccessTime;
File.Copy(originalFile.FullName, originalFile.FullName + "_backup", true);
static internal void Backup(string originalFile)
Backup(new FileInfo(originalFile));
static internal void RestoreFile(FileInfo originalFile)
File.Copy(originalFile.FullName + "_backup", originalFile.FullName, true);
static internal void RestoreFile(string originalFile)
RestoreFile(new FileInfo(originalFile));
static internal void RestoreTimestamps(FileInfo originalFile)
originalFile.CreationTime = timestamps[0];
originalFile.LastWriteTime = timestamps[1];
originalFile.LastAccessTime = timestamps[2];
static internal void RestoreTimestamps(string originalFile)
RestoreTimestamps(new FileInfo(originalFile));
static internal void DeleteBackup(FileInfo originalFile)
timestamps = new DateTime[3];
File.Delete(originalFile + "_backup");
static internal void DeleteBackup(string originalFile)
DeleteBackup(new FileInfo(originalFile));
I tried my best as I plan to publish source code online but still feel that there is room for improvement.
I guess methods ProcessFile()
and IsCorrupted()
are not important for that question as well as Logger
class.
Important note - StackOverflowException
and OutOfMemoryException
can't be thrown by this code as it don't do many method calls and don't use much memory.
c# error-handling
edited Feb 9 at 9:42
asked Feb 9 at 9:34
InfernumDeus
1306
1306
2
How can the file be corrupted mid-processing? Are you updating the file on the disk as you go? Is it an option to simply process the file and only write to disk after processing has completed? Why not simply write to a different file and not touch the original one, to avoid losing the initial data?
â Flater
Feb 9 at 12:50
You are right. Great idea. Thank you!
â InfernumDeus
Feb 12 at 3:51
add a comment |Â
2
How can the file be corrupted mid-processing? Are you updating the file on the disk as you go? Is it an option to simply process the file and only write to disk after processing has completed? Why not simply write to a different file and not touch the original one, to avoid losing the initial data?
â Flater
Feb 9 at 12:50
You are right. Great idea. Thank you!
â InfernumDeus
Feb 12 at 3:51
2
2
How can the file be corrupted mid-processing? Are you updating the file on the disk as you go? Is it an option to simply process the file and only write to disk after processing has completed? Why not simply write to a different file and not touch the original one, to avoid losing the initial data?
â Flater
Feb 9 at 12:50
How can the file be corrupted mid-processing? Are you updating the file on the disk as you go? Is it an option to simply process the file and only write to disk after processing has completed? Why not simply write to a different file and not touch the original one, to avoid losing the initial data?
â Flater
Feb 9 at 12:50
You are right. Great idea. Thank you!
â InfernumDeus
Feb 12 at 3:51
You are right. Great idea. Thank you!
â InfernumDeus
Feb 12 at 3:51
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
How great is the risk that your files will be corrupted? Is there a particular reason why you think this might happen, and if so, is that cause something you can address?
Are you concerned that it is your act of processing the file that might corrupt it? If so, you may want to do the opposite of what you're doing. Don't create a backup, but instead leave the original alone and create a working copy which you discard when you're done. If something goes wrong then at least your original file is still there, untouched.
This may sound facetious but it's not: If you don't trust your file system then it's not a good place for your application's data. How do you know that even after backing up, the file you're reading won't become corrupt as your're reading it? How do you know it wasn't already corrupt when it was backed up? How do you know that the backups won't be corrupted?
The best approach depends not only on how much you trust (or distrust) your storage but also how great the risk is if your data gets lost. If your server gets backed up regularly then that might be all the protection you need. Perhaps you could create a hash or a checksum for each file and store it with the file. When you process the file, validate that it matches the hash. That gives you about as much certainty as you could possibly want.
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
add a comment |Â
up vote
2
down vote
I am a little concerned about the static fields used to store timestamps in your BackupHelper
class. If someone was using your class in different threads, or backing up more than one at a time, processing them all, then restoring them all, they would potentially end up with incorrect timestamp fields.
One way to resolve this might be to use a Dictionary<string, List<string>>
, where the key is the filePath and the timestamps are the value.
Another thing would be to store the string "_backup"
as a const. Repeated hard-coded strings can sometimes lead to problems.
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
add a comment |Â
up vote
1
down vote
All I have for now
I don't like
File.Delete(originalFile + "_backup");
You are passing a FileInfo. I have not tested but I suspect you are getting an implicit cast to string.
FileInfo has a Delete method. Have BackupHelper return a FileInfo so you can delete directly.
The thing isoriginalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I passFileInfo
to this method. I definitely should useoriginalFile.FullName
in this case.
â InfernumDeus
Feb 12 at 4:04
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
How great is the risk that your files will be corrupted? Is there a particular reason why you think this might happen, and if so, is that cause something you can address?
Are you concerned that it is your act of processing the file that might corrupt it? If so, you may want to do the opposite of what you're doing. Don't create a backup, but instead leave the original alone and create a working copy which you discard when you're done. If something goes wrong then at least your original file is still there, untouched.
This may sound facetious but it's not: If you don't trust your file system then it's not a good place for your application's data. How do you know that even after backing up, the file you're reading won't become corrupt as your're reading it? How do you know it wasn't already corrupt when it was backed up? How do you know that the backups won't be corrupted?
The best approach depends not only on how much you trust (or distrust) your storage but also how great the risk is if your data gets lost. If your server gets backed up regularly then that might be all the protection you need. Perhaps you could create a hash or a checksum for each file and store it with the file. When you process the file, validate that it matches the hash. That gives you about as much certainty as you could possibly want.
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
add a comment |Â
up vote
2
down vote
accepted
How great is the risk that your files will be corrupted? Is there a particular reason why you think this might happen, and if so, is that cause something you can address?
Are you concerned that it is your act of processing the file that might corrupt it? If so, you may want to do the opposite of what you're doing. Don't create a backup, but instead leave the original alone and create a working copy which you discard when you're done. If something goes wrong then at least your original file is still there, untouched.
This may sound facetious but it's not: If you don't trust your file system then it's not a good place for your application's data. How do you know that even after backing up, the file you're reading won't become corrupt as your're reading it? How do you know it wasn't already corrupt when it was backed up? How do you know that the backups won't be corrupted?
The best approach depends not only on how much you trust (or distrust) your storage but also how great the risk is if your data gets lost. If your server gets backed up regularly then that might be all the protection you need. Perhaps you could create a hash or a checksum for each file and store it with the file. When you process the file, validate that it matches the hash. That gives you about as much certainty as you could possibly want.
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
How great is the risk that your files will be corrupted? Is there a particular reason why you think this might happen, and if so, is that cause something you can address?
Are you concerned that it is your act of processing the file that might corrupt it? If so, you may want to do the opposite of what you're doing. Don't create a backup, but instead leave the original alone and create a working copy which you discard when you're done. If something goes wrong then at least your original file is still there, untouched.
This may sound facetious but it's not: If you don't trust your file system then it's not a good place for your application's data. How do you know that even after backing up, the file you're reading won't become corrupt as your're reading it? How do you know it wasn't already corrupt when it was backed up? How do you know that the backups won't be corrupted?
The best approach depends not only on how much you trust (or distrust) your storage but also how great the risk is if your data gets lost. If your server gets backed up regularly then that might be all the protection you need. Perhaps you could create a hash or a checksum for each file and store it with the file. When you process the file, validate that it matches the hash. That gives you about as much certainty as you could possibly want.
How great is the risk that your files will be corrupted? Is there a particular reason why you think this might happen, and if so, is that cause something you can address?
Are you concerned that it is your act of processing the file that might corrupt it? If so, you may want to do the opposite of what you're doing. Don't create a backup, but instead leave the original alone and create a working copy which you discard when you're done. If something goes wrong then at least your original file is still there, untouched.
This may sound facetious but it's not: If you don't trust your file system then it's not a good place for your application's data. How do you know that even after backing up, the file you're reading won't become corrupt as your're reading it? How do you know it wasn't already corrupt when it was backed up? How do you know that the backups won't be corrupted?
The best approach depends not only on how much you trust (or distrust) your storage but also how great the risk is if your data gets lost. If your server gets backed up regularly then that might be all the protection you need. Perhaps you could create a hash or a checksum for each file and store it with the file. When you process the file, validate that it matches the hash. That gives you about as much certainty as you could possibly want.
answered Feb 10 at 3:33
Scott Hannen
24128
24128
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
add a comment |Â
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
Thank you! It was so obvious. I will work with temporary copy of file and apply changes to the original file only if processed version is correct.
â InfernumDeus
Feb 12 at 6:20
add a comment |Â
up vote
2
down vote
I am a little concerned about the static fields used to store timestamps in your BackupHelper
class. If someone was using your class in different threads, or backing up more than one at a time, processing them all, then restoring them all, they would potentially end up with incorrect timestamp fields.
One way to resolve this might be to use a Dictionary<string, List<string>>
, where the key is the filePath and the timestamps are the value.
Another thing would be to store the string "_backup"
as a const. Repeated hard-coded strings can sometimes lead to problems.
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
add a comment |Â
up vote
2
down vote
I am a little concerned about the static fields used to store timestamps in your BackupHelper
class. If someone was using your class in different threads, or backing up more than one at a time, processing them all, then restoring them all, they would potentially end up with incorrect timestamp fields.
One way to resolve this might be to use a Dictionary<string, List<string>>
, where the key is the filePath and the timestamps are the value.
Another thing would be to store the string "_backup"
as a const. Repeated hard-coded strings can sometimes lead to problems.
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
add a comment |Â
up vote
2
down vote
up vote
2
down vote
I am a little concerned about the static fields used to store timestamps in your BackupHelper
class. If someone was using your class in different threads, or backing up more than one at a time, processing them all, then restoring them all, they would potentially end up with incorrect timestamp fields.
One way to resolve this might be to use a Dictionary<string, List<string>>
, where the key is the filePath and the timestamps are the value.
Another thing would be to store the string "_backup"
as a const. Repeated hard-coded strings can sometimes lead to problems.
I am a little concerned about the static fields used to store timestamps in your BackupHelper
class. If someone was using your class in different threads, or backing up more than one at a time, processing them all, then restoring them all, they would potentially end up with incorrect timestamp fields.
One way to resolve this might be to use a Dictionary<string, List<string>>
, where the key is the filePath and the timestamps are the value.
Another thing would be to store the string "_backup"
as a const. Repeated hard-coded strings can sometimes lead to problems.
edited Feb 10 at 5:54
answered Feb 10 at 5:12
Rufus L
29614
29614
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
add a comment |Â
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
Great points. Thank you!
â InfernumDeus
Feb 12 at 3:58
add a comment |Â
up vote
1
down vote
All I have for now
I don't like
File.Delete(originalFile + "_backup");
You are passing a FileInfo. I have not tested but I suspect you are getting an implicit cast to string.
FileInfo has a Delete method. Have BackupHelper return a FileInfo so you can delete directly.
The thing isoriginalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I passFileInfo
to this method. I definitely should useoriginalFile.FullName
in this case.
â InfernumDeus
Feb 12 at 4:04
add a comment |Â
up vote
1
down vote
All I have for now
I don't like
File.Delete(originalFile + "_backup");
You are passing a FileInfo. I have not tested but I suspect you are getting an implicit cast to string.
FileInfo has a Delete method. Have BackupHelper return a FileInfo so you can delete directly.
The thing isoriginalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I passFileInfo
to this method. I definitely should useoriginalFile.FullName
in this case.
â InfernumDeus
Feb 12 at 4:04
add a comment |Â
up vote
1
down vote
up vote
1
down vote
All I have for now
I don't like
File.Delete(originalFile + "_backup");
You are passing a FileInfo. I have not tested but I suspect you are getting an implicit cast to string.
FileInfo has a Delete method. Have BackupHelper return a FileInfo so you can delete directly.
All I have for now
I don't like
File.Delete(originalFile + "_backup");
You are passing a FileInfo. I have not tested but I suspect you are getting an implicit cast to string.
FileInfo has a Delete method. Have BackupHelper return a FileInfo so you can delete directly.
edited Feb 9 at 15:39
answered Feb 9 at 15:34
paparazzo
4,8131730
4,8131730
The thing isoriginalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I passFileInfo
to this method. I definitely should useoriginalFile.FullName
in this case.
â InfernumDeus
Feb 12 at 4:04
add a comment |Â
The thing isoriginalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I passFileInfo
to this method. I definitely should useoriginalFile.FullName
in this case.
â InfernumDeus
Feb 12 at 4:04
The thing is
originalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I pass FileInfo
to this method. I definitely should use originalFile.FullName
in this case.â InfernumDeus
Feb 12 at 4:04
The thing is
originalFile + "_backup"
is an another file. So I decided that this would be a lesser evil. But I actually missed that I pass FileInfo
to this method. I definitely should use originalFile.FullName
in this case.â InfernumDeus
Feb 12 at 4:04
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187171%2fbacking-up-a-file-to-prevent-its-corruption-during-processing%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
How can the file be corrupted mid-processing? Are you updating the file on the disk as you go? Is it an option to simply process the file and only write to disk after processing has completed? Why not simply write to a different file and not touch the original one, to avoid losing the initial data?
â Flater
Feb 9 at 12:50
You are right. Great idea. Thank you!
â InfernumDeus
Feb 12 at 3:51