private bool _filebackup(bool checkdate, bFile file) { int count = 0; bool uploaded = false; bFileDetails.newfile = file; bFileDetails.status = false; while (!bFileDetails.status && count < 10) { log.log("Trying to upload the file " + bFileDetails.newfile.local_path); count++; if (reference != null && checkdate) { if (file.last_modified.CompareTo(reference) > 0) { _message.message = "Uploading " + file.local_path; OnSendLogMessage(_message); _UpdateCachedFileETag(file); bFileDetails.status = file.upload(); } else { _message.message = "It is not changed since last backup" + file.local_path; OnSendLogMessage(_message); file.status = "not updated since last backup"; bFileDetails.status = true; } } else { _message.message = "Uploading " + file.local_path; OnSendLogMessage(_message); bFileDetails.status = file.upload(); } log.log("Status: " + bFileDetails.status.ToString() + " for " + bFileDetails.newfile.local_path); OnAddedFileToList(bFileDetails); } // In case of failure try again with a last resource if (bFileDetails.status == false) { string tempPath = System.IO.Path.GetTempPath(); string tempfilename = tempPath + "\\" + file.ETag; File.Delete(tempfilename); File.Copy(file.local_path, tempfilename); string old_local_path = file.local_path; file.local_path = tempfilename; _message.message = "Last try to upload " + file.local_path; OnSendLogMessage(_message); bFileDetails.status = file.upload(); log.log("Status with rename: " + bFileDetails.status.ToString() + " for " + bFileDetails.newfile.local_path); File.Delete(tempfilename); file.local_path = old_local_path; OnAddedFileToList(bFileDetails); } return bFileDetails.status; }
private void _UpdateCachedFileETag(bFile file) { if (!String.IsNullOrEmpty(file.local_path)) { if (file.last_modified.CompareTo(reference) == 1) { file.genETag(); file.status = "ETag cache"; if (FileCache.ContainsKey(file.local_path)) { FileCache[file.local_path] = file.ETag; } else { FileCache.Add(file.local_path, file.ETag); } } else { if (String.IsNullOrEmpty(file.ETag)) { if (FileCache.ContainsKey(file.local_path)) { file.ETag = FileCache[file.local_path]; } else { _message.message = "Cache miss for Etag" + file.local_path; OnSendLogMessage(_message); file.genETag(); file.status = "ETag cache"; FileCache.Add(file.local_path, file.ETag); } } } } }
private void OnChanged(object source, FileSystemEventArgs e) { _message.message = "Somefile changed.. " + e.FullPath; OnSendLogMessage(_message); FileAttributes attr = File.GetAttributes(e.FullPath); if ((attr & FileAttributes.Directory) == FileAttributes.Directory) { _message.message = "This is a directory, skipping for now:" + e.FullPath; OnSendLogMessage(_message); return; } // Calls the call back for the interface if (e.ChangeType.ToString() == "Created") { // Refactor add bFile file_to_queue = new bFile(e.FullPath, node); filelist.Add(file_to_queue.path, file_to_queue); // queue ( pointer it ? ) file_to_queue.action = "upload"; this.AddToQueue(file_to_queue); _message.message = "Fine, added this file to queue of processing for upload" + e.FullPath; OnSendLogMessage(_message); //filelist[newfile.path].upload(); } if (e.ChangeType.ToString() == "Deleted") { bFile file_to_queue = filelist[e.FullPath]; file_to_queue.action = "delete"; this.AddToQueue( file_to_queue ); //.delete(); _message.message = "Fine, added this file to queue of processing for delete" + e.FullPath; OnSendLogMessage(_message); } if (e.ChangeType.ToString() == "Changed") { // Class method for return just paths bFile file_to_queue = new bFile(e.FullPath, node); file_to_queue.action = "upload"; this.AddToQueue(file_to_queue); //filelist[file_to_queue.path].upload(); _message.message = "Fine, added this file to queue of processing for update" + e.FullPath; OnSendLogMessage(_message); } // This event is trigged now by the sincronize //OnUpdated(EventArgs.Empty); }
/* FileCacheLoadFileList() * * Create a merged list of files to delete, to sent to queue. * * first the files, after the directories, ordered inversed by lenght ( not the best yet but works for now ) * * In true, send the all files to queue direct above, after send all directories ordered to the queue * * When the queue works... :) * * TODO: Only delete from manifesto the file after it is really deleted from server * * Dot net 4.0 * var pathsdodelete = from element in paths.Keys * orderby element.Lenght * select element; */ private void FileCacheLoadFileList() { _message.message = "Reading cache file list"; OnSendLogMessage(_message); ReadFileCacheInfo(); List<string> todelete = new List<string>(); Dictionary<string, string> paths = new Dictionary<string, string>(); foreach (string filename in FileCache.Keys) { if (!filelist.ContainsKey(filename)) { // create the bFile first. // Race condition detected!!! if (Directory.Exists(filename) || File.Exists(filename)) { bFile cached = this.addfile(filename); cached.ETag = FileCache[filename]; } else { // Class method? bFile FileToDelete = new bFile(filename, this.node); _message.message = " " + filename + " marked to delete"; OnSendLogMessage(_message); if (FileToDelete.IsDirectory) { if (! paths.ContainsKey( FileToDelete.local_path ) ) paths.Add(FileToDelete.local_path, "d"); } else { todelete.Add(filename); } } } } List<string> pathstodelete = new List<string>(); foreach (string path in paths.Keys) { pathstodelete.Add(path); } pathstodelete.Sort(SortByLength); todelete.AddRange(pathstodelete); int count = 0; while (todelete.Count > 0 && count > 5 ) { count++; Thread.Sleep(1000); todelete = Delete(todelete); _message.message = "Trying again the files that failed to delete at server"; OnSendLogMessage(_message); } }
public void _FastAddFile(string path) { bFile newfile = new bFile(path, node); filelist.Add(newfile.path, newfile); }
// For each file on path... // prepare... // run public void AddToQueue(bFile file) { lock (this) { syncronize.Enqueue(file); } }
public bFile addfile(string path) { bFile newfile = new bFile(path, node); if (!filelist.ContainsKey(newfile.path)) { // When renaming the file, created/modified date does not work // Should be a flag to upload, but this is fine for now if ( ! FileCache.ContainsKey(path)) { newfile.last_modified = DateTime.Now; } filelist.Add(newfile.path, newfile); return newfile; } else { return filelist[newfile.path]; } }