// The function provides the path to the "database" in the default database folder. // It does this in case "database" contains no path. // If it has a path, then it returns the path as given. string database_sqlite_file (string database) { if (filter_url_dirname (database) == ".") { return filter_url_create_root_path ("databases", database + database_sqlite_suffix ()); } return database; }
void Database_Config_Bible::setValue (string bible, const char * key, string value) { if (bible.empty ()) return; string filename = file (bible, key); string dirname = filter_url_dirname (filename); if (!file_or_dir_exists (dirname)) filter_url_mkdir (dirname); filter_url_file_put_contents (filename, value); }
// Wrapper for the mkdir function: make a directory. // Creates parents where needed. void filter_url_mkdir (string directory) { int status; status = mkdir(directory.c_str(), 0777); if (status != 0) { vector <string> paths; paths.push_back (directory); directory = filter_url_dirname (directory); while (directory.length () > 2) { paths.push_back (directory); directory = filter_url_dirname (directory); } reverse (paths.begin (), paths.end ()); for (unsigned int i = 0; i < paths.size (); i++) { mkdir (paths[i].c_str (), 0777); } } }
// Compresses a file identified by $filename into gzipped tar format. // Returns the path to the compressed archive it created. string filter_archive_tar_gzip_file (string filename) { string tarball = filter_url_tempfile () + ".tar.gz"; string dirname = filter_url_escape_shell_argument (filter_url_dirname (filename)); string basename = filter_url_escape_shell_argument (filter_url_basename (filename)); string logfile = filter_url_tempfile () + ".log"; string command = "cd " + dirname + " && tar -czf " + tarball + " " + basename + " > " + logfile + " 2>&1"; int return_var = system (command.c_str()); if (return_var != 0) { filter_url_unlink (tarball); tarball.clear(); string errors = filter_url_file_get_contents (logfile); Database_Logs::log (errors); } return tarball; }
// Compresses a file identified by $filename into zip format. // Returns the path to the zipfile it created. string filter_archive_zip_file (string filename) { if (!file_or_dir_exists (filename)) return ""; string zippedfile = filter_url_tempfile () + ".zip"; string logfile = filter_url_tempfile () + ".log"; string dirname = filter_url_escape_shell_argument (filter_url_dirname (filename)); string basename = filter_url_escape_shell_argument (filter_url_basename (filename)); string command = "cd " + dirname + " && zip " + zippedfile + " " + basename + " > " + logfile + " 2>&1"; int return_var = system (command.c_str()); if (return_var != 0) { filter_url_unlink (zippedfile); zippedfile.clear(); string errors = filter_url_file_get_contents (logfile); Database_Logs::log (errors); } return zippedfile; }
void database_privileges_client_create (const string & user, bool force) { // The path to the file with privileges for the $user. string path = database_privileges_client_path (user); // Without $force, if the file exists, we're done. if (!force) { if (file_or_dir_exists (path)) return; } // If needed, create the folder. string folder = filter_url_dirname (path); if (!file_or_dir_exists (folder)) filter_url_mkdir (folder); // The bits of privileges in human-readable form. string privileges = Database_Privileges::save (user); // Write the privileges to disk. filter_url_file_put_contents (path, privileges); }
void database_privileges_client_remove (const string & user) { string path = database_privileges_client_path (user); path = filter_url_dirname (path); filter_url_rmdir (path); }
int main (int argc, char **argv) { if (argc) {}; if (argv[0]) {}; // Ctrl-C initiates a clean shutdown sequence, so there's no memory leak. signal (SIGINT, sigint_handler); #ifdef HAVE_EXECINFO // Handler for logging segmentation fault. signal (SIGSEGV, sigsegv_handler); #endif // Get the executable path and base the document root on it. string webroot; { // The following works on Linux but not on Mac OS X: char *linkname = (char *) malloc (256); memset (linkname, 0, 256); // valgrind uninitialized value(s) ssize_t r = readlink ("/proc/self/exe", linkname, 256); if (r) {}; webroot = filter_url_dirname (linkname); free (linkname); } { #ifdef HAVE_LIBPROC // The following works on Linux plus on Mac OS X: int ret; pid_t pid; char pathbuf [2048]; pid = getpid (); ret = proc_pidpath (pid, pathbuf, sizeof (pathbuf)); if (ret > 0 ) { webroot = filter_url_dirname (pathbuf); } #endif } bibledit_initialize_library (webroot.c_str(), webroot.c_str()); // Start the Bibledit library. bibledit_start_library (); bibledit_log ("The server started"); cout << "Listening on http://localhost:" << config_logic_http_network_port () << " and https://localhost:" << config_logic_https_network_port () << endl; cout << "Press Ctrl-C to quit" << endl; // Log possible backtrace from a previous crash. string backtrace = filter_url_file_get_contents (backtrace_path ()); filter_url_unlink (backtrace_path ()); if (!backtrace.empty ()) { Database_Logs::log ("Backtrace of the last segmentation fault:"); vector <string> lines = filter_string_explode (backtrace, '\n'); for (auto & line : lines) { Database_Logs::log (line); } } // Bibledit Cloud should restart itself at midnight. // This is to be sure that any memory leaks don't accumulate too much // in case Bibledit Cloud runs for months and years. bibledit_set_quit_at_midnight (); // Keep running till Bibledit stops or gets interrupted. while (bibledit_is_running ()) { }; bibledit_shutdown_library (); return EXIT_SUCCESS; }