Пример #1
0
        /// <summary>
        /// Read data direct from drive to file
        /// </summary>
        /// <param name="driveLetter"></param>
        /// <param name="fileName"></param>
        /// <param name="eCompType"></param>
        /// <returns></returns>
        public bool ReadDrive(string driveLetter, string fileName, EnumCompressionType eCompType, bool bUseMBR)
        {
            IsCancelling = false;

            var dtStart = DateTime.Now;

            //
            // Map to physical drive
            //
            var physicalDrive = _diskAccess.GetPhysicalPathForLogicalPath(driveLetter);

            if (string.IsNullOrEmpty(physicalDrive))
            {
                LogMsg(Resources.Disk_WriteDrive_Error__Couldn_t_map_partition_to_physical_drive);
                _diskAccess.UnlockDrive();
                return(false);
            }

            //
            // Lock logical drive
            //
            var success = _diskAccess.LockDrive(driveLetter);

            if (!success)
            {
                LogMsg(Resources.Disk_WriteDrive_Failed_to_lock_drive);
                return(false);
            }

            //
            // Get drive size
            //
            var driveSize = _diskAccess.GetDriveSize(physicalDrive);

            if (driveSize <= 0)
            {
                LogMsg(Resources.Disk_WriteDrive_Failed_to_get_device_size);
                _diskAccess.UnlockDrive();
                return(false);
            }

            var readSize = driveSize;

            //
            // Open the physical drive
            //
            var physicalHandle = _diskAccess.Open(physicalDrive);

            if (physicalHandle == null)
            {
                LogMsg(Resources.Disk_WriteDrive_Failed_to_open_physical_drive);
                _diskAccess.UnlockDrive();
                return(false);
            }

            //
            // Start doing the read
            //

            var buffer = new byte[Globals.MaxBufferSize];
            var offset = 0L;

            using (var basefs = (Stream) new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                Stream fs;

                switch (eCompType)
                {
                case EnumCompressionType.Zip:
                    var zfs = new ZipOutputStream(basefs);

                    // Default to middle of the range compression
                    zfs.SetLevel(Globals.CompressionLevel);

                    var fi        = new FileInfo(fileName);
                    var entryName = fi.Name;
                    entryName = entryName.ToLower().Replace(".zip", "");
                    entryName = ZipEntry.CleanName(entryName);
                    var zipEntry = new ZipEntry(entryName)
                    {
                        DateTime = fi.LastWriteTime
                    };
                    zfs.IsStreamOwner = true;

                    // Todo: Consider whether size needs setting for older utils ?

                    zfs.PutNextEntry(zipEntry);

                    fs = zfs;

                    break;

                case EnumCompressionType.Gzip:

                    var gzis = new GZipOutputStream(basefs);
                    gzis.SetLevel(Globals.CompressionLevel);
                    gzis.IsStreamOwner = true;

                    fs = gzis;

                    break;

                case EnumCompressionType.Targzip:

                    var gzos = new GZipOutputStream(basefs);
                    gzos.SetLevel(Globals.CompressionLevel);
                    gzos.IsStreamOwner = true;

                    var tos = new TarOutputStream(gzos);

                    fs = tos;

                    break;

                case EnumCompressionType.XZ:

                    var xzs = new XZOutputStream(basefs);
                    fs = xzs;

                    break;

                default:

                    // No compression - direct to file stream
                    fs = basefs;

                    break;
                }

                while (offset < readSize && !IsCancelling)
                {
                    // NOTE: If we provide a buffer that extends past the end of the physical device ReadFile() doesn't
                    //       seem to do a partial read. Deal with this by reading the remaining bytes at the end of the
                    //       drive if necessary

                    var readMaxLength =
                        (int)
                        ((((ulong)readSize - (ulong)offset) < (ulong)buffer.Length)
                                 ? ((ulong)readSize - (ulong)offset)
                                 : (ulong)buffer.Length);

                    int readBytes;
                    if (_diskAccess.Read(buffer, readMaxLength, out readBytes) < 0)
                    {
                        LogMsg(Resources.Disk_ReadDrive_Error_reading_data_from_drive__ +
                               Marshal.GetHRForLastWin32Error());
                        goto readfail1;
                    }

                    if (readBytes == 0)
                    {
                        LogMsg(Resources.Disk_ReadDrive_Error_reading_data_from_drive___past_EOF_);
                        goto readfail1;
                    }

                    // Check MBR
                    if (bUseMBR && offset == 0)
                    {
                        var truncatedSize = ParseMBRForSize(buffer);

                        if (truncatedSize > driveSize)
                        {
                            LogMsg(Resources.Disk_ReadDrive_Problem_with_filesystem__It_reports_it_is_larger_than_the_disk_);
                            goto readfail1;
                        }

                        if (truncatedSize == 0)
                        {
                            LogMsg(Resources.Disk_ReadDrive_No_valid_partitions_on_drive);
                            goto readfail1;
                        }

                        readSize = truncatedSize;
                    }

                    if (offset == 0)
                    {
                        switch (eCompType)
                        {
                        case EnumCompressionType.Targzip:
                            var fi        = new FileInfo(fileName);
                            var entryName = fi.Name;
                            entryName = entryName.ToLower().Replace(".tar.gz", "");
                            entryName = entryName.ToLower().Replace(".tgz", "");

                            var tarEntry = TarEntry.CreateTarEntry(entryName);
                            tarEntry.Size    = readSize;
                            tarEntry.ModTime = DateTime.SpecifyKind(fi.LastWriteTime, DateTimeKind.Utc);

                            ((TarOutputStream)fs).PutNextEntry(tarEntry);

                            break;
                        }
                    }

                    fs.Write(buffer, 0, readBytes);

                    offset += (uint)readBytes;

                    var percentDone = (int)(100 * offset / readSize);
                    var tsElapsed   = DateTime.Now.Subtract(dtStart);
                    var bytesPerSec = offset / tsElapsed.TotalSeconds;

                    Progress(percentDone);
                    LogMsg(Resources.Disk_ReadDrive_Read + @": " + (offset / Globals.MbModifier) + @" / " +
                           (readSize / Globals.MbModifier) + @" MB " + @"(" + Resources.Disk_ReadDrive_Physical + @": " + (driveSize / Globals.MbModifier) + " MB); " +
                           string.Format("{0:F}", (bytesPerSec / Globals.MbModifier)) + @" MB/s; " + Resources.Disk_Elapsed_time + ": " +
                           tsElapsed.ToString(@"hh\:mm\:ss"));
                }

                if (fs is ZipOutputStream)
                {
                    ((ZipOutputStream)fs).CloseEntry();
                    ((ZipOutputStream)fs).Close();
                }
                else if (fs is TarOutputStream)
                {
                    ((TarOutputStream)fs).CloseEntry();
                    fs.Close();
                }
                else if (fs is GZipOutputStream)
                {
                    fs.Close();
                }
                else if (fs is XZOutputStream)
                {
                    fs.Close();
                }
            }

readfail1:

            _diskAccess.Close();

            _diskAccess.UnlockDrive();

            var tstotalTime = DateTime.Now.Subtract(dtStart);

            if (IsCancelling)
            {
                LogMsg(Resources.Disk_WriteDrive_Cancelled);
            }
            else
            {
                LogMsg(Resources.Disk_ReadDrive_All_Done_Read + @" " + offset + @" " + Resources.Disk_WriteDrive_bytes + @". " + Resources.Disk_Elapsed_time + @": " + tstotalTime.ToString(@"hh\:mm\:ss"));
            }
            Progress(0);
            return(true);
        }
