Example #1
0
        public static void CompressFiles()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(Context.DbInfo.Developer))
                {
                    Failed?.Invoke("Developer cannot be empty");
                    return;
                }

                if (string.IsNullOrWhiteSpace(Context.DbInfo.Product))
                {
                    Failed?.Invoke("Product cannot be empty");
                    return;
                }

                if (string.IsNullOrWhiteSpace(Context.DbInfo.Version))
                {
                    Failed?.Invoke("Version cannot be empty");
                    return;
                }

                string destinationFolder = "";
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Developer);
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Product);
                destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Version);
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Languages))
                {
                    destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Languages);
                }
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Architecture))
                {
                    destinationFolder = Path.Combine(destinationFolder, Context.DbInfo.Architecture);
                }
                if (Context.DbInfo.Oem)
                {
                    destinationFolder = Path.Combine(destinationFolder, "oem");
                }
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Machine))
                {
                    destinationFolder = Path.Combine(destinationFolder, "for " + Context.DbInfo.Machine);
                }

                string destinationFile = "";
                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Format))
                {
                    destinationFile += "[" + Context.DbInfo.Format + "]";
                }
                if (Context.DbInfo.Files)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "files";
                }

                if (Context.DbInfo.Netinstall)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "netinstall";
                }

                if (Context.DbInfo.Source)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "source";
                }

                if (Context.DbInfo.Update)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "update";
                }

                if (Context.DbInfo.Upgrade)
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += "upgrade";
                }

                if (!string.IsNullOrWhiteSpace(Context.DbInfo.Description))
                {
                    if (destinationFile != "")
                    {
                        destinationFile += "_";
                    }
                    destinationFile += Context.DbInfo.Description;
                }
                else if (destinationFile == "")
                {
                    destinationFile = "archive";
                }

                string destination = Path.Combine(destinationFolder, destinationFile) + ".zip";

                Md5Context md5 = new Md5Context();
                md5.Init();
                byte[] tmp;
                string mdid = md5.Data(Encoding.UTF8.GetBytes(destination), out tmp);
                Console.WriteLine("MDID: {0}", mdid);

                if (dbCore.DbOps.ExistsOs(mdid))
                {
                    if (File.Exists(destination))
                    {
                        Failed?.Invoke("OS already exists.");
                        return;
                    }

                    Failed?.Invoke("OS already exists in the database but not in the repository, check for inconsistencies.");
                    return;
                }

                if (File.Exists(destination))
                {
                    Failed?.Invoke("OS already exists in the repository but not in the database, check for inconsistencies.");
                    return;
                }

                Context.DbInfo.Mdid = mdid;

                string filesPath;

                if (!string.IsNullOrEmpty(Context.TmpFolder) && Directory.Exists(Context.TmpFolder))
                {
                    filesPath = Context.TmpFolder;
                }
                else
                {
                    filesPath = Context.Path;
                }

                string extension = null;

                switch (Settings.Current.CompressionAlgorithm)
                {
                case AlgoEnum.GZip:
                    extension = ".gz";
                    break;

                case AlgoEnum.BZip2:
                    extension = ".bz2";
                    break;

                case AlgoEnum.LZMA:
                    extension = ".lzma";
                    break;

                case AlgoEnum.LZip:
                    extension = ".lz";
                    break;
                }

                long totalSize = 0, currentSize = 0;
                foreach (KeyValuePair <string, DbOsFile> file in Context.Hashes)
                {
                    totalSize += file.Value.Length;
                }

                #if DEBUG
                stopwatch.Restart();
                #endif
                foreach (KeyValuePair <string, DbOsFile> file in Context.Hashes)
                {
                    UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                    destinationFolder = Path.Combine(Settings.Current.RepositoryPath, file.Value.Sha256[0].ToString(),
                                                     file.Value.Sha256[1].ToString(), file.Value.Sha256[2].ToString(),
                                                     file.Value.Sha256[3].ToString(), file.Value.Sha256[4].ToString());
                    Directory.CreateDirectory(destinationFolder);

                    destinationFile = Path.Combine(destinationFolder, file.Value.Sha256 + extension);

                    if (!File.Exists(destinationFile))
                    {
                        FileStream inFs = new FileStream(Path.Combine(filesPath, file.Value.Path), FileMode.Open,
                                                         FileAccess.Read);
                        FileStream outFs   = new FileStream(destinationFile, FileMode.CreateNew, FileAccess.Write);
                        Stream     zStream = null;

                        switch (Settings.Current.CompressionAlgorithm)
                        {
                        case AlgoEnum.GZip:
                            zStream = new GZipStream(outFs, CompressionMode.Compress,
                                                     CompressionLevel.BestCompression);
                            break;

                        case AlgoEnum.BZip2:
                            zStream = new BZip2Stream(outFs, CompressionMode.Compress);
                            break;

                        case AlgoEnum.LZMA:
                            zStream = new LzmaStream(new LzmaEncoderProperties(), false, outFs);
                            outFs.Write(((LzmaStream)zStream).Properties, 0,
                                        ((LzmaStream)zStream).Properties.Length);
                            outFs.Write(BitConverter.GetBytes(inFs.Length), 0, 8);
                            break;

                        case AlgoEnum.LZip:
                            zStream = new LZipStream(outFs, CompressionMode.Compress);
                            break;
                        }

                        byte[] buffer = new byte[BUFFER_SIZE];

                        while (inFs.Position + BUFFER_SIZE <= inFs.Length)
                        {
                            UpdateProgress2?.Invoke($"{inFs.Position / (double)inFs.Length:P}",
                                                    $"{inFs.Position} / {inFs.Length} bytes", inFs.Position,
                                                    inFs.Length);
                            UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                            inFs.Read(buffer, 0, buffer.Length);
                            zStream.Write(buffer, 0, buffer.Length);
                            currentSize += buffer.Length;
                        }

                        buffer = new byte[inFs.Length - inFs.Position];
                        UpdateProgress2?.Invoke($"{inFs.Position / (double)inFs.Length:P}",
                                                $"{inFs.Position} / {inFs.Length} bytes", inFs.Position, inFs.Length);
                        UpdateProgress?.Invoke("Compressing...", file.Value.Path, currentSize, totalSize);

                        inFs.Read(buffer, 0, buffer.Length);
                        zStream.Write(buffer, 0, buffer.Length);
                        currentSize += buffer.Length;

                        UpdateProgress2?.Invoke($"{inFs.Length / (double)inFs.Length:P}", "Finishing...", inFs.Length,
                                                inFs.Length);

                        inFs.Close();
                        zStream.Close();
                        outFs.Dispose();
                    }
                    else
                    {
                        currentSize += file.Value.Length;
                    }
                }
                #if DEBUG
                stopwatch.Stop();
                Console.WriteLine("Core.CompressFiles(): Took {0} seconds to compress files",
                                  stopwatch.Elapsed.TotalSeconds);
                #endif

                if (Context.Metadata != null)
                {
                    MemoryStream  xms = new MemoryStream();
                    XmlSerializer xs  = new XmlSerializer(typeof(CICMMetadataType));
                    xs.Serialize(xms, Context.Metadata);
                    xms.Position = 0;

                    JsonSerializer js = new JsonSerializer
                    {
                        Formatting        = Formatting.Indented,
                        NullValueHandling = NullValueHandling.Ignore
                    };
                    MemoryStream jms = new MemoryStream();
                    StreamWriter sw  = new StreamWriter(jms, Encoding.UTF8, 1048576, true);
                    js.Serialize(sw, Context.Metadata, typeof(CICMMetadataType));
                    sw.Close();
                    jms.Position = 0;

                    destinationFolder = Path.Combine(Settings.Current.RepositoryPath, "metadata", mdid[0].ToString(),
                                                     mdid[1].ToString(), mdid[2].ToString(), mdid[3].ToString(),
                                                     mdid[4].ToString());
                    Directory.CreateDirectory(destinationFolder);

                    FileStream xfs = new FileStream(Path.Combine(destinationFolder, mdid + ".xml"), FileMode.CreateNew,
                                                    FileAccess.Write);
                    xms.CopyTo(xfs);
                    xfs.Close();
                    FileStream jfs = new FileStream(Path.Combine(destinationFolder, mdid + ".json"), FileMode.CreateNew,
                                                    FileAccess.Write);
                    jms.CopyTo(jfs);
                    jfs.Close();

                    xms.Position = 0;
                    jms.Position = 0;
                }

                FinishedWithText?.Invoke($"Correctly added operating system with MDID {mdid}");
            }
            catch (ThreadAbortException) { }
            catch (Exception ex)
            {
                if (Debugger.IsAttached)
                {
                    throw;
                }

                Failed?.Invoke($"Exception {ex.Message}\n{ex.InnerException}");
                #if DEBUG
                Console.WriteLine("Exception {0}\n{1}", ex.Message, ex.InnerException);
                #endif
            }
        }
