/// <summary>
        /// Copies the <paramref name="sourceFileInfo"/> to the <paramref name="targetFileInfo"/>.
        /// </summary>
        /// <param name="sourceFileInfo">Source file to be copied.</param>
        /// <param name="targetFileInfo">Target file.</param>
        /// <returns><see langword="true"/>, if the file was created or overwritten; <see langword="false"/>, if the file was skipped.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="sourceFileInfo"/> or <paramref name="targetFileInfo"/> is <see langword="null"/>.</exception>
        /// <exception cref="FileNotFoundException"><paramref name="sourceFileInfo"/> does not exist.</exception>
        public static bool CopyFile(LongPathFileInfo sourceFileInfo, LongPathFileInfo targetFileInfo)
        {
            if (sourceFileInfo == null)
            {
                throw new ArgumentNullException(nameof(sourceFileInfo));
            }

            if (targetFileInfo == null)
            {
                throw new ArgumentNullException(nameof(targetFileInfo));
            }

            // Will throw an exception, if the source file does not exist.
            if (sourceFileInfo.Match(targetFileInfo))
            {
                return(false);
            }

            LongPathDirectory.CreateDirectory(targetFileInfo.DirectoryName);
            sourceFileInfo.CopyTo(targetFileInfo.FullName, true);

            LongPathCommon.SetAttributes(targetFileInfo.FullName, FileAttributes.Normal);
            LongPathCommon.SetTimestamps(targetFileInfo.FullName, sourceFileInfo.CreationTime, sourceFileInfo.LastAccessTime, sourceFileInfo.LastWriteTime);

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates all directories and subdirectories in the specified path unless they already exist.
        /// </summary>
        /// <param name="path">The directory to create.</param>
        public static void CreateDirectory(string path)
        {
#if DOTNET5_4
            Directory.CreateDirectory(path);
#else
            LongPathDirectory.CreateDirectory(path);
#endif
        }
        /// <summary>
        /// Creates all directories and subdirectories in the specified path unless they already exist.
        /// </summary>
        /// <param name="path">The directory to create.</param>
        public static void CreateDirectory(string path)
        {
            try
            {
                path = DMLibTestConstants.SupportUNCPath ?
                       LongPath.GetFullPath(LongPath.ToUncPath(path)) :
                       LongPath.GetFullPath(path);
            }
            catch (Exception)
            { }

#if DOTNET5_4
            Directory.CreateDirectory(path);
#else
            LongPathDirectory.CreateDirectory(path);
#endif
        }
Exemplo n.º 4
0
        protected override async Task <bool> DoWorkInternalAsync()
        {
            status = Status.Started;

            await Task.Run(
                () =>
            {
                if (this.TransferJob.Source.Type == TransferLocationType.AzureBlob &&
                    this.TransferJob.Destination.Type == TransferLocationType.FilePath)
                {
                    // Dummy transfer for downloading dummy blobs.
                    var filePath = (this.TransferJob.Destination as FileLocation).FilePath.ToLongPath();

                    if (LongPathFile.Exists(filePath))
                    {
                        string exceptionMessage = string.Format(
                            CultureInfo.CurrentCulture,
                            Resources.FailedToCreateDirectoryException,
                            filePath);

                        throw new TransferException(
                            TransferErrorCode.FailedToCreateDirectory,
                            exceptionMessage);
                    }
                    else
                    {
                        LongPathDirectory.CreateDirectory(filePath);
                    }
                }
                // Hint: adding new dummy directions here.
                else
                {
                    string exceptionMessage = string.Format(
                        CultureInfo.CurrentCulture,
                        Resources.UnsupportedDummyTransferException);

                    throw new TransferException(
                        TransferErrorCode.UnsupportedDummyTransfer,
                        exceptionMessage);
                }

                status = Status.Finished;
            }, this.CancellationToken);

            return(status == Status.Finished || status == Status.ErrorOccured);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a new <c>LazyCopy</c> file.
        /// </summary>
        /// <param name="path">Path to the reparse point to get data from.</param>
        /// <param name="fileData">Reparse file data to be set for the <paramref name="path"/>.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="path"/> is <see langword="null"/> or empty.
        ///     <para>-or-</para>
        /// <paramref name="fileData"/> is <see langword="null"/>.
        ///     <para>-or-</para>
        /// <paramref name="fileData"/> contains <see langword="null"/> or empty file path.
        /// </exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="fileData"/> contains negative file size.</exception>
        /// <exception cref="IOException">File cannot be created.</exception>
        /// <exception cref="InvalidOperationException">Reparse point data cannot be set.</exception>
        public static void CreateLazyCopyFile(string path, LazyCopyFileData fileData)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }

            if (fileData == null)
            {
                throw new ArgumentNullException(nameof(fileData));
            }

            if (string.IsNullOrEmpty(fileData.RemotePath))
            {
                throw new ArgumentNullException(nameof(fileData));
            }

            if (fileData.FileSize < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(fileData), fileData.FileSize, "File size is negative.");
            }

            string           normalizedPath = LongPathCommon.NormalizePath(path);
            LongPathFileInfo fileInfo       = new LongPathFileInfo(normalizedPath);

            bool shouldCreateFile = false;

            if (!fileInfo.Exists || fileInfo.Length > 0)
            {
                LongPathDirectory.CreateDirectory(fileInfo.DirectoryName);
                shouldCreateFile = true;
            }
            else if (!fileInfo.Attributes.HasFlag(FileAttributes.ReparsePoint))
            {
                shouldCreateFile = true;
            }
            else if (fileInfo.Attributes.HasFlag(FileAttributes.ReadOnly))
            {
                fileInfo.Attributes &= ~FileAttributes.ReadOnly;
            }

            if (shouldCreateFile)
            {
                using (fileInfo.Create())
                {
                    // Do nothing.
                }
            }

            // If the original file is empty, we don't need to set a reparse point for it.
            if (fileData.FileSize == 0)
            {
                return;
            }

            ReparsePointHelper.SetReparsePointData(
                path,
                new object[] // Custom serialization layout for the LazyCopyFileData object.
            {
                (long)(fileData.UseCustomHandler ? 1L : 0L),
                (long)fileData.FileSize,
                // Add the prefix, if the custom handling is needed for the file.
                Encoding.Unicode.GetBytes(
                    (fileData.UseCustomHandler ? fileData.RemotePath : PathHelper.ChangeDriveLetterToDeviceName(fileData.RemotePath))
                    + '\0')
            },
                LazyCopyFileHelper.LazyCopyReparseTag,
                LazyCopyFileHelper.LazyCopyReparseGuid);

            // Set the proper file attributes.
            LongPathCommon.SetAttributes(path, FileAttributes.ReparsePoint | FileAttributes.NotContentIndexed | FileAttributes.Offline);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Creates a directory.
 /// </summary>
 /// <exception cref="IOException">The directory cannot be created.</exception>
 public void Create()
 {
     LongPathDirectory.CreateDirectory(this.FullName);
     this.Refresh();
 }