Пример #2
0
        /// <summary>
        /// 将文件打包进TAR中
        /// </summary>
        /// <param name="TarFile"></param>
        /// <param name="files"></param>
        public void AddFilesToTar(string TarFile, params string[] files)
        {
            FileStream      fs = new FileStream(TarFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            TarOutputStream taroutputstream = new TarOutputStream(fs);

            try
            {
                TarArchive tar = TarArchive.CreateOutputTarArchive(taroutputstream);
                tar.RootPath = TarFile;
                tar.SetKeepOldFiles(true);
                TarHeader.EncodingName = "gb2312";

                Console.WriteLine("目标文件:" + TarFile);
                foreach (string f in files)
                {
                    TarEntry tarEntry = TarEntry.CreateEntryFromFile(f);

                    if (tarEntry.IsDirectory)
                    {
                        tar.WriteEntry(tarEntry, false);
                    }
                    else
                    {
                        FileStream ins = ins = System.IO.File.OpenRead(f);

                        string tempfile = f.Substring(3, f.Length - 3);

                        //tarEntry.Name = tempfile;
                        tarEntry.Size = ins.Length;

                        //taroutputstream.PutNextEntry(tarEntry);
                        tar.WriteEntry(tarEntry, false);

                        Console.Write("打包:" + System.IO.Path.GetFileName(f) + "           ");
                        long packedsize = 0;
                        while (true)
                        {
                            byte[] buffer = new byte[1024 * 1024];
                            int    bl     = ins.Read(buffer, 0, buffer.Length);
                            if (bl > 0)
                            {
                                taroutputstream.Write(buffer, 0, bl);
                                packedsize += bl;
                                float per = (float)packedsize / ins.Length * 100;
                                Console.Write("\b\b\b\b{0,4:G}", per.ToString("###") + "%");
                            }
                            else
                            {
                                Console.WriteLine("\b\b\b\b{0,4:G}", "100%");
                                break;
                            }
                        }
                        ins.Close();
                    }

                    taroutputstream.CloseEntry();
                }
                tar.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                taroutputstream.Close();
            }
        }
Пример #3
0
        public async Task VerifyIncludeBaseDirectory_Async(bool includeBaseDirectory)
        {
            using TempDirectory source      = new TempDirectory();
            using TempDirectory destination = new TempDirectory();

            UnixFileMode baseDirectoryMode = TestPermission1;

            SetUnixFileMode(source.Path, baseDirectoryMode);

            string fileName1 = "file1.txt";
            string filePath1 = Path.Join(source.Path, fileName1);

            File.Create(filePath1).Dispose();
            UnixFileMode filename1Mode = TestPermission2;

            SetUnixFileMode(filePath1, filename1Mode);

            string subDirectoryName = "dir/"; // The trailing separator is preserved in the TarEntry.Name
            string subDirectoryPath = Path.Join(source.Path, subDirectoryName);

            Directory.CreateDirectory(subDirectoryPath);
            UnixFileMode subDirectoryMode = TestPermission3;

            SetUnixFileMode(subDirectoryPath, subDirectoryMode);

            string fileName2 = "file2.txt";
            string filePath2 = Path.Join(subDirectoryPath, fileName2);

            File.Create(filePath2).Dispose();
            UnixFileMode filename2Mode = TestPermission4;

            SetUnixFileMode(filePath2, filename2Mode);

            string destinationArchiveFileName = Path.Join(destination.Path, "output.tar");

            TarFile.CreateFromDirectory(source.Path, destinationArchiveFileName, includeBaseDirectory);

            List <TarEntry> entries = new List <TarEntry>();

            FileStreamOptions readOptions = new()
            {
                Access  = FileAccess.Read,
                Mode    = FileMode.Open,
                Options = FileOptions.Asynchronous,
            };

            await using (FileStream fileStream = File.Open(destinationArchiveFileName, readOptions))
            {
                await using (TarReader reader = new TarReader(fileStream))
                {
                    TarEntry entry;
                    while ((entry = await reader.GetNextEntryAsync()) != null)
                    {
                        entries.Add(entry);
                    }
                }
            }

            int expectedCount = 3 + (includeBaseDirectory ? 1 : 0);

            Assert.Equal(expectedCount, entries.Count);

            string prefix = includeBaseDirectory ? Path.GetFileName(source.Path) + '/' : string.Empty;

            if (includeBaseDirectory)
            {
                TarEntry baseEntry = entries.FirstOrDefault(x =>
                                                            x.EntryType == TarEntryType.Directory &&
                                                            x.Name == prefix);
                Assert.NotNull(baseEntry);
                AssertEntryModeFromFileSystemEquals(baseEntry, baseDirectoryMode);
            }

            TarEntry entry1 = entries.FirstOrDefault(x =>
                                                     x.EntryType == TarEntryType.RegularFile &&
                                                     x.Name == prefix + fileName1);

            Assert.NotNull(entry1);
            AssertEntryModeFromFileSystemEquals(entry1, filename1Mode);

            TarEntry directory = entries.FirstOrDefault(x =>
                                                        x.EntryType == TarEntryType.Directory &&
                                                        x.Name == prefix + subDirectoryName);

            Assert.NotNull(directory);
            AssertEntryModeFromFileSystemEquals(directory, subDirectoryMode);

            string   actualFileName2 = subDirectoryName + fileName2; // Notice the trailing separator in subDirectoryName
            TarEntry entry2          = entries.FirstOrDefault(x =>
                                                              x.EntryType == TarEntryType.RegularFile &&
                                                              x.Name == prefix + actualFileName2);

            Assert.NotNull(entry2);
            AssertEntryModeFromFileSystemEquals(entry2, filename2Mode);
        }
Пример #4
0
        public void InvalidVersionName()
        {
            TarEntry e = TarEntry.CreateTarEntry("test");

            e.TarHeader.Version = null;
        }
Пример #5
0
        private static async Task TarFolderCore(FileSystemStorageFolder Folder, TarOutputStream OutputStream, string BaseFolderName, ByteReadChangedEventHandler ByteReadHandler = null)
        {
            List <FileSystemStorageItemBase> ItemList = await Folder.GetChildItemsAsync(true, true).ConfigureAwait(false);

            if (ItemList.Count == 0)
            {
                if (!string.IsNullOrEmpty(BaseFolderName))
                {
                    TarEntry NewEntry = TarEntry.CreateTarEntry($"{BaseFolderName}/");
                    OutputStream.PutNextEntry(NewEntry);
                    OutputStream.CloseEntry();
                }
            }
            else
            {
                ulong CurrentPosition = 0;

                foreach (FileSystemStorageItemBase Item in ItemList)
                {
                    switch (Item)
                    {
                    case FileSystemStorageFolder InnerFolder:
                    {
                        ulong InnerFolderSize = 0;

                        await TarFolderCore(InnerFolder, OutputStream, $"{BaseFolderName}/{InnerFolder.Name}", ByteReadHandler : (ByteRead) =>
                            {
                                InnerFolderSize = ByteRead;
                                ByteReadHandler?.Invoke(CurrentPosition + ByteRead);
                            }).ConfigureAwait(false);

                        ByteReadHandler?.Invoke(CurrentPosition += InnerFolderSize);

                        break;
                    }

                    case FileSystemStorageFile InnerFile:
                    {
                        using (FileStream FileStream = await InnerFile.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                        {
                            TarEntry NewEntry = TarEntry.CreateTarEntry($"{BaseFolderName}/{InnerFile.Name}");
                            NewEntry.ModTime = DateTime.Now;
                            NewEntry.Size    = FileStream.Length;

                            OutputStream.PutNextEntry(NewEntry);

                            await FileStream.CopyToAsync(OutputStream, ProgressHandler : (s, e) =>
                                {
                                    ByteReadHandler?.Invoke(CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * InnerFile.SizeRaw));
                                }).ConfigureAwait(false);
                        }

                        OutputStream.CloseEntry();

                        ByteReadHandler?.Invoke(CurrentPosition += InnerFile.SizeRaw);

                        break;
                    }
                    }
                }
            }
        }
Пример #6
0
        public void InvalidSize()
        {
            TarEntry e = TarEntry.CreateTarEntry("test");

            e.Size = -6;
        }
Пример #7
0
        public void InvalidMagic()
        {
            TarEntry e = TarEntry.CreateTarEntry("test");

            e.TarHeader.Magic = null;
        }
        private void AddDirectoryFilesToTar(TarArchive tarArchive, string sourceDirectory)
        {
            TarEntry tarEntry = TarEntry.CreateEntryFromFile(sourceDirectory);

            tarArchive.WriteEntry(tarEntry, false);
        }
Пример #9
0
        private async void btnBuild_Click(object sender, EventArgs e)
        {
            if (_baseRegistryClient == null ||
                string.IsNullOrWhiteSpace(comboBaseRepository.Text) ||
                lstTags.SelectedIndex < 0
                )
            {
                MessageBox.Show("something is not prepared");
                return;
            }

            List <string> dockerFileLines = new List <string>();

            dockerFileLines.Add($"FROM {comboBaseRepository.Text}:{lstTags.SelectedItem.ToString()}");
            dockerFileLines.Add(@"RUN apt-get update && apt-get install libcurl4-openssl-dev libv8-3.14-dev -y &&\");
            dockerFileLines.Add("    mkdir -p /var/lib/shiny-server/bookmarks/shiny");
            dockerFileLines.Add("COPY *.R /srv/shiny-server/");
            if (lstPackages.Items.Count > 0)
            {
                List <string> packageList = new List <string>();
                foreach (var item in lstPackages.Items)
                {
                    packageList.Add($"'{item.ToString()}'");
                }
                string packages = string.Join(",", packageList);
                dockerFileLines.Add($"RUN R -e \"install.packages(c({packages}))\"");
            }
            dockerFileLines.Add("RUN chmod -R 755 /srv/shiny-server/");
            dockerFileLines.Add("EXPOSE 3838");
            dockerFileLines.Add("CMD [\"/usr/bin/shiny-server.sh\"]");

            File.WriteAllLines(Path.Combine(lblApplicationFolder.Text, "dockerfile"), dockerFileLines);

            Directory.SetCurrentDirectory(lblApplicationFolder.Text);

            // The contents of the application have to be put in a tar file
            DirectoryInfo directoryOfFilesToBeTarred = new DirectoryInfo(lblApplicationFolder.Text);

            FileInfo[] filesInDirectory = directoryOfFilesToBeTarred.GetFiles();
            String     tarArchiveName   = Path.Combine(lblApplicationFolder.Text, "mytararchive.tar");

            using (TarArchive tarArchive = TarArchive.CreateOutputTarArchive(File.Create(tarArchiveName), TarBuffer.DefaultBlockFactor))
            {
                foreach (FileInfo fileToBeTarred in filesInDirectory)
                {
                    TarEntry entry = TarEntry.CreateEntryFromFile(fileToBeTarred.FullName);
                    tarArchive.WriteEntry(entry, true);
                }
            }

            string dockerfile = Path.Combine(lblApplicationFolder.Text, "dockerfile").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

            try
            {
                using (Stream result = await _localEngineClient.Images.BuildImageFromDockerfileAsync(
                           new FileStream(tarArchiveName, FileMode.Open),
                           new ImageBuildParameters {
                    Tags = new List <string> {
                        $"{comboTargetRegistry.Text}/{txtTargetTag.Text}"
                    }
                }))
                {
                    using (FileStream writer = new FileStream(Path.Combine(lblApplicationFolder.Text, "output.file").Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar), FileMode.Create))
                    {
                        int bytesread = 0;
                        do
                        {
                            byte[] buffer = new byte[1000];
                            bytesread = result.Read(buffer, 0, 1000);
                            writer.Write(buffer, 0, bytesread);
                            writer.Flush();
                        }while (bytesread > 0);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception with message {ex.Message}");
            }

            File.Delete(tarArchiveName);
        }
Пример #10
0
        private void ParseStream()
        {
            _entries = new Dictionary <string, TarEntry>();
            _stream.Seek(0, SeekOrigin.Begin);

            while (true)
            {
                byte[] header = new byte[512];
                if (_stream.Read(header, 0, 512) != 512)
                {
                    throw new FileFormatException("Stream contains incomplete Tape Archive entry header.");
                }

                // Is this a null entry? If so, we've reached the supposed end of the
                //   stream. On real tape archives using the TAR format, there may be
                //   data after the null entry, but it is ignored. In addition, there
                //   should be two null entries to signify the end of the archive,
                //   but 7-Zip handles just fine with only one null entry.
                if (header.All(x => (x == 0)))
                {
                    return;
                }

                // Is this a ustar file?
                if (!Magic.SequenceEqual(header.Skip(257).Take(5)))
                {
                    throw new FileFormatException("Tape Archive entry is not a valid ustar entry.");
                }

                // Get file name and file size. Trim null characters while doing so.
                char[] arrfileName = Encoding.ASCII.GetChars(header, 0, 100);
                if (arrfileName[0] == 0)
                {
                    throw new FileFormatException("Invalid file name.");
                }
                string strFileName = new String(arrfileName.TakeWhile(x => x != 0).ToArray());
                char[] arrfileSize = Encoding.ASCII.GetChars(header, 124, 12);
                string strFileSize = new String(arrfileSize.TakeWhile(x => x != 0).ToArray());

                // Before adding the entry to the list, ensure the stream contains that many bytes.
                // Interpret the file size as unsigned to ensure it's not read as a signed number.
                //   TODO: Are numbers interpreted as signed /at all/?
                ulong    fileSize = Convert.ToUInt64(strFileSize, 8);
                TarEntry entry    = new TarEntry(_stream.Position, (long)fileSize);

                // Round file size up to multiple of 512
                // Hacker's Delight 2nd Ed., pg. 59
                ulong paddedFileLength = (fileSize + 511UL) & ~511UL;

                // Skip over file
                try
                {
                    _stream.Seek((long)paddedFileLength, SeekOrigin.Current);
                }
                catch (IOException ex)
                {
                    throw new FileFormatException("File length extends past end of stream.", ex);
                }

                // Add the entry
                if (_entries.ContainsKey(strFileName))
                {
                    throw new FileFormatException("Encountered a file with an already encountered file path.");
                }

                _entries.Add(strFileName, entry);
            }
        }
Пример #11
0
 partial void VerifyPlatformSpecificMetadata(string filePath, TarEntry entry);
Пример #12
0
        private static async void InstallUpdate(UpdateDialog updateDialog, string updateFile)
        {
            // Extract Update
            updateDialog.MainText.Text     = "Extracting Update...";
            updateDialog.ProgressBar.Value = 0;

            if (OperatingSystem.IsLinux())
            {
                using (Stream inStream = File.OpenRead(updateFile))
                    using (Stream gzipStream = new GZipInputStream(inStream))
                        using (TarInputStream tarStream = new TarInputStream(gzipStream, Encoding.ASCII))
                        {
                            updateDialog.ProgressBar.MaxValue = inStream.Length;

                            await Task.Run(() =>
                            {
                                TarEntry tarEntry;
                                while ((tarEntry = tarStream.GetNextEntry()) != null)
                                {
                                    if (tarEntry.IsDirectory)
                                    {
                                        continue;
                                    }

                                    string outPath = Path.Combine(UpdateDir, tarEntry.Name);

                                    Directory.CreateDirectory(Path.GetDirectoryName(outPath));

                                    using (FileStream outStream = File.OpenWrite(outPath))
                                    {
                                        tarStream.CopyEntryContents(outStream);
                                    }

                                    File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));

                                    TarEntry entry = tarEntry;

                                    Application.Invoke(delegate
                                    {
                                        updateDialog.ProgressBar.Value += entry.Size;
                                    });
                                }
                            });

                            updateDialog.ProgressBar.Value = inStream.Length;
                        }
            }
            else
            {
                using (Stream inStream = File.OpenRead(updateFile))
                    using (ZipFile zipFile = new ZipFile(inStream))
                    {
                        updateDialog.ProgressBar.MaxValue = zipFile.Count;

                        await Task.Run(() =>
                        {
                            foreach (ZipEntry zipEntry in zipFile)
                            {
                                if (zipEntry.IsDirectory)
                                {
                                    continue;
                                }

                                string outPath = Path.Combine(UpdateDir, zipEntry.Name);

                                Directory.CreateDirectory(Path.GetDirectoryName(outPath));

                                using (Stream zipStream = zipFile.GetInputStream(zipEntry))
                                    using (FileStream outStream = File.OpenWrite(outPath))
                                    {
                                        zipStream.CopyTo(outStream);
                                    }

                                File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));

                                Application.Invoke(delegate
                                {
                                    updateDialog.ProgressBar.Value++;
                                });
                            }
                        });
                    }
            }

            // Delete downloaded zip
            File.Delete(updateFile);

            List <string> allFiles = EnumerateFilesToDelete().ToList();

            updateDialog.MainText.Text        = "Renaming Old Files...";
            updateDialog.ProgressBar.Value    = 0;
            updateDialog.ProgressBar.MaxValue = allFiles.Count;

            // Replace old files
            await Task.Run(() =>
            {
                foreach (string file in allFiles)
                {
                    try
                    {
                        File.Move(file, file + ".ryuold");

                        Application.Invoke(delegate
                        {
                            updateDialog.ProgressBar.Value++;
                        });
                    }
                    catch
                    {
                        Logger.Warning?.Print(LogClass.Application, "Updater was unable to rename file: " + file);
                    }
                }

                Application.Invoke(delegate
                {
                    updateDialog.MainText.Text        = "Adding New Files...";
                    updateDialog.ProgressBar.Value    = 0;
                    updateDialog.ProgressBar.MaxValue = Directory.GetFiles(UpdatePublishDir, "*", SearchOption.AllDirectories).Length;
                });

                MoveAllFilesOver(UpdatePublishDir, HomeDir, updateDialog);
            });

            Directory.Delete(UpdateDir, true);

            SetUnixPermissions();

            updateDialog.MainText.Text      = "Update Complete!";
            updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?";
            updateDialog.Modal = true;

            updateDialog.ProgressBar.Hide();
            updateDialog.YesButton.Show();
            updateDialog.NoButton.Show();
        }
