DigestContent() private method

private DigestContent ( [ stream ) : string
stream [
return string
Beispiel #1
0
        /// <summary>
        /// Creates a manifest node for a file.
        /// </summary>
        /// <param name="file">The file object to create a node for.</param>
        /// <param name="format">The manifest format containing digest rules.</param>
        /// <param name="externalXbits">A list of fully qualified paths of files that are named in the <see cref="FlagUtils.SymlinkFile"/>.</param>
        /// <param name="externalSymlinks">A list of fully qualified paths of files that are named in the <see cref="FlagUtils.SymlinkFile"/>.</param>
        /// <returns>The node for the list.</returns>
        /// <exception cref="NotSupportedException">The <paramref name="file"/> has illegal properties (e.g. is a device file, has line breaks in the filename, etc.).</exception>
        /// <exception cref="IOException">There was an error reading the file.</exception>
        /// <exception cref="UnauthorizedAccessException">You have insufficient rights to read the file.</exception>
        private ManifestNode GetFileNode(FileInfo file, ManifestFormat format, ICollection <string> externalXbits, ICollection <string> externalSymlinks)
        {
            if (_isUnixFS)
            {
                // Symlink
                string symlinkContents;
                if (FileUtils.IsSymlink(file.FullName, out symlinkContents))
                {
                    var symlinkData = Encoding.UTF8.GetBytes(symlinkContents);
                    return(new ManifestSymlink(format.DigestContent(symlinkData), symlinkData.Length, file.Name));
                }

                // Executable file
                if (FileUtils.IsExecutable(file.FullName))
                {
                    using (var stream = File.OpenRead(file.FullName))
                        return(new ManifestExecutableFile(format.DigestContent(stream), file.LastWriteTimeUtc.ToUnixTime(), file.Length, file.Name));
                }

                // Invalid file type
                if (!FileUtils.IsRegularFile(file.FullName))
                {
                    throw new NotSupportedException(string.Format(Resources.IllegalFileType, file.FullName));
                }
            }
            else
            {
                // External symlink
                if (externalSymlinks.Contains(file.FullName))
                {
                    using (var stream = File.OpenRead(file.FullName))
                        return(new ManifestSymlink(format.DigestContent(stream), file.Length, file.Name));
                }

                // External executable file
                if (externalXbits.Contains(file.FullName))
                {
                    using (var stream = File.OpenRead(file.FullName))
                        return(new ManifestExecutableFile(format.DigestContent(stream), file.LastWriteTimeUtc.ToUnixTime(), file.Length, file.Name));
                }
            }

            // Normal file
            using (var stream = File.OpenRead(file.FullName))
                return(new ManifestNormalFile(format.DigestContent(stream), file.LastWriteTimeUtc.ToUnixTime(), file.Length, file.Name));
        }
Beispiel #2
0
        /// <summary>
        /// Creates a manifest node for a directory.
        /// </summary>
        /// <param name="directory">The directory object to create a node for.</param>
        /// <param name="format">The manifest format containing digest rules.</param>
        /// <param name="rootPath">The fully qualified path of the root directory the manifest is being generated for.</param>
        /// <returns>The node for the list.</returns>
        /// <exception cref="IOException">There was an error reading the directory.</exception>
        /// <exception cref="UnauthorizedAccessException">You have insufficient rights to read the directory.</exception>
        private ManifestNode GetDirectoryNode(DirectoryInfo directory, ManifestFormat format, string rootPath)
        {
            // Directory symlinks
            string symlinkContents;

            if (_isUnixFS && FileUtils.IsSymlink(directory.FullName, out symlinkContents))
            {
                var symlinkData = Encoding.UTF8.GetBytes(symlinkContents);
                return(new ManifestSymlink(format.DigestContent(symlinkData), symlinkData.Length, directory.Name));
            }

            // Remove leading portion of path and use Unix slashes
            string trimmedName = directory.FullName.Substring(rootPath.Length).Replace(Path.DirectorySeparatorChar, '/');

            return(new ManifestDirectory(directory.LastWriteTime.ToUnixTime(), trimmedName));
        }
        /// <summary>
        /// Creates a manifest node for a file.
        /// </summary>
        /// <param name="file">The file object to create a node for.</param>
        /// <param name="format">The manifest format containing digest rules.</param>
        /// <param name="externalXbits">A list of fully qualified paths of files that are named in the <see cref="FlagUtils.SymlinkFile"/>.</param>
        /// <param name="externalSymlinks">A list of fully qualified paths of files that are named in the <see cref="FlagUtils.SymlinkFile"/>.</param>
        /// <returns>The node for the list.</returns>
        /// <exception cref="NotSupportedException">The <paramref name="file"/> has illegal properties (e.g. is a device file, has line breaks in the filename, etc.).</exception>
        /// <exception cref="IOException">There was an error reading the file.</exception>
        /// <exception cref="UnauthorizedAccessException">You have insufficient rights to read the file.</exception>
        private ManifestNode GetFileNode(FileInfo file, ManifestFormat format, ICollection<string> externalXbits, ICollection<string> externalSymlinks)
        {
            if (_isUnixFS)
            {
                // Symlink
                string symlinkContents;
                if (FileUtils.IsSymlink(file.FullName, out symlinkContents))
                {
                    var symlinkData = Encoding.UTF8.GetBytes(symlinkContents);
                    return new ManifestSymlink(format.DigestContent(symlinkData), symlinkData.Length, file.Name);
                }

                // Executable file
                if (FileUtils.IsExecutable(file.FullName))
                {
                    using (var stream = File.OpenRead(file.FullName))
                        return new ManifestExecutableFile(format.DigestContent(stream), file.LastWriteTimeUtc.ToUnixTime(), file.Length, file.Name);
                }

                // Invalid file type
                if (!FileUtils.IsRegularFile(file.FullName))
                    throw new NotSupportedException(string.Format(Resources.IllegalFileType, file.FullName));
            }
            else
            {
                // External symlink
                if (externalSymlinks.Contains(file.FullName))
                {
                    using (var stream = File.OpenRead(file.FullName))
                        return new ManifestSymlink(format.DigestContent(stream), file.Length, file.Name);
                }

                // External executable file
                if (externalXbits.Contains(file.FullName))
                {
                    using (var stream = File.OpenRead(file.FullName))
                        return new ManifestExecutableFile(format.DigestContent(stream), file.LastWriteTimeUtc.ToUnixTime(), file.Length, file.Name);
                }
            }

            // Normal file
            using (var stream = File.OpenRead(file.FullName))
                return new ManifestNormalFile(format.DigestContent(stream), file.LastWriteTimeUtc.ToUnixTime(), file.Length, file.Name);
        }
        /// <summary>
        /// Creates a manifest node for a directory.
        /// </summary>
        /// <param name="directory">The directory object to create a node for.</param>
        /// <param name="format">The manifest format containing digest rules.</param>
        /// <param name="rootPath">The fully qualified path of the root directory the manifest is being generated for.</param>
        /// <returns>The node for the list.</returns>
        /// <exception cref="IOException">There was an error reading the directory.</exception>
        /// <exception cref="UnauthorizedAccessException">You have insufficient rights to read the directory.</exception>
        private ManifestNode GetDirectoryNode(DirectoryInfo directory, ManifestFormat format, string rootPath)
        {
            // Directory symlinks
            string symlinkContents;
            if (_isUnixFS && FileUtils.IsSymlink(directory.FullName, out symlinkContents))
            {
                var symlinkData = Encoding.UTF8.GetBytes(symlinkContents);
                return new ManifestSymlink(format.DigestContent(symlinkData), symlinkData.Length, directory.Name);
            }

            // Remove leading portion of path and use Unix slashes
            string trimmedName = directory.FullName.Substring(rootPath.Length).Replace(Path.DirectorySeparatorChar, '/');
            return new ManifestDirectory(directory.LastWriteTime.ToUnixTime(), trimmedName);
        }