Exemple #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="cInfo"></param>
        public static void SetupProtocol(ClientInfo cInfo)
        {
            IOStream f       = cInfo.IoStream;
            Options  options = cInfo.Options;

            if (options.remoteProtocol == 0)
            {
                if (!options.readBatch)
                {
                    f.writeInt(options.protocolVersion);
                }
                options.remoteProtocol = f.readInt();
                if (options.protocolVersion > options.remoteProtocol)
                {
                    options.protocolVersion = options.remoteProtocol;
                }
            }
            if (options.readBatch && options.remoteProtocol > options.protocolVersion)
            {
                MainClass.Exit("The protocol version in the batch file is too new", null);
            }
            if (options.verbose > 3)
            {
                Log.WriteLine("(" + (options.amServer ? "Server" : "Client") + ") Protocol versions: remote=" + options.remoteProtocol + ", negotiated=" + options.protocolVersion);
            }
            if (options.remoteProtocol < Options.MIN_PROTOCOL_VERSION || options.remoteProtocol > Options.MAX_PROTOCOL_VERSION)
            {
                MainClass.Exit("Protocol version mistmatch", null);
            }
            if (options.amServer)
            {
                if (options.checksumSeed == 0)
                {
                    options.checksumSeed = (int)System.DateTime.Now.Ticks;
                }
                f.writeInt(options.checksumSeed);
            }
            else
            {
                options.checksumSeed = f.readInt();
            }
        }
Exemple #2
0
        public void ReadSumHead(ClientInfo clientInfo, ref SumStruct sum)
        {
            IOStream ioStream = clientInfo.IoStream;

            sum.count   = ioStream.readInt();
            sum.bLength = (UInt32)ioStream.readInt();
            if (options.protocolVersion < 27)
            {
                sum.s2Length = checkSum.length;
            }
            else
            {
                sum.s2Length = ioStream.readInt();
                if (sum.s2Length > CheckSum.MD4_SUM_LENGTH)
                {
                    MainClass.Exit("Invalid checksum length " + sum.s2Length, clientInfo);
                }
            }
            sum.remainder = (UInt32)ioStream.readInt();
        }
Exemple #3
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);
        }
Exemple #4
0
        static void DoServerSender(ClientInfo clientInfo, string[] args)
        {
            string   dir      = args[0];
            IOStream ioStream = clientInfo.IoStream;
            Options  options  = clientInfo.Options;

            if (options.verbose > 2)
            {
                Log.Write("Server sender starting");
            }
            if (options.amDaemon && config.ModuleIsWriteOnly(options.ModuleId))
            {
                MainClass.Exit("ERROR: module " + config.GetModuleName(options.ModuleId) + " is write only", clientInfo);
                return;
            }

            if (!options.relativePaths && !Util.pushDir(dir))
            {
                MainClass.Exit("Push_dir#3 " + dir + "failed", clientInfo);
                return;
            }

            FileList          fList    = new FileList(options);
            List <FileStruct> fileList = fList.sendFileList(clientInfo, args);

            if (options.verbose > 3)
            {
                Log.WriteLine("File list sent");
            }
            if (fileList.Count == 0)
            {
                MainClass.Exit("File list is empty", clientInfo);
                return;
            }
            ioStream.IOStartBufferingIn();
            ioStream.IOStartBufferingOut();

            Sender sender = new Sender(options);

            sender.SendFiles(fileList, clientInfo);
            ioStream.Flush();
            MainClass.Report(clientInfo);
            if (options.protocolVersion >= 24)
            {
                ioStream.readInt();
            }
            ioStream.Flush();
        }
Exemple #5
0
		public int SimpleReceiveToken(IOStream f, ref byte[] data, int offset)
		{
			int n;
			if (residue == 0) 
			{
				int i = f.readInt();
				if (i <= 0)
					return i;
				residue = i;
			}

			n = Math.Min(Match.CHUNK_SIZE,residue);
			residue -= n;
			data = f.ReadBuf(n);			
			return n; 
		}
Exemple #6
0
        /// <summary>
        /// Receives exclude list from stream
        /// </summary>
        /// <param name="ioStream"></param>
        public void ReceiveExcludeList(IOStream ioStream)
        {
            string line = String.Empty;
            int    length;

            while ((length = ioStream.readInt()) != 0)
            {
                if (length >= Options.MAXPATHLEN + 3)
                {
                    Log.Write("Overflow: recv_exclude_list");
                    continue;
                }

                line = ioStream.ReadStringFromBuffer(length);
                AddExclude(ref options.excludeList, line, 0);
            }
        }
