Beispiel #1
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;
        }
Beispiel #2
0
        public void SendFiles(List <FileStruct> fileList, ClientInfo clientInfo)
        {
            ShowMessage("Processing...");
            try
            {
                var       ioStream = clientInfo.IoStream;
                string    fileName = String.Empty, fileName2 = String.Empty;
                SumStruct s               = null;
                var       phase           = 0;
                var       saveMakeBackups = _options.MakeBackups;
                var       match           = new Match(_options);

                if (_options.Verbose > 2)
                {
                    Log.WriteLine("SendFiles starting");
                }
                while (true)
                {
                    fileName = String.Empty;
                    var i = ioStream.ReadInt();
                    if (i == -1)
                    {
                        if (phase == 0)
                        {
                            phase++;
                            _checkSum.Length = CheckSum.SumLength;
                            ioStream.WriteInt(-1);
                            if (_options.Verbose > 2)
                            {
                                Log.WriteLine("SendFiles phase=" + phase);
                            }
                            _options.MakeBackups = false;
                            continue;
                        }
                        break;
                    }

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

                    var 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;
                    }

                    var 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;
                    }

                    var st = new FStat();
                    var 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)
                    {
                        var mapSize = (int)Math.Max(s.BLength * 3, Options.MaxMapSize);
                        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);
                    var 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);
                    }

                    var token = new Token(_options);
                    token.SetCompression(fileName);

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

                    if (mbuf != null)
                    {
                        var 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();
            }
        }
Beispiel #3
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();
            }
        }
Beispiel #4
0
        public int ReceiveFiles(ClientInfo clientInfo, List <FileStruct> fileList, string localName)
        {
            var        st = new FStat();
            FileStruct file;
            var        ioStream = clientInfo.IoStream;

            string fileName;
            string fNameCmp = String.Empty, fNameTmp = String.Empty;
            var    saveMakeBackups = _options.MakeBackups;
            int    i, phase = 0;
            bool   recvOk;

            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.SumLength;
                    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)
                {
                    WinRsync.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.IsBlank())
                {
                    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
                {
                    var fi = new FileInfo(fNameCmp);
                    // TODO: path length
                    st.Size = fi.Length;
                }
                catch { }

                var        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 */
                recvOk = 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 (recvOk || _options.Inplace)
                {
                    FinishTransfer(fileName, fNameTmp, file, recvOk);
                }
            }
            _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);
        }
Beispiel #5
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();
            }
        }
Beispiel #6
0
        public void ReceiveGenerator(string fileName, FileStruct file, int i, IOStream f)
        {
            fileName = Path.Combine(options.dir, fileName);

            if (UnchangedFile(fileName, file))
            {
                return;
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("Receive Generator(" + fileName + "," + i + ")\n");
            }
            int statRet;
            FStat st = new FStat();
            if (options.dryRun)
            {
                statRet = -1;
            }
            else
            {
                statRet = 0;
                try
                {
                    FileInfo fi = new FileInfo(fileName);
                    // TODO: path length
                    st.size = fi.Length;
                    // TODO: path length
                    st.mTime = fi.LastWriteTime;
                }
                catch
                {
                    statRet = -1;
                }
            }

            if (options.onlyExisting && statRet == -1)
            {
                /* we only want to update existing files */
                if (options.verbose > 1)
                {
                    Log.WriteLine("not creating new file \"" + fileName + "\"");
                }
                return;
            }
            string fNameCmp = fileName;
            if (options.wholeFile > 0)
            {
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }
            FileStream fd;
            try
            {
                fd = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
            }
            catch
            {
                if (options.verbose > 3)
                {
                    Log.WriteLine("failed to open " + Util.fullFileName(fNameCmp) + ", continuing");
                }
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }

            if (options.verbose > 3)
            {
                Log.WriteLine("gen mapped " + fNameCmp + " of size " + st.size);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("generating and sending sums for " + i);
            }

            f.writeInt(i);
            Stream fCopy = null;
            GenerateAndSendSums(fd, st.size, f, fCopy);

            if (fCopy != null)
            {
                fCopy.Close();
            }
            fd.Close();
        }
