Ejemplo n.º 1
0
        public SumStruct ReceiveSums(ClientInfo cInfo)
        {
            IOStream  f = cInfo.IoStream;
            SumStruct s = new SumStruct();
            int       i;
            int       offset = 0;

            ReadSumHead(cInfo, ref s);
            s.sums = null;

            if (options.verbose > 3)
            {
                Log.WriteLine("count=" + s.count + " n=" + s.bLength + " rem=" + s.remainder);
            }

            if (s.count == 0)
            {
                return(s);
            }

            s.sums = new SumBuf[s.count];

            for (i = 0; i < s.count; i++)
            {
                s.sums[i]        = new SumBuf();
                s.sums[i].sum1   = (UInt32)f.readInt();
                s.sums[i].sum2   = f.ReadBuffer(s.s2Length);
                s.sums[i].offset = offset;
                s.sums[i].flags  = 0;

                if (i == s.count - 1 && s.remainder != 0)
                {
                    s.sums[i].len = s.remainder;
                }
                else
                {
                    s.sums[i].len = s.bLength;
                }
                offset += (int)s.sums[i].len;

                if (options.verbose > 3)
                {
                    Log.WriteLine("chunk[" + i + "] len=" + s.sums[i].len);
                }
            }

            s.fLength = offset;
            return(s);
        }
Ejemplo n.º 2
0
        public int SimpleReceiveToken(IOStream ioStream, ref byte[] data, int offset)
        {
            int n;
            if (residue == 0)
            {
                int i = ioStream.readInt();
                if (i <= 0)
                {
                    return i;
                }
                residue = i;
            }

            n = Math.Min(Match.CHUNK_SIZE, residue);
            residue -= n;
            data = ioStream.ReadBuffer(n);
            return n;
        }
Ejemplo n.º 3
0
        public int SimpleReceiveToken(IOStream ioStream, ref byte[] data, int offset)
        {
            int n;

            if (residue == 0)
            {
                int i = ioStream.readInt();
                if (i <= 0)
                {
                    return(i);
                }
                residue = i;
            }

            n        = Math.Min(Match.CHUNK_SIZE, residue);
            residue -= n;
            data     = ioStream.ReadBuffer(n);
            return(n);
        }
Ejemplo n.º 4
0
        public FileStruct receiveFileEntry(UInt32 flags, ClientInfo clientInfo)
        {
            if (clientInfo == null)
            {
                lastName = String.Empty;
                return(null);
            }
            IOStream f = clientInfo.IoStream;

            int l1 = 0, l2 = 0;

            if ((flags & Options.XMIT_SAME_NAME) != 0)
            {
                l1 = f.readByte();
            }

            if ((flags & Options.XMIT_LONG_NAME) != 0)
            {
                l2 = f.readInt();
            }
            else
            {
                l2 = f.readByte();
            }
            if (l2 >= Options.MAXPATHLEN - l1)
            {
                MainClass.Exit("overflow: lastname=" + lastName, clientInfo);
            }

            string thisName = lastName.Substring(0, l1);

            thisName += f.ReadStringFromBuffer(l2);
            lastName  = thisName;

            thisName = Util.cleanFileName(thisName, false);
            if (options.sanitizePath)
            {
                thisName = Util.sanitizePath(thisName, String.Empty, 0);
            }

            string baseName = String.Empty;
            string dirName  = String.Empty;

            if (thisName.LastIndexOf("/") != -1)
            {
                baseName = Path.GetFileName(thisName);
                dirName  = Path.GetDirectoryName(thisName);
            }
            else
            {
                baseName = thisName;
                dirName  = null;
            }

            Int64 fileLength = f.ReadLongInt();

            if ((flags & Options.XMIT_SAME_TIME) == 0)
            {
                modTime = DateTime.FromFileTime(f.readInt());
            }
            if ((flags & Options.XMIT_SAME_MODE) == 0)
            {
                mode = (UInt32)f.readInt();
            }

            if (options.preserveUID && (flags & Options.XMIT_SAME_UID) == 0)
            {
                int uid = f.readInt();
            }
            if (options.preserveGID && (flags & Options.XMIT_SAME_GID) == 0)
            {
                int gid = f.readInt();
            }

            byte[] sum = new byte[0];
            if (options.alwaysChecksum && !Util.S_ISDIR(mode))
            {
                sum = new byte[CheckSum.MD4_SUM_LENGTH];
                sum = f.ReadBuffer(options.protocolVersion < 21 ? 2 : CheckSum.MD4_SUM_LENGTH);
            }

            FileStruct fs = new FileStruct();

            fs.length   = (int)fileLength;
            fs.baseName = baseName;
            fs.dirName  = dirName;
            fs.sum      = sum;
            fs.mode     = mode;
            fs.modTime  = modTime;
            fs.flags    = flags;
            return(fs);
        }
