public void AddHigherChange(ulong id, StorageFile change) { if (!HigherChanges.ContainsKey(id)) HigherChanges[id] = new List<StorageItem>(); HigherChanges[id].Add(change); }
public FileItem(StorageView view, FolderNode parent, StorageFile file, bool temp) { View = view; Folder = parent; SubItems.Add(""); SubItems.Add(""); Details = file; Temp = temp; UpdateInterface(); }
private LocalFile CreateNewFile(string name) { StorageFile info = new StorageFile(); LocalFile file = new LocalFile(info); info.UID = Utilities.StrongRandUInt64(Core.StrongRndGen); info.Name = name; info.Date = Core.TimeNow.ToUniversalTime(); info.Revs = 5; file.Archived.SafeAddFirst(info); return file; }
public DiffForm(InfoPanel info, ulong whoID, string what, StorageFile file, bool history) { InitializeComponent(); Core = info.ParentView.Core; Info = info; Target = file; TargetID = whoID; TargetHistory = history; Text = Target.Name + " Differences"; // set what txt // my/ben's Changes // my/ben's Integrated Changes // my/ben's History from <date> string who = (whoID == Core.UserID) ? "My" : (Core.GetName(whoID) + "'s"); WhatLabel.Text = who + " " + what; // local if (!info.CurrentFile.Temp) { if (Utilities.MemCompare(Target.InternalHash, ((StorageFile)Info.CurrentFile.Details).InternalHash)) LocalNote.Text = "Identical"; } else CurrentRadio.Enabled = false; // changes foreach (ChangeRow change in Info.SortChanges(Info.CurrentChanges)) ChangesCombo.Items.Add(new ComboFileItem(this, change.ID, (StorageFile) change.Item)); if (ChangesCombo.Items.Count > 0) ChangesCombo.SelectedIndex = 0; else ChangesRadio.Enabled = false; // integrated foreach (ChangeRow integrated in Info.SortChanges(Info.CurrentIntegrated)) IntegratedCombo.Items.Add(new ComboFileItem(this, integrated.ID, (StorageFile)integrated.Item)); if (IntegratedCombo.Items.Count > 0) IntegratedCombo.SelectedIndex = 0; else IntegratedRadio.Enabled = false; // history info.CurrentFile.Archived.LockReading(delegate() { foreach (StorageFile item in info.CurrentFile.Archived) HistoryCombo.Items.Add(new ComboFileItem(this, 0, item)); }); if (HistoryCombo.Items.Count > 0) HistoryCombo.SelectedIndex = 0; else HistoryRadio.Enabled = false; // using UsingCombo.Items.Add("WinMerge"); UsingCombo.Items.Add("Open Seperately"); UsingCombo.Items.Add("Another Tool..."); UsingCombo.SelectedIndex = 0; }
public StorageFile Clone() { // clones everything except notes StorageFile clone = new StorageFile(); clone.UID = UID; clone.Name = Name; clone.Date = Date; clone.Flags = Flags; clone.Revs = Revs; clone.Size = Size; clone.Hash = Hash; clone.HashID = HashID; clone.FileKey = FileKey; clone.InternalSize = InternalSize; clone.InternalHash = InternalHash; clone.InternalHashID = InternalHashID; return clone; }
public FileItem AddFileInfo(StorageFile info, bool remote) { if (!Files.ContainsKey(info.UID)) Files[info.UID] = new FileItem(View, this, info, remote); FileItem file = Files[info.UID]; if (!remote) { if (info.IntegratedID != 0) file.Integrated.SafeAdd(info.IntegratedID, info); else file.Archived.SafeAddLast(info); } return file; }
public void ReadyChange(LocalFile file, StorageFile newInfo) { Modified = true; PeriodicSave = true; file.Modify(Core.TimeNow, newInfo); }
// used for unexpected (file) and existing errors public LockError(string path, string message, bool isFile, LockErrorType type, StorageFile file, bool history) { Path = path; Message = message; IsFile = isFile; Type = type; File = file; History = history; }
public string UnlockFile(ulong dht, uint project, string path, StorageFile file, bool history, List<LockError> errors) { // path needs to include name, because for things like history files name is diff than file.Info string finalpath = GetRootPath(dht, project) + path; finalpath += history ? Path.DirectorySeparatorChar + ".history" + Path.DirectorySeparatorChar : Path.DirectorySeparatorChar.ToString(); if (!CreateFolder(finalpath, errors, false)) return null; finalpath += history ? GetHistoryName(file) : file.Name; // file not in storage if(!FileMap.SafeContainsKey(file.HashID) || !File.Exists(GetFilePath(file.HashID))) { errors.Add(new LockError(finalpath, "", true, LockErrorType.Missing)); return null; } // check if already unlocked if (File.Exists(finalpath) && file.IsFlagged(StorageFlags.Unlocked)) return finalpath; // file already exists if(File.Exists(finalpath)) { // ask user about local if (dht == Core.UserID) { errors.Add(new LockError(finalpath, "", true, LockErrorType.Existing, file, history)); return null; } // overwrite remote else { try { File.Delete(finalpath); } catch { // not an existing error, dont want to give user option to 'use' the old remote file errors.Add(new LockError(finalpath, "", true, LockErrorType.Unexpected, file, history)); return null; } } } // extract file try { Utilities.DecryptTagFile(GetFilePath(file.HashID), finalpath, file.FileKey, Core); } catch (Exception ex) { Core.Network.UpdateLog("Storage", "UnlockFile: " + ex.Message); errors.Add(new LockError(finalpath, "", true, LockErrorType.Unexpected, file, history)); return null; } file.SetFlag(StorageFlags.Unlocked); if (dht != Core.UserID) { //FileInfo info = new FileInfo(finalpath); //info.IsReadOnly = true; } // local else if (Working.ContainsKey(project) ) { // let caller trigger event because certain ops unlock multiple files // set watch on root path Working[project].StartWatchers(); } return finalpath; }
public void LockFile(ulong dht, uint project, string path, StorageFile file, bool history) { string finalpath = GetRootPath(dht, project) + path; if (history) finalpath += Path.DirectorySeparatorChar + ".history" + Path.DirectorySeparatorChar + GetHistoryName(file); else finalpath += Path.DirectorySeparatorChar + file.Name; try { if (File.Exists(finalpath)) File.Delete(finalpath); file.RemoveFlag(StorageFlags.Unlocked); } catch { } }
public bool IsFileUnlocked(ulong dht, uint project, string path, StorageFile file, bool history) { string finalpath = GetRootPath(dht, project) + path; if (history) finalpath += Path.DirectorySeparatorChar + ".history" + Path.DirectorySeparatorChar + GetHistoryName(file); else finalpath += Path.DirectorySeparatorChar + file.Name; return File.Exists(finalpath); }
public bool FileExists(StorageFile file) { if (FileMap.SafeContainsKey(file.HashID) && File.Exists(GetFilePath(file.HashID))) return true; return false; }
public string DownloadStatus(StorageFile file) { // returns null if file not being handled by transfer component if (file.Hash == null) // happens if file is being added to storage return null; return Core.Transfers.GetDownloadStatus(ServiceID, file.Hash, file.Size); }
public void DownloadFile(ulong id, StorageFile file) { // called from hash thread if (Core.InvokeRequired) { Core.RunInCoreAsync(delegate() { DownloadFile(id, file); }); return; } // if file still processing return if (file.Hash == null) return; FileDetails details = new FileDetails(ServiceID, FileTypeData, file.Hash, file.Size, null); Core.Transfers.StartDownload(id, details, GetFilePath(file.HashID), new EndDownloadHandler(EndDownloadFile), new object[] { file }); }
private void ReviseFile(HashPack pack, StorageFile info) { // called from hash thread if (Core.InvokeRequired) { Core.RunInCoreAsync(() => ReviseFile(pack, info)); return; } if (Working.ContainsKey(pack.Project)) Working[pack.Project].ReadyChange(pack.File, info); CallFileUpdate(pack.Project, pack.Dir, info.UID, WorkingChange.Updated); }
public LocalFile AddFileInfo(StorageFile info) { LocalFile file = null; if (!Files.SafeTryGetValue(info.UID, out file)) { file = new LocalFile(info); Files.SafeAdd(info.UID, file); } if(info.IntegratedID != 0) file.Integrated.SafeAdd(info.IntegratedID, info); else file.Archived.SafeAddLast(info); return file; }
void ReplaceFile(LocalFile file, StorageFile replacement, string dir, bool setModified, List<LockError> errors) { // this will lock file if it is unlocked bool unlocked = Storages.IsFileUnlocked(Core.UserID, ProjectID, dir, file.Info, false); Storages.LockFile(Core.UserID, ProjectID, dir, file.Info, false); if (setModified) ReadyChange(file, replacement); else { file.Info = replacement; file.Archived.SafeAddFirst(replacement); } if (unlocked) Storages.UnlockFile(Core.UserID, ProjectID, dir, file.Info, false, errors); }
public OpFile(StorageFile file) { HashID = file.HashID; Hash = file.Hash; Size = file.Size; Key = file.FileKey; Working = true; }
public LocalFile(StorageFile info) { Info = info; }
private string GetHistoryName(StorageFile file) { string name = file.Name; int pos = name.LastIndexOf('.'); if (pos == -1) pos = name.Length; string tag = "unhashed"; if(file.InternalHash != null) tag = Utilities.BytestoHex(file.InternalHash, 0, 3, false); name = name.Insert(pos, "-" + tag); return name; }
public void IntegrateFile(string path, ulong who, StorageFile change) { // put file in local's integration map for this user id LocalFolder folder = GetLocalFolder(path); LocalFile file = null; if(!folder.Files.SafeTryGetValue(change.UID, out file)) return; file.Integrated.SafeAdd(who, change.Clone()); // dont set file.info modified, because hash hasn't changed Modified = true; PeriodicSave = true; Storages.CallFileUpdate(ProjectID, path, file.Info.UID, WorkingChange.Updated); }
public void ReplaceFile(string path, StorageFile replacement, List<LockError> errors) { string name = Path.GetFileName(path); string dir = Utilities.StripOneLevel(path); LocalFolder folder = GetLocalFolder(dir); LocalFile file = folder.GetFile(name); ReplaceFile(file, replacement, dir, true, errors); Storages.CallFileUpdate(ProjectID, folder.GetPath(), file.Info.UID, WorkingChange.Updated); }
private void SelectFolder(FolderNode folder) { if (folder == null) return; /* * * if (!SelectedInfo.IsFile && SelectedInfo.CurrentFolder != null) { if (SelectedInfo.CurrentFolder.Details.UID == SelectedFolder.Details.UID) SelectedInfo.ShowItem(SelectedFolder, null); else SelectedInfo.ShowDefault(); } * */ bool infoSet = false; string infoPath = SelectedInfo.CurrentPath; SelectedFolder = folder; folder.Selected = true; string dirpath = null; if (Working != null) dirpath = Working.RootPath + folder.GetPath(); ulong selectedUID = 0; if (SelectedInfo.CurrentItem != null) selectedUID = SelectedInfo.CurrentItem.UID; FileListView.Items.Clear(); FileListView.SmallImageList = FileIcons; // add sub folders if (!FoldersButton.Checked && folder.Parent.GetType() == typeof(FolderNode)) { FileItem dots = new FileItem(this, new FolderNode(this, new StorageFolder(), folder.Parent, false)); dots.Folder.Details.Name = ".."; dots.Text = ".."; if (string.Compare("..", infoPath, true) == 0) { infoSet = true; dots.Selected = true; SelectedInfo.ShowDots(); } FileListView.Items.Add(dots); } foreach (FolderNode sub in folder.Nodes) { FileItem subItem = new FileItem(this, sub); if (string.Compare(sub.GetPath(), infoPath, true) == 0) { infoSet = true; subItem.Selected = true; SelectedInfo.ShowItem(sub, null); } FileListView.Items.Add(subItem); } // if folder unlocked, add untracked folders, mark temp if(folder.Details.IsFlagged(StorageFlags.Unlocked) && Directory.Exists(dirpath)) try { foreach (string dir in Directory.GetDirectories(dirpath)) { string name = Path.GetFileName(dir); if (name.CompareTo(".history") == 0) continue; if (folder.GetFolder(name) != null) continue; StorageFolder details = new StorageFolder(); details.Name = name; FileItem tempFolder = new FileItem(this, new FolderNode(this, details, folder, true)); if (string.Compare(tempFolder.GetPath(), infoPath, true) == 0) { infoSet = true; tempFolder.Selected = true; SelectedInfo.ShowItem(tempFolder.Folder, null); } FileListView.Items.Add(tempFolder); } } catch { } // add tracked files foreach (FileItem file in folder.Files.Values) if (!file.Details.IsFlagged(StorageFlags.Archived) || GhostsButton.Checked) { if (string.Compare(file.GetPath(), infoPath, true) == 0) { infoSet = true; file.Selected = true; SelectedInfo.ShowItem(folder, file); } else file.Selected = false; FileListView.Items.Add(file); } // if folder unlocked, add untracked files, mark temp if (folder.Details.IsFlagged(StorageFlags.Unlocked) && Directory.Exists(dirpath)) try { foreach (string filepath in Directory.GetFiles(dirpath)) { string name = Path.GetFileName(filepath); if (folder.GetFile(name) != null) continue; StorageFile details = new StorageFile(); details.Name = name; details.InternalSize = new FileInfo(filepath).Length; FileItem tempFile = new FileItem(this, folder, details, true); if (string.Compare(tempFile.GetPath(), infoPath, true) == 0) { infoSet = true; tempFile.Selected = true; SelectedInfo.ShowItem(folder, tempFile); } FileListView.Items.Add(tempFile); } } catch { } UpdateListItems(); if (!infoSet && SelectedFolder.GetPath() == infoPath) { infoSet = true; SelectedInfo.ShowItem(SelectedFolder, null); } if (!infoSet) SelectedInfo.ShowDiffs(); }
public void Modify(DateTime time, StorageFile newInfo) { // Details is mirror of file in working folder, it can change multiple times before the user commits, archive the // original info when the user starts messing with stuff, only once, dont want to archive stuff not accessible on the network if (Info.IsFlagged(StorageFlags.Modified)) { Info = newInfo; Archived.SafeRemoveFirst(); Archived.SafeAddFirst(newInfo); } else { Archived.SafeAddFirst(newInfo); Info = newInfo; } Info.SetFlag(StorageFlags.Modified); Info.Date = time.ToUniversalTime(); }
public void TrackFile(string path, StorageFile track) { LocalFolder folder = GetLocalFolder(path); if (folder == null) return; // increase references OpFile commonFile = null; if (Storages.FileMap.SafeTryGetValue(track.HashID, out commonFile)) commonFile.References++; LocalFile file = new LocalFile(track); file.Info.SetFlag(StorageFlags.Modified); file.Archived.SafeAddFirst(track); folder.AddFile(file); Modified = true; PeriodicSave = true; Storages.CallFileUpdate(ProjectID, folder.GetPath(), file.Info.UID, WorkingChange.Created); }
public void UnintegrateFile(string path, ulong who, StorageFile change) { // put file in local's integration map for this user id LocalFolder folder = GetLocalFolder(path); LocalFile file = null; if (!folder.Files.SafeTryGetValue(change.UID, out file)) return; file.Integrated.SafeRemove(who); Modified = true; PeriodicSave = true; Storages.CallFileUpdate(ProjectID, path, file.Info.UID, WorkingChange.Updated); }
public static StorageFile Decode(G2Header root) { StorageFile file = new StorageFile(); G2Header child = new G2Header(root.Data); while (G2Protocol.ReadNextChild(root, child) == G2ReadResult.PACKET_GOOD) { if (!G2Protocol.ReadPayload(child)) continue; switch (child.Name) { case Packet_UID: file.UID = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Name: file.Name = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Date: file.Date = DateTime.FromBinary(BitConverter.ToInt64(child.Data, child.PayloadPos)); break; case Packet_Flags: file.Flags = (StorageFlags) BitConverter.ToUInt16(child.Data, child.PayloadPos); break; case Packet_Note: file.Note = UTF8Encoding.UTF8.GetString(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Revs: file.Revs = child.Data[child.PayloadPos]; break; case Packet_Size: file.Size = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_Hash: file.Hash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); file.HashID = BitConverter.ToUInt64(file.Hash, 0); break; case Packet_FileKey: file.FileKey = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_InternalSize: file.InternalSize = CompactNum.ToInt64(child.Data, child.PayloadPos, child.PayloadSize); break; case Packet_InternalHash: file.InternalHash = Utilities.ExtractBytes(child.Data, child.PayloadPos, child.PayloadSize); file.InternalHashID = BitConverter.ToUInt64(file.InternalHash, 0); break; case Packet_Integrated: file.IntegratedID = BitConverter.ToUInt64(child.Data, child.PayloadPos); break; case Packet_Scope: file.Scope[BitConverter.ToUInt64(child.Data, child.PayloadPos)] = BitConverter.ToInt16(child.Data, child.PayloadPos + 8); break; } } return file; }
public ComboFileItem(DiffForm diff, ulong id, StorageFile file) { File = file; ID = id; if (id == 0) Text = file.Date.ToLocalTime().ToString(); else Text = diff.Core.GetName(id); if (Utilities.MemCompare(file.InternalHash, diff.Target.InternalHash)) Text += " (Identical)"; else if(!diff.Info.Storages.FileExists(file) ) Text += " (Unavailable)"; }