Beispiel #1
0
 public static async Task CopyFileAsync(string sourcePath, string destinationPath)
 {
     using (Stream source = File.OpenRead(sourcePath))
     {
         using (Stream destination = File.Create(destinationPath))
         {
             await source.CopyToAsync(destination);
         }
     }
 }
Beispiel #2
0
 /// <summary>
 /// Saves this instance to file.
 /// </summary>
 /// <param name="path">The file path to save to.</param>
 /// <param name="asBinary">If set to <c>true</c>, saves this instance as binary.</param>
 public void SaveToFile(string path, bool asBinary)
 {
     try
     {
         using (var f = File.Create(path))
         {
             SaveToStream(f, asBinary);
         }
     }
     catch (Exception ex)
     {
         logger.Fatal(ex);
     }
 }
Beispiel #3
0
        private static void DoDirectCopy(string vmBackupPath, string snapshotPath, int volumePathLength, string vmPath)
        {
            var srcPath = Path.Combine(snapshotPath, vmPath.Substring(volumePathLength));

            if (Directory.Exists(srcPath))
            {
                foreach (var srcChildPath in Directory.GetFileSystemEntries(srcPath))
                {
                    var srcChildPathRel =
                        srcChildPath.Substring(snapshotPath.EndsWith(Path.PathSeparator.ToString(),
                                                                     StringComparison.CurrentCultureIgnoreCase)
                            ? snapshotPath.Length
                            : snapshotPath.Length + 1);
                    var childPath = Path.Combine(vmPath.Substring(0, volumePathLength), srcChildPathRel);
                    DoDirectCopy(vmBackupPath, snapshotPath, volumePathLength, childPath);
                }
            }
            else if (File.Exists(srcPath))
            {
                var outputName = Path.Combine(vmBackupPath, vmPath.Substring(volumePathLength));
                using (var s = File.OpenRead(srcPath))
                {
                    var folder = Path.GetDirectoryName(outputName);
                    if (!Directory.Exists(folder))
                    {
                        Directory.CreateDirectory(folder);
                    }

                    using (var ns = File.Create(outputName))
                    {
                        s.CopyTo(ns);
                    }
                }
            }
            else
            {
                var lowerPath   = srcPath.ToLowerInvariant();
                var isIgnorable = lowerPath.EndsWith(".avhdx") || lowerPath.EndsWith(".vmrs") ||
                                  lowerPath.EndsWith(".bin") || lowerPath.EndsWith(".vsv");

                if (!isIgnorable)
                {
                    throw new Exception($"Entry \"{srcPath}\" not found in snapshot");
                }
            }
        }
            private const uint CKM_Magic = 0x52415442; // BTAR
            private async Task ConvertCKMToZip(string src, string dest)
            {
                using var reader = new BinaryReader(File.OpenRead(src));
                var magic = reader.ReadUInt32();

                if (magic != CKM_Magic)
                {
                    throw new InvalidDataException("Invalid magic format in CKM parsing");
                }

                ushort majorVersion = reader.ReadUInt16();
                ushort minorVersion = reader.ReadUInt16();

                if (majorVersion != 1)
                {
                    throw new InvalidDataException("Archive major version is unknown. Should be 1.");
                }

                if (minorVersion < 2 || minorVersion > 4)
                {
                    throw new InvalidDataException("Archive minor version is unknown. Should be 2, 3, or 4.");
                }

                await using var fos = File.Create(dest);
                using var archive   = new ZipArchive(fos, ZipArchiveMode.Create);
                while (reader.PeekChar() != -1)
                {
                    ushort nameLength = reader.ReadUInt16();
                    string name       = Encoding.UTF8.GetString(reader.ReadBytes(nameLength));
                    ulong  dataLength = reader.ReadUInt64();

                    if (dataLength > int.MaxValue)
                    {
                        throw new Exception();
                    }

                    var entry = archive.CreateEntry(name, CompressionLevel.NoCompression);
                    await using var output = entry.Open();
                    await reader.BaseStream.CopyToLimitAsync(output, (long)dataLength);
                }
            }
