コード例 #1
0
        public Task <bool> Start(Action <string> outputReader, bool fullBackup = true)
        {
            // uses a tmp dir because its faster on tmpfs
            var tmpPath = IOUtility.GetRandomTempPath();

            try
            {
                _logger.Info($"Creating temp directory on {tmpPath}");
                var d = Directory.CreateDirectory(tmpPath);
                if (!d.Exists)
                {
                    _logger.Error("Failed to create temp dir");
                    d.Create();
                }

                //TODO: Needs pairing
                _logger.Info("Starting backup");
                var exitCode = ProcessUtility.Instance.StartAndRunCommand(
                    outputReader
                    , "idevicebackup2"
                    //TODO: Create a manager for idevice to handle this
                    , "backup"
                    , fullBackup ? "--full" : ""
                    , tmpPath
                    );

                //TODO: Check the exit codes
                //TODO: Better error handling
                return(exitCode.ContinueWith(t =>
                {
                    _logger.Info("Backup completed");

                    if (t.Status != TaskStatus.RanToCompletion || t.Result != 0)
                    {
                        if (Directory.Exists(tmpPath))
                        {
                            Directory.Delete(tmpPath, true);
                        }
                        return false;
                    }

                    MoveContentsToDestination(tmpPath, _destPath);

                    return true;
                }));
            }
            catch (Exception e)
            {
                _logger.Error($"Error on backup.\n{e.Message}");
                return(Task.FromResult(false));
            }
        }