Ejemplo n.º 1
0
        public static void UpdateRepository(string repoUrl)
        {
            Module.CheckInitalized();

            lock (RepositoryUpdateLock)
            {
                GitGlobalFsRepository?repoData;
                lock (Data.DataLock)
                {
                    repoData = Data.ManagedData.RepositoryList.Where(x => x.Url._IsSamei(repoUrl)).SingleOrDefault();
                }
                if (repoData == null || repoData.Repository == null)
                {
                    throw new ApplicationException($"The repository \"{repoUrl}\" is not found by the registered list with StartRepository().");
                }

                try
                {
                    repoData.Repository.Fetch(repoData.Url !);
                }
                catch (Exception ex)
                {
                    Con.WriteError($"repoData.Repository.Fetch error: {ex.ToString()}");
                    throw;
                }
            }
        }
Ejemplo n.º 2
0
        async Task <bool> DefaultExceptionCallback(CopyDirectoryStatus status, FileSystemEntity entity, Exception exception)
        {
            Con.WriteError($"Error: '{entity.FullPath}': {exception.Message}");

            await Task.CompletedTask;

            return(true);
        }
Ejemplo n.º 3
0
        async Task LogDataReceivedInternalAsync(string srcHostName, IReadOnlyList <Memory <byte> > dataList)
        {
            if (dataList.Count == 0)
            {
                return;
            }

            List <LogServerReceivedData> list = new List <LogServerReceivedData>();

            foreach (Memory <byte> data in dataList)
            {
                try
                {
                    string str = data._GetString_UTF8();

                    LogServerReceivedData d = new LogServerReceivedData()
                    {
                        BinaryData = data,
                        JsonData   = str._JsonToObject <LogJsonData>(),
                    };

                    d.JsonData !.NormalizeReceivedLog(srcHostName);

                    list.Add(d);
                }
                catch (Exception ex)
                {
                    Con.WriteError($"LogDataReceivedInternalAsync: {ex.ToString()}");
                }
            }

            if (list.Count >= 1)
            {
                await LogReceiveImplAsync(list);
            }
        }
Ejemplo n.º 4
0
        public static void StartRepository(string repoUrl)
        {
            Module.CheckInitalized();

            lock (RepositoryUpdateLock)
            {
                if (repoUrl.IndexOf("@") != -1)
                {
                    throw new ArgumentException($"The repository name \'{repoUrl}\' must not contain '@'.");
                }

                if (Data.IsReadOnly)
                {
                    throw new ApplicationException("Data.IsReadOnly");
                }

                lock (Data.DataLock)
                {
                    GitGlobalFsRepository?repoData = Data.ManagedData.RepositoryList.Where(x => x.Url._IsSamei(repoUrl)).SingleOrDefault();

L_RETRY:

                    if (repoData == null)
                    {
                        string dirName     = GenerateNewRepositoryDirName(repoUrl);
                        string dirFullPath = Lfs.PathParser.Combine(RepoDir, dirName);

                        Con.WriteDebug($"Clone the repository \"{repoUrl}\" to \"{dirFullPath}\" ...");
                        try
                        {
                            GitUtil.Clone(dirFullPath, repoUrl);
                        }
                        catch (Exception ex)
                        {
                            Con.WriteError($"GitUtil.Clone error: {ex.ToString()}");
                            throw;
                        }
                        Con.WriteDebug("Done.");
                        GitRepository gitRepo = new GitRepository(dirFullPath);
                        repoData = new GitGlobalFsRepository()
                        {
                            Url          = repoUrl,
                            LocalWorkDir = dirName,
                            Repository   = gitRepo,
                        };

                        Data.ManagedData.RepositoryList.Add(repoData);
                    }
                    else
                    {
                        repoData.Url = repoUrl;

                        if (repoData.Repository == null)
                        {
                            string dirName     = repoData.LocalWorkDir._NullCheck();
                            string dirFullPath = Lfs.PathParser.Combine(RepoDir, dirName);

                            try
                            {
                                GitRepository gitRepo = new GitRepository(dirFullPath);
                                repoData.Repository = gitRepo;
                            }
                            catch (Exception ex)
                            {
                                Con.WriteDebug($"Repository local dir \"{dirFullPath}\" load error: {ex.ToString()}");
                                Con.WriteDebug($"Trying to clone as a new local dir.");

                                Data.ManagedData.RepositoryList.Remove(repoData);
                                Data.SyncWithStorage(HiveSyncFlags.SaveToFile, false);

                                repoData = null;
                                goto L_RETRY;
                            }
                        }
                    }
                }

                Data.SyncWithStorage(HiveSyncFlags.SaveToFile, false);

                StartUpdateLoop();
            }
        }