Exemplo n.º 7
0
 /// <summary>
 /// Creates a subdirectory or subdirectories on the specified path.
 /// The specified path can be relative to this instance of the <see cref="LongPathDirectoryInfo"/> class.
 /// </summary>
 /// <param name="path">The specified path.</param>
 /// <returns>The last directory specified in <paramref name="path"/>.</returns>
 /// <exception cref="ArgumentException"><paramref name="path"/> does not specify a valid file path or contains invalid characters.</exception>
 /// <exception cref="ArgumentNullException"><paramref name="path"/> is <see langword="null"/>.</exception>
 /// <exception cref="DirectoryNotFoundException">The specified path is invalid, such as being on an unmapped drive.</exception>
 /// <exception cref="IOException">
 /// The subdirectory cannot be created.
 ///     <para>-or-</para>
 /// A file or directory already has the name specified by <paramref name="path"/>.
 /// </exception>
 /// <exception cref="PathTooLongException">The caller does not have code access permission to create the directory.</exception>
 /// <exception cref="NotSupportedException"><paramref name="path"/> contains a colon character (<c>:</c>) that is not part of a drive label ("<c>C:\</c>").</exception>
 public LongPathDirectoryInfo CreateSubdirectory(string path)
 {
     return(LongPathDirectory.CreateDirectory(Path.Combine(this.FullName, path)));
 }