Exemple #7
0
        public void ReceiveExcludeList(IOStream f)
        {
            string line = "";
            int    l;

            while ((l = f.readInt()) != 0)
            {
                if (l >= Options.MAXPATHLEN + 3)
                {
                    Log.Write("overflow: recv_exclude_list");
                    continue;
                }

                line = f.ReadSBuf(l);
                AddExclude(ref options.excludeList, line, 0);
            }
        }
Exemple #8
0
        public int SimpleReceiveToken(IOStream f, ref byte[] data, int offset)
        {
            int n;

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

            n        = Math.Min(Match.CHUNK_SIZE, residue);
            residue -= n;
            data     = f.ReadBuf(n);
            return(n);
        }
Exemple #9
0
		public void ReceiveExcludeList(IOStream f)		
		{			
			string line = "";
			int l;
			while ((l = f.readInt()) != 0) 
			{
				if (l >= Options.MAXPATHLEN+3)
				{
					Log.Write("overflow: recv_exclude_list");
					continue;
				}

				line = f.ReadSBuf(l);
				AddExclude(ref options.excludeList, line, 0);				
			}
		}
Exemple #10
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);
        }
Exemple #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="clientInfo"></param>
        /// <returns></returns>
        public List <FileStruct> receiveFileList(ClientInfo clientInfo)
        {
            IOStream          ioStream = clientInfo.IoStream;
            List <FileStruct> fileList = new List <FileStruct>();

            if (showFileListProgress())
            {
                startFileListProgress("receiving file list");
            }

            Int64 startRead = Options.stats.totalRead;

            UInt32 flags;

            while ((flags = ioStream.readByte()) != 0)
            {
                if (options.protocolVersion >= 28 && (flags & Options.XMIT_EXTENDED_FLAGS) != 0)
                {
                    flags |= (UInt32)(ioStream.readByte() << 8);
                }
                FileStruct file = receiveFileEntry(flags, clientInfo);
                if (file == null)
                {
                    continue;
                }
                fileList.Add(file);
                Options.stats.totalSize += (fileList[fileList.Count - 1]).length;
                EmitFileListProgress(fileList);
                if (options.verbose > 2)
                {
                    Log.WriteLine("receiveFileName(" + (fileList[fileList.Count - 1]).GetFullName() + ")");
                }
            }
            receiveFileEntry(0, null);

            if (options.verbose > 2)
            {
                Log.WriteLine("received " + fileList.Count + " names");
            }

            if (showFileListProgress())
            {
                finishFileListProgress(fileList);
            }

            cleanFileList(fileList, options.relativePaths, true);

            if (ioStream != null)
            {
                ioStream.readInt();
            }

            if (options.verbose > 3)
            {
                outputFileList(fileList);
            }
            if (options.listOnly)
            {
                logFileList(fileList);
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("receiveFileList done");
            }

            Options.stats.fileListSize = (int)(Options.stats.totalRead - startRead);
            Options.stats.numFiles     = fileList.Count;
            return(fileList);
        }