Пример #13
0
        public static long WriteFileOrDirectoryToStream(Stream stream, string targetPath, bool closeStreamWhenComplete = true)
        {
            long       bytesSent = 0;
            TarArchive tar       = TarArchive.CreateOutputTarArchive(stream);

            tar.IsStreamOwner = closeStreamWhenComplete;
            string[] fileList = null;
            string   rootPath = null;

            // handle globs, directories, and individual files.
            if (targetPath.Contains("*") || targetPath.Contains("?"))
            {
                // we're handling a glob
                rootPath = Path.GetDirectoryName(targetPath);
                fileList = GlobFunctions.Glob(targetPath);
            }
            else if (File.GetAttributes(targetPath).HasFlag(FileAttributes.Directory))
            {
                // handling a directory
                rootPath = targetPath;
                fileList = Directory.GetFileSystemEntries(targetPath, "*", SearchOption.AllDirectories);
            }
            else
            {
                // handling a single file
                rootPath = Path.GetDirectoryName(targetPath);
                fileList = new string[] { targetPath };
            }

            foreach (var entry in fileList)
            {
                var tarEntry = TarEntry.CreateEntryFromFile(entry);

                // Work around for icsharpcode/SharpZipLib issues #334, #337, #338
                // This manually rebuilds the tar header entry name
                var newEntryName = entry;

                // remove the root path, if present and not an empty string, from the entry path
                if (!string.IsNullOrEmpty(rootPath) && newEntryName.StartsWith(rootPath, StringComparison.OrdinalIgnoreCase))
                {
                    newEntryName = newEntryName.Substring(rootPath.Length + 1);
                }

                // in the event this was a unc path name (started with \\),
                // remove leading '\' entries.
                while (newEntryName.StartsWith(@"\", StringComparison.Ordinal))
                {
                    newEntryName = newEntryName.Substring(1);
                }

                // switch all back slashes to forwrd slashes
                newEntryName = newEntryName.Replace(Path.DirectorySeparatorChar, '/');

                // if this is a directory, it should have a trailing slash.
                if (File.GetAttributes(entry).HasFlag(FileAttributes.Directory))
                {
                    newEntryName += '/';
                }

                // rewrite the header block name
                tarEntry.TarHeader.Name = newEntryName;

                // end work around for icsharpcode/SharpZipLib.

                tar.WriteEntry(tarEntry, false);

                // if it's a file, count it's size
                if (!File.GetAttributes(entry).HasFlag(FileAttributes.Directory))
                {
                    bytesSent += (new FileInfo(entry)).Length;
                }
            }

            // close the archive
            tar.Close();

            // return our byte count
            return(bytesSent);
        }
Пример #14
0
        /// <summary>
        /// Returns available modules from the supplied tar.gz file.
        /// </summary>
        private static List <CkanModule> UpdateRegistryFromTarGz(string path, out SortedDictionary <string, int> downloadCounts)
        {
            log.DebugFormat("Starting registry update from tar.gz file: \"{0}\".", path);

            downloadCounts = null;
            List <CkanModule> modules = new List <CkanModule>();

            // Open the gzip'ed file.
            using (Stream inputStream = File.OpenRead(path))
            {
                // Create a gzip stream.
                using (GZipInputStream gzipStream = new GZipInputStream(inputStream))
                {
                    // Create a handle for the tar stream.
                    using (TarInputStream tarStream = new TarInputStream(gzipStream))
                    {
                        // Walk the archive, looking for .ckan files.
                        const string filter = @"\.ckan$";

                        while (true)
                        {
                            TarEntry entry = tarStream.GetNextEntry();

                            // Check for EOF.
                            if (entry == null)
                            {
                                break;
                            }

                            string filename = entry.Name;

                            if (filename.EndsWith("download_counts.json"))
                            {
                                downloadCounts = JsonConvert.DeserializeObject <SortedDictionary <string, int> >(
                                    tarStreamString(tarStream, entry)
                                    );
                                continue;
                            }
                            else if (!Regex.IsMatch(filename, filter))
                            {
                                // Skip things we don't want.
                                log.DebugFormat("Skipping archive entry {0}", filename);
                                continue;
                            }

                            log.DebugFormat("Reading CKAN data from {0}", filename);

                            // Read each file into a buffer.
                            string metadata_json = tarStreamString(tarStream, entry);

                            CkanModule module = ProcessRegistryMetadataFromJSON(metadata_json, filename);
                            if (module != null)
                            {
                                modules.Add(module);
                            }
                        }
                    }
                }
            }
            return(modules);
        }
Пример #15
0
 void EntryCounter(TarArchive archive, TarEntry entry, string message)
 {
     entryCount++;
 }
Пример #16
0
        private string PackFiles(List <CompilationTask> taskList)
        {
            byte[] package;
            using (var memoryStream = new MemoryStream())
            {
                using (var archive = TarArchive.CreateOutputTarArchive(memoryStream))
                {
                    List <string> addedEntries = new List <string>();

                    // Package precompiled header if used
                    foreach (CompilationTask task in taskList)
                    {
                        if (task.PrecompiledHeader.Length > 0)
                        {
                            TarEntry entry = TarEntry.CreateEntryFromFile(task.PrecompiledHeader);
                            entry.Name = Path.GetFileName(task.PrecompiledHeader);
                            archive.WriteEntry(entry, false);
                            break;
                        }
                    }

                    foreach (CompilationTask task in taskList)
                    {
                        // Package sourcefiles
                        // not needed since this file is already in the task.Includes
                        // TODO: should it be like this?
                        {
                            TarEntry entry = TarEntry.CreateEntryFromFile(task.FilePath);
                            entry.Name = Path.GetFileName(task.FilePath);
                            archive.WriteEntry(entry, false);
                        }

                        // Package includes
                        string projectPath           = task.ProjectPath;
                        string dstLibIncludePath     = "includes";
                        string dstProjectIncludePath = "";

                        foreach (string include in task.Includes)
                        {
                            string dstFilePath = null;
                            if (include.StartsWith(projectPath))
                            {
                                string relative = include.Replace(projectPath, "").TrimStart('\\', '/');
                                dstFilePath = Path.Combine(dstProjectIncludePath, relative);
                            }
                            else
                            {
                                for (int i = 0; i < task.IncludeDirs.Count; ++i)
                                {
                                    string srcIncludePath = task.IncludeDirs[i];
                                    if (include.StartsWith(srcIncludePath))
                                    {
                                        string relative = include.Replace(srcIncludePath, "").TrimStart('\\', '/');
                                        dstFilePath = Path.Combine(dstLibIncludePath + i.ToString(), relative);
                                        break;
                                    }
                                }
                            }

                            AddFileToTar(archive, include, dstFilePath, addedEntries);
                        }
                    }

                    // Package build batch
                    TextWriter batch = new StreamWriter("golembuild.bat", false);

                    // CD to the directory the batch file is in
                    batch.WriteLine("cd %~DP0");
                    // Create output folder
                    batch.WriteLine("mkdir output");

                    int numberOfIncludeDirs         = 0;
                    List <CompilerArg> compilerArgs = new List <CompilerArg>();
                    foreach (CompilationTask task in taskList)
                    {
                        bool found = false;
                        foreach (CompilerArg compilerArg in compilerArgs)
                        {
                            if (compilerArg.compiler == task.Compiler && compilerArg.args == task.CompilerArgs)
                            {
                                compilerArg.files.Add(Path.GetFileName(task.FilePath));
                                found = true;
                                break;
                            }
                        }

                        if (found)
                        {
                            continue;
                        }

                        numberOfIncludeDirs = Math.Max(numberOfIncludeDirs, task.IncludeDirs.Count);

                        CompilerArg newCompilerArg = new CompilerArg();
                        newCompilerArg.compiler = task.Compiler;
                        newCompilerArg.args     = task.CompilerArgs;
                        newCompilerArg.files.Add(Path.GetFileName(task.FilePath));
                        compilerArgs.Add(newCompilerArg);
                    }

                    // Add compilation commands, once per CompilerArg
                    foreach (CompilerArg compilerArg in compilerArgs)
                    {
                        for (int i = 0; i < numberOfIncludeDirs; ++i)
                        {
                            compilerArg.args += " /I\"includes" + i.ToString() + "\" /FS";
                        }
                        compilerArg.args += " /Fo\"output/\"";
                        compilerArg.args += " /Fd\"output/" + Path.GetFileNameWithoutExtension(compilerArg.files[0]) + ".pdb\"";
                        compilerArg.args += " /MP" + TaskCapacity;

                        batch.Write("\"../" + Path.GetFileName(compilerArg.compiler) + "\" " + compilerArg.args);

                        foreach (string file in compilerArg.files)
                        {
                            batch.Write(" " + file);
                        }
                        batch.WriteLine();
                    }

                    // Zip output folder
                    batch.WriteLine("powershell.exe -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('output', 'output.zip'); }\"");

                    // stop the service
                    batch.WriteLine("\"../mspdbsrv.exe\" -stop");
                    batch.WriteLine("exit 0");//assume no error

                    batch.Close();

                    TarEntry batchEntry = TarEntry.CreateEntryFromFile("golembuild.bat");
                    batchEntry.Name = "golembuild.bat";
                    archive.WriteEntry(batchEntry, false);
                }
                package = memoryStream.ToArray();

                string hash = GolemCache.RegisterTasksPackage(package);

                FileStream debug = new FileStream(hash + ".tar", FileMode.Create);
                debug.Write(package, 0, package.Length);
                debug.Close();

                return(hash);
            }
        }
Пример #17
0
        public void InvalidModTime()
        {
            TarEntry e = TarEntry.CreateTarEntry("test");

            e.ModTime = DateTime.MinValue;
        }
Пример #18
0
        private string PackFilesPreProcessed(List <CompilationTask> taskList)
        {
            byte[] package;
            using (var memoryStream = new MemoryStream())
            {
                using (var archive = new TarOutputStream(memoryStream))
                {
                    List <string> addedEntries = new List <string>();

                    //precompiled headers are not used in preprocessed build

                    // Package build batch
                    TextWriter batch = new StreamWriter("golembuild.bat", false);

                    // CD to the directory the batch file is in
                    batch.WriteLine("cd %~DP0");
                    // Create output folder
                    batch.WriteLine("mkdir output");

                    List <CompilerArg> compilerArgs = new List <CompilerArg>();
                    foreach (CompilationTask task in taskList)
                    {
                        bool found = false;

                        string args = task.CompilerArgs;

                        foreach (CompilerArg compilerArg in compilerArgs)
                        {
                            bool includesMatch = task.IncludeDirs.Count == compilerArg.includeDirs.Count;
                            if (includesMatch)
                            {
                                for (int i = 0; i < task.IncludeDirs.Count; ++i)
                                {
                                    if (!compilerArg.includeDirs[i].Equals(task.IncludeDirs[i]))
                                    {
                                        includesMatch = false;
                                        break;
                                    }
                                }
                            }
                            if (compilerArg.compiler == task.Compiler && compilerArg.args == args && includesMatch)
                            {
                                compilerArg.files.Add(task.FilePath);
                                found = true;
                                break;
                            }
                        }

                        if (found)
                        {
                            continue;
                        }

                        CompilerArg newCompilerArg = new CompilerArg();
                        newCompilerArg.compiler = task.Compiler;
                        newCompilerArg.args     = args;
                        newCompilerArg.files.Add(task.FilePath);
                        foreach (string e in task.IncludeDirs)
                        {
                            newCompilerArg.includeDirs.Add(e);
                        }

                        compilerArgs.Add(newCompilerArg);
                    }

                    string tempFolder = "iGolemBuild" + Peer.NodeId;
                    Directory.CreateDirectory(tempFolder);

                    //foreach compilation task, preprocess the cpp file into a temporary folder
                    foreach (CompilerArg compilerArg in compilerArgs)
                    {
                        //preprocess file, grab output, write the file as file to compile on external machine
                        Process proc = new Process();
                        string  args = compilerArg.args;
                        //add includes
                        foreach (string inc in compilerArg.includeDirs)
                        {
                            args += " /I\"" + inc + "\" ";
                        }
                        //add preprocessing flag
                        args += "/P /Fi" + tempFolder + "\\ ";
                        args += "/MP" + TaskCapacity;
                        //add source files
                        foreach (string srcFile in compilerArg.files)
                        {
                            args += " " + srcFile;
                        }
                        proc.StartInfo.Arguments              = args;
                        proc.StartInfo.FileName               = compilerArg.compiler;
                        proc.StartInfo.WindowStyle            = ProcessWindowStyle.Hidden;
                        proc.StartInfo.UseShellExecute        = false;
                        proc.StartInfo.RedirectStandardInput  = false;
                        proc.StartInfo.RedirectStandardOutput = true;
                        proc.StartInfo.RedirectStandardError  = true;
                        proc.StartInfo.CreateNoWindow         = true;

                        proc.Start();

                        System.Text.StringBuilder outputStr = new System.Text.StringBuilder();

                        proc.OutputDataReceived += (sender, e) =>
                        {
                            if (e.Data != null)
                            {
                                string output = e.Data;
                                Logger.LogMessage(output);
                            }
                        };

                        proc.ErrorDataReceived += (sender, e) =>
                        {
                            if (e.Data != null)
                            {
                                outputStr.AppendLine(e.Data);
                            }
                        };
                        proc.BeginOutputReadLine();
                        proc.BeginErrorReadLine();

                        proc.WaitForExit();

                        Logger.LogMessage(outputStr.ToString());

                        if (proc.ExitCode == 0)
                        {
                            //now read back the files and add them to tar
                            foreach (string srcFile in compilerArg.files)
                            {
                                //TODO: this might be inside a folder
                                string   precompiledFile = tempFolder + "\\" + Path.GetFileNameWithoutExtension(srcFile) + ".i";
                                TarEntry entry           = TarEntry.CreateEntryFromFile(precompiledFile);
                                entry.Name = Path.GetFileName(srcFile);
                                archive.PutNextEntry(entry);
                                using (Stream inputStream = File.OpenRead(precompiledFile))
                                {
                                    writeStreamToTar(archive, inputStream);
                                    archive.CloseEntry();
                                }
                            }
                        }
                        else
                        {
                            Logger.LogError($"Preprocessing of file package failed");
                        }
                    }

                    Directory.Delete(tempFolder, true);

                    // Add compilation commands, once per CompilerArg
                    foreach (CompilerArg compilerArg in compilerArgs)
                    {
                        //remove precompiled header args /Yu /Fp
                        Match match = Regex.Match(compilerArg.args, "/Yu\".+?\"");
                        if (match.Success)
                        {
                            compilerArg.args = compilerArg.args.Remove(match.Index, match.Length);
                        }
                        match = Regex.Match(compilerArg.args, "/Fp\".+?\"");
                        if (match.Success)
                        {
                            compilerArg.args = compilerArg.args.Remove(match.Index, match.Length);
                        }
                        compilerArg.args += " /FS";
                        compilerArg.args += " /Fo\"output/\"";
                        compilerArg.args += " /Fd\"output/" + Path.GetFileNameWithoutExtension(compilerArg.files[0]) + ".pdb\"";
                        compilerArg.args += " /MP" + TaskCapacity;

                        batch.Write("\"../" + Path.GetFileName(compilerArg.compiler) + "\" " + compilerArg.args);

                        foreach (string file in compilerArg.files)
                        {
                            batch.Write(" " + Path.GetFileName(file));
                        }
                        batch.WriteLine();
                    }

                    // Zip output folder
                    batch.WriteLine("powershell.exe -nologo -noprofile -command \"& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('output', 'output.zip'); }\"");

                    // stop the service
                    batch.WriteLine("\"../mspdbsrv.exe\" -stop");
                    batch.WriteLine("exit 0");//assume no error

                    batch.Close();

                    TarEntry batchEntry = TarEntry.CreateEntryFromFile("golembuild.bat");
                    batchEntry.Name = "golembuild.bat";
                    using (Stream inputStream = File.OpenRead("golembuild.bat"))
                    {
                        batchEntry.Size = inputStream.Length;
                        archive.PutNextEntry(batchEntry);
                        writeStreamToTar(archive, inputStream);
                        archive.CloseEntry();
                    }
                }
                package = memoryStream.ToArray();

                string hash = GolemCache.RegisterTasksPackage(package);

                FileStream debug = new FileStream(hash + ".tar", FileMode.Create);
                debug.Write(package, 0, package.Length);
                debug.Close();

                return(hash);
            }
        }
Пример #19
0
        public void InvalidName()
        {
            TarEntry e = TarEntry.CreateTarEntry("test");

            e.Name = null;
        }
Пример #20
0
        public override TaskStatus Run()
        {
            Info("Creating tgz archive...");

            bool success = true;

            var files = SelectFiles();

            if (files.Length > 0)
            {
                var tgzPath = Path.Combine(Workflow.WorkflowTempFolder, TgzFileName);

                try
                {
                    using (var gz = new GZipOutputStream(File.Create(tgzPath)))
                        using (var tar = new TarOutputStream(gz))
                        {
                            foreach (FileInf file in files)
                            {
                                using (Stream inputStream = File.OpenRead(file.Path))
                                {
                                    string tarName = file.FileName;

                                    long fileSize = inputStream.Length;

                                    // Create a tar entry named as appropriate. You can set the name to anything,
                                    // but avoid names starting with drive or UNC.
                                    var entry = TarEntry.CreateTarEntry(tarName);

                                    // Must set size, otherwise TarOutputStream will fail when output exceeds.
                                    entry.Size = fileSize;

                                    // Add the entry to the tar stream, before writing the data.
                                    tar.PutNextEntry(entry);

                                    var localBuffer = new byte[32 * 1024];
                                    while (true)
                                    {
                                        var numRead = inputStream.Read(localBuffer, 0, localBuffer.Length);
                                        if (numRead <= 0)
                                        {
                                            break;
                                        }
                                        tar.Write(localBuffer, 0, numRead);
                                    }
                                }
                                tar.CloseEntry();
                            }

                            // Finish/Close arent needed strictly as the using statement does this automatically
                            tar.Close();

                            // Finish is important to ensure trailing information for a Zip file is appended.  Without this
                            // the created file would be invalid.
                            gz.Finish();

                            // Close is important to wrap things up and unlock the file.
                            gz.Close();

                            InfoFormat("Tgz {0} created.", tgzPath);
                            Files.Add(new FileInf(tgzPath, Id));
                        }
                }
                catch (ThreadAbortException)
                {
                    throw;
                }
                catch (Exception e)
                {
                    ErrorFormat("An error occured while creating the Tar {0}", e, tgzPath);
                    success = false;
                }
            }

            var status = Status.Success;

            if (!success)
            {
                status = Status.Error;
            }

            Info("Task finished.");
            return(new TaskStatus(status, false));
        }
Пример #21
0
        public void InvalidLinkName()
        {
            TarEntry e = TarEntry.CreateTarEntry("test");

            e.TarHeader.LinkName = null;
        }
Пример #22
0
        public static async Task UpdateRyujinx(UpdateDialog updateDialog, string downloadUrl)
        {
            // Empty update dir, although it shouldn't ever have anything inside it
            if (Directory.Exists(UpdateDir))
            {
                Directory.Delete(UpdateDir, true);
            }

            Directory.CreateDirectory(UpdateDir);

            string updateFile = Path.Combine(UpdateDir, "update.bin");

            // Download the update .zip
            updateDialog.MainText.Text        = "Downloading Update...";
            updateDialog.ProgressBar.Value    = 0;
            updateDialog.ProgressBar.MaxValue = 100;

            using (WebClient client = new WebClient())
            {
                client.DownloadProgressChanged += (_, args) =>
                {
                    updateDialog.ProgressBar.Value = args.ProgressPercentage;
                };

                await client.DownloadFileTaskAsync(downloadUrl, updateFile);
            }

            // Extract Update
            updateDialog.MainText.Text     = "Extracting Update...";
            updateDialog.ProgressBar.Value = 0;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                using (Stream inStream = File.OpenRead(updateFile))
                    using (Stream gzipStream = new GZipInputStream(inStream))
                        using (TarInputStream tarStream = new TarInputStream(gzipStream, Encoding.ASCII))
                        {
                            updateDialog.ProgressBar.MaxValue = inStream.Length;

                            await Task.Run(() =>
                            {
                                TarEntry tarEntry;
                                while ((tarEntry = tarStream.GetNextEntry()) != null)
                                {
                                    if (tarEntry.IsDirectory)
                                    {
                                        continue;
                                    }

                                    string outPath = Path.Combine(UpdateDir, tarEntry.Name);

                                    Directory.CreateDirectory(Path.GetDirectoryName(outPath));

                                    using (FileStream outStream = File.OpenWrite(outPath))
                                    {
                                        tarStream.CopyEntryContents(outStream);
                                    }

                                    File.SetLastWriteTime(outPath, DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc));

                                    TarEntry entry = tarEntry;

                                    Application.Invoke(delegate
                                    {
                                        updateDialog.ProgressBar.Value += entry.Size;
                                    });
                                }
                            });

                            updateDialog.ProgressBar.Value = inStream.Length;
                        }
            }
            else
            {
                using (Stream inStream = File.OpenRead(updateFile))
                    using (ZipFile zipFile = new ZipFile(inStream))
                    {
                        updateDialog.ProgressBar.MaxValue = zipFile.Count;

                        await Task.Run(() =>
                        {
                            foreach (ZipEntry zipEntry in zipFile)
                            {
                                if (zipEntry.IsDirectory)
                                {
                                    continue;
                                }

                                string outPath = Path.Combine(UpdateDir, zipEntry.Name);

                                Directory.CreateDirectory(Path.GetDirectoryName(outPath));

                                using (Stream zipStream = zipFile.GetInputStream(zipEntry))
                                    using (FileStream outStream = File.OpenWrite(outPath))
                                    {
                                        zipStream.CopyTo(outStream);
                                    }

                                File.SetLastWriteTime(outPath, DateTime.SpecifyKind(zipEntry.DateTime, DateTimeKind.Utc));

                                Application.Invoke(delegate
                                {
                                    updateDialog.ProgressBar.Value++;
                                });
                            }
                        });
                    }
            }

            // Delete downloaded zip
            File.Delete(updateFile);

            string[] allFiles = Directory.GetFiles(HomeDir, "*", SearchOption.AllDirectories);

            updateDialog.MainText.Text        = "Renaming Old Files...";
            updateDialog.ProgressBar.Value    = 0;
            updateDialog.ProgressBar.MaxValue = allFiles.Length;

            // Replace old files
            await Task.Run(() =>
            {
                foreach (string file in allFiles)
                {
                    if (!Path.GetExtension(file).Equals(".log"))
                    {
                        try
                        {
                            File.Move(file, file + ".ryuold");

                            Application.Invoke(delegate
                            {
                                updateDialog.ProgressBar.Value++;
                            });
                        }
                        catch
                        {
                            Logger.Warning?.Print(LogClass.Application, "Updater wasn't able to rename file: " + file);
                        }
                    }
                }

                Application.Invoke(delegate
                {
                    updateDialog.MainText.Text        = "Adding New Files...";
                    updateDialog.ProgressBar.Value    = 0;
                    updateDialog.ProgressBar.MaxValue = Directory.GetFiles(UpdatePublishDir, "*", SearchOption.AllDirectories).Length;
                });

                MoveAllFilesOver(UpdatePublishDir, HomeDir, updateDialog);
            });

            Directory.Delete(UpdateDir, true);

            updateDialog.MainText.Text      = "Update Complete!";
            updateDialog.SecondaryText.Text = "Do you want to restart Ryujinx now?";
            updateDialog.Modal = true;

            updateDialog.ProgressBar.Hide();
            updateDialog.YesButton.Show();
            updateDialog.NoButton.Show();
        }
