void initConfig() { _config_param = parameter_new(); /* config-dir */ char *home = getenv("HOME"); if (home == NULL) { error_set(ERROR_CONFIG_HOME, "Unable to get home directory."); return; } STRINGBUFFER *tmp_cfg = stringbuffer_new(); /* config-directory */ stringbuffer_append(tmp_cfg, home); stringbuffer_append(tmp_cfg, SEPARATOR); stringbuffer_append(tmp_cfg, CONFIG_DIRECTORY); _config_directory = stringbuffer_clone(tmp_cfg); /* config-file */ stringbuffer_append(tmp_cfg, SEPARATOR); stringbuffer_append(tmp_cfg, CONFIG_FILE); _config_file = stringbuffer_clone(tmp_cfg); printf ( "CONFIGFILE:\t%s\n", stringbuffer_getTextPointer(_config_file) ); stringbuffer_free(tmp_cfg); /* load config */ _config_param = parameter_new(); parameter_loadFromFile(_config_param, stringbuffer_getTextPointer(_config_file)); checkParameter(CONFIG_DIRECTORY_STORE_KEY, CONFIG_DIRECTORY_STORE_VALUE); checkParameter(CONFIG_DIRECTORY_RESTORE_KEY, CONFIG_DIRECTORY_RESTORE_VALUE); checkParameter(CONFIG_DIRECTORY_KEY_KEY, CONFIG_DIRECTORY_KEY_VALUE); /* make directories */ char *tmp_dir; /* make store directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_STORE_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); /* make restore directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_RESTORE_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); /* make key directory */ tmp_dir = parameter_get(_config_param, CONFIG_DIRECTORY_KEY_KEY); assert(tmp_dir); if (!directory_exists(tmp_dir)) directory_create(tmp_dir); free(tmp_dir); }
/* Note createConversation does not use getConversationFileName since it must create directories along the way */ int createConversation(const char * userA, const char * userB){ int order = strncmp(userA,userB,BUFFER_LENGTH-1); if(order == 0) return 0; //You can't talk to yourself. char buffer[(BUFFER_LENGTH*3) + strlen(DATA_DIR)]; //*3 for safety bzero(buffer, sizeof(buffer)); if(order < 1){ snprintf(buffer, sizeof(buffer), "%s%s-%s", DATA_DIR, userA, userB); }else{ snprintf(buffer, sizeof(buffer), "%s%s-%s", DATA_DIR, userB, userA); } int existsAlready = directory_exists(buffer); if(existsAlready == -1){ fprintf(stderr, "%s%s\n", FAILED_DIR_EXISTS, buffer); return 0; } if(existsAlready != 1){ if( directory_create(buffer) != 0 ){ fprintf(stderr, "%s %s\n", FAILED_DIR_CREATION, buffer); return 0; } } /* Conversation Directory Exists, Order conversations by time */ char timeBuffer[BUFFER_LENGTH]; bzero(timeBuffer, sizeof(timeBuffer)); /* pretty much exactly from strftime's man page: */ time_t t; struct tm * tmp; t = time(NULL); tmp = localtime(&t); if(tmp == NULL){ fprintf(stderr, "Failed to determine local time\n"); return 0; } strftime(timeBuffer, sizeof(timeBuffer), "/%F.txt", tmp); if(strlen(timeBuffer) + strlen(buffer) + 1 > sizeof(buffer)){ fprintf(stderr, "Filename too large for conversation between %s and %s\n:%s/%s\n", userA, userB, buffer, timeBuffer); return 0; } char * fileName = strncat(buffer, timeBuffer, sizeof(buffer)); if(file_exists(fileName) == 1){ //No sense in creating it again. return 1; } FILE *fp = fopen(fileName, "a"); if(!fp){ fprintf(stderr, "%s %s\n", FAILED_FILE_CREATION, fileName); return 0; } fflush(fp); fclose(fp); return 1; }
int main(int argc, char *argv[]) { if (argc != 4) { fprintf(stderr, "USAGE:\t\t%s SERVER-ADDRESS FROM TO", argv[0]); } char *address = argv[1]; char *from = argv[2]; char *to = argv[3]; /* check/create local directory */ if (!directory_exists(to)) { directory_create(to); if (!directory_exists(to)) { error("local directory doesn't exists"); } } /* get remote file-list */ LIST *dirs = active_client_file_list(address, from); if (!dirs) { printf ( "ERROR:\t\t%s\n", "no files found" ); return EXIT_FAILURE; } char *file; char *file_from; char *file_to; unsigned int i; for (i = 0; i < list_size(dirs); ++i) { file = list_get(dirs, i); file_from = (char *)malloc(strlen(from) + 1 + strlen(file) + 1); strcpy(file_from, from); strcat(file_from, DIRECTORY_SEPARATOR_STRING); strcat(file_from, file); file_to = (char *)malloc(strlen(to) + 1 + strlen(file) + 1); strcpy(file_to, to); strcat(file_to, DIRECTORY_SEPARATOR_STRING); strcat(file_to, file); active_client_file_get(address, file_from, file_to); free(file_from); free(file_to); free(file); } // list_show(dirs); return EXIT_SUCCESS; }
int bforce_create_dirs(void) { const char *p_dirname = conf_string(cf_status_directory); if( p_dirname ) { if( access(p_dirname, F_OK) == -1 || !is_directory(p_dirname) ) { if( directory_create(p_dirname, 0700) == -1 ) { logerr("cannot create spool directory \"%s\"", p_dirname); return -1; } } } return 0; }
APIE program_scheduler_create(ProgramScheduler *program_scheduler, ProgramSchedulerProcessSpawnedFunction process_spawned, ProgramSchedulerStateChangedFunction state_changed, void *opaque) { int phase = 0; Program *program = containerof(program_scheduler, Program, scheduler); APIE error_code; char bin_directory[1024]; char *log_directory; String *dev_null_file_name; int i; String *environment; // format bin directory name if (robust_snprintf(bin_directory, sizeof(bin_directory), "%s/bin", program->root_directory->buffer) < 0) { error_code = api_get_error_code_from_errno(); log_error("Could not format program bin directory name: %s (%d)", get_errno_name(errno), errno); goto cleanup; } // create bin directory as default user (UID 1000, GID 1000) error_code = directory_create(bin_directory, DIRECTORY_FLAG_RECURSIVE, 0755, 1000, 1000); if (error_code != API_E_SUCCESS) { goto cleanup; } // format log directory name if (asprintf(&log_directory, "%s/log", program->root_directory->buffer) < 0) { error_code = api_get_error_code_from_errno(); log_error("Could not format program log directory name: %s (%d)", get_errno_name(errno), errno); goto cleanup; } phase = 1; // create log directory as default user (UID 1000, GID 1000) error_code = directory_create(log_directory, DIRECTORY_FLAG_RECURSIVE, 0755, 1000, 1000); if (error_code != API_E_SUCCESS) { goto cleanup; } // get '/dev/null' stock string object error_code = inventory_get_stock_string("/dev/null", &dev_null_file_name); if (error_code != API_E_SUCCESS) { goto cleanup; } phase = 2; program_scheduler->process_spawned = process_spawned; program_scheduler->state_changed = state_changed; program_scheduler->opaque = opaque; program_scheduler->absolute_working_directory = NULL; program_scheduler->absolute_stdin_file_name = NULL; program_scheduler->absolute_stdout_file_name = NULL; program_scheduler->absolute_stderr_file_name = NULL; program_scheduler->log_directory = log_directory; program_scheduler->dev_null_file_name = dev_null_file_name; program_scheduler->observer.function = program_scheduler_handle_observer; program_scheduler->observer.opaque = program_scheduler; program_scheduler->observer_state = PROCESS_OBSERVER_STATE_FINISHED; program_scheduler->shutdown = false; program_scheduler->waiting_for_brickd = !network_is_brickd_connected(); program_scheduler->timer_active = false; program_scheduler->cron_active = false; program_scheduler->last_spawned_process = NULL; program_scheduler->last_spawned_timestamp = 0; program_scheduler->state = PROGRAM_SCHEDULER_STATE_STOPPED; program_scheduler->timestamp = time(NULL); program_scheduler->message = NULL; // if X11 is enabled... if (_x11_enabled) { for (i = 0; i < program->config.environment->items.count; ++i) { environment = *(String **)array_get(&program->config.environment->items, i); // ...and the DISPLAY environment variable is set... if (strncmp(environment->buffer, "DISPLAY=", strlen("DISPLAY=")) == 0) { // ...then use the process monitor to wait for lxpanel to start program_scheduler->observer_state = PROCESS_OBSERVER_STATE_PENDING; break; } } } // FIXME: only create timer for interval mode, otherwise this wastes a // file descriptor per non-interval scheduler if (timer_create_(&program_scheduler->timer, program_scheduler_handle_timer, program_scheduler) < 0) { error_code = api_get_error_code_from_errno(); log_error("Could not create timer: %s (%d)", get_errno_name(errno), errno); goto cleanup; } phase = 3; cleanup: switch (phase) { // no breaks, all cases fall through intentionally case 2: string_unlock_and_release(dev_null_file_name); case 1: free(log_directory); default: break; } return phase == 3 ? API_E_SUCCESS : error_code; }
static APIE program_scheduler_prepare_filesystem(ProgramScheduler *program_scheduler) { int phase = 0; Program *program = containerof(program_scheduler, Program, scheduler); APIE error_code; String *absolute_working_directory; String *absolute_stdin_file_name; String *absolute_stdout_file_name; char *p; String *absolute_stderr_file_name; // create absolute working directory string object error_code = string_asprintf(NULL, OBJECT_CREATE_FLAG_INTERNAL | OBJECT_CREATE_FLAG_LOCKED, NULL, &absolute_working_directory, "%s/bin/%s", program->root_directory->buffer, program->config.working_directory->buffer); if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not wrap absolute program working directory name into string object: %s (%d)", api_get_error_code_name(error_code), error_code); goto cleanup; } phase = 1; // create absolute working directory as default user (UID 1000, GID 1000) error_code = directory_create(absolute_working_directory->buffer, DIRECTORY_FLAG_RECURSIVE, 0755, 1000, 1000); if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not create absolute program working directory: %s (%d)", api_get_error_code_name(error_code), error_code); string_unlock_and_release(absolute_working_directory); goto cleanup; } // create absolute stdin filename string object if (program->config.stdin_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { error_code = string_asprintf(NULL, OBJECT_CREATE_FLAG_INTERNAL | OBJECT_CREATE_FLAG_LOCKED, NULL, &absolute_stdin_file_name, "%s/bin/%s", program->root_directory->buffer, program->config.stdin_file_name->buffer); if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not wrap absolute stdin file name into string object: %s (%d)", api_get_error_code_name(error_code), error_code); goto cleanup; } } else { absolute_stdin_file_name = NULL; } phase = 2; // create absolute stdout filename string object if (program->config.stdout_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { error_code = string_asprintf(NULL, OBJECT_CREATE_FLAG_INTERNAL | OBJECT_CREATE_FLAG_LOCKED, NULL, &absolute_stdout_file_name, "%s/bin/%s", program->root_directory->buffer, program->config.stdout_file_name->buffer); if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not wrap absolute stdout file name into string object: %s (%d)", api_get_error_code_name(error_code), error_code); goto cleanup; } } else { absolute_stdout_file_name = NULL; } phase = 3; // ensure that directory part of stdout file name exists if (program->config.stdout_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { p = strrchr(absolute_stdout_file_name->buffer, '/'); if (p != NULL) { *p = '\0'; error_code = directory_create(absolute_stdout_file_name->buffer, DIRECTORY_FLAG_RECURSIVE, 0755, 1000, 1000); *p = '/'; if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not create directory for stdout file '%s': %s (%d)", absolute_stdout_file_name->buffer, api_get_error_code_name(error_code), error_code); goto cleanup; } } } // create absolute stderr filename string object if (program->config.stderr_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { error_code = string_asprintf(NULL, OBJECT_CREATE_FLAG_INTERNAL | OBJECT_CREATE_FLAG_LOCKED, NULL, &absolute_stderr_file_name, "%s/bin/%s", program->root_directory->buffer, program->config.stderr_file_name->buffer); if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not wrap absolute stderr file name into string object: %s (%d)", api_get_error_code_name(error_code), error_code); goto cleanup; } } else { absolute_stderr_file_name = NULL; } phase = 4; // ensure that directory part of stderr file name exists if (program->config.stderr_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { p = strrchr(absolute_stderr_file_name->buffer, '/'); if (p != NULL) { *p = '\0'; error_code = directory_create(absolute_stderr_file_name->buffer, DIRECTORY_FLAG_RECURSIVE, 0755, 1000, 1000); *p = '/'; if (error_code != API_E_SUCCESS) { program_scheduler_handle_error(program_scheduler, false, "Could not create directory for stderr file '%s': %s (%d)", absolute_stderr_file_name->buffer, api_get_error_code_name(error_code), error_code); goto cleanup; } } } // update stored string objects if (program_scheduler->absolute_working_directory != NULL) { string_unlock_and_release(program_scheduler->absolute_working_directory); } program_scheduler->absolute_working_directory = absolute_working_directory; if (program_scheduler->absolute_stdin_file_name != NULL) { string_unlock_and_release(program_scheduler->absolute_stdin_file_name); } program_scheduler->absolute_stdin_file_name = absolute_stdin_file_name; if (program_scheduler->absolute_stdout_file_name != NULL) { string_unlock_and_release(program_scheduler->absolute_stdout_file_name); } program_scheduler->absolute_stdout_file_name = absolute_stdout_file_name; if (program_scheduler->absolute_stderr_file_name != NULL) { string_unlock_and_release(program_scheduler->absolute_stderr_file_name); } program_scheduler->absolute_stderr_file_name = absolute_stderr_file_name; cleanup: switch (phase) { // no breaks, all cases fall through intentionally case 3: if (program->config.stdout_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { string_unlock_and_release(absolute_stdout_file_name); } case 2: if (program->config.stdin_redirection == PROGRAM_STDIO_REDIRECTION_FILE) { string_unlock_and_release(absolute_stdin_file_name); } case 1: string_unlock_and_release(absolute_working_directory); default: break; } return phase == 4 ? API_E_SUCCESS : error_code; }