Exemple #1
0
 public FooFileInfo(FooSyncEngine foo, string path)
 {
     this.Foo = foo;
     this.Path = path;
     this.Source = string.Empty;
     this.Info.Refresh();
 }
Exemple #2
0
        public NetClient(FooSyncEngine foo, string hostname, int port, string username, string password, string repoName = null)
        {
            _foo      = foo;
            _hostname = hostname;
            _port     = port;
            _username = username;
            _password = password;
            _repoName = repoName;
            _client   = null;
            _stream   = null;
            _reader   = null;
            _writer   = null;

            ReportedHostname = null;
            ServerDescription = null;
            ServerProtocolVersion = null;
        }
Exemple #3
0
        /// <summary>
        /// Creates a new FooTree, fully populated with files.
        /// </summary>
        /// <param name="foo">FooSyncEngine instance; its configuration is used</param>
        /// <param name="path">Directory the tree is rooted at.</param>
        /// <param name="exceptions">Regular expressions to exclude from the tree.
        ///     If the regex ends with '/$', it applies to directories, otherwise it applies to files.
        ///     Pass 'null' to not use any exceptions.</param>
        /// <param name="callback">Progress callback, invoked once per file found.
        ///     The 'total' parameter passed in is always -1, and the 'item' is the directory currently being enumerated.
        ///     Pass 'null' if no callback is desired.</param>
        public FooTree(FooSyncEngine foo, string path, IEnumerable<string> exceptions = null, Progress callback = null)
        {
            this.Foo   = foo;
            this.Base  = new FooSyncUrl(path);
            this.Files = new Dictionary<string, FooFileInfoBase>();

            Walk(Foo, path, path, exceptions,
                (trimmedPath, info) =>
                    {
                        if (info != null)
                        {
                            Files.Add(trimmedPath, info);
                        }

                        if (callback != null)
                        {
                            callback(Files.Count, -1, trimmedPath);
                        }
                    }
            );
        }
Exemple #4
0
        /// <summary>
        /// Creates a new FooTree from a stream.
        /// </summary>
        /// <param name="foo">FooSyncEngine instance; its configuration is used</param>
        /// <param name="url">fs:// URL the stream is from</param>
        /// <param name="input">Stream to load data from</param>
        /// <param name="callback">Progress callback, invoked once per file found.
        ///     The 'total' parameter passed in is always -1, and the 'item' is the directory currently being enumerated.
        ///     Pass 'null' if no callback is desired.</param>
        public FooTree(FooSyncEngine foo, string url, Stream input, Progress callback = null)
        {
            this.Foo   = foo;
            this.Base  = new FooSyncUrl(url);
            this.Files = new Dictionary<string, FooFileInfoBase>();

            string path = string.Empty;
            string source = string.Empty;
            long mTime = 0;
            long size = 0;

            var reader = new BinaryReader(input);

            while (true)
            {
                path   = reader.ReadString();
                source = reader.ReadString();
                mTime  = reader.ReadInt64();
                size   = reader.ReadInt64();

                if (path == string.Empty && source == string.Empty && mTime == 0 && size == 0)
                {
                    return;
                }

                var info = new FooFileInfoBase();
                info.Path   = path;
                info.Source = source;
                info.MTime  = new DateTime(mTime);
                info.Size   = size;

                if (callback != null)
                {
                    callback(Files.Count + 1, -1, path);
                }

                Files.Add(path, info);
            }
        }
