public AntiChunk makeChunk(int nn) { AntiChunk rv = new AntiChunk(); // to make the path first we back up to the uniq... int ix = ActualPath.LastIndexOf("."); ix = ActualPath.LastIndexOf(".", ix - 1); if (originalExtension) { ix = ActualPath.LastIndexOf(".", ix - 1); } // ... then we string it back together string ext = (originalExtension) ? Path.GetExtension(ActualPath) : ""; rv.ActualPath = ActualPath.Substring(0, ix) + "." + nn + "." + of + ext; // rv.ActualLength = ActualLength; rv.LogicalName = LogicalName; rv.BaseOffset = BaseOffset + ActualLength; rv.uniq = uniq; rv.n = nn; rv.of = of; rv.originalExtension = originalExtension; return(rv); }
// return a (approximate) representation of the chunk following this public AntiChunk next() { if (n >= of) { return(null); } int nn = n + 1; AntiChunk rv = makeChunk(nn); return(rv); }
public void Restore(string infile, string outpath) { CancelRequested = false; bool success = false; FileStream opf = null; FileStream inf = null; DateTime fileModTime = new DateTime();; try { FileInfo fi = new FileInfo(infile); if (!fi.Exists) { Logger.getLogger().log("input file does not exist"); return; } DirectoryInfo di = new DirectoryInfo(outpath); if (!di.Exists) { Logger.getLogger().log("output directory does not exist"); return; } fileModTime = fi.LastWriteTime; AntiChunk c = new AntiChunk(fi); if (c == null || !c.IsValid) { Logger.getLogger().log("invalid input file"); return; } string ofn = di.FullName; if (!ofn.EndsWith("\\")) { ofn += "\\"; } ofn += c.LogicalName; byte[] buffer = new byte[1024 * 64]; opf = new FileStream(ofn, FileMode.Create, FileAccess.Write); Logger.getLogger().log("total matching files needed: " + c.of); if (c.n != 1) { Logger.getLogger().log("restore must start from file #1, not file #" + c.n); c = c.makeChunk(1); } do { while (!File.Exists(c.ActualPath)) { if (!Logger.getLogger().logAndWait("press enter when the file is available: " + c.ActualPath)) { break; } } if (File.Exists(c.ActualPath)) { inf = new FileStream(c.ActualPath, FileMode.Open, FileAccess.Read, FileShare.Read, buffer.Length * 2, FileOptions.SequentialScan); Logger.getLogger().log("processing input file: " + c.ActualPath + " (" + inf.Length + " bytes)"); while (inf.Position < inf.Length) { int readbytes = buffer.Length; if (inf.Position + readbytes > inf.Length) { readbytes = (int)(inf.Length - inf.Position); } readbytes = inf.Read(buffer, 0, readbytes); opf.Write(buffer, 0, readbytes); if (CancelRequested) { throw new OperationCanceledException(); } } inf.Close(); inf = null; c = c.next(); success = true; // so far at least } else { c = null; Logger.getLogger().log("Giving up"); success = false; } } while (c != null); } catch (Exception ex) { Logger.getLogger().log("failed:" + ex.Message + ex.StackTrace); success = false; } finally { try { if (inf != null) { inf.Close(); } if (opf != null) { String fp = opf.Name; opf.Close(); if (fileModTime.Ticks > 0) { new FileInfo(fp).LastWriteTime = fileModTime; } } } catch (Exception ex) { Logger.getLogger().log("exception on close after apparent success:" + ex.Message + ex.StackTrace); } CFSRestoreCompletionHandler handler = this.CFSRestoreCompletion; if (null != handler) { handler(this, success); } } }
public void Restore(string infile, string outpath) { CancelRequested = false; bool success = false; FileStream opf = null; FileStream inf = null; DateTime fileModTime = new DateTime(); ; try { FileInfo fi = new FileInfo(infile); if (!fi.Exists) { Logger.getLogger().log("input file does not exist"); return; } DirectoryInfo di = new DirectoryInfo(outpath); if (!di.Exists) { Logger.getLogger().log("output directory does not exist"); return; } fileModTime = fi.LastWriteTime; AntiChunk c = new AntiChunk(fi); if (c == null || !c.IsValid) { Logger.getLogger().log("invalid input file"); return; } string ofn = di.FullName; if (!ofn.EndsWith("\\")) ofn += "\\"; ofn += c.LogicalName; byte[] buffer = new byte[1024 * 64]; opf = new FileStream(ofn, FileMode.Create, FileAccess.Write); Logger.getLogger().log("total matching files needed: " + c.of); if (c.n != 1) { Logger.getLogger().log("restore must start from file #1, not file #" + c.n); c = c.makeChunk(1); } do { while (!File.Exists(c.ActualPath)) { if (!Logger.getLogger().logAndWait("press enter when the file is available: " + c.ActualPath)) { break; } } if (File.Exists(c.ActualPath)) { inf = new FileStream(c.ActualPath, FileMode.Open, FileAccess.Read, FileShare.Read, buffer.Length * 2, FileOptions.SequentialScan); Logger.getLogger().log("processing input file: " + c.ActualPath + " (" + inf.Length + " bytes)"); while (inf.Position < inf.Length) { int readbytes = buffer.Length; if (inf.Position + readbytes > inf.Length) readbytes = (int)(inf.Length - inf.Position); readbytes = inf.Read(buffer, 0, readbytes); opf.Write(buffer, 0, readbytes); if (CancelRequested) throw new OperationCanceledException(); } inf.Close(); inf = null; c = c.next(); success = true; // so far at least } else { c = null; Logger.getLogger().log("Giving up"); success = false; } } while (c != null); } catch (Exception ex) { Logger.getLogger().log("failed:" + ex.Message + ex.StackTrace); success = false; } finally { try { if (inf != null) inf.Close(); if (opf != null) { String fp = opf.Name; opf.Close(); if (fileModTime.Ticks>0) new FileInfo(fp).LastWriteTime = fileModTime; } } catch (Exception ex) { Logger.getLogger().log("exception on close after apparent success:" + ex.Message + ex.StackTrace); } CFSRestoreCompletionHandler handler = this.CFSRestoreCompletion; if (null != handler) { handler(this, success); } } }
public AntiChunk makeChunk(int nn) { AntiChunk rv = new AntiChunk(); // to make the path first we back up to the uniq... int ix = ActualPath.LastIndexOf("."); ix = ActualPath.LastIndexOf(".", ix - 1); if (originalExtension) ix = ActualPath.LastIndexOf(".", ix - 1); // ... then we string it back together string ext = (originalExtension) ? Path.GetExtension(ActualPath) : ""; rv.ActualPath = ActualPath.Substring(0, ix) + "." + nn + "." + of + ext; // rv.ActualLength = ActualLength; rv.LogicalName = LogicalName; rv.BaseOffset = BaseOffset + ActualLength; rv.uniq = uniq; rv.n = nn; rv.of = of; rv.originalExtension = originalExtension; return rv; }