Beispiel #7
0
        public void ReceiveGenerator(string fileName, FileStruct file, int i, IOStream f)
        {
            fileName = Path.Combine(options.dir, fileName);

            if (UnchangedFile(fileName, file))
            {
                return;
            }
            if (options.verbose > 2)
            {
                Log.WriteLine("Receive Generator(" + fileName + "," + i + ")\n");
            }
            int   statRet;
            FStat st = new FStat();

            if (options.dryRun)
            {
                statRet = -1;
            }
            else
            {
                statRet = 0;
                try
                {
                    FileInfo fi = new FileInfo(fileName);
                    // TODO: path length
                    st.size = fi.Length;
                    // TODO: path length
                    st.mTime = fi.LastWriteTime;
                }
                catch
                {
                    statRet = -1;
                }
            }

            if (options.onlyExisting && statRet == -1)
            {
                /* we only want to update existing files */
                if (options.verbose > 1)
                {
                    Log.WriteLine("not creating new file \"" + fileName + "\"");
                }
                return;
            }
            string fNameCmp = fileName;

            if (options.wholeFile > 0)
            {
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }
            FileStream fd;

            try
            {
                fd = new FileStream(fNameCmp, FileMode.Open, FileAccess.Read);
            }
            catch
            {
                if (options.verbose > 3)
                {
                    Log.WriteLine("failed to open " + Util.fullFileName(fNameCmp) + ", continuing");
                }
                f.writeInt(i);
                WriteSumHead(f, null);
                return;
            }

            if (options.verbose > 3)
            {
                Log.WriteLine("gen mapped " + fNameCmp + " of size " + st.size);
            }

            if (options.verbose > 2)
            {
                Log.WriteLine("generating and sending sums for " + i);
            }

            f.writeInt(i);
            Stream fCopy = null;

            GenerateAndSendSums(fd, st.size, f, fCopy);

            if (fCopy != null)
            {
                fCopy.Close();
            }
            fd.Close();
        }
Beispiel #8
0
        public int ReceiveFiles(ClientInfo cInfo, ArrayList fileList, string localName)
        {
            FStat      st = new FStat();
            FileStruct file;
            IOStream   f = cInfo.IoStream;

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

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

                    phase = 1;
                    checkSum.cSumLength = CheckSum.SUM_LENGTH;
                    if (options.verbose > 2)
                    {
                        Log.WriteLine("ReceiveFiles phase=" + phase);
                    }
                    f.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 + ")", cInfo);
                }

                file = ((FileStruct)fileList[i]);

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

                if (localName != null && localName.CompareTo("") != 0)
                {
                    fileName = localName;
                }
                else
                {
                    fileName = Path.Combine(options.dir, LocalizePath(cInfo, file.FNameTo().Replace(":", "")).Replace("\\", "/"));
                    //fileName = Path.Combine(options.dir, file.FNameTo().Replace(":","")).Replace("\\", "/");
                    // TODO: path length
                    FileSystem.Directory.CreateDirectory(Path.Combine(options.dir, LocalizePath(cInfo, file.dirName.Replace(":", ""))).Replace("\\", "/"));
                    Log.WriteLine(Path.Combine(options.dir, file.dirName));
                    //FileSystem.Directory.CreateDirectory(Path.Combine(options.dir,file.dirName.Replace(":","")).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("") != 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
                {
                    FileSystem.FileInfo fi = new FileSystem.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(cInfo, fNameCmp, fd1, st.size,
                                      fileName, fd2, file.length);

                if (fd1 != null)
                {
                    fd1.Close();
                }
                if (fd2 != null)
                {
                    fd2.Close();
                }
                // TODO: path length
                FileSystem.File.Copy(tempFileName, fileName, true);
                // TODO: path length
                FileSystem.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);
        }