Пример #23
0
        private static async Task CreateTarAsync(IEnumerable <FileSystemStorageItemBase> SourceItemGroup, string NewZipPath, ProgressChangedEventHandler ProgressHandler = null)
        {
            if (await FileSystemStorageItemBase.CreateAsync(NewZipPath, StorageItemTypes.File, CreateOption.GenerateUniqueName).ConfigureAwait(false) is FileSystemStorageFile NewFile)
            {
                ulong TotalSize       = 0;
                ulong CurrentPosition = 0;

                foreach (FileSystemStorageItemBase StorageItem in SourceItemGroup)
                {
                    switch (StorageItem)
                    {
                    case FileSystemStorageFile File:
                    {
                        TotalSize += File.SizeRaw;
                        break;
                    }

                    case FileSystemStorageFolder Folder:
                    {
                        TotalSize += await Folder.GetFolderSizeAsync().ConfigureAwait(false);

                        break;
                    }
                    }
                }

                if (TotalSize > 0)
                {
                    using (FileStream NewFileStream = await NewFile.GetFileStreamFromFileAsync(AccessMode.Exclusive).ConfigureAwait(false))
                        using (TarOutputStream OutputTarStream = new TarOutputStream(NewFileStream, EncodingSetting))
                        {
                            OutputTarStream.IsStreamOwner = false;

                            foreach (FileSystemStorageItemBase StorageItem in SourceItemGroup)
                            {
                                switch (StorageItem)
                                {
                                case FileSystemStorageFile File:
                                {
                                    using (FileStream FileStream = await File.GetFileStreamFromFileAsync(AccessMode.Read).ConfigureAwait(false))
                                    {
                                        TarEntry NewEntry = TarEntry.CreateTarEntry(File.Name);
                                        NewEntry.ModTime = DateTime.Now;
                                        NewEntry.Size    = FileStream.Length;

                                        OutputTarStream.PutNextEntry(NewEntry);

                                        await FileStream.CopyToAsync(OutputTarStream, ProgressHandler : (s, e) =>
                                            {
                                                ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + Convert.ToUInt64(e.ProgressPercentage / 100d * File.SizeRaw)) * 100d / TotalSize), null));
                                            }).ConfigureAwait(false);
                                    }

                                    OutputTarStream.CloseEntry();

                                    CurrentPosition += File.SizeRaw;
                                    ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null));

                                    break;
                                }

                                case FileSystemStorageFolder Folder:
                                {
                                    ulong InnerFolderSize = 0;

                                    await TarFolderCore(Folder, OutputTarStream, Folder.Name, (ByteRead) =>
                                        {
                                            InnerFolderSize = ByteRead;
                                            ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32((CurrentPosition + ByteRead) * 100d / TotalSize), null));
                                        }).ConfigureAwait(false);

                                    CurrentPosition += InnerFolderSize;
                                    ProgressHandler?.Invoke(null, new ProgressChangedEventArgs(Convert.ToInt32(CurrentPosition * 100d / TotalSize), null));

                                    break;
                                }
                                }
                            }

                            await OutputTarStream.FlushAsync().ConfigureAwait(false);
                        }
                }
            }
            else
            {
                throw new UnauthorizedAccessException();
            }
        }