Example #2
0
        public static void CheckUnar()
        {
            if (string.IsNullOrWhiteSpace(Settings.Current.UnArchiverPath))
            {
                Failed?.Invoke("unar path is not set.");
                return;
            }

            string unarFolder   = Path.GetDirectoryName(Settings.Current.UnArchiverPath);
            string extension    = Path.GetExtension(Settings.Current.UnArchiverPath);
            string unarfilename = Path.GetFileNameWithoutExtension(Settings.Current.UnArchiverPath);
            string lsarfilename = unarfilename.Replace("unar", "lsar");
            string unarPath     = Path.Combine(unarFolder, unarfilename + extension);
            string lsarPath     = Path.Combine(unarFolder, lsarfilename + extension);

            if (!File.Exists(unarPath))
            {
                Failed?.Invoke($"Cannot find unar executable at {unarPath}.");
                return;
            }

            if (!File.Exists(lsarPath))
            {
                Failed?.Invoke("Cannot find unar executable.");
                return;
            }

            string unarOut, lsarOut;

            try
            {
                Process unarProcess = new Process
                {
                    StartInfo =
                    {
                        FileName               = unarPath,
                        CreateNoWindow         = true,
                        RedirectStandardOutput = true,
                        UseShellExecute        = false
                    }
                };
                unarProcess.Start();
                unarProcess.WaitForExit();
                unarOut = unarProcess.StandardOutput.ReadToEnd();
            }
            catch
            {
                Failed?.Invoke("Cannot run unar.");
                return;
            }

            try
            {
                Process lsarProcess = new Process
                {
                    StartInfo =
                    {
                        FileName               = lsarPath,
                        CreateNoWindow         = true,
                        RedirectStandardOutput = true,
                        UseShellExecute        = false
                    }
                };
                lsarProcess.Start();
                lsarProcess.WaitForExit();
                lsarOut = lsarProcess.StandardOutput.ReadToEnd();
            }
            catch
            {
                Failed?.Invoke("Cannot run lsar.");
                return;
            }

            if (!unarOut.StartsWith("unar ", StringComparison.CurrentCulture))
            {
                Failed?.Invoke("Not the correct unar executable");
                return;
            }

            if (!lsarOut.StartsWith("lsar ", StringComparison.CurrentCulture))
            {
                Failed?.Invoke("Not the correct unar executable");
                return;
            }

            Process versionProcess = new Process
            {
                StartInfo =
                {
                    FileName               = unarPath,
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    Arguments              = "-v"
                }
            };

            versionProcess.Start();
            versionProcess.WaitForExit();

            FinishedWithText?.Invoke(versionProcess.StandardOutput.ReadToEnd().TrimEnd('\n'));
        }