Exemple #12
0
        public void SendFiles(List <FileStruct> fileList, ClientInfo clientInfo)
        {
            ShowMessage("Processing...");
            try
            {
                IOStream  ioStream = clientInfo.IoStream;
                string    fileName = String.Empty, fileName2 = String.Empty;
                SumStruct s               = null;
                int       phase           = 0;
                bool      saveMakeBackups = options.makeBackups;
                Match     match           = new Match(options);

                if (options.verbose > 2)
                {
                    Log.WriteLine("SendFiles starting");
                }
                while (true)
                {
                    fileName = String.Empty;
                    int i = ioStream.readInt();
                    if (i == -1)
                    {
                        if (phase == 0)
                        {
                            phase++;
                            checkSum.length = CheckSum.SUM_LENGTH;
                            ioStream.writeInt(-1);
                            if (options.verbose > 2)
                            {
                                Log.WriteLine("SendFiles phase=" + phase);
                            }
                            options.makeBackups = false;
                            continue;
                        }
                        break;
                    }

                    if (i < 0 || i >= fileList.Count)
                    {
                        MainClass.Exit("Invalid file index " + i + " (count=" + fileList.Count + ")", clientInfo);
                    }

                    FileStruct file = fileList[i];

                    Options.stats.currentFileIndex = i;
                    Options.stats.numTransferredFiles++;
                    Options.stats.totalTransferredSize += file.length;

                    if (!string.IsNullOrEmpty(file.baseDir))
                    {
                        fileName = file.baseDir;
                        if (!fileName.EndsWith("/"))
                        {
                            fileName += "/";
                        }
                    }
                    fileName2 = file.GetFullName();
                    fileName += file.GetFullName();
                    ShowMessage("uploading " + fileName);

                    if (options.verbose > 2)
                    {
                        Log.WriteLine("sendFiles(" + i + ", " + fileName + ")");
                    }

                    if (options.dryRun)
                    {
                        if (!options.amServer && options.verbose != 0)
                        {
                            Log.WriteLine(fileName2);
                        }
                        ioStream.writeInt(i);
                        continue;
                    }

                    Stats initialStats = Options.stats;
                    s = ReceiveSums(clientInfo);

                    Stream fd;
                    try
                    {
                        fd = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                    }
                    catch (FileNotFoundException)
                    {
                        Log.WriteLine("file has vanished: " + Util.fullFileName(fileName));
                        s = null;
                        continue;
                    }
                    catch (Exception)
                    {
                        Log.WriteLine("SendFiles failed to open " + Util.fullFileName(fileName));
                        s = null;
                        continue;
                    }

                    FStat    st = new FStat();
                    FileInfo fi = new FileInfo(fileName);
                    // TODO: path length
                    st.mTime = fi.LastWriteTime;
                    // TODO: path length
                    st.size = fi.Length;

                    MapFile mbuf = null;
                    if (st.size != 0)
                    {
                        int mapSize = (int)Math.Max(s.bLength * 3, Options.MAX_MAP_SIZE);
                        mbuf = new MapFile(fd, (int)st.size, mapSize, (int)s.bLength);
                    }

                    if (options.verbose > 2)
                    {
                        Log.WriteLine("SendFiles mapped " + fileName + " of size " + st.size);
                    }

                    ioStream.writeInt(i);
                    Generator gen = new Generator(options);
                    gen.WriteSumHead(ioStream, s);

                    if (options.verbose > 2)
                    {
                        Log.WriteLine("calling MatchSums " + fileName);
                    }

                    if (!options.amServer && options.verbose != 0)
                    {
                        Log.WriteLine(fileName2);
                    }

                    Token token = new Token(options);
                    token.SetCompression(fileName);

                    match.MatchSums(ioStream, s, mbuf, (int)st.size);
                    Log.LogSend(file, initialStats);

                    if (mbuf != null)
                    {
                        bool j = mbuf.UnMapFile();
                        if (j)
                        {
                            Log.WriteLine("read errors mapping " + Util.fullFileName(fileName));
                        }
                    }
                    fd.Close();

                    s.sums = null;

                    if (options.verbose > 2)
                    {
                        Log.WriteLine("sender finished " + fileName);
                    }
                }
                options.makeBackups = saveMakeBackups;

                if (options.verbose > 2)
                {
                    Log.WriteLine("send files finished");
                }

                match.MatchReport(ioStream);
                ioStream.writeInt(-1);
            }
            finally
            {
                HideMessage();
            }
        }
Exemple #13
0
        /// <summary>
        /// Receives exclude list from stream
        /// </summary>
        /// <param name="ioStream"></param>
        public void ReceiveExcludeList(IOStream ioStream)
        {
            string line = String.Empty;
            int length;
            while ((length = ioStream.readInt()) != 0)
            {
                if (length >= Options.MAXPATHLEN + 3)
                {
                    Log.Write("Overflow: recv_exclude_list");
                    continue;
                }

                line = ioStream.ReadStringFromBuffer(length);
                AddExclude(ref options.excludeList, line, 0);
            }
        }
Exemple #14
0
        public ArrayList receiveFileList(ClientInfo cInfo)
        {
            IOStream  f        = cInfo.IoStream;
            ArrayList fileList = new ArrayList();

            if (showFileListP())
            {
                startFileListProgress("receiving file list");
            }

            Int64 startRead = Options.stats.totalRead;

            UInt32 flags;

            while ((flags = f.readByte()) != 0)
            {
                if (options.protocolVersion >= 28 && (flags & Options.XMIT_EXTENDED_FLAGS) != 0)
                {
                    flags |= (UInt32)(f.readByte() << 8);
                }
                FileStruct file = receiveFileEntry(flags, cInfo);
                if (file == null)
                {
                    continue;
                }
                fileList.Add(file);
                Options.stats.totalSize += ((FileStruct)fileList[fileList.Count - 1]).length;
                mayBeEmitFileListProgress(fileList);
                if (options.verbose > 2)
                {
                    Log.WriteLine("receiveFileName(" + ((FileStruct)fileList[fileList.Count - 1]).FNameTo() + ")");
                }
            }
            receiveFileEntry(0, null);

            if (options.verbose > 2)
            {
                Log.WriteLine("received " + fileList.Count + " names");
            }

            if (showFileListP())
            {
                finishFileListProgress(fileList);
            }

            cleanFileList(fileList, (options.relativePaths == 0) ? false : true, true);

            if (f != null)
            {
                f.readInt();
            }

            if (options.verbose > 3)
            {
                outputFileList(fileList);
            }
            if (options.listOnly)
            {
                for (int i = 0; i < fileList.Count; i++)
                {
                    listFileEntry((FileStruct)fileList[i]);
                }
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("receiveFileList done");
            }

            Options.stats.flistSize = (int)(Options.stats.totalRead - startRead);
            Options.stats.numFiles  = fileList.Count;
            return(fileList);
        }
