public void Connect(String connectionString, String baseDataDir) { if (connectionString == null) { connectionString = defaultConnectionString; } try { dbConnection = new MySqlConnection(connectionString); dbConnection.Open(); // TODO: "CREATE DATABASE IF NOT EXISTS `mybox`;"; DbCommand command = dbConnection.CreateCommand(); command.CommandText = @"CREATE TABLE IF NOT EXISTS `files` ( `id` int(11) NOT NULL AUTO_INCREMENT, `path` varchar(512) NOT NULL, `user` int(10) NOT NULL, `parent` int(20) NOT NULL, `size` int(20) NOT NULL, `modtime` int(20) NOT NULL, `checksum` varchar(32) NOT NULL, `type` varchar(1) NOT NULL, PRIMARY KEY (`id`), KEY `user` (`user`) ); CREATE TABLE IF NOT EXISTS `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(75) NOT NULL, `email` varchar(300) NOT NULL, `password` varchar(75) NOT NULL, `salt` varchar(75) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) );"; command.ExecuteNonQuery(); #if DEBUG // password is 'badpassword' dbConnection.CreateCommand(); command.CommandText = @"INSERT IGNORE INTO `mybox`.`users` (`name`,`password`) VALUES ('test', '3693d93220b28a03d3c70bdc1cab2b890c65a2e6baff3d4a2a651b713c161c5c');"; command.ExecuteNonQuery(); #endif } catch (Exception) { throw new Exception("Error connecting to database."); } if (baseDataDir != null) { this.baseDataDir = Common.EndDirWithSlash(baseDataDir); } if (!Directory.Exists(this.baseDataDir)) { Directory.CreateDirectory(this.baseDataDir); // TODO: make this recursive to create parents } }
public void Connect(String connectionString, String baseDataDir) { if (connectionString == null) { connectionString = defaultConnectionString; } try { dbConnection = new MySqlConnection(connectionString); dbConnection.Open(); // todo: initialize DB structure } catch (Exception) { throw new Exception("Error connecting to database."); } if (baseDataDir != null) { this.baseDataDir = Common.EndDirWithSlash(baseDataDir); } if (!Directory.Exists(this.baseDataDir)) { Directory.CreateDirectory(this.baseDataDir); // TODO: make this recursive to create parents } }
public ClientSetup() { ClientServerConnection.LogHandlers.Add(new ClientServerConnection.LoggingHandlerDelegate(logToConsole)); // set up the defaults account = new ClientAccount(); account.ServerName = "localhost"; account.ServerPort = Common.DefaultCommunicationPort; account.User = "******"; account.Password = "******"; configDir = ClientServerConnection.DefaultConfigDir; Console.WriteLine("Welcome to the Mybox client setup wizard"); gatherInput(); account.Directory = Common.EndDirWithSlash(account.Directory); configDir = Common.EndDirWithSlash(configDir); // attach the account to the server to get the user ClientServerConnection client = new ClientServerConnection(); Console.WriteLine("client initialized. trying coonection..."); client.StartGetAccountMode(account); // data directory if (!Common.CreateLocalDirectory(account.Directory)) { Console.WriteLine("Data directory could not be created: " + account.Directory); Common.ExitError(); } // config directory if (!Common.CreateLocalDirectory(configDir)) { Console.WriteLine("Config directory could not be created: " + configDir); Common.ExitError(); } try { ClientServerConnection.SetConfigDir(configDir); } catch (Exception) { // toss config file not found exception since it is expected for a new setup } if (!saveConfig()) { Console.WriteLine("Unable to save config file"); } else { Console.WriteLine("Setup finished successfully"); } }
/// <summary> /// Load a config file and set member variables accordingly /// </summary> /// <param name="configFile"></param> public void LoadConfig(String configFile) { writeMessage("Loading config file " + configFile); try { IniParser iniParser = new IniParser(configFile); // TODO: turn these strings into constants that can be referred to account.ServerName = iniParser.GetSetting("settings", CONFIG_SERVER); // returns NULL when not found account.ServerPort = int.Parse(iniParser.GetSetting("settings", CONFIG_PORT)); account.User = iniParser.GetSetting("settings", CONFIG_USER); account.Directory = iniParser.GetSetting("settings", CONFIG_DIR); account.Password = iniParser.GetSetting("settings", CONFIG_PASSWORD); } catch (FileNotFoundException e) { throw new Exception(e.Message); } // make sure directory ends with a slash account.Directory = Common.EndDirWithSlash(account.Directory); // check values if (account.ServerName == null || account.ServerName == string.Empty) { throw new Exception("Unable to determine host from settings file"); } if (account.User == null || account.User == string.Empty) { throw new Exception("Unable to determine user id"); } if (account.Directory == null) { account.Directory = DefaultClientDir; } if (!Directory.Exists(account.Directory)) { throw new Exception("Directory " + account.Directory + " does not exist"); } dataDir = account.Directory; }
/// <summary> /// Handle command line args /// </summary> /// <param name="args"></param> public static void Main(string[] args) { OptionSet options = new OptionSet(); String configDir = ClientServerConnection.DefaultConfigDir; options.Add("c|configdir=", "configuration directory (default=" + configDir + ")", delegate(string v) { configDir = Common.EndDirWithSlash(v); }); options.Add("h|help", "show help screen", delegate(string v) { Common.ShowCliHelp(options, System.Reflection.Assembly.GetExecutingAssembly()); }); options.Add("v|version", "print the Mybox version", delegate(string v) { Console.WriteLine(Common.AppVersion); System.Diagnostics.Process.GetCurrentProcess().Kill(); }); // Note: all additional arguments are invalid since it does not take non-options List <string> extra = new List <string>(); try { extra = options.Parse(args); } catch (OptionException) { Console.WriteLine("Invalid argument entered"); // print the help screen Common.ShowCliHelp(options, System.Reflection.Assembly.GetExecutingAssembly()); } if (extra.Count > 0) { Console.WriteLine("Invalid argument entered"); // print the help screen Common.ShowCliHelp(options, System.Reflection.Assembly.GetExecutingAssembly()); } new Client(configDir); }