public void Printing()
        {
            StoragePath path = new StoragePath("////some\\..\\malformed/path\\\\here");

            Assert.AreEqual(path.ToString(), @"malformed\path\here");
            Assert.AreEqual(path.ToString(PathSeparator.ForwardSlash, true, true), "/malformed/path/here/");
            Assert.AreEqual(path.ToString(PathSeparator.BackSlash, false, true), @"malformed\path\here\");
        }
Ejemplo n.º 2
0
        public static void ShouldBeWithNormalizedPathSeparators(this StoragePath?path, string?other)
        {
            var first  = NormalizePathSeparators(path?.ToString(), path?.FileSystem.PathInformation);
            var second = NormalizePathSeparators(other, path?.FileSystem.PathInformation);

            first.ShouldBe(second);
        }
Ejemplo n.º 3
0
        private void DeleteConflictingDestinationFolderWithoutDeletingThisFolder(string fullDestinationPath)
        {
            // Both Move and Copy require manual deletion of a conflicting folder at the destination
            // with the ReplaceExisting option.
            // This can be a problem if we want to move/copy a folder to the same location,
            // because we'd delete the destination folder which is *also the source folder*.
            // In essence, the folder would be gone for good.
            // We prevent this with a little hack: Temporarily renaming the source folder
            // so that deleting the destination does not automatically delete the source.
            // After the deletion, we undo the rename and can continue with the actual moving/copying.
            var tmpFolderName = PhysicalPathHelper.GetTemporaryElementName();
            var tmpFolderPath = IOPath.Combine(_fullParentPath?.ToString() ?? "", tmpFolderName);

            Directory.Move(_fullPath.ToString(), tmpFolderPath);

            try
            {
                Directory.Delete(fullDestinationPath, recursive: true);
            }
            catch (DirectoryNotFoundException)
            {
            }
            finally
            {
                Directory.Move(tmpFolderPath, _fullPath.ToString());
            }
        }
Ejemplo n.º 4
0
        public override Task SetAttributesAsync(IOFileAttributes attributes, CancellationToken cancellationToken = default)
        {
            // There's no "native" API for setting file/folder attributes.
            // We can at least try to use System.IO's API - it should at least work in certain locations
            // like the application data.
            if (!EnumInfo.IsDefined(attributes))
            {
                throw new ArgumentException(ExceptionStrings.Enum.UndefinedValue(attributes), nameof(attributes));
            }

            return(Task.Run(async() =>
            {
                try
                {
                    // Get the folder to ensure that it exists and to throw the appropriate exception.
                    await FsHelper.GetFolderAsync(_fullPath, cancellationToken).ConfigureAwait(false);

                    cancellationToken.ThrowIfCancellationRequested();
                    File.SetAttributes(_fullPath.ToString(), attributes);
                }
                catch (FileNotFoundException ex)
                {
                    // Since we're using a File API, we must manually convert the FileNotFoundException.
                    throw new DirectoryNotFoundException(message: null, ex);
                }
            }, cancellationToken));
        }
Ejemplo n.º 5
0
        public override Task <Stream> CreateAndOpenAsync(
            bool recursive,
            CreationCollisionOption options,
            CancellationToken cancellationToken = default
            )
        {
            if (!EnumInfo.IsDefined(options))
            {
                throw new ArgumentException(ExceptionStrings.Enum.UndefinedValue(options), nameof(options));
            }

            return(Task.Run <Stream>(() =>
            {
                if (recursive)
                {
                    Directory.CreateDirectory(_fullParentPath.ToString());
                }

                try
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    return new FileStream(_fullPath.ToString(), options.ToFileMode());
                }
                catch (UnauthorizedAccessException ex)
                {
                    EnsureNoConflictingFolderExists(_fullPath.ToString(), ex);
                    throw;
                }
            }, cancellationToken));
        }
Ejemplo n.º 6
0
 internal PhysicalStorageFile(PhysicalFileSystem fileSystem, PhysicalStoragePath path)
     : base(fileSystem, path)
 {
     _fullPath       = path.FullPath;
     _fullParentPath = path.FullPath.Parent !;
     _fileInfo       = new FileInfo(_fullPath.ToString());
 }
        public void Combining()
        {
            StoragePath path = new StoragePath();

            path += "C:/";
            Assert.AreEqual(@"C:", path.ToString());

            path += (StoragePath)(new Uri("some/other", UriKind.RelativeOrAbsolute));
            Assert.AreEqual(@"C:\some\other", path.ToString());

            path = path.Combine("some///malformed\\\\other.some").SetExtension(".txt");
            Assert.AreEqual(@"C:\some\other\some\malformed\other.txt", path.ToString());

            path = path.SetExtension("txt");
            Assert.AreEqual(@"C:\some\other\some\malformed\other.txt", path.ToString());

            Assert.AreEqual("other.txt", path.ScopeToName().ToString());
            Assert.AreEqual("other", path.ScopeToNameWithoutExtension().ToString());
            Assert.AreEqual(@"C:\some\other\some\malformed", path.ParentDirectory.ToString());
            Assert.AreEqual(@"C:\some\other\some", path.GetNthParentDirectory(2).ToString());
        }
        public void Properties()
        {
            StoragePath path = new StoragePath("////some\\malformed/path\\\\here");

            Assert.AreEqual("here", path.Name);
            Assert.AreEqual("", path.Extension);

            path = path.SetExtension("txt");

            Assert.AreEqual(".txt", path.Extension);
            Assert.AreEqual("here.txt", path.Name);
            Assert.AreEqual(false, path.IsAbsolute);

            path = new StoragePath("C:\\") + path;

            Assert.AreEqual(true, path.IsAbsolute);
            Assert.AreEqual("C:somemalformedpathhere.txt", string.Join("", path.Segments));
            Assert.AreEqual(new Uri(path.ToString()), path.ToUri());
        }
Ejemplo n.º 9
0
        public override Task <StorageFolderProperties> GetPropertiesAsync(CancellationToken cancellationToken = default)
        {
            return(Task.Run(() =>
            {
                EnsureExists();

                // Attempting to get the real folder name can fail, e.g. the folder might have been deleted in between.
                // In such a case, simply return the last fetched name. It will happen rarely and is good enough
                // for such cases.
                cancellationToken.ThrowIfCancellationRequested();
                var realFolderName = FsHelper.GetRealDirectoryName(_fullPath.ToString()) ?? Path.Name;
                var lastWriteTime = Directory.GetLastWriteTimeUtc(_fullPath.ToString());

                return new StorageFolderProperties(
                    realFolderName,
                    IOPath.GetFileNameWithoutExtension(realFolderName),
                    PhysicalPathHelper.GetExtensionWithoutTrailingExtensionSeparator(realFolderName)?.ToNullIfEmpty(),
                    Directory.GetCreationTimeUtc(_fullPath.ToString()),
                    lastWriteTime
                    );
            }, cancellationToken));
        }