Exemple #15
0
        public int ReceiveFiles(ClientInfo clientInfo, List <FileStruct> fileList, string localName)
        {
            FStat      st = new FStat();
            FileStruct file;
            IOStream   ioStream = clientInfo.IoStream;

            string fileName;
            string fNameCmp = String.Empty, fNameTmp = String.Empty;
            bool   saveMakeBackups = options.makeBackups;
            int    i, phase = 0;
            bool   recv_ok;

            if (options.verbose > 2)
            {
                Log.WriteLine("ReceiveFiles(" + fileList.Count + ") starting");
            }
            while (true)
            {
                i = ioStream.readInt();
                if (i == -1)
                {
                    if (phase != 0)
                    {
                        break;
                    }

                    phase           = 1;
                    checkSum.length = CheckSum.SUM_LENGTH;
                    if (options.verbose > 2)
                    {
                        Log.WriteLine("ReceiveFiles phase=" + phase);
                    }
                    ioStream.writeInt(0); //send_msg DONE
                    if (options.keepPartial)
                    {
                        options.makeBackups = false;
                    }
                    continue;
                }

                if (i < 0 || i >= fileList.Count)
                {
                    MainClass.Exit("Invalid file index " + i + " in receiveFiles (count=" + fileList.Count + ")", clientInfo);
                }

                file = (fileList[i]);

                Options.stats.currentFileIndex = i;
                Options.stats.numTransferredFiles++;
                Options.stats.totalTransferredSize += file.length;

                if (localName != null && localName.CompareTo(String.Empty) != 0)
                {
                    fileName = localName;
                }
                else
                {
                    fileName = Path.Combine(options.dir, LocalizePath(clientInfo, file.GetFullName().Replace(":", String.Empty)).Replace("\\", "/"));
                    //fileName = Path.Combine(options.dir, file.FNameTo().Replace(":",String.Empty)).Replace("\\", "/");
                    // TODO: path length
                    Directory.CreateDirectory(Path.Combine(options.dir, LocalizePath(clientInfo, file.dirName.Replace(":", String.Empty))).Replace("\\", "/"));
                    Log.WriteLine(Path.Combine(options.dir, file.dirName));
                    //FileSystem.Directory.CreateDirectory(Path.Combine(options.dir,file.dirName.Replace(":",String.Empty)).Replace("\\", "/"));
                }

                if (options.dryRun)
                {
                    if (!options.amServer && options.verbose > 0)
                    {
                        Log.WriteLine(fileName);
                    }
                    continue;
                }

                if (options.verbose > 2)
                {
                    Log.WriteLine("receiveFiles(" + fileName + ")");
                }

                if (options.partialDir != null && options.partialDir.CompareTo(String.Empty) != 0)
                {
                }
                else
                {
                    fNameCmp = fileName;
                }

                FileStream fd1 = null;
                try
                {
                    fd1 = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
                }
                catch (FileNotFoundException)
                {
                    fNameCmp = fileName;
                    try
                    {
                        fd1 = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
                    }
                    catch (FileNotFoundException)
                    {
                    }
                }
                catch (Exception e)
                {
                    Log.Write(e.Message);
                }
                try
                {
                    FileInfo fi = new FileInfo(fNameCmp);
                    // TODO: path length
                    st.size = fi.Length;
                }
                catch { }

                String     tempFileName = getTmpName(fileName);
                FileStream fd2          = null;
                fd2 = new FileStream(tempFileName, FileMode.OpenOrCreate, FileAccess.Write);

                if (!options.amServer && options.verbose > 0)
                {
                    Log.WriteLine(fileName);
                }

                /* recv file data */
                recv_ok = ReceiveData(clientInfo, fNameCmp, fd1, st.size,
                                      fileName, fd2, file.length);

                if (fd1 != null)
                {
                    fd1.Close();
                }
                if (fd2 != null)
                {
                    fd2.Close();
                }
                // TODO: path length
                File.Copy(tempFileName, fileName, true);
                // TODO: path length
                File.Delete(tempFileName);
                if (recv_ok || options.inplace)
                {
                    FinishTransfer(fileName, fNameTmp, file, recv_ok);
                }
            }
            options.makeBackups = saveMakeBackups;

            if (options.deleteAfter && options.recurse && localName == null && fileList.Count > 0)
            {
                DeleteFiles(fileList);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("ReceiveFiles finished");
            }

            return(0);
        }