public MockFileSystemInfoStream(MockFileSystemInfo info, bool writable)
            : base()
        {
            this.writable = writable;
            this.info = info;

            if (!this.writable)
            {
                this.writable = true;
                Write(info.FileData, 0, info.FileData.Length);
                Position = 0;
                this.writable = false;
            }
        }
 public static MockFileSystemInfo CreateFileObject(string path, Stream content = null)
 {
     var file = new MockFileSystemInfo()
     {
         Path = path,
         ItemType = ReplacementFileSystem.ItemType.File
     };
     if (content != null)
     {
         using (MemoryStream ms = new MemoryStream())
         {
             content.CopyTo(ms);
             file.FileData = ms.ToArray();
         }
     }
     return file;
 }
        public virtual void CopyFile(string sourcePath, string targetPath, bool overwrite)
        {
            if (!overwrite && Locate(targetPath) != null)
                throw new IOException("The target file already exists.");

            MockFileSystemInfo sourceInfo = Locate(sourcePath);
            MockFileSystemInfo originalTarget = Locate(targetPath);

            if (originalTarget != null)
                Info.Remove(originalTarget);

            MockFileSystemInfo targetInfo = new MockFileSystemInfo(sourceInfo.ItemType, targetPath, Path.GetFileName(targetPath), sourceInfo.Attributes, sourceInfo.LastWriteTime, "");

            targetInfo.FileData = sourceInfo.FileData;

            Info.Add(targetInfo);
        }
        /// <summary>
        /// Adds a mock file to the MockFileSystem object.  Will create the full directory structure by default.
        /// </summary>
        /// <param name="file">The file.</param>
        public virtual void AddMockFile(MockFileSystemInfo file, Boolean createDirectoryTree = true)
        {
            var fullpath = new FileInfo(file.Path).DirectoryName;
            
            //Recurse a bit to add directories if required.
            if (createDirectoryTree)
                AddMockDirectoryStructure(fullpath);

            Info.Add(file);
        }
        public virtual Stream OpenFile(string path, FileMode mode, FileAccess access, FileShare share)
        {
            bool mustExist = mode == FileMode.Truncate || mode == FileMode.Open;
            bool cantExist = mode == FileMode.CreateNew;

            MockFileSystemInfo info = Locate(path);
            if (mustExist && (info == null || info.ItemType != ItemType.File))
                throw new FileNotFoundException();

            if (info != null && cantExist)
                throw new IOException("The file already exists.");

            if (info == null)
            {
                info = new MockFileSystemInfo(ItemType.File, path, DateTime.Now, "");

                Info.Add(info);
            }

            bool writable = access == FileAccess.ReadWrite || access == FileAccess.Write;

            if (writable)
                info.LastWriteTime = DateTime.Now;

            return new MockFileSystemInfoStream(info, writable);
        }