Ejemplo n.º 5
0
 public static void WriteJsonError(object?obj, bool includeNull = false, bool escapeHtml = false, int?maxDepth = Json.DefaultMaxDepth, bool compact = false, bool referenceHandling = false, Type?type = null)
 => Con.WriteError(obj._ObjectToJson(includeNull, escapeHtml, maxDepth, compact, referenceHandling, type: type));
        public void Show()
        {
            HiveData.SyncWithStorage(HiveSyncFlags.LoadFromFile, true);

            long pid  = HiveData.ManagedData.Pid;
            int  port = HiveData.ManagedData.LocalLogWatchPort;

            if (pid != 0 && port != 0)
            {
                Con.WriteLine("Starting the real-time log session.");
                Con.WriteLine("Pressing Ctrl + D or Ctrl + Q to disconnect the session.");
                Con.WriteLine();

                Con.WriteLine($"Connecting to localhost:{port} ...");

                CancellationTokenSource cancelSource = new CancellationTokenSource();
                CancellationToken       cancel       = cancelSource.Token;

                Task task = TaskUtil.StartAsyncTaskAsync(async() =>
                {
                    try
                    {
                        using (var sock = await LocalNet.ConnectAsync(new TcpConnectParam(IPAddress.Loopback, port), cancel))
                            using (var stream = sock.GetStream())
                                using (MemoryHelper.FastAllocMemoryWithUsing(65536, out Memory <byte> tmp))
                                {
                                    Con.WriteLine("The real-time log session is connected.");
                                    Con.WriteLine();
                                    try
                                    {
                                        while (true)
                                        {
                                            int r = await stream.ReadAsync(tmp, cancel);
                                            if (r <= 0)
                                            {
                                                break;
                                            }
                                            ReadOnlyMemory <byte> data = tmp.Slice(0, r);
                                            string s = Str.Utf8Encoding.GetString(data.Span);
                                            Console.Write(s);
                                        }
                                    }
                                    catch { }

                                    Con.WriteLine();
                                    Con.WriteLine("The real-time log session is disconnected.");
                                }
                    }
                    catch (Exception ex)
                    {
                        Con.WriteError(ex.Message);
                    }
                });

                try
                {
                    while (true)
                    {
                        var key = Console.ReadKey();
                        if ((key.Key == ConsoleKey.D || key.Key == ConsoleKey.Q) && key.Modifiers == ConsoleModifiers.Control)
                        {
                            break;
                        }
                    }
                }
                catch { }

                cancelSource._TryCancelNoBlock();

                task._TryWait(true);
            }
            else
            {
                Con.WriteLine($"The daemon \"{Name}\" is not running.");
            }
        }
