#region Disclaimer / License // Copyright (C) 2011, Kenneth Skovhede // http://www.hexad.dk, opensource@hexad.dk // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // #endregion using System; using System.Collections.Generic; using System.Text; using Duplicati.Library.Interface; namespace Duplicati.Library.Compression { /// /// This class exposes a local directory as a file archive. /// Used only internally for debugging, cannot be used as a storage method. /// public class FileArchiveDirectory : ICompression { string m_folder; /// /// Constructs a new FileArchive /// /// The folder to base the archive on public FileArchiveDirectory(string basefolder) { m_folder = Utility.Utility.AppendDirSeparator(basefolder); } #region IFileArchive Members /// /// Unsupported filename extension property, throws a MissingMethodException /// public string FilenameExtension { get { throw new MissingMethodException(); } } /// /// Unsupported displayname property, throws a MissingMethodException /// public string DisplayName { get { throw new MissingMethodException(); } } /// /// Unsupported description property, throws a MissingMethodException /// public string Description { get { throw new MissingMethodException(); } } /// /// Unsupported supported commands property, throws a MissingMethodException /// public IList SupportedCommands { get { throw new MissingMethodException(); } } /// /// Returns a list of files in the archive /// /// An optional prefix that is used to filter the list /// A filtered list of filenames public string[] ListFiles(string prefix) { if (prefix == null) prefix = ""; return Utility.Utility.EnumerateFiles(System.IO.Path.Combine(m_folder, prefix)).ToArray(); } /// /// Returns a list of directories in the archive /// /// An optional prefix that is used to filter the list /// A filtered list of directories public string[] ListDirectories(string prefix) { if (prefix == null) prefix = ""; return Utility.Utility.EnumerateFolders(System.IO.Path.Combine(m_folder, prefix)).ToArray(); } /// /// Lists all entries in the archive, folders are suffixed with System.IO.Path.DirectorySeparator /// /// An optional prefix that is used to filter the list /// A filtered list of entries public string[] ListEntries(string prefix) { if (prefix == null) prefix = ""; return Utility.Utility.EnumerateFileSystemEntries(System.IO.Path.Combine(m_folder, prefix)).ToArray(); } /// /// Returns all the bytes in a file as a byte[] /// /// The name of the file /// All the bytes contained in the file public byte[] ReadAllBytes(string file) { return System.IO.File.ReadAllBytes(System.IO.Path.Combine(m_folder, file)); } /// /// Reads all the lines in the given file /// /// The name of the file /// An array of text lines public string[] ReadAllLines(string file) { return System.IO.File.ReadAllLines(System.IO.Path.Combine(m_folder, file)); } /// /// Opens the file for reading /// /// The name of the file /// A stream with the file contents public System.IO.Stream OpenRead(string file) { return System.IO.File.OpenRead(System.IO.Path.Combine(m_folder, file)); } /// /// Opens the file for writing /// /// The name of the file /// A stream that can be used to write the file public System.IO.Stream OpenWrite(string file) { return System.IO.File.OpenWrite(System.IO.Path.Combine(m_folder, file)); } /// /// Writes all bytes in the array to the file /// /// The name of the file /// The data to write public void WriteAllBytes(string file, byte[] data) { System.IO.File.WriteAllBytes(System.IO.Path.Combine(m_folder, file), data); } /// /// Writes all the lines in the array to the file /// /// The name of the file /// The data to write public void WriteAllLines(string file, string[] data) { System.IO.File.WriteAllLines(System.IO.Path.Combine(m_folder, file), data); } /// /// Deletes a file from the archive /// /// The name of the file to delete public void DeleteFile(string file) { System.IO.File.Delete(System.IO.Path.Combine(m_folder, file)); } /// /// Creates a new empty file /// /// The name of the file to create /// The time the file was last written /// The stream used to access the file public System.IO.Stream CreateFile(string file) { return CreateFile(file, DateTime.Now); } /// /// Creates a new empty file /// /// The name of the file to create /// The time the file was last written /// The stream used to access the file public System.IO.Stream CreateFile(string file, DateTime lastWrite) { string path = System.IO.Path.Combine(m_folder, file); System.IO.Stream res = System.IO.File.Create(path); //TODO: This should actually be set when closing the stream System.IO.File.SetLastWriteTime(path, lastWrite); return res; } /// /// Deletes a folder from the archive /// /// The name of the directory to delete public void DeleteDirectory(string file) { System.IO.Directory.Delete(System.IO.Path.Combine(m_folder, file), false); } /// /// Adds a folder to the archive /// /// The name of the folder to add public void AddDirectory(string file) { System.IO.Directory.CreateDirectory(System.IO.Path.Combine(m_folder, file)); } /// /// Returns a value that indicates if the file exists /// /// The name of the file to test existence for /// True if the file exists, false otherwise public bool FileExists(string file) { return System.IO.File.Exists(System.IO.Path.Combine(m_folder, file)); } /// /// Returns a value that indicates if the folder exists /// /// The name of the folder to test existence for /// True if the folder exists, false otherwise public bool DirectoryExists(string file) { return System.IO.Directory.Exists(System.IO.Path.Combine(m_folder, file)); } /// /// Gets the current size of the archive /// public long Size { get { return Utility.Utility.GetDirectorySize(m_folder, null); } } /// /// Gets the last write time for a file /// /// The name of the file to query /// The last write time for the file public DateTime GetLastWriteTime(string file) { return System.IO.File.GetLastWriteTime(System.IO.Path.Combine(m_folder, file)); } /// /// The size of the current unflushed buffer /// public long FlushBufferSize { get { return 0; } } #endregion #region IDisposable Members /// /// Disposes the instance /// public void Dispose() { m_folder = null; } #endregion } }