Exemple #5
0
        void Run(string hostname, int port, string repo)
        {
            var foo = new FooSyncEngine();

            var client = new TcpClient(hostname, port);
            var stream = client.GetStream();
            var writer = new BinaryWriter(stream);
            var reader = new BinaryReader(stream);

            var passwd = new System.Security.SecureString();
            foreach (char c in "qwerty")
                passwd.AppendChar(c);

            RetCode ret;
            int count = 0;

            writer.Write(OpCode.Hello);
            ret = reader.ReadRetCode();

            Console.WriteLine("hello returned {0}", ret);

            Version protocolVersion = new Version(reader.ReadInt32(), reader.ReadInt32());
            String serverName = reader.ReadString();
            String serverDesc = reader.ReadString();
            Version serverVersion = new Version(reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32(), reader.ReadInt32());
            String helloString = reader.ReadString();

            Console.WriteLine("Protocol {0}\nName: {1}\nDescription: {2}\nServer {3}\n{4}", protocolVersion, serverName, serverDesc, serverVersion, helloString);

            writer.Write(OpCode.Auth);
            writer.Write("test");
            writer.Write(passwd);
            ret = reader.ReadRetCode();

            Console.WriteLine("auth returned {0}", ret);

            writer.Write(OpCode.ListRepos);
            ret = reader.ReadRetCode();
            count = reader.ReadInt32();

            Console.WriteLine("listrepos returned {0}", ret);
            Console.WriteLine("{0} repositories", count);

            for (int x = 0; x < count; x++)
            {
                var repoName = reader.ReadString();
                Console.WriteLine("> {0}", repoName);
            }

            writer.Write(OpCode.Hello);
            ret = reader.ReadRetCode();
            var s = reader.ReadString();

            Console.WriteLine("hello replied {0}", s);

            writer.Write(OpCode.Tree);
            writer.Write("test");
            ret = reader.ReadRetCode();

            Console.WriteLine("tree returned {0}", ret);

            var tree = new FooTree(foo, string.Format("fs://{0}:{1}/{2}", hostname, port, repo), stream,
                (item, total, path) =>
                    {
                        Console.WriteLine("{0}: {1}", item, path);
                    }
                );

            Console.WriteLine("{0} items in tree", tree.Files.Count);

            writer.Write(OpCode.GetFile);
            writer.Write("test");
            writer.Write(tree.Files.Keys.First());
            ret = reader.ReadRetCode();

            Console.WriteLine("getfile returned {0}", ret);

            var len = reader.ReadInt64();
            var bytes = reader.ReadBytes((int)len);

            stream.Close();
        }
