예제 #1
0
        public async Task <bool> WriteFileAsync(string relativePath, Func <Stream> content, ILibraryInstallationState state, CancellationToken cancellationToken)
        {
            FileInfo absolutePath = new FileInfo(Path.Combine(WorkingDirectory, relativePath));

            if (absolutePath.Exists)
            {
                return(true);
            }

            if (!absolutePath.FullName.StartsWith(WorkingDirectory))
            {
                throw new UnauthorizedAccessException();
            }

            absolutePath.Directory.Create();

            using (Stream stream = content.Invoke())
            {
                if (stream == null)
                {
                    return(false);
                }

                await VsHelpers.CheckFileOutOfSourceControlAsync(absolutePath.FullName);

                await FileHelpers.SafeWriteToFileAsync(absolutePath.FullName, stream, cancellationToken);
            }

            Logger.Log(string.Format(LibraryManager.Resources.Text.FileWrittenToDisk, relativePath.Replace(Path.DirectorySeparatorChar, '/')), LogLevel.Operation);

            return(true);
        }
예제 #2
0
        /// <inheritdoc />
        public async Task <bool> CopyFileAsync(string sourcePath, string destinationPath, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            string absoluteDestinationPath = Path.Combine(WorkingDirectory, destinationPath);

            if (!FileHelpers.IsUnderRootDirectory(absoluteDestinationPath, WorkingDirectory))
            {
                throw new UnauthorizedAccessException();
            }

            await VsHelpers.CheckFileOutOfSourceControlAsync(absoluteDestinationPath);

            bool result = await FileHelpers.CopyFileAsync(sourcePath, absoluteDestinationPath, cancellationToken);

            if (result)
            {
                Logger.Log(string.Format(LibraryManager.Resources.Text.FileWrittenToDisk, destinationPath.Replace(Path.DirectorySeparatorChar, '/')), LogLevel.Operation);
            }

            return(result);
        }