Beispiel #1
0
        /// <inheritdoc />
        /// <remarks>Ref count limit is not implemented for the In Memory file system.</remarks>
        public CreateHardLinkResult CreateHardLink(AbsolutePath sourceFileName, AbsolutePath destinationFileName, bool replaceExisting)
        {
            lock (_drives)
            {
                if (!_useHardLinks)
                {
                    return(CreateHardLinkResult.FailedNotSupported);
                }

                FileObject fileObj = FindFileObject(sourceFileName);

                if (fileObj == null)
                {
                    return(CreateHardLinkResult.FailedSourceDoesNotExist);
                }

                if (destinationFileName.Length >= FileSystemConstants.MaxPath)
                {
                    // Please note, we use FileSystemConstants.MaxPath that returns
                    // ShortMaxPath or LongMaxPath depending on whether the system supports long paths or not.
                    return(CreateHardLinkResult.FailedPathTooLong);
                }

                FileObject destination = FindFileObjectAndParent(destinationFileName, out var parentDestination);

                if (parentDestination == null)
                {
                    throw new IOException(
                              string.Format(CultureInfo.InvariantCulture, "Parent of destination {0} does not exist", destinationFileName.Path));
                }

                // ReSharper disable PossibleNullReferenceException
                char sourceDrive      = sourceFileName.DriveLetter;
                char destinationDrive = destinationFileName.DriveLetter;

                // ReSharper restore PossibleNullReferenceException
                if (sourceDrive != destinationDrive)
                {
                    return(CreateHardLinkResult.FailedSourceAndDestinationOnDifferentVolumes);
                }

                if (!fileObj.CanAddLink())
                {
                    return(CreateHardLinkResult.FailedMaxHardLinkLimitReached);
                }

                if (destination != null)
                {
                    if (!replaceExisting)
                    {
                        return(CreateHardLinkResult.FailedDestinationExists);
                    }

                    try
                    {
                        destination.DeleteLink(destinationFileName, false, true);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        return(CreateHardLinkResult.FailedAccessDenied);
                    }
                }

                fileObj.AddLink(destinationFileName);
                parentDestination.Children[destinationFileName.FileName] = fileObj;
            }

            return(CreateHardLinkResult.Success);
        }