Example #3
0
        public bool CheckUnAr(string unArPath)
        {
            if (string.IsNullOrWhiteSpace(unArPath))
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = Localization.UnArPathNotSet
                });

                return(false);
            }

            string unarFolder   = Path.GetDirectoryName(unArPath);
            string extension    = Path.GetExtension(unArPath);
            string unarfilename = Path.GetFileNameWithoutExtension(unArPath);
            string lsarfilename = unarfilename.Replace("unar", "lsar");
            string unarPath     = Path.Combine(unarFolder, unarfilename + extension);
            string lsarPath     = Path.Combine(unarFolder, lsarfilename + extension);

            if (!File.Exists(unarPath))
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = string.Format(Localization.CannotFindUnArAtPath, unarPath)
                });

                return(false);
            }

            if (!File.Exists(lsarPath))
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = Localization.CannotFindLsAr
                });

                return(false);
            }

            string unarOut, lsarOut;

            try
            {
                var unarProcess = new Process
                {
                    StartInfo =
                    {
                        FileName               = unarPath,
                        CreateNoWindow         = true,
                        RedirectStandardOutput = true,
                        UseShellExecute        = false
                    }
                };

                unarProcess.Start();
                unarProcess.WaitForExit();
                unarOut = unarProcess.StandardOutput.ReadToEnd();
            }
            catch
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = Localization.CannotRunUnAr
                });

                return(false);
            }

            try
            {
                var lsarProcess = new Process
                {
                    StartInfo =
                    {
                        FileName               = lsarPath,
                        CreateNoWindow         = true,
                        RedirectStandardOutput = true,
                        UseShellExecute        = false
                    }
                };

                lsarProcess.Start();
                lsarProcess.WaitForExit();
                lsarOut = lsarProcess.StandardOutput.ReadToEnd();
            }
            catch
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = Localization.CannotRunLsAr
                });

                return(false);
            }

            if (!unarOut.StartsWith("unar ", StringComparison.CurrentCulture))
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = Localization.NotCorrectUnAr
                });

                return(false);
            }

            if (!lsarOut.StartsWith("lsar ", StringComparison.CurrentCulture))
            {
                FailedWithText?.Invoke(this, new ErrorEventArgs
                {
                    Message = Localization.NotCorrectLsAr
                });

                return(false);
            }

            var versionProcess = new Process
            {
                StartInfo =
                {
                    FileName               = unarPath,
                    CreateNoWindow         = true,
                    RedirectStandardOutput = true,
                    UseShellExecute        = false,
                    Arguments              = "-v"
                }
            };

            versionProcess.Start();
            versionProcess.WaitForExit();

            FinishedWithText?.Invoke(this, new MessageEventArgs
            {
                Message = versionProcess.StandardOutput.ReadToEnd().TrimEnd('\n')
            });

            return(true);
        }