Exemple #6
0
        private void ProcessArguments(ProgramArguments args)
        {
            _args = args;

            var fooOptions = new Options();

            if (_args.Flags.ContainsKey("casesensitive"))
            {
                fooOptions.CaseInsensitive = _args.Flags["casesensitive"];
            }
            else
            {
                //
                // default to case-sensitive on Unix
                //
                fooOptions.CaseInsensitive = !(Environment.OSVersion.Platform == PlatformID.Unix);
            }

            _listenPort = DefaultPort;
            if (_args.Options.ContainsKey("port"))
            {
                try
                {
                    _listenPort = int.Parse(_args.Options["port"]);
                }
                catch (FormatException)
                {
                    // keep default port
                }
            }

            var configPath = DefaultConfig;
            if (_args.Options.ContainsKey("config"))
            {
                configPath = _args.Options["config"];
            }

            try
            {
                string err = string.Empty;
                _config = ServerRepositoryConfigLoader.GetConfig(configPath, out err);

                if (_config == null)
                {
                    Trace.TraceError("Error loading FooSync daemon config: {0}", err);
                    throw new Exception(string.Format("Error loading FooSync daemon config: {0}", err));
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError("Error loading FooSync daemon config: {0}: {1}{2}", ex.GetType().Name, ex.Message, ex.StackTrace);
                throw;
            }

            _exceptions = new Dictionary<string, ICollection<string>>();

            foreach (var repo in _config.Repositories)
            {
                _exceptions.Add(repo.Key, FooSyncEngine.PrepareExceptions(repo.Value.IgnorePatterns));
            }

            _foo = new FooSyncEngine(fooOptions);
        }
Exemple #7
0
        private static void Walk(FooSyncEngine foo, string path, string basePath, IEnumerable<string> exceptions, Action<string, FooFileInfoBase> OnItem)
        {
            foreach (string entry in Directory.EnumerateFileSystemEntries(path))
            {
                Debug.Assert(entry.StartsWith(basePath), "file is supposed to start with basePath");

                string trimmedName = entry.Substring(basePath.Length + 1);
                if (trimmedName == FooSyncEngine.RepoStateFileName)
                {
                    continue;
                }

                bool isDirectory = (File.GetAttributes(entry) & FileAttributes.Directory) == FileAttributes.Directory;

                bool failsRegex = false;
                foreach (string ex in exceptions ?? Enumerable.Empty<string>())
                {
                    string regex = ex;
                    string searchAgainst = IOPath.GetFileName(entry);

                    if (ex.EndsWith("/$") || ex.EndsWith("/$)"))
                    {
                        if (!isDirectory)
                        {
                            continue;
                        }

                        //
                        // Don't use Path.DirectorySeparatorChar here because the regex ends with slash, not system dependent.
                        //
                        searchAgainst += "/";
                    }
                    else if (isDirectory)
                    {
                        continue;
                    }

                    if (Regex.Match(searchAgainst, regex, foo.Options.CaseInsensitive ? RegexOptions.IgnoreCase : RegexOptions.None).Success)
                    {
                        failsRegex = true;
                        break;
                    }
                }

                if (!failsRegex)
                {
                    if (isDirectory)
                    {
                        OnItem(trimmedName, null);
                        Walk(foo, entry, basePath, exceptions, OnItem);
                    }
                    else
                    {
                        var info = new FooFileInfo(foo, entry);
                        OnItem(trimmedName, info);
                    }
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Create a FooTree, writing the data to a stream.
        /// </summary>
        /// <param name="foo">FooSyncEngine instance; its configuration is used</param>
        /// <param name="path">Directory the tree is rooted at</param>
        /// <param name="exceptions">Regular expressions to exclude from the tree.
        ///     If the regex ends with '/$', it applies to directories, otherwise it applies to files.
        ///     Pass 'null' to not use any exceptions.</param>
        /// <param name="output">Stream to write the data to</param>
        public static void ToStream(FooSyncEngine foo, string path, IEnumerable<string> exceptions, Stream output)
        {
            var writer = new BinaryWriter(output);
            Walk(foo, path, path, exceptions,
                (trimmedPath, info) =>
                    {
                        if (info != null)
                        {
                            writer.Write(trimmedPath);
                            writer.Write(info.Source);
                            writer.Write(info.MTime.Ticks);
                            writer.Write(info.Size);
                        }
                    }
            );

            writer.Write(string.Empty);
            writer.Write(string.Empty);
            writer.Write(0L);
            writer.Write(0L);
        }
Exemple #9
0
 public FooFileInfoBase(FooSyncEngine foo)
 {
     Foo = foo;
 }
Exemple #10
0
 Program(FooSyncEngine foo)
 {
     this.Foo = foo;
 }
Exemple #11
0
        static void Main(string[] args)
        {
            var programArgs = new ProgramArguments(args);
            var fooOptions = new Options();

            if (programArgs.Flags.ContainsKey("casesensitive"))
            {
                fooOptions.CaseInsensitive = !programArgs.Flags["casesensitive"];
            }

            var foo = new FooSyncEngine(fooOptions);

            Console.WriteLine("FooSync.ConsoleApp v{0} / FooSync v{1}",
                System.Reflection.Assembly.GetExecutingAssembly().GetName().Version,
                foo.GetType().Assembly.GetName().Version.ToString());

            Console.WriteLine("{0} / {1} / {2}",
                Environment.MachineName,
                Environment.OSVersion.Platform,
                Environment.OSVersion.VersionString);
            if (Type.GetType("Mono.Runtime") != null)
            {
                Console.WriteLine("Using the Mono runtime.");
            }
            Console.WriteLine();

            if (programArgs.Flags.ContainsKey("help"))
            {
                Console.WriteLine("usage: {0} [options]", ProgramName);
                //TODO
                return;
            }

            var program = new Program(foo);
            program.Run(programArgs);
        }