コード例 #1
0
ファイル: FileStorage.cs プロジェクト: supcry/Plex
        private FileNode PreRead(string path)
        {
            var fullpath = Path.Combine(WorkDir, path);
            var fi = new FileInfo(fullpath);
            if (fi.Exists)
            {
                var size = fi.Length;
                ulong hash;
                if (_masterCon != null)
                {
                    var gw = new StorageClient(_taskName, _masterCon);
                    var gwSize = gw.GetSize(path);
                    if (gwSize != size)
                    {
                        if (gwSize.HasValue)
                            fi.Delete();
                        return null;
                    }
                    hash = HashLib.HashFactory.Hash64.CreateMurmur2().ComputeFile(fullpath).GetULong();
                    var gwHash = gw.GetHash(path);
                    if (gwHash != hash)
                    {
                        if (gwHash.HasValue)
                            fi.Delete();
                        return null;
                    }
                }
                else
                    hash = HashLib.HashFactory.Hash64.CreateMurmur2().ComputeFile(fullpath).GetULong();

                var n = new FileNode
                {
                    Hash = hash,
                    Length = fi.Length,
                    ChangeDate = fi.LastWriteTime
                };
                _fset.Tree.Add(path, n);
                return n;
            }
            return null;
        }
コード例 #2
0
ファイル: FileStorage.cs プロジェクト: supcry/Plex
        private FileNode Download(string path, bool check = false)
        {
            if (_masterCon == null)
                return null;

            var fullpath = Path.Combine(WorkDir, path);
            //Debug.Assert(!File.Exists(fullpath));
            var gw = new StorageClient(_taskName, _masterCon);
            using(var f = File.Create(fullpath, FileNode.MaxCachedSize))
            {
                long pos = 0;
                while (true)
                {
                    byte[] buf = gw.ReadData(path, pos, FileNode.MaxCachedSize);
                    f.Write(buf, 0, buf.Length);
                    pos += buf.Length;
                    if (buf.Length != FileNode.MaxCachedSize)
                        break;
                }
                f.Flush();
            }

            var ret = PreRead(path);
            if (check)
            {
                var len = gw.GetSize(path);
                var hs = gw.GetHash(path);
                Debug.Assert(len.HasValue && hs.HasValue);
                if (ret.Length != len.Value)
                    throw new Exception("File '" + path + "' corrupted: length");
                if (ret.Hash != hs.Value)
                    throw new Exception("File '" + path + "' corrupted: hash wrong");
            }

            return ret;
        }