Beispiel #5
0
        // the stream version will get called if the file is in an archive
        public List <GrepSearchResult> Search(Stream input, string fileName, string searchPattern, SearchType searchType, GrepSearchOption searchOptions, Encoding encoding)
        {
            // write the stream to a temp folder, and run the file version of the search
            string tempFolder = Path.Combine(Utils.GetTempFolder(), "dnGREP-PDF");
            // the fileName may contain the partial path of the directory structure in the archive
            string filePath = Path.Combine(tempFolder, fileName);

            // use the directory name to also include folders within the archive
            string directory = Path.GetDirectoryName(filePath);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (var fileStream = File.Create(filePath))
            {
                input.Seek(0, SeekOrigin.Begin);
                input.CopyTo(fileStream);
            }

            return(Search(filePath, searchPattern, searchType, searchOptions, encoding));
        }
        static int Main(string[] args)
        {
            return(Parser.Default.ParseArguments <UnpackOptions, ShowOptions, PackOptions>(args)
                   .MapResult(
                       (UnpackOptions opts) => {
                if (opts.extractTarOnly)
                {
                    using (var inflater = GetTarInflaterInputStream(File.OpenRead(opts.file))) {
                        using (var tar = File.Create(Path.Combine(opts.directory, "backup.abh.tar"))) {
                            inflater.CopyTo(tar);
                        }
                    }
                }
                else
                {
                    using (var tarStream = GetTarInputStream(File.OpenRead(opts.file))) {
                        ExtractTarByEntry(tarStream, opts.directory);
                    }
                    // tarArchive.ExtractContents(opts.directory);
                }

                return 0;
            },
                       (ShowOptions opts) => {
                using (var tarStream = GetTarInputStream(File.OpenRead(opts.file))) {
                    TarEntry tarEntry;
                    while ((tarEntry = tarStream.GetNextEntry()) != null)
                    {
                        var entryStr = tarEntry.IsDirectory ? "  [D] " : "  [F] ";

                        entryStr += $"{tarEntry.Name} {tarEntry.Size:n0} bytes";

                        Console.WriteLine(entryStr);
                    }
                }

                return 0;
            },
                       (PackOptions opts) => {
                var tarname = $"{opts.file}.tar";
                using (var tar = File.Create(tarname)) {
                    AddAppsToTar(tar, opts.apps_dir, !opts.disableConventions);
                }

                using (var outAB = File.OpenWrite(opts.file)) {
                    outputAndroidBackupHeader(outAB);

                    outAB.WriteByte(0x78);
                    outAB.WriteByte(0xDA);

                    var chksum = new Adler32();

                    using (var fTar = File.OpenRead(tarname)) {
                        using (var br = new BinaryReader(fTar, Encoding.UTF8, true)) {
                            var BUFLEN = 4096;
                            while (true)
                            {
                                var buf = br.ReadBytes(BUFLEN);
                                if (buf.Length <= 0)
                                {
                                    break;
                                }
                                chksum.Update(buf);
                            }
                        }

                        fTar.Seek(0, SeekOrigin.Begin);
                        using (var defOut = new DeflateStream(outAB, CompressionMode.Compress, true)) {
                            fTar.CopyTo(defOut);
                        }
                    }

                    outAB.Write(BitConverter.GetBytes((uint)chksum.Value), 0, 4);
                }

                return 0;
            },
                       errs => 1));
        }
Beispiel #7
0
 public override Stream Create(string path, int bufferSize, FileOptions options, FileSecurity fileSecurity)
 {
     return(AfsFile.Create(path, bufferSize, options, fileSecurity));
 }
Beispiel #8
0
 public override Stream Create(string path, int bufferSize)
 {
     return(AfsFile.Create(path, bufferSize));
 }
Beispiel #9
0
 public override Stream Create(string path)
 {
     return(AfsFile.Create(path));
 }