Пример #24
0
        /// <summary>
        /// Appends the Files of a map to an existing Archive
        /// </summary>
        /// <param name="map">the map that contains files which need to be added to an archive</param>
        /// <param name="wrappedName">the name of the taret wrapper-file</param>
        public override void AppendFiles(FileMap map, string wrappedName)
        {
            bool existing = false;

            if (File.Exists(wrappedName))
            {
                File.Move(wrappedName, $"{wrappedName}.lock");
                existing = true;
            }

            using (TarStreamHelper helper = new TarStreamHelper(true, useGz, wrappedName))
            {
                foreach (FileMapEntry entry in map)
                {
                    TarEntry ent = TarEntry.CreateEntryFromFile(entry.LocationInFileSystem);
                    ent.Name = entry.ArchiveFileName;
                    helper.OutputStream.PutNextEntry(ent);
                    using (Stream fs = entry.Open())
                    {
                        fs.CopyTo(helper.OutputStream);
                    }

                    helper.OutputStream.CloseEntry();
                }

                if (existing)
                {
                    try
                    {
                        using (var exhelper = new TarStreamHelper(false, useGz, $"{wrappedName}.lock"))
                        {
                            UnTarFiles(".", exhelper, true, e =>
                            {
                                if (!map.Contains(e.ArchiveFileName))
                                {
                                    TarEntry ent = TarEntry.CreateTarEntry(e.ArchiveFileName);
                                    helper.OutputStream.PutNextEntry(ent);
                                    return(helper.OutputStream);
                                }

                                return(null);
                            }, (input, output) =>
                            {
                                try
                                {
                                    input.CopyTo(output);
                                }
                                finally
                                {
                                    helper.OutputStream.CloseEntry();
                                }
                            }, true);
                        }
                    }
                    finally
                    {
                        File.Delete($"{wrappedName}.lock");
                    }
                }
            }
        }
