/// <summary> /// Execute this backup /// </summary> public void Execute() { string backupfolder = "\\backup_" + Name + "_" + DateTime.Now.Ticks; string savedir = TargetDirectory + backupfolder; string target = TargetDirectory + backupfolder; if (Compression) { target = Fl.SafeLocation(RequestFile.Temp) + backupfolder; } foreach (string folder in Folders) { DirectoryInfo di = new DirectoryInfo(folder); FsUtil.CopyFolder(di.FullName, target); } foreach (string file in Files) { FileInfo fi = new FileInfo(file); FsUtil.CopyFolder(fi.FullName, target); } if (Compression) { Core.Util.Compression.Compress(target, savedir + ".zip"); } }
/// <summary> /// Execute this backup /// </summary> public void Execute() { string backupfolder = "\\backup_" + Name + "_" + DateTime.Now.Ticks / 10000000; // divide by 10 000 000 for a shorter name string savedir = TargetDirectory + backupfolder; string target = TargetDirectory + backupfolder; try { // if we need to compress, move to intermediary folder if (Compression) { target = Fl.SafeLocation(RequestFile.Temp) + backupfolder; } foreach (string folder in _folders) { DirectoryInfo di = new DirectoryInfo(folder); FsUtil.CopyFolder(di.FullName, target); } foreach (var file in _files) { FileInfo fi = new FileInfo(file); FsUtil.CopyFolder(fi.FullName, target); } if (Compression) { // compress target to savedir.zip Core.Util.Compression.Compress(target, savedir + ".zip"); // delete temporary folder Directory.Delete(target, true); } } catch (PathTooLongException) { MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr( "A backup couldn't be executed. The path is too long! Try using shorter filenames.") + Environment.NewLine + "Backup name:" + Name, Locale.Tr("Backup failed"), MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception) { MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr( "A backup couldn't be executed. Maybe you don't have sufficient rights, or maybe the files are locked by another application.") + Environment.NewLine + "Backup name:" + Name, Locale.Tr("Backup failed"), MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void InitStore(EventStoreTypes type) { //this.EventStoreType = type.ToString(); switch (type) { case EventStoreTypes.None: store = new NullEventStore(); break; case EventStoreTypes.Xml: storeFile = FsUtil.GetCurrentUserAppDataFolder() + "EventStore.xml"; store = new EventStore(); if (File.Exists(storeFile)) { FileInfo info = new FileInfo(storeFile); if (info.IsReadOnly) { throw new EventStoreInitializationException(storeFile); } store.Hydrate(storeFile); } break; case EventStoreTypes.MSSql: store = new DatabaseEventStore(DaoDbType.MSSql); try { DatabaseAgent.GetAgent(LogEventData.ContextName, DaoDbType.MSSql).EnsureSchema <EventDefinition>(); } catch { // we tried } break; case EventStoreTypes.SQLite: store = new DatabaseEventStore(DaoDbType.SQLite); try { DatabaseAgent.GetAgent(LogEventData.ContextName, DaoDbType.SQLite).EnsureSchema <EventDefinition>(); } catch (UnableToDetermineConnectionStringException utdcse) { //AppDb.Current; } catch { //we tried } break; } }
public void LogFatal(string message) { if (fatalLogger == null) { fatalLogger = (XmlLogger)CreateLogger(Naizari.Logging.LogType.Xml); string folder = FsUtil.GetCurrentUserAppDataFolder(); fatalLogger.LogLocation = folder; fatalLogger.LogName = FsUtil.GetAssemblyName(this.GetType().Assembly); } fatalLogger.AddEntry(message); }
/// <summary> /// Create a new (empty) config file. /// </summary> private static void CreateEmptyConfig(string filepath) { Logger.Log(LogLevel.Info, "Config", "Creating config file"); DirectoryInfo dirInfo = new FileInfo(filepath).Directory; if (dirInfo != null) { string parent = dirInfo.ToString(); FsUtil.CreateDirectoryIfNotExists(parent); } FileStream fs = File.Create(filepath); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("<xml></xml>"); sw.Close(); fs.Close(); }
private static void LoadFile(string file) { _filepath = file; if (!File.Exists(file)) { DirectoryInfo dirInfo = new FileInfo(file).Directory; if (dirInfo != null) { string parent = dirInfo.ToString(); FsUtil.CreateDirectoryIfNotExists(parent); } FileStream fs = File.Create(file); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(XmlHead + XmlTail); sw.Close(); fs.Close(); } _xmldoc = new XmlDocument(); _xmldoc.Load(file); }
/// <summary> /// Load a json list file /// </summary> private void LoadList() { List = new Dictionary <string, ServerListItem>(); if (!File.Exists(_path)) { return; } try { string jsonText = FsUtil.ReadFileInUse(_path); if (string.IsNullOrEmpty(jsonText)) { return; } JsonArray array = JsonConvert.Import <JsonArray>(jsonText); foreach (JsonObject obj in array) { ServerListItem item = new ServerListItem(obj); if (string.IsNullOrEmpty(item.Name)) { continue; } if (!List.ContainsKey(item.Name)) { List.Add(item.Name, item); } } OnListUpdated(); } catch (Exception ex) { Logger.Log(LogLevel.Warning, "Failed to load list: " + _type, ex.Message); } }
/// <summary> /// Install plguins from a zip file /// </summary> /// <param name="version">Version to install</param> /// <param name="showUi">Allow pop-up dialogs</param> /// <remarks></remarks> private static Boolean InstallZip(BukgetPluginVersion version, bool showUi = true) { if (showUi) { if ( MetroMessageBox.Show(Application.OpenForms[0], Locale.Tr("You are about to install") + " " + version.Filename.Replace(".zip", "") + " (" + version.VersionNumber + ")" + Constants.vbCrLf + Locale.Tr("Do you wish to continue?"), Locale.Tr("Continue?"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return(false); } } Logger.Log(LogLevel.Info, "BukGetAPI", "Installing plugin:" + version.Filename + ", packed as zip file"); string randomName = DateTime.Now.Ticks.ToString(); string zipfile = Fl.SafeLocation(RequestFile.Temp) + "install.zip"; string extraction = Fl.SafeLocation(RequestFile.Temp) + "/install_" + randomName + "/"; FileDownloader fdd = new FileDownloader(); fdd.AddFile(version.DownloadLink, zipfile); fdd.ShowDialog(); Compression.Decompress(extraction, zipfile); // ****************************** // At this point, the zip file is extracted to a temporary location // Now only the needed files should be moved Boolean hasFileBeenMoved = false; Boolean hasFolderBeenMoved = false; //file is decompressed, now search the needed files DirectoryInfo extracteDirectoryInfo = new DirectoryInfo(extraction); List <string> extractedFileNamesList = new List <string>(); foreach (FileInfo fileInfo in extracteDirectoryInfo.GetFiles()) { if (fileInfo.Extension == ".jar") { File.Copy(fileInfo.FullName, Fl.Location(RequestFile.Plugindir) + "/" + fileInfo.Name, true); extractedFileNamesList.Add(fileInfo.Name); hasFileBeenMoved = true; Logger.Log(LogLevel.Info, "BukGetAPI", "Jar file found in .zip (L1), copied:" + fileInfo.Name); } } // now we check if there's a folder with the same name as the plugin. This folder should also be moved to the /plugins folder foreach (DirectoryInfo directoryInZipInfo in extracteDirectoryInfo.GetDirectories()) { Boolean folderShouldBeMoved = false; foreach (string f in extractedFileNamesList) { if (f.Contains(directoryInZipInfo.Name)) { folderShouldBeMoved = true; Logger.Log(LogLevel.Info, "BukgetAPI", "Config/Info folder found in .zip, marked directory for copy:" + directoryInZipInfo.Name); } } if (!folderShouldBeMoved) { foreach (FileInfo fileInfo in directoryInZipInfo.GetFiles()) { if (fileInfo.Extension == ".txt" | fileInfo.Extension == ".yml" | fileInfo.Extension == ".cfg" | fileInfo.Extension == ".csv" | fileInfo.Extension == ".js") { folderShouldBeMoved = true; Logger.Log(LogLevel.Info, "BukgetAPI", "Config/Info file found in .zip, marked directory for copy:" + fileInfo.Name); } } } if (folderShouldBeMoved) { Directory.Move(directoryInZipInfo.FullName, Fl.Location(RequestFile.Plugindir) + "/" + directoryInZipInfo.Name); hasFileBeenMoved = false; hasFolderBeenMoved = true; } // If we didn't copy a file yet, check other folders for jar files //L2 if (!hasFileBeenMoved) { foreach (FileInfo fileInfo in directoryInZipInfo.GetFiles()) { if (fileInfo.Extension != ".jar") { continue; } fileInfo.MoveTo(Fl.Location(RequestFile.Plugindir) + "/" + fileInfo.Name); hasFileBeenMoved = true; Logger.Log(LogLevel.Info, "BukgetAPI", "Jar file found in .zip (L2), copied:" + fileInfo.Name); } } if (hasFolderBeenMoved) { continue; } // If we didn't find a config folder yet, check deeper. config folders are not required foreach (DirectoryInfo dir2 in directoryInZipInfo.GetDirectories()) { bool copy2 = false; foreach (string f in extractedFileNamesList) { if (!f.Contains(dir2.Name)) { continue; } copy2 = true; Logger.Log(LogLevel.Info, "BukgetAPI", "Config/Info folder found in .zip, marked directory for copy:" + dir2.Name); } foreach (FileInfo fileInfo in dir2.GetFiles()) { if (fileInfo.Extension == ".txt" | fileInfo.Extension == ".yml" | fileInfo.Extension == ".cfg" | fileInfo.Extension == ".csv" | fileInfo.Extension == ".js") { copy2 = true; Logger.Log(LogLevel.Info, "BukgetAPI", "Config/Info file found in .zip, marked directory for copy:" + fileInfo.Name); } } if (copy2) { FsUtil.CopyFolder(directoryInZipInfo.FullName, Fl.Location(RequestFile.Plugindir) + "/" + dir2.Name, true); } } // end of second level searching } Logger.Log(LogLevel.Info, "BukgetAPI", "Finished plugin installation: Success?" + (hasFileBeenMoved || hasFolderBeenMoved)); //refresh installed list if (showUi) { ShowInstallationComplete(version.PluginName, version.VersionNumber); } return(hasFileBeenMoved || hasFolderBeenMoved); }
private void InitEmbeddedDb() { if ((this.DbType == DaoDbType.Firebird && !string.IsNullOrEmpty(this.ConnectionString)) || this.DbType == DaoDbType.SQLite && !string.IsNullOrEmpty(this.ConnectionString)) { DbConnectionStringBuilder connectionStringBuilder = this.CreateConnectionStringBuilder(); connectionStringBuilder.ConnectionString = this.ConnectionString; string dbName = ""; if (this.DbType != DaoDbType.SQLite && connectionStringBuilder["Initial Catalog"] != null) { dbName = connectionStringBuilder["Initial Catalog"].ToString(); } if (string.IsNullOrEmpty(dbName) && connectionStringBuilder.ContainsKey("Database") && connectionStringBuilder["Database"] != null) { dbName = connectionStringBuilder["Database"].ToString(); } if (string.IsNullOrEmpty(dbName) && connectionStringBuilder.ContainsKey("Data Source") && connectionStringBuilder["Data Source"] != null) { dbName = connectionStringBuilder["Data Source"].ToString(); } if (string.IsNullOrEmpty(dbName)) { dbName = "default.db"; } string path = ""; if (HttpContext.Current == null) { path = FsUtil.GetCurrentUserAppDataFolder() + dbName; } else { path = HttpContext.Current.Server.MapPath("~/App_Data/" + dbName); } if (this.DbType == DaoDbType.Firebird) { this.ConnectionString = "Database=" + path + ";ServerType=1;"; if (!File.Exists(path)) { FbConnection.CreateDatabase(this.ConnectionString); } } else if (this.DbType == DaoDbType.SQLite) { FileInfo file = new FileInfo(path); if (!Directory.Exists(file.DirectoryName)) { Directory.CreateDirectory(file.DirectoryName); } connectionStringBuilder["Data Source"] = file.FullName; this.ConnectionString = connectionStringBuilder.ConnectionString; } } }
public static void Main(string[] args) { if (args.Length < 1) { Help(); return; } var command = args[0]; if (command == "generate") { if (args.Length < 2) { Help(); return; } var baseDirInfo = new DirectoryInfo(args[1]); if (!baseDirInfo.Exists) { Console.WriteLine("base directory is not found"); return; } Console.WriteLine($"base directory: {baseDirInfo.FullName}"); var archiveItems = new List <ArchiveItem>(); var outputFilePath = Path.GetFullPath(Path.Combine(baseDirInfo.FullName, "..", $"{baseDirInfo.Name}.pra")); foreach (var fileInfo in baseDirInfo.EnumerateFiles("*", SearchOption.AllDirectories)) { // path string relativeFilePath = FsUtil.RelativePath(baseDirInfo.FullName, fileInfo.FullName); // buffer byte[] buffer; using (var fileStream = fileInfo.OpenRead()) { buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); } Console.WriteLine($"* Path=\"{relativeFilePath}\", Length={buffer.Length}"); archiveItems.Add(new ArchiveItem(relativeFilePath, buffer)); } using (var fileStream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write)) { ArchiveManager.Write(fileStream, archiveItems); } } else if (command == "metadata") { if (args.Length < 2) { Help(); return; } var archiveFileInfo = new FileInfo(args[1]); if (!archiveFileInfo.Exists) { Console.WriteLine("file is not found"); return; } Console.WriteLine($"target file: {archiveFileInfo.FullName}"); ArchiveMetaData metadata; using (var fileStream = archiveFileInfo.OpenRead()) { metadata = ArchiveManager.ReadMetaData(fileStream); } Console.WriteLine(metadata.Raw.ToJson()); } else { Help(); } }