Ejemplo n.º 7
0
        public static async Task <long> CopyBetweenFileBaseAsync(FileBase src, FileBase dest, CopyFileParams?param = null, ProgressReporterBase?reporter = null,
                                                                 long estimatedSize = -1, CancellationToken cancel = default, RefBool?readErrorIgnored = null, Ref <uint>?srcZipCrc = null, long truncateSize = -1)
        {
            if (param == null)
            {
                param = new CopyFileParams();
            }
            if (reporter == null)
            {
                reporter = new NullProgressReporter(null);
            }
            if (readErrorIgnored == null)
            {
                readErrorIgnored = new RefBool();
            }
            if (srcZipCrc == null)
            {
                srcZipCrc = new Ref <uint>();
            }
            if (estimatedSize < 0)
            {
                estimatedSize = src.Size;
            }

            if (truncateSize >= 0)
            {
                estimatedSize = Math.Min(estimatedSize, truncateSize);
            }

            ZipCrc32 srcCrc = new ZipCrc32();

            readErrorIgnored.Set(false);

            checked
            {
                long currentPosition = 0;

                if (param.IgnoreReadError)
                {
                    long fileSize     = src.Size;
                    int  errorCounter = 0;

                    if (truncateSize >= 0)
                    {
                        fileSize = truncateSize; // Truncate
                    }
                    // Ignore read error mode
                    using (MemoryHelper.FastAllocMemoryWithUsing(param.IgnoreReadErrorSectorSize, out Memory <byte> buffer))
                    {
                        for (long pos = 0; pos < fileSize; pos += param.IgnoreReadErrorSectorSize)
                        {
                            int           size    = (int)Math.Min(param.IgnoreReadErrorSectorSize, (fileSize - pos));
                            Memory <byte> buffer2 = buffer.Slice(0, size);

                            int readSize = 0;

                            try
                            {
                                //if (pos >= 10000000 && pos <= (10000000 + 100000)) throw new ApplicationException("*err*");
                                readSize = await src.ReadRandomAsync(pos, buffer2, cancel);
                            }
                            catch (Exception ex)
                            {
                                errorCounter++;
                                if (errorCounter >= 100)
                                {
                                    // Skip error display
                                    if (errorCounter == 100)
                                    {
                                        Con.WriteError($"The read error counter of the file \"{src.FileParams.Path}\" exceeds 100. No more errors will be reported.");
                                    }
                                }
                                else
                                {
                                    // Display the error
                                    Con.WriteError($"Ignoring the read error at the offset {pos} of the file \"{src.FileParams.Path}\". Error: {ex.Message}");
                                }
                                readErrorIgnored.Set(true);
                            }

                            if (readSize >= 1)
                            {
                                await dest.WriteRandomAsync(pos, buffer2.Slice(0, readSize), cancel);
                            }

                            currentPosition = pos + readSize;
                            reporter.ReportProgress(new ProgressData(currentPosition, fileSize));
                        }
                    }
                }
                else if (param.AsyncCopy == false)
                {
                    // Normal copy
                    using (MemoryHelper.FastAllocMemoryWithUsing(param.BufferSize, out Memory <byte> buffer))
                    {
                        while (true)
                        {
                            Memory <byte> thisTimeBuffer = buffer;

                            if (truncateSize >= 0)
                            {
                                // Truncate
                                long remainSize = Math.Max(truncateSize - currentPosition, 0);

                                if (thisTimeBuffer.Length > remainSize)
                                {
                                    thisTimeBuffer = thisTimeBuffer.Slice(0, (int)remainSize);
                                }

                                if (remainSize == 0)
                                {
                                    break;
                                }
                            }

                            int readSize = await src.ReadAsync(thisTimeBuffer, cancel);

                            Debug.Assert(readSize <= thisTimeBuffer.Length);

                            if (readSize <= 0)
                            {
                                break;
                            }

                            ReadOnlyMemory <byte> sliced = thisTimeBuffer.Slice(0, readSize);

                            if (param.Flags.Bit(FileFlags.CopyFile_Verify))
                            {
                                srcCrc.Append(sliced.Span);
                            }

                            await dest.WriteAsync(sliced, cancel);

                            currentPosition += readSize;
                            reporter.ReportProgress(new ProgressData(currentPosition, estimatedSize));
                        }
                    }
                }
                else
                {
                    // Async copy
                    using (MemoryHelper.FastAllocMemoryWithUsing(param.BufferSize, out Memory <byte> buffer1))
                    {
                        using (MemoryHelper.FastAllocMemoryWithUsing(param.BufferSize, out Memory <byte> buffer2))
                        {
                            Task?lastWriteTask = null;
                            int  number        = 0;
                            int  writeSize     = 0;

                            long currentReadPosition = 0;

                            Memory <byte>[] buffers = new Memory <byte>[2] {
                                buffer1, buffer2
                            };

                            while (true)
                            {
                                Memory <byte> buffer = buffers[(number++) % 2];

                                Memory <byte> thisTimeBuffer = buffer;

                                if (truncateSize >= 0)
                                {
                                    // Truncate
                                    long remainSize = Math.Max(truncateSize - currentReadPosition, 0);

                                    if (thisTimeBuffer.Length > remainSize)
                                    {
                                        thisTimeBuffer = thisTimeBuffer.Slice(0, (int)remainSize);
                                    }
                                }

                                int readSize = await src.ReadAsync(thisTimeBuffer, cancel);

                                Debug.Assert(readSize <= buffer.Length);

                                if (lastWriteTask != null)
                                {
                                    await lastWriteTask;
                                    currentPosition += writeSize;
                                    reporter.ReportProgress(new ProgressData(currentPosition, estimatedSize));
                                }

                                if (readSize <= 0)
                                {
                                    break;
                                }

                                currentReadPosition += readSize;

                                writeSize = readSize;

                                ReadOnlyMemory <byte> sliced = buffer.Slice(0, writeSize);

                                if (param.Flags.Bit(FileFlags.CopyFile_Verify))
                                {
                                    srcCrc.Append(sliced.Span);
                                }

                                lastWriteTask = dest.WriteAsync(sliced, cancel);
                            }

                            reporter.ReportProgress(new ProgressData(currentPosition, estimatedSize));
                        }
                    }
                }

                srcZipCrc.Set(srcCrc.Value);

                return(currentPosition);
            }
        }