2017-04-20 06:56:49 +01:00
//
// Author:
// Natalia Portillo claunia@claunia.com
//
// Copyright (c) 2017, © Claunia.com
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the distribution.
// * Neither the name of the [ORGANIZATION] nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
using System ;
using System.Collections.Generic ;
2017-04-20 21:20:37 +01:00
using System.Diagnostics ;
2017-04-20 06:56:49 +01:00
using System.IO ;
using System.Text ;
2017-04-20 19:32:24 +01:00
using Ionic.Zip ;
2017-04-23 02:35:47 +01:00
using Newtonsoft.Json ;
2017-04-20 06:56:49 +01:00
namespace osrepodbmgr
{
public static class Core
{
// Sets a 128Kbyte buffer
const long bufferSize = 131072 ;
public delegate void UpdateProgressDelegate ( string text , string inner , long current , long maximum ) ;
public delegate void UpdateProgress2Delegate ( string text , string inner , long current , long maximum ) ;
public delegate void FailedDelegate ( string text ) ;
public delegate void FinishedWithoutErrorDelegate ( ) ;
2017-04-20 19:32:24 +01:00
public delegate void FinishedWithTextDelegate ( string text ) ;
2017-04-20 06:56:49 +01:00
public delegate void AddEntryDelegate ( string filename , string hash , bool known ) ;
public static event UpdateProgressDelegate UpdateProgress ;
public static event UpdateProgress2Delegate UpdateProgress2 ;
public static event FailedDelegate Failed ;
public static event FinishedWithoutErrorDelegate Finished ;
2017-04-20 19:32:24 +01:00
public static event FinishedWithTextDelegate FinishedWithText ;
2017-04-20 06:56:49 +01:00
public static event AddEntryDelegate AddEntry ;
static DBCore dbCore ;
2017-04-20 19:32:24 +01:00
static int zipCounter ;
static string zipCurrentEntryName ;
2017-04-20 06:56:49 +01:00
public static void FindFiles ( )
{
2017-04-23 02:35:47 +01:00
string filesPath ;
if ( ! string . IsNullOrEmpty ( MainClass . tmpFolder ) & & Directory . Exists ( MainClass . tmpFolder ) )
filesPath = MainClass . tmpFolder ;
else
filesPath = MainClass . path ;
if ( string . IsNullOrEmpty ( filesPath ) )
2017-04-20 06:56:49 +01:00
{
if ( Failed ! = null )
Failed ( "Path is null or empty" ) ;
}
2017-04-23 02:35:47 +01:00
if ( ! Directory . Exists ( filesPath ) )
2017-04-20 06:56:49 +01:00
{
if ( Failed ! = null )
Failed ( "Directory not found" ) ;
}
try
{
2017-04-23 02:35:47 +01:00
MainClass . files = new List < string > ( Directory . EnumerateFiles ( filesPath , "*" , SearchOption . AllDirectories ) ) ;
2017-04-20 06:56:49 +01:00
MainClass . files . Sort ( ) ;
if ( Finished ! = null )
Finished ( ) ;
}
catch ( Exception ex )
{
2017-04-20 21:20:37 +01:00
if ( Debugger . IsAttached )
2017-04-20 06:56:49 +01:00
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
public static void HashFiles ( )
{
try
{
MainClass . hashes = new Dictionary < string , string > ( ) ;
long counter = 1 ;
foreach ( string file in MainClass . files )
{
2017-04-23 02:35:47 +01:00
string filesPath ;
if ( ! string . IsNullOrEmpty ( MainClass . tmpFolder ) & & Directory . Exists ( MainClass . tmpFolder ) )
filesPath = MainClass . tmpFolder ;
else
filesPath = MainClass . path ;
string relpath = file . Substring ( filesPath . Length + 1 ) ;
2017-04-20 06:56:49 +01:00
if ( UpdateProgress ! = null )
UpdateProgress ( string . Format ( "Hashing file {0} of {1}" , counter , MainClass . files . Count ) , null , counter , MainClass . files . Count ) ;
FileStream fileStream = new FileStream ( file , FileMode . Open , FileAccess . Read ) ;
byte [ ] dataBuffer = new byte [ bufferSize ] ;
SHA256Context sha256Context = new SHA256Context ( ) ;
sha256Context . Init ( ) ;
if ( fileStream . Length > bufferSize )
{
long offset ;
long remainder = fileStream . Length % bufferSize ;
for ( offset = 0 ; offset < ( fileStream . Length - remainder ) ; offset + = ( int ) bufferSize )
{
if ( UpdateProgress2 ! = null )
UpdateProgress2 ( string . Format ( "{0:P}" , offset / ( double ) fileStream . Length ) , relpath , offset , fileStream . Length ) ;
dataBuffer = new byte [ bufferSize ] ;
fileStream . Read ( dataBuffer , 0 , ( int ) bufferSize ) ;
sha256Context . Update ( dataBuffer ) ;
}
if ( UpdateProgress2 ! = null )
UpdateProgress2 ( string . Format ( "{0:P}" , offset / ( double ) fileStream . Length ) , relpath , offset , fileStream . Length ) ;
dataBuffer = new byte [ remainder ] ;
fileStream . Read ( dataBuffer , 0 , ( int ) remainder ) ;
sha256Context . Update ( dataBuffer ) ;
}
else {
if ( UpdateProgress2 ! = null )
UpdateProgress2 ( string . Format ( "{0:P}" , 0 / ( double ) fileStream . Length ) , relpath , 0 , fileStream . Length ) ;
dataBuffer = new byte [ fileStream . Length ] ;
fileStream . Read ( dataBuffer , 0 , ( int ) fileStream . Length ) ;
sha256Context . Update ( dataBuffer ) ;
}
fileStream . Close ( ) ;
string hash = stringify ( sha256Context . Final ( ) ) ;
MainClass . hashes . Add ( relpath , hash ) ;
counter + + ;
}
if ( Finished ! = null )
Finished ( ) ;
}
catch ( Exception ex )
{
2017-04-20 21:20:37 +01:00
if ( Debugger . IsAttached )
2017-04-20 06:56:49 +01:00
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
public static void CheckDbForFiles ( )
{
try
{
long counter = 0 ;
foreach ( KeyValuePair < string , string > kvp in MainClass . hashes )
{
if ( UpdateProgress ! = null )
UpdateProgress ( null , "Checking files in database" , counter , MainClass . hashes . Count ) ;
if ( AddEntry ! = null )
AddEntry ( kvp . Key , kvp . Value , dbCore . DBEntries . ExistsFile ( kvp . Value ) ) ;
counter + + ;
}
if ( Finished ! = null )
Finished ( ) ;
}
catch ( Exception ex )
{
2017-04-20 21:20:37 +01:00
if ( Debugger . IsAttached )
2017-04-20 06:56:49 +01:00
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
public static void AddFilesToDb ( )
{
try
{
long counter = 0 ;
foreach ( KeyValuePair < string , string > kvp in MainClass . hashes )
{
if ( UpdateProgress ! = null )
UpdateProgress ( null , "Adding files to database" , counter , MainClass . hashes . Count ) ;
if ( ! dbCore . DBEntries . ExistsFile ( kvp . Value ) )
dbCore . DBEntries . AddFile ( kvp . Value ) ;
counter + + ;
}
if ( UpdateProgress ! = null )
UpdateProgress ( null , "Adding OS information" , counter , MainClass . hashes . Count ) ;
dbCore . DBEntries . AddOS ( MainClass . dbInfo ) ;
if ( Finished ! = null )
Finished ( ) ;
}
catch ( Exception ex )
{
2017-04-20 21:20:37 +01:00
if ( Debugger . IsAttached )
2017-04-20 06:56:49 +01:00
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
static string stringify ( byte [ ] hash )
{
StringBuilder hashOutput = new StringBuilder ( ) ;
for ( int i = 0 ; i < hash . Length ; i + + )
{
hashOutput . Append ( hash [ i ] . ToString ( "x2" ) ) ;
}
return hashOutput . ToString ( ) ;
}
public static void InitDB ( )
{
CloseDB ( ) ;
dbCore = null ;
try
{
if ( string . IsNullOrEmpty ( Settings . Current . DatabasePath ) )
{
if ( Failed ! = null )
Failed ( "No database file specified" ) ;
return ;
}
dbCore = new SQLite ( ) ;
if ( File . Exists ( Settings . Current . DatabasePath ) )
{
if ( ! dbCore . OpenDB ( Settings . Current . DatabasePath , null , null , null ) )
{
if ( Failed ! = null )
Failed ( "Could not open database, correct file selected?" ) ;
dbCore = null ;
return ;
}
}
else {
if ( ! dbCore . CreateDB ( Settings . Current . DatabasePath , null , null , null ) )
{
if ( Failed ! = null )
Failed ( "Could not create database, correct file selected?" ) ;
dbCore = null ;
return ;
}
if ( ! dbCore . OpenDB ( Settings . Current . DatabasePath , null , null , null ) )
{
if ( Failed ! = null )
Failed ( "Could not open database, correct file selected?" ) ;
dbCore = null ;
return ;
}
}
if ( Finished ! = null )
Finished ( ) ;
}
catch ( Exception ex )
{
2017-04-20 21:20:37 +01:00
if ( Debugger . IsAttached )
2017-04-20 06:56:49 +01:00
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
public static void CloseDB ( )
{
if ( dbCore ! = null )
dbCore . CloseDB ( ) ;
}
2017-04-20 19:32:24 +01:00
public static void CompressFiles ( )
{
try
{
if ( string . IsNullOrWhiteSpace ( MainClass . dbInfo . developer ) )
{
if ( Failed ! = null )
Failed ( "Developer cannot be empty" ) ;
return ;
}
if ( string . IsNullOrWhiteSpace ( MainClass . dbInfo . product ) )
{
if ( Failed ! = null )
Failed ( "Product cannot be empty" ) ;
return ;
}
if ( string . IsNullOrWhiteSpace ( MainClass . dbInfo . version ) )
{
if ( Failed ! = null )
Failed ( "Version cannot be empty" ) ;
return ;
}
// Check if repository folder exists
string destinationFolder = Settings . Current . RepositoryPath ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
// Check if developer folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . developer ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
// Check if product folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . product ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
// Check if version folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . version ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . languages ) )
{
// Check if languages folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . languages ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . architecture ) )
{
// Check if architecture folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . architecture ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
if ( MainClass . dbInfo . oem )
{
// Check if oem folder exists
destinationFolder = Path . Combine ( destinationFolder , "oem" ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . machine ) )
{
// Check if architecture folder exists
destinationFolder = Path . Combine ( destinationFolder , "for " + MainClass . dbInfo . machine ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
string destinationFile = "" ;
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . format ) )
destinationFile + = "[" + MainClass . dbInfo . format + "]" ;
if ( MainClass . dbInfo . files )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "files" ;
}
if ( MainClass . dbInfo . netinstall )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "netinstall" ;
}
if ( MainClass . dbInfo . source )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "source" ;
}
if ( MainClass . dbInfo . update )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "update" ;
}
if ( MainClass . dbInfo . upgrade )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "upgrade" ;
}
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . description ) )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = MainClass . dbInfo . description ;
}
else if ( destinationFile = = "" )
{
destinationFile = "archive" ;
}
string destination = Path . Combine ( destinationFolder , destinationFile ) + ".zip" ;
if ( File . Exists ( destination ) )
{
if ( Failed ! = null )
Failed ( "File already exists" ) ;
return ;
}
ZipFile zf = new ZipFile ( destination ) ;
zf . CompressionLevel = Ionic . Zlib . CompressionLevel . BestCompression ;
zf . CompressionMethod = CompressionMethod . Deflate ;
zf . UseZip64WhenSaving = Zip64Option . AsNecessary ;
2017-04-23 02:35:47 +01:00
string filesPath ;
if ( ! string . IsNullOrEmpty ( MainClass . tmpFolder ) & & Directory . Exists ( MainClass . tmpFolder ) )
filesPath = MainClass . tmpFolder ;
else
filesPath = MainClass . path ;
2017-04-20 19:32:24 +01:00
int counter = 0 ;
foreach ( string file in MainClass . files )
{
if ( UpdateProgress ! = null )
UpdateProgress ( "Choosing files..." , file , counter , MainClass . files . Count ) ;
//FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
FileInfo fi = new FileInfo ( file ) ;
ZipEntry ze = zf . AddFile ( file ) ;
ze . AccessedTime = fi . LastAccessTimeUtc ;
ze . Attributes = fi . Attributes ;
ze . CreationTime = fi . CreationTimeUtc ;
ze . EmitTimesInUnixFormatWhenSaving = true ;
ze . LastModified = fi . LastWriteTimeUtc ;
2017-04-23 02:35:47 +01:00
ze . FileName = file . Substring ( filesPath . Length + 1 ) ;
2017-04-20 19:32:24 +01:00
//fs.Close();
if ( file = = "metadata.json" )
File . Copy ( file , Path . Combine ( destinationFolder , destinationFile ) + ".json" ) ;
if ( file = = "metadata.xml" )
File . Copy ( file , Path . Combine ( destinationFolder , destinationFile ) + ".xml" ) ;
counter + + ;
}
zipCounter = 0 ;
zipCurrentEntryName = "" ;
zf . SaveProgress + = Zf_SaveProgress ;
if ( UpdateProgress ! = null )
UpdateProgress ( null , "Saving..." , 0 , 0 ) ;
zf . Save ( ) ;
}
catch ( Exception ex )
{
2017-04-20 21:20:37 +01:00
if ( Debugger . IsAttached )
2017-04-20 19:32:24 +01:00
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
if ( Finished ! = null )
Finished ( ) ;
}
static void Zf_SaveProgress ( object sender , SaveProgressEventArgs e )
{
if ( e . CurrentEntry ! = null & & e . CurrentEntry . FileName ! = zipCurrentEntryName )
{
zipCurrentEntryName = e . CurrentEntry . FileName ;
zipCounter + + ;
}
if ( UpdateProgress ! = null & & e . CurrentEntry ! = null )
UpdateProgress ( "Compressing..." , e . CurrentEntry . FileName , zipCounter , e . EntriesTotal ) ;
if ( UpdateProgress2 ! = null )
UpdateProgress2 ( string . Format ( "{0:P}" , e . BytesTransferred / ( double ) e . TotalBytesToTransfer ) ,
string . Format ( "{0} / {1}" , e . BytesTransferred , e . TotalBytesToTransfer ) ,
e . BytesTransferred , e . TotalBytesToTransfer ) ;
2017-04-20 21:20:37 +01:00
Console . WriteLine ( "{0}" , e . EventType ) ;
2017-04-20 19:32:24 +01:00
if ( e . EventType = = ZipProgressEventType . Error_Saving & & Failed ! = null )
Failed ( "Failed compression" ) ;
if ( e . EventType = = ZipProgressEventType . Saving_Completed & & FinishedWithText ! = null )
FinishedWithText ( e . ArchiveName ) ;
}
2017-04-20 21:20:37 +01:00
public static void CheckUnar ( )
{
if ( string . IsNullOrWhiteSpace ( Settings . Current . UnArchiverPath ) )
{
if ( Failed ! = null )
Failed ( "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 ) )
{
if ( Failed ! = null )
Failed ( string . Format ( "Cannot find unar executable at {0}." , unarPath ) ) ;
return ;
}
if ( ! File . Exists ( lsarPath ) )
{
if ( Failed ! = null )
Failed ( "Cannot find unar executable." ) ;
return ;
}
string unarOut , lsarOut ;
try
{
Process unarProcess = new Process ( ) ;
unarProcess . StartInfo . FileName = unarPath ;
unarProcess . StartInfo . CreateNoWindow = true ;
unarProcess . StartInfo . RedirectStandardOutput = true ;
unarProcess . StartInfo . UseShellExecute = false ;
unarProcess . Start ( ) ;
unarProcess . WaitForExit ( ) ;
unarOut = unarProcess . StandardOutput . ReadToEnd ( ) ;
}
catch
{
if ( Failed ! = null )
Failed ( "Cannot run unar." ) ;
return ;
}
try
{
Process lsarProcess = new Process ( ) ;
lsarProcess . StartInfo . FileName = lsarPath ;
lsarProcess . StartInfo . CreateNoWindow = true ;
lsarProcess . StartInfo . RedirectStandardOutput = true ;
lsarProcess . StartInfo . UseShellExecute = false ;
lsarProcess . Start ( ) ;
lsarProcess . WaitForExit ( ) ;
lsarOut = lsarProcess . StandardOutput . ReadToEnd ( ) ;
}
catch
{
if ( Failed ! = null )
Failed ( "Cannot run lsar." ) ;
return ;
}
if ( ! unarOut . StartsWith ( "unar " , StringComparison . CurrentCulture ) )
{
if ( Failed ! = null )
Failed ( "Not the correct unar executable" ) ;
return ;
}
if ( ! lsarOut . StartsWith ( "lsar " , StringComparison . CurrentCulture ) )
{
if ( Failed ! = null )
Failed ( "Not the correct unar executable" ) ;
return ;
}
Process versionProcess = new Process ( ) ;
versionProcess . StartInfo . FileName = unarPath ;
versionProcess . StartInfo . CreateNoWindow = true ;
versionProcess . StartInfo . RedirectStandardOutput = true ;
versionProcess . StartInfo . UseShellExecute = false ;
versionProcess . StartInfo . Arguments = "-v" ;
versionProcess . Start ( ) ;
versionProcess . WaitForExit ( ) ;
if ( FinishedWithText ! = null )
FinishedWithText ( versionProcess . StandardOutput . ReadToEnd ( ) . TrimEnd ( new char [ ] { '\n' } ) ) ;
}
2017-04-23 02:35:47 +01:00
public static void OpenArchive ( )
{
if ( ! MainClass . unarUsable )
{
if ( Failed ! = null )
Failed ( "The UnArchiver is not correctly installed" ) ;
return ;
}
if ( ! File . Exists ( MainClass . path ) )
{
if ( Failed ! = null )
Failed ( "Specified file cannot be found" ) ;
return ;
}
try
{
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 lsarPath = Path . Combine ( unarFolder , lsarfilename + extension ) ;
Process lsarProcess = new Process ( ) ;
lsarProcess . StartInfo . FileName = lsarPath ;
lsarProcess . StartInfo . CreateNoWindow = true ;
lsarProcess . StartInfo . RedirectStandardOutput = true ;
lsarProcess . StartInfo . UseShellExecute = false ;
lsarProcess . StartInfo . Arguments = string . Format ( "-j \"\"\"{0}\"\"\"" , MainClass . path ) ;
lsarProcess . Start ( ) ;
string lsarOutput = lsarProcess . StandardOutput . ReadToEnd ( ) ;
lsarProcess . WaitForExit ( ) ;
long counter = 0 ;
string format = null ;
JsonTextReader jsReader = new JsonTextReader ( new StringReader ( lsarOutput ) ) ;
while ( jsReader . Read ( ) )
{
if ( jsReader . TokenType = = JsonToken . PropertyName & & jsReader . Value ! = null & & jsReader . Value . ToString ( ) = = "XADFileName" )
counter + + ;
else if ( jsReader . TokenType = = JsonToken . PropertyName & & jsReader . Value ! = null & & jsReader . Value . ToString ( ) = = "lsarFormatName" )
{
jsReader . Read ( ) ;
if ( jsReader . TokenType = = JsonToken . String & & jsReader . Value ! = null )
format = jsReader . Value . ToString ( ) ;
}
}
// TODO: Check if ZIP file contains Mac OS X metadata
MainClass . copyArchive = false ;
MainClass . archiveFormat = format ;
MainClass . noFilesInArchive = counter ;
if ( string . IsNullOrEmpty ( format ) )
{
if ( Failed ! = null )
Failed ( "File not recognized as an archive" ) ;
return ;
}
if ( counter = = 0 )
{
if ( Failed ! = null )
Failed ( "Archive contains no files" ) ;
return ;
}
if ( Finished ! = null )
Finished ( ) ;
}
catch ( Exception ex )
{
if ( Debugger . IsAttached )
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
public static void ExtractArchive ( )
{
if ( ! MainClass . unarUsable )
{
if ( Failed ! = null )
Failed ( "The UnArchiver is not correctly installed" ) ;
return ;
}
if ( ! File . Exists ( MainClass . path ) )
{
if ( Failed ! = null )
Failed ( "Specified file cannot be found" ) ;
return ;
}
if ( ! Directory . Exists ( Settings . Current . TemporaryFolder ) )
{
if ( Failed ! = null )
Failed ( "Temporary folder cannot be found" ) ;
return ;
}
string tmpFolder = Path . Combine ( Settings . Current . TemporaryFolder , Path . GetRandomFileName ( ) ) ;
try
{
Directory . CreateDirectory ( tmpFolder ) ;
}
catch ( Exception )
{
if ( Debugger . IsAttached )
throw ;
if ( Failed ! = null )
Failed ( "Cannot create temporary folder" ) ;
}
try
{
MainClass . unarProcess = new Process ( ) ;
MainClass . unarProcess . StartInfo . FileName = Settings . Current . UnArchiverPath ;
MainClass . unarProcess . StartInfo . CreateNoWindow = true ;
MainClass . unarProcess . StartInfo . RedirectStandardOutput = true ;
MainClass . unarProcess . StartInfo . UseShellExecute = false ;
MainClass . unarProcess . StartInfo . Arguments = string . Format ( "-o \"\"\"{0}\"\"\" -r -D -k hidden \"\"\"{1}\"\"\"" , tmpFolder , MainClass . path ) ;
long counter = 0 ;
MainClass . unarProcess . OutputDataReceived + = ( sender , e ) = >
{
counter + + ;
if ( UpdateProgress2 ! = null )
UpdateProgress2 ( "" , e . Data , counter , MainClass . noFilesInArchive ) ;
} ;
MainClass . unarProcess . Start ( ) ;
MainClass . unarProcess . BeginOutputReadLine ( ) ;
MainClass . unarProcess . WaitForExit ( ) ;
MainClass . unarProcess . Close ( ) ;
if ( Finished ! = null )
Finished ( ) ;
MainClass . tmpFolder = tmpFolder ;
}
catch ( Exception ex )
{
if ( Debugger . IsAttached )
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
public static void CopyArchive ( )
{
try
{
if ( string . IsNullOrWhiteSpace ( MainClass . dbInfo . developer ) )
{
if ( Failed ! = null )
Failed ( "Developer cannot be empty" ) ;
return ;
}
if ( string . IsNullOrWhiteSpace ( MainClass . dbInfo . product ) )
{
if ( Failed ! = null )
Failed ( "Product cannot be empty" ) ;
return ;
}
if ( string . IsNullOrWhiteSpace ( MainClass . dbInfo . version ) )
{
if ( Failed ! = null )
Failed ( "Version cannot be empty" ) ;
return ;
}
// Check if repository folder exists
string destinationFolder = Settings . Current . RepositoryPath ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
// Check if developer folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . developer ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
// Check if product folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . product ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
// Check if version folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . version ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . languages ) )
{
// Check if languages folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . languages ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . architecture ) )
{
// Check if architecture folder exists
destinationFolder = Path . Combine ( destinationFolder , MainClass . dbInfo . architecture ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
if ( MainClass . dbInfo . oem )
{
// Check if oem folder exists
destinationFolder = Path . Combine ( destinationFolder , "oem" ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . machine ) )
{
// Check if architecture folder exists
destinationFolder = Path . Combine ( destinationFolder , "for " + MainClass . dbInfo . machine ) ;
if ( ! Directory . Exists ( destinationFolder ) )
Directory . CreateDirectory ( destinationFolder ) ;
}
string destinationFile = "" ;
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . format ) )
destinationFile + = "[" + MainClass . dbInfo . format + "]" ;
if ( MainClass . dbInfo . files )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "files" ;
}
if ( MainClass . dbInfo . netinstall )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "netinstall" ;
}
if ( MainClass . dbInfo . source )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "source" ;
}
if ( MainClass . dbInfo . update )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "update" ;
}
if ( MainClass . dbInfo . upgrade )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = "upgrade" ;
}
if ( ! string . IsNullOrWhiteSpace ( MainClass . dbInfo . description ) )
{
if ( destinationFile ! = "" )
destinationFile + = "_" ;
destinationFile + = MainClass . dbInfo . description ;
}
else if ( destinationFile = = "" )
{
destinationFile = "archive" ;
}
string destination = Path . Combine ( destinationFolder , destinationFile ) + ".zip" ;
if ( File . Exists ( destination ) )
{
if ( Failed ! = null )
Failed ( "File already exists" ) ;
return ;
}
File . Copy ( MainClass . path , destination ) ;
}
catch ( Exception ex )
{
if ( Debugger . IsAttached )
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
if ( Finished ! = null )
Finished ( ) ;
}
public static void RemoveTempFolder ( )
{
try
{
if ( Directory . Exists ( MainClass . tmpFolder ) )
{
Directory . Delete ( MainClass . tmpFolder , true ) ;
if ( Finished ! = null )
Finished ( ) ;
}
}
catch ( Exception ex )
{
if ( Debugger . IsAttached )
throw ;
if ( Failed ! = null )
Failed ( string . Format ( "Exception {0}\n{1}" , ex . Message , ex . InnerException ) ) ;
}
}
2017-04-20 06:56:49 +01:00
}
}