Ejemplo n.º 5
0
        public bool ReceiveData(ClientInfo clientInfo, string fileNameR, Stream fdR, long sizeR, string fileName, Stream fd, int totalSize)
        {
            IOStream f = clientInfo.IoStream;

            byte[]    fileSum1  = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[]    fileSum2  = new byte[CheckSum.MD4_SUM_LENGTH];
            byte[]    data      = new byte[Match.CHUNK_SIZE];
            SumStruct sumStruct = new SumStruct();
            MapFile   mapBuf    = null;
            Sender    sender    = new Sender(options);

            sender.ReadSumHead(clientInfo, ref sumStruct);
            int    offset = 0;
            UInt32 len;

            if (fdR != null && sizeR > 0)
            {
                int mapSize = (int)Math.Max(sumStruct.bLength * 2, 16 * 1024);
                mapBuf = new MapFile(fdR, (int)sizeR, mapSize, (int)sumStruct.bLength);
                if (options.verbose > 2)
                {
                    Log.WriteLine("recv mapped " + fileNameR + " of size " + sizeR);
                }
            }
            Sum sum = new Sum(options);

            sum.Init(options.checksumSeed);

            int   i;
            Token token = new Token(options);

            while ((i = token.ReceiveToken(f, ref data, 0)) != 0)
            {
                if (options.doProgress)
                {
                    Progress.ShowProgress(offset, totalSize);
                }

                if (i > 0)
                {
                    if (options.verbose > 3)
                    {
                        Log.WriteLine("data recv " + i + " at " + offset);
                    }
                    Options.stats.literalData += i;
                    sum.Update(data, 0, i);
                    if (fd != null && FileIO.WriteFile(fd, data, 0, i) != i)
                    {
                        goto report_write_error;
                    }
                    offset += i;
                    continue;
                }

                i = -(i + 1);
                int offset2 = (int)(i * sumStruct.bLength);
                len = sumStruct.bLength;
                if (i == sumStruct.count - 1 && sumStruct.remainder != 0)
                {
                    len = sumStruct.remainder;
                }

                Options.stats.matchedData += len;

                if (options.verbose > 3)
                {
                    Log.WriteLine("chunk[" + i + "] of size " + len + " at " + offset2 + " offset=" + offset);
                }

                byte[] map = null;
                int    off = 0;
                if (mapBuf != null)
                {
                    off = mapBuf.MapPtr(offset2, (int)len);
                    map = mapBuf.p;

                    token.SeeToken(map, offset, (int)len);
                    sum.Update(map, off, (int)len);
                }

                if (options.inplace)
                {
                    if (offset == offset2 && fd != null)
                    {
                        offset += (int)len;
                        if (fd.Seek(len, SeekOrigin.Current) != offset)
                        {
                            MainClass.Exit("seek failed on " + Util.fullFileName(fileName), clientInfo);
                        }
                        continue;
                    }
                }
                if (fd != null && FileIO.WriteFile(fd, map, off, (int)len) != (int)len)
                {
                    goto report_write_error;
                }
                offset += (int)len;
            }

            if (options.doProgress)
            {
                Progress.EndProgress(totalSize);
            }
            if (fd != null && offset > 0 && FileIO.SparseEnd(fd) != 0)
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }

            fileSum1 = sum.End();

            if (mapBuf != null)
            {
                mapBuf = null;
            }

            fileSum2 = f.ReadBuffer(CheckSum.MD4_SUM_LENGTH);
            if (options.verbose > 2)
            {
                Log.WriteLine("got fileSum");
            }
            if (fd != null && Util.MemoryCompare(fileSum1, 0, fileSum2, 0, CheckSum.MD4_SUM_LENGTH) != 0)
            {
                return(false);
            }
            return(true);

report_write_error:
            {
                MainClass.Exit("write failed on " + Util.fullFileName(fileName), clientInfo);
            }
            return(true);
        }