Пример #25
0
        /// <summary>
        /// 打包多个文件进TAR
        /// </summary>
        /// <param name="TarFile"></param>
        /// <param name="files"></param>
        public void SerFilesToTar(Stream TarStream, string rootPath, params string[] files)
        {
            TarOutputStream taroutputstream = new TarOutputStream(TarStream);

            try
            {
                TarHeader.EncodingName = "gb2312";
                foreach (string f in files)
                {
                    string    tempfile = string.IsNullOrEmpty(rootPath) || !f.ToLower().StartsWith(rootPath.ToLower()) ? f.Substring(3, f.Length - 3) : f.Substring(rootPath.Length).Trim('\\');
                    TarHeader th       = new TarHeader();

                    TarEntry tarEntry = TarEntry.CreateEntryFromFile(f);
                    tarEntry.Name = tempfile;

                    if (tarEntry.IsDirectory)
                    {
                        taroutputstream.PutNextEntry(tarEntry);
                    }
                    else
                    {
                        FileStream ins = new FileStream(f, FileMode.Open, FileAccess.Read);

                        tarEntry.Size = ins.Length;

                        taroutputstream.PutNextEntry(tarEntry);
                        //tar.WriteEntry(tarEntry, true);

                        Console.Write("打包:" + System.IO.Path.GetFileName(f) + "           ");
                        long packedsize = 0;
                        while (true)
                        {
                            if (packedsize >= tarEntry.Size)
                            {
                                break;
                            }
                            byte[] buffer = new byte[1024 * 1024];
                            int    bl     = ins.Read(buffer, 0, buffer.Length);
                            if (bl > 0)
                            {
                                taroutputstream.Write(buffer, 0, bl);
                                packedsize += bl;
                                float per = (float)packedsize / ins.Length * 100;
                                Console.Write("\b\b\b\b{0,4:G}", per.ToString("###") + "%");
                            }
                            else
                            {
                                Console.WriteLine("\b\b\b\b{0,4:G}", "100%");
                                break;
                            }
                        }
                        ins.Close();
                    }
                    taroutputstream.CloseEntry();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                taroutputstream.Close();
            }
        }
Пример #26
0
        private static IFolder findOrCreateFolder(string folderName, IFolder searchInFolder, TarEntry tarEntry)
        {
            foreach (IFolder folder in searchInFolder.Folders)
            {
                if ((folder as ItemInDatabase).Name == folderName)
                {
                    return(folder);
                }
            }
            FolderInDatabase newFolder = new FolderInDatabase(searchInFolder);

            // newFolder.Description = tarEntry.Comment;
            newFolder.CreationTime = tarEntry.ModTime;
            newFolder.Name         = folderName;
            newFolder.Attributes   = FileAttributes.Directory; // na sztywno
            searchInFolder.AddToFolders(newFolder);
            return(newFolder);
        }
Пример #27
0
        /// <summary>
        /// Updates the supplied registry from the supplied zip file.
        /// This will *clear* the registry of available modules first.
        /// This does not *save* the registry. For that, you probably want Repo.Update
        /// </summary>
        internal static void UpdateRegistryFromTarGz(string path, Registry registry)
        {
            log.DebugFormat("Starting registry update from tar.gz file: \"{0}\".", path);

            // Open the gzip'ed file.
            using (Stream inputStream = File.OpenRead(path))
            {
                // Create a gzip stream.
                using (GZipInputStream gzipStream = new GZipInputStream(inputStream))
                {
                    // Create a handle for the tar stream.
                    using (TarInputStream tarStream = new TarInputStream(gzipStream))
                    {
                        // Walk the archive, looking for .ckan files.
                        const string filter = @"\.ckan$";

                        while (true)
                        {
                            TarEntry entry = tarStream.GetNextEntry();

                            // Check for EOF.
                            if (entry == null)
                            {
                                break;
                            }

                            string filename = entry.Name;

                            // Skip things we don't want.
                            if (!Regex.IsMatch(filename, filter))
                            {
                                log.DebugFormat("Skipping archive entry {0}", filename);
                                continue;
                            }

                            log.DebugFormat("Reading CKAN data from {0}", filename);

                            // Read each file into a buffer.
                            int buffer_size;

                            try
                            {
                                buffer_size = Convert.ToInt32(entry.Size);
                            }
                            catch (OverflowException)
                            {
                                log.ErrorFormat("Error processing {0}: Metadata size too large.", entry.Name);
                                continue;
                            }

                            byte[] buffer = new byte[buffer_size];

                            tarStream.Read(buffer, 0, buffer_size);

                            // Convert the buffer data to a string.
                            string metadata_json = Encoding.ASCII.GetString(buffer);

                            ProcessRegistryMetadataFromJSON(metadata_json, registry, filename);
                        }
                    }
                }
            }
        }
Пример #28
0
        public void TrailerContainsNulls()
        {
            const int TestBlockFactor = 3;

            for (int iteration = 0; iteration < TestBlockFactor * 2; ++iteration)
            {
                MemoryStream ms = new MemoryStream();

                using (TarOutputStream tarOut = new TarOutputStream(ms, TestBlockFactor))
                {
                    TarEntry entry = TarEntry.CreateTarEntry("TestEntry");
                    if (iteration > 0)
                    {
                        entry.Size = (TarBuffer.BlockSize * (iteration - 1)) + 9;
                    }
                    tarOut.PutNextEntry(entry);

                    byte[] buffer = new byte[TarBuffer.BlockSize];

                    Random r = new Random();
                    r.NextBytes(buffer);

                    if (iteration > 0)
                    {
                        for (int i = 0; i < iteration - 1; ++i)
                        {
                            tarOut.Write(buffer, 0, buffer.Length);
                        }

                        // Last block is a partial one
                        for (int i = 1; i < 10; ++i)
                        {
                            tarOut.WriteByte((byte)i);
                        }
                    }
                }

                byte[] tarData = ms.ToArray();
                Assert.IsNotNull(tarData, "Data written is null");

                // Blocks = Header + Data Blocks + Zero block + Record trailer
                int usedBlocks  = 1 + iteration + 1;
                int totalBlocks = usedBlocks + (TestBlockFactor - 1);
                totalBlocks /= TestBlockFactor;
                totalBlocks *= TestBlockFactor;

                Assert.AreEqual(TarBuffer.BlockSize * totalBlocks, tarData.Length,
                                string.Format("Tar file should be {0} blocks in length", totalBlocks));

                if (usedBlocks < totalBlocks)
                {
                    // Start at first byte after header.
                    int byteIndex = TarBuffer.BlockSize * (iteration + 1);
                    while (byteIndex < tarData.Length)
                    {
                        int blockNumber = byteIndex / TarBuffer.BlockSize;
                        int offset      = blockNumber % TarBuffer.BlockSize;
                        Assert.AreEqual(0, tarData[byteIndex],
                                        string.Format("Trailing block data should be null iteration {0} block {1} offset {2}  index {3}",
                                                      iteration,
                                                      blockNumber, offset, byteIndex));
                        byteIndex += 1;
                    }
                }
            }
        }
Пример #29
0
    /// <summary>
    /// This is the "real" main. The class main() instantiates a tar object
    /// for the application and then calls this method. Process the arguments
    /// and perform the requested operation.
    /// </summary>
    public void InstanceMain(string[] argv)
    {
        TarArchive archive = null;

        int argIdx = this.ProcessArguments(argv);

        if (this.archiveName != null && !this.archiveName.Equals("-"))
        {
            if (operation == Operation.Create)
            {
                string dirName = Path.GetDirectoryName(archiveName);
                if ((dirName.Length > 0) && !Directory.Exists(dirName))
                {
                    Console.Error.WriteLine("Directory for archive doesnt exist");
                    return;
                }
            }
            else
            {
                if (File.Exists(this.archiveName) == false)
                {
                    Console.Error.WriteLine("File does not exist " + this.archiveName);
                    return;
                }
            }
        }

        if (operation == Operation.Create)                                 // WRITING
        {
            Stream outStream = Console.OpenStandardOutput();

            if (this.archiveName != null && !this.archiveName.Equals("-"))
            {
                outStream = File.Create(archiveName);
            }

            if (outStream != null)
            {
                switch (this.compression)
                {
                case Compression.Compress:
                    outStream = new DeflaterOutputStream(outStream);
                    break;

                case Compression.Gzip:
                    outStream = new GZipOutputStream(outStream);
                    break;

                case Compression.Bzip2:
                    outStream = new BZip2OutputStream(outStream, 9);
                    break;
                }
                archive = TarArchive.CreateOutputTarArchive(outStream, this.blockingFactor);
            }
        }
        else                                            // EXTRACTING OR LISTING
        {
            Stream inStream = Console.OpenStandardInput();

            if (this.archiveName != null && !this.archiveName.Equals("-"))
            {
                inStream = File.OpenRead(archiveName);
            }

            if (inStream != null)
            {
                switch (this.compression)
                {
                case Compression.Compress:
                    inStream = new InflaterInputStream(inStream);
                    break;

                case Compression.Gzip:
                    inStream = new GZipInputStream(inStream);
                    break;

                case Compression.Bzip2:
                    inStream = new BZip2InputStream(inStream);
                    break;
                }
                archive = TarArchive.CreateInputTarArchive(inStream, this.blockingFactor);
            }
        }

        if (archive != null)                                // SET ARCHIVE OPTIONS
        {
            archive.SetKeepOldFiles(this.keepOldFiles);
            archive.AsciiTranslate = this.asciiTranslate;

            archive.SetUserInfo(this.userId, this.userName, this.groupId, this.groupName);
        }

        if (archive == null)
        {
            Console.Error.WriteLine("no processing due to errors");
        }
        else if (operation == Operation.Create)                                    // WRITING
        {
            if (verbose)
            {
                archive.ProgressMessageEvent += ShowTarProgressMessage;
            }

            for (; argIdx < argv.Length; ++argIdx)
            {
                string[] fileNames = GetFilesForSpec(argv[argIdx]);
                if (fileNames.Length > 0)
                {
                    foreach (string name in fileNames)
                    {
                        TarEntry entry = TarEntry.CreateEntryFromFile(name);
                        archive.WriteEntry(entry, true);
                    }
                }
                else
                {
                    Console.Error.Write("No files for " + argv[argIdx]);
                }
            }
        }
        else if (operation == Operation.List)                               // LISTING
        {
            archive.ProgressMessageEvent += ShowTarProgressMessage;
            archive.ListContents();
        }
        else                                                                // EXTRACTING
        {
            string userDir = Environment.CurrentDirectory;
            if (verbose)
            {
                archive.ProgressMessageEvent += ShowTarProgressMessage;
            }

            if (userDir != null)
            {
                archive.ExtractContents(userDir);
            }
        }

        if (archive != null)                                             // CLOSE ARCHIVE
        {
            archive.Close();
        }
    }
Пример #30
0
        public static void CreateArchive(IProgressMonitor mon, string folder, string targetFile)
        {
            string tf = Path.GetFileNameWithoutExtension(targetFile);

            if (tf.EndsWith(".tar"))
            {
                tf = Path.GetFileNameWithoutExtension(tf);
            }

            if (File.Exists(targetFile))
            {
                File.Delete(targetFile);
            }

            using (Stream os = File.Create(targetFile)) {
                Stream outStream = os;
                // Create the zip file
                switch (GetArchiveExtension(targetFile))
                {
                case ".tar.gz":
                    outStream = new GZipOutputStream(outStream);
                    goto case ".tar";

                case ".tar.bz2":
                    outStream = new BZip2OutputStream(outStream, 9);
                    goto case ".tar";

                case ".tar":
                    TarArchive archive = TarArchive.CreateOutputTarArchive(outStream);
                    archive.SetAsciiTranslation(false);
                    archive.RootPath              = folder;
                    archive.ProgressMessageEvent += delegate(TarArchive ac, TarEntry e, string message) {
                        if (message != null)
                        {
                            mon.Log.WriteLine(message);
                        }
                    };

                    foreach (FilePath f in GetFilesRec(new DirectoryInfo(folder)))
                    {
                        TarEntry entry = TarEntry.CreateEntryFromFile(f);
                        entry.Name = f.ToRelative(folder);
                        if (!Platform.IsWindows)
                        {
                            UnixFileInfo fi = new UnixFileInfo(f);
                            entry.TarHeader.Mode = (int)fi.Protection;
                        }
                        else
                        {
                            entry.Name = entry.Name.Replace('\\', '/');
                            FilePermissions p = FilePermissions.S_IFREG | FilePermissions.S_IROTH | FilePermissions.S_IRGRP | FilePermissions.S_IRUSR;
                            if (!new FileInfo(f).IsReadOnly)
                            {
                                p |= FilePermissions.S_IWUSR;
                            }
                            entry.TarHeader.Mode = (int)p;
                        }
                        archive.WriteEntry(entry, false);
                    }

                    // HACK: GNU tar expects to find a double zero record at the end of the archive. TarArchive only emits one.
                    // This hack generates the second zero block.
                    FieldInfo tarOutField = typeof(TarArchive).GetField("tarOut", BindingFlags.Instance | BindingFlags.NonPublic);
                    if (tarOutField != null)
                    {
                        TarOutputStream tarOut = (TarOutputStream)tarOutField.GetValue(archive);
                        tarOut.Finish();
                    }

                    archive.CloseArchive();
                    break;

                case ".zip":
                    ZipOutputStream zs = new ZipOutputStream(outStream);
                    zs.SetLevel(5);

                    byte[] buffer = new byte [8092];
                    foreach (FilePath f in GetFilesRec(new DirectoryInfo(folder)))
                    {
                        string name = f.ToRelative(folder);
                        if (Platform.IsWindows)
                        {
                            name = name.Replace('\\', '/');
                        }
                        ZipEntry infoEntry = new ZipEntry(name);
                        zs.PutNextEntry(infoEntry);
                        using (Stream s = File.OpenRead(f)) {
                            int nr;
                            while ((nr = s.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                zs.Write(buffer, 0, nr);
                            }
                        }
                    }
                    zs.Finish();
                    zs.Close();
                    break;

                default:
                    mon.Log.WriteLine("Unsupported file format: " + Path.GetFileName(targetFile));
                    return;
                }
            }
        }