Exemplo n.º 1
0
        public static void SetCustomData(Microsoft.Win32.SafeHandles.SafeFileHandle safeHandle, string originalPath)
        {
            CustomData customData = new CustomData {
                OriginalPath = originalPath
            };

            PlaceholderItem.SetCustomData(safeHandle, customData.Serialize());
        }
Exemplo n.º 2
0
        public static void SetOriginalPath(this PlaceholderItem placeholder, string originalPath)
        {
            byte[]     customDataRaw = placeholder.GetCustomData();
            CustomData customData    = (customDataRaw.Length > 0) ? CustomData.Deserialize(customDataRaw) : new CustomData();

            customData.OriginalPath = originalPath;
            placeholder.SetCustomData(customData.Serialize());
        }
Exemplo n.º 3
0
        public static void SetCustomData(this PlaceholderItem placeholder, string originalPath)
        {
            CustomData customData = new CustomData {
                OriginalPath = originalPath
            };

            placeholder.SetCustomData(customData.Serialize());
        }
        private async Task CreateOrUpdateFileAsync(string userFileSystemPath, bool create)
        {
            try
            {
                Program.RemoteStorageMonitorInstance.Enabled = false; // Disable RemoteStorageMonitor to avoid circular calls.

                FileInfo userFileSystemFile = new FileInfo(userFileSystemPath);
                FileInfo remoteStorageFile  = new FileInfo(remoteStoragePath);

                FileMode fileMode = create ? FileMode.CreateNew : FileMode.Open;


                await using (FileStream userFileSystemStream = userFileSystemFile.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    // If another thread is trying to sync the same file, this call will fail in other threads.
                    // In your implemntation you must lock your remote storage file, or block it for reading and writing by other means.
                    await using (FileStream remoteStorageStream = remoteStorageFile.Open(fileMode, FileAccess.Write, FileShare.None))
                    {
                        userFileSystemFile.Refresh(); // Ensures LastWriteTimeUtc is in sync with file content after Open() was called.

                        // Update ETag/LastWriteTime in user file system, so the synchronyzation or remote storage monitor would not start the new update.
                        byte[] customData = BitConverter.GetBytes(userFileSystemFile.LastWriteTime.ToBinary());
                        PlaceholderItem.SetCustomData(userFileSystemStream.SafeFileHandle, customData);

                        // Update remote storage file content.
                        await userFileSystemStream.CopyToAsync(remoteStorageStream);

                        remoteStorageStream.SetLength(userFileSystemStream.Length);

                        // Update remote storage file basic info.
                        WindowsFileSystemItem.SetFileInformation(remoteStorageStream.SafeFileHandle,
                                                                 userFileSystemFile.Attributes & (FileAttributes) ~FileAttributesExt.Pinned, // Remove Pinned flag.
                                                                 userFileSystemFile.CreationTimeUtc,
                                                                 userFileSystemFile.LastWriteTimeUtc,
                                                                 userFileSystemFile.LastAccessTimeUtc,
                                                                 userFileSystemFile.LastWriteTimeUtc);

                        // If you are using ETags, here you will also send to the remote storage a new file ETag.

                        PlaceholderItem.SetInSync(userFileSystemStream.SafeFileHandle, true);
                    }
                }
            }
            finally
            {
                Program.RemoteStorageMonitorInstance.Enabled = true;
            }
        }