|
| 1 | +using ITHit.FileSystem; |
| 2 | +using ITHit.FileSystem.Windows; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.IO; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace VirtualFileSystem |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Custom data stored with a file or folder placeholder, such original file/folder path. Max 4KB. |
| 13 | + /// </summary> |
| 14 | + /// <remarks>To avoid storing metatadata and keep footprit small, this class is is using custom serialization.</remarks> |
| 15 | + internal class CustomData |
| 16 | + { |
| 17 | + /// <summary> |
| 18 | + /// Keeps the original file/folder path. Used to sync file/folder from user file system to remote storage |
| 19 | + /// if this app was not running when the file/folder was moved or renamed. This field allows to avoid |
| 20 | + /// delete-create sequence during client to server synchronization after app failure. |
| 21 | + /// </summary> |
| 22 | + internal string OriginalPath = ""; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Serializes all custom data fields into the byte array. |
| 26 | + /// </summary> |
| 27 | + /// <returns>Byte array representing custom data.</returns> |
| 28 | + internal byte[] Serialize() |
| 29 | + { |
| 30 | + using (MemoryStream m = new MemoryStream()) |
| 31 | + { |
| 32 | + using (BinaryWriter writer = new BinaryWriter(m)) |
| 33 | + { |
| 34 | + writer.Write(OriginalPath); |
| 35 | + } |
| 36 | + return m.ToArray(); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Deserializes custom data from byte array into object. |
| 42 | + /// </summary> |
| 43 | + /// <param name="data">Byte array representing custom data.</param> |
| 44 | + /// <returns></returns> |
| 45 | + internal static CustomData Desserialize(byte[] data) |
| 46 | + { |
| 47 | + if(data == null) |
| 48 | + { |
| 49 | + throw new ArgumentNullException("data"); |
| 50 | + } |
| 51 | + |
| 52 | + CustomData obj = new CustomData(); |
| 53 | + using (MemoryStream m = new MemoryStream(data)) |
| 54 | + { |
| 55 | + using (BinaryReader reader = new BinaryReader(m)) |
| 56 | + { |
| 57 | + obj.OriginalPath = reader.ReadString(); |
| 58 | + } |
| 59 | + } |
| 60 | + return obj; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Placeholder methods to get and set custom data associated with a placeholder, such as OriginalPath. |
| 66 | + /// </summary> |
| 67 | + internal static class PlaceholderItemExtensions |
| 68 | + { |
| 69 | + public static void SetCustomData(this PlaceholderItem placeholder, string originalPath) |
| 70 | + { |
| 71 | + CustomData customData = new CustomData { OriginalPath = originalPath }; |
| 72 | + placeholder.SetCustomData(customData.Serialize()); |
| 73 | + } |
| 74 | + |
| 75 | + public static void SetCustomData(Microsoft.Win32.SafeHandles.SafeFileHandle safeHandle, string originalPath) |
| 76 | + { |
| 77 | + CustomData customData = new CustomData { OriginalPath = originalPath }; |
| 78 | + PlaceholderItem.SetCustomData(safeHandle, customData.Serialize()); |
| 79 | + } |
| 80 | + |
| 81 | + public static void SetOriginalPath(this PlaceholderItem placeholder, string originalPath) |
| 82 | + { |
| 83 | + byte[] customDataRaw = placeholder.GetCustomData(); |
| 84 | + CustomData customData = (customDataRaw.Length > 0) ? CustomData.Desserialize(customDataRaw) : new CustomData(); |
| 85 | + |
| 86 | + customData.OriginalPath = originalPath; |
| 87 | + placeholder.SetCustomData(customData.Serialize()); |
| 88 | + } |
| 89 | + |
| 90 | + public static string GetOriginalPath(this PlaceholderItem placeholder) |
| 91 | + { |
| 92 | + byte[] customDataRaw = placeholder.GetCustomData(); |
| 93 | + CustomData customData = (customDataRaw.Length > 0) ? CustomData.Desserialize(customDataRaw) : new CustomData(); |
| 94 | + return customData.OriginalPath; |
| 95 | + } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Returns true if the file was moved in the user file system and |
| 99 | + /// changes not yet synched to the remote storage. |
| 100 | + /// </summary> |
| 101 | + public static bool IsMoved(this PlaceholderItem placeholder) |
| 102 | + { |
| 103 | + // If original path was never set, the file was just created, not moved. |
| 104 | + string originalPath = placeholder.GetOriginalPath(); |
| 105 | + if (string.IsNullOrEmpty(originalPath)) |
| 106 | + { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + // Otherwise verify that current file path and original file path are equal. |
| 111 | + return !originalPath.TrimEnd(Path.DirectorySeparatorChar).Equals(placeholder.Path.TrimEnd(Path.DirectorySeparatorChar), StringComparison.InvariantCultureIgnoreCase); |
| 112 | + } |
| 113 | + |
| 114 | + /// <summary> |
| 115 | + /// Returns true if the item was created and must be synched to remote storage. |
| 116 | + /// </summary> |
| 117 | + /// <returns> |
| 118 | + /// True if the item was created in the user file system and does not exists |
| 119 | + /// in the remote storage. False otherwise. |
| 120 | + /// </returns> |
| 121 | + public static bool IsNew(this PlaceholderItem placeholder) |
| 122 | + { |
| 123 | + // ETag absence signals that the item is new. |
| 124 | + // However, ETag file may not exists during move operation, |
| 125 | + // additionally checking OriginalPath presence. |
| 126 | + // Can not rely on OriginalPath only, |
| 127 | + // because MS Office files are being deleted and re-created during transactional save. |
| 128 | + |
| 129 | + string originalPath = placeholder.GetOriginalPath(); |
| 130 | + |
| 131 | + bool eTagFileExists = File.Exists(ETag.GetETagFilePath(placeholder.Path)); |
| 132 | + |
| 133 | + return !eTagFileExists && string.IsNullOrEmpty(originalPath); |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | +} |
0 commit comments