示例#1
0
 private static void CopyDateTimeFields(
     FatDirectoryEntry src, FatDirectoryEntry dst)
 {
     dst.SetCreated(src.GetCreated());
     dst.SetLastAccessed(src.GetLastAccessed());
     dst.SetLastModified(src.GetLastModified());
 }
示例#2
0
        internal void Read()
        {
            var dataA = new byte[GetCapacity() * FatDirectoryEntry.SIZE];
            var data  = new MemoryStream(dataA);

            Read(data);
            data.SetLength(data.Position);
            data.Position = 0;

            for (var i = 0; i < GetCapacity(); i++)
            {
                var e =
                    FatDirectoryEntry.Read(data, IsReadOnly());

                if (e == null)
                {
                    break;
                }

                if (e.IsVolumeLabel())
                {
                    if (!this.isRoot)
                    {
                        throw new IOException(
                                  "volume label in non-root directory");
                    }

                    this.volumeLabel = e.GetVolumeLabel();
                }
                else
                {
                    entries.Add(e);
                }
            }
        }
示例#3
0
        public FatDirectoryEntry CreateSub(Fat fat)
        {
            var chain = new ClusterChain(fat, false);

            chain.SetChainLength(1);

            var entry = FatDirectoryEntry.Create(true);

            entry.SetStartCluster(chain.GetStartCluster());

            var dir =
                new ClusterChainDirectory(chain, false);

            /* add "." entry */

            var dot = FatDirectoryEntry.Create(true);

            dot.SetShortName(ShortName.Dot);
            dot.SetStartCluster(dir.GetStorageCluster());
            CopyDateTimeFields(entry, dot);
            dir.AddEntry(dot);

            /* add ".." entry */

            var dotDot = FatDirectoryEntry.Create(true);

            dotDot.SetShortName(ShortName.DotDot);
            dotDot.SetStartCluster(GetStorageCluster());
            CopyDateTimeFields(entry, dotDot);
            dir.AddEntry(dotDot);

            dir.Flush();

            return(entry);
        }
示例#4
0
 internal FatLfnDirectoryEntry(FatLfnDirectory parent,
                               FatDirectoryEntry realEntry, string fileName) : base(parent.IsReadOnly())
 {
     this.parent    = parent;
     this.RealEntry = realEntry;
     this.fileName  = fileName;
 }
示例#5
0
        internal FatDirectoryEntry[] CompactForm()
        {
            if (RealEntry.GetShortName().Equals(ShortName.Dot) ||
                RealEntry.GetShortName().Equals(ShortName.DotDot) ||
                RealEntry.HasShortNameOnly)
            {
                /* the dot entries must not have a LFN */
                return(new FatDirectoryEntry[] { RealEntry });
            }

            var totalEntrySize = TotalEntrySize();

            var entries =
                new FatDirectoryEntry[totalEntrySize];

            var checkSum = RealEntry.GetShortName().CheckSum();
            var j        = 0;

            for (var i = totalEntrySize - 2; i > 0; i--)
            {
                entries[i] = CreatePart(fileName.Substring(j * 13, j * 13 + 13),
                                        j + 1, checkSum, false);
                j++;
            }

            entries[0] = CreatePart(fileName.Substring(j * 13),
                                    j + 1, checkSum, true);

            entries[totalEntrySize - 1] = RealEntry;

            return(entries);
        }
示例#6
0
        public void RemoveEntry(FatDirectoryEntry entry)
        {
            if (entry == null)
            {
                throw new Exception();
            }

            entries.Remove(entry);
            ChangeSize(GetSize());
        }
示例#7
0
        internal FatFile GetFile(FatDirectoryEntry entry)
        {
            entryToFile.TryGetValue(entry, out var file);

            if (file == null)
            {
                file = FatFile.Get(fat, entry);
                entryToFile.Add(entry, file);
            }

            return(file);
        }
示例#8
0
        internal FatLfnDirectoryEntry(string name, ShortName sn, FatLfnDirectory parent, bool directory) : base(false)
        {
            this.parent = parent;
            fileName    = name;

            var now = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;

            RealEntry = FatDirectoryEntry.Create(directory);
            RealEntry.SetShortName(sn);
            RealEntry.SetCreated(now);
            RealEntry.SetLastAccessed(now);
        }
示例#9
0
        internal FatLfnDirectory GetDirectory(FatDirectoryEntry entry)
        {
            entryToDirectory.TryGetValue(entry, out var result);

            if (result == null)
            {
                var storage = Read(entry, fat);
                result = new FatLfnDirectory(storage, fat, IsReadOnly());
                entryToDirectory.Add(entry, result);
            }

            return(result);
        }
示例#10
0
        public static FatDirectoryEntry CreateVolumeLabel(string volumeLabel)
        {
            var data = new byte[SIZE];

            Array.Copy(
                Encoding.ASCII.GetBytes(volumeLabel), 0,
                data, 0,
                volumeLabel.Length);

            var result = new FatDirectoryEntry(data, false);

            result.SetFlags(F_VOLUME_ID);
            return(result);
        }
示例#11
0
        public void AddEntry(FatDirectoryEntry e)
        {
            if (e == null)
            {
                throw new Exception();
            }

            if (GetSize() == GetCapacity())
            {
                ChangeSize(GetCapacity() + 1);
            }

            entries.Add(e);
        }
示例#12
0
        private static ClusterChainDirectory Read(FatDirectoryEntry entry, Fat fat)
        {
            if (!entry.IsDirectory())
            {
                throw
                    new ArgumentException(entry + " is no directory");
            }

            var chain = new ClusterChain(
                fat, entry.GetStartCluster(),
                entry.IsReadonlyFlag());

            var result =
                new ClusterChainDirectory(chain, false);

            result.Read();
            return(result);
        }
示例#13
0
        internal static FatFile Get(Fat fat, FatDirectoryEntry entry)
        {
            if (entry.IsDirectory())
            {
                throw new ArgumentException(entry + " is a directory");
            }

            var cc = new ClusterChain(
                fat, entry.GetStartCluster(), entry.IsReadonlyFlag());

            if (entry.GetLength() > cc.GetLengthOnDisk())
            {
                throw new IOException(
                          "entry is larger than associated cluster chain");
            }

            return(new FatFile(entry, cc));
        }
示例#14
0
        public static FatDirectoryEntry Create(bool directory)
        {
            var result = new FatDirectoryEntry();

            if (directory)
            {
                result.SetFlags(F_DIRECTORY);
            }

            /* initialize date and time fields */

            var now = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;

            result.SetCreated(now);
            result.SetLastAccessed(now);
            result.SetLastModified(now);

            return(result);
        }
示例#15
0
        /// <summary>
        /// Flush the contents of this directory to the persistent storage
        /// </summary>
        public void Flush()
        {
            var dataA = new byte[GetCapacity() * FatDirectoryEntry.SIZE];
            var data  = new MemoryStream(dataA);

            for (var i = 0; i < entries.Count; i++)
            {
                var entry = entries[i];

                if (entry != null)
                {
                    entry.Write(data);
                }
            }

            /* TODO: the label could be placed directly the dot entries */

            if (volumeLabel != null)
            {
                var labelEntry =
                    FatDirectoryEntry.CreateVolumeLabel(volumeLabel);

                labelEntry.Write(data);
            }

            if (data.Length - data.Position > 0)
            {
                FatDirectoryEntry.WriteNullEntry(data);
            }

            data.SetLength(data.Position);
            data.Position = 0;

            Write(data);
            ResetDirty();
        }
示例#16
0
 private FatFile(FatDirectoryEntry myEntry, ClusterChain chain) : base(myEntry.IsReadOnly())
 {
     entry      = myEntry;
     this.chain = chain;
 }