int ypfs_rename(const char *path, const char *newpath) { int ret_code = 0; char* full_path; char* new_full_path; sqlite3_stmt *stmt; #ifdef DEBUG if(1){ FILE *f2 = fopen("/opfs/log2.txt", "a"); fprintf(f2, "RENAME: %s TO %s\n", path, newpath); fclose(f2); } #endif full_path = build_path(path); new_full_path = build_path(newpath); if ( !hasPermission(CURRENT->uid, path, conn) ) { ret_code = -EACCES; } else if (!strstr(path, "+private") && strstr(newpath, "+private")) { char* command; asprintf(&command, "/opfs/encode \"%s\" \"%s\" \"%d\"", full_path, new_full_path, CURRENT->uid); ret_code = system(command); if ( WEXITSTATUS(ret_code) ) ret_code = -EACCES; free(command); } else if (strstr(path, "+private") && !strstr(newpath, "+private")) { char* command; asprintf(&command, "/opfs/decode \"%s\" \"%s\" \"%d\"", new_full_path, full_path, CURRENT->uid); ret_code = system(command); if ( WEXITSTATUS(ret_code) ) ret_code = -EACCES; free(command); } else { ret_code = rename(full_path, new_full_path); } if (!ret_code) { sqlite3_prepare_v2(conn, "UPDATE pictures SET filename=? WHERE filename=?", -1, &stmt, NULL); sqlite3_bind_text(stmt, 2, path + last_index_of(path, '/'), -1, SQLITE_TRANSIENT); sqlite3_bind_text(stmt, 1, newpath + last_index_of(newpath, '/'), -1, SQLITE_TRANSIENT); sqlite3_step(stmt); sqlite3_finalize(stmt); } free(full_path); free(new_full_path); return ret_code; }
/** Remove a file */ int ypfs_unlink(const char *path) { int ret_code = 0; char* full_path = NULL; sqlite3_stmt *stmt; #ifdef DEBUG if(1){ FILE *f2 = fopen("/opfs/log2.txt", "a"); fprintf(f2, "UNLINK: %s\n", path); fclose(f2); } #endif if ( !hasPermission(CURRENT->uid, path, conn) ) { ret_code = -EACCES; } else { full_path = build_path(path); ret_code = unlink(full_path); } if (!ret_code) { sqlite3_prepare_v2(conn, "DELETE FROM pictures WHERE filename=?", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, path + last_index_of(path, '/'), -1, SQLITE_TRANSIENT); sqlite3_step(stmt); sqlite3_finalize(stmt); } free(full_path); return ret_code; }
int send_file(int clientfd, const char* filepath) { //char filename[60]; memset(filename, '\0', sizeof(filename)); int flen = strlen(filepath); last_index_of(filename, filepath, flen, '/'); printf("send the file %s\n", filepath); int filed = open(filepath, O_RDONLY); if (filed == -1) { printf("can not open the file %s %s\n", filepath, strerror(errno)); databuf.end = SENDEND; write(clientfd, &databuf, sizeof(databuf)); return -1; } int rsize = 0; databuf.end = SENDNAME; strcpy(databuf.filename, filename); write(clientfd, &databuf, sizeof(databuf)); while ((rsize = read(filed, databuf.buffer, 2048)) > 0) { databuf.size = rsize; databuf.end = SENDDATA; write(clientfd, &databuf, sizeof(databuf)); printf("send %d bytes\n", rsize); } databuf.end = SENDEND; write(clientfd, &databuf, sizeof(databuf)); }
char* save_output(const char* path_to_binary, const char* name, int n) { int last_dash; char dir_of_executable[300]; char* buffer = (char*)malloc(n * sizeof(char) + 300); // cut the name of the executable and take just the folder. strcpy(dir_of_executable, path_to_binary); last_dash = last_index_of(dir_of_executable, '/'); dir_of_executable[last_dash] = '\0'; // navigate to the output directory. buffer = strcpy(buffer, dir_of_executable); buffer = strcat(buffer, "/../output/"); buffer = strcat(buffer, name); buffer = strcat(buffer, ".txt"); // create the file. FILE* file = fopen(buffer, "w+"); if (!file) { perror("failed to open file: "); exit(-1); } else { fclose(file); } return buffer; }
/** * Looks for a lemmatized keyword of the form XXX.YYY where YYY * is the forbidden code. Then, it returns 1 and XXX is copied into * res. Otherwise, 0 is returned. */ int get_forbidden_keyword(KeyWord* list,unichar* code,Ustring* res) { if (list==NULL) return 0; int pos=last_index_of(list->sequence,(unichar)'.'); if (pos!=-1 && !u_strcmp(code,list->sequence+pos+1)) { /* If the forbidden code has been found */ u_strcpy(res,list->sequence); truncate(res,pos); return 1; } return 0; }
/** * Looks for a keyword that has a forbidden lemma or is a forbidden lemma * if the keyword is not a lemmatized one of the form XXX.YYY */ int has_forbidden_lemma(KeyWord* list,struct string_hash* lemmas) { if (list==NULL || list->sequence==NULL) return 0; int pos=last_index_of(list->sequence,(unichar)'.'); if (pos==-1) { /* If the keyword is not lemmatized, we just test * if it is a forbidden lemma */ return (-1!=get_value_index(list->sequence,lemmas,DONT_INSERT)); } Ustring* tmp=new_Ustring(list->sequence); truncate(tmp,pos); int index=get_value_index(tmp->str,lemmas,DONT_INSERT); free_Ustring(tmp); return index!=-1; }
static int ypfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) { (void) offset; (void) fi; filler(buf, ".", NULL, 0); filler(buf, "..", NULL, 0); #ifdef DEBUG if(1){ FILE *f2 = fopen("/opfs/log2.txt", "a"); fprintf(f2, "READDIR: %s\n", path); fclose(f2); } #endif #ifdef DEBUG if(!strcmp(path,"/")) filler(buf, "debug", NULL, 0); #endif if ( !strcmp(path, "/") ) { dir_root(path, buf, filler, NULL); } else if ( !regexec(&all_rx, path, 0, NULL, 0) ) { dir_all(path, buf, filler, conn); } else if( !regexec(&formats_rx, path, 0, NULL, 0) ) { dir_formats(path, buf, filler, NULL); } else if ( !regexec(&formats_ext_rx, path, 0, NULL, 0) ) { dir_formats_ext(path, buf, filler, path + last_index_of(path, '/'), conn); } else if ( !regexec(&dates_rx, path, 0, NULL, 0) ) { dir_dates(path, buf, filler, conn); } else if ( !regexec(&dates_year_rx, path, 0, NULL, 0) ) { dir_dates_year(path, buf, filler, conn); } else if ( !regexec(&dates_year_month_rx, path, 0, NULL, 0) ) { dir_dates_year_month(path, buf, filler, conn); } else if ( !regexec(&search_rx, path, 0, NULL, 0) ) { return 0; } else if ( !regexec(&search_term_rx, path, 0, NULL, 0) ) { dir_search_term(path, buf, filler, conn); } else { return -ENOENT; } return 0; }
// TODO: fix fixed string length int Request_parse_request_line(Request *request) { char line[256]; int count = conn_scan(request->conn, "\r\n", (char *)line); // printf("--------: %s\n", line); if (count == 16 && !strcmp(line, "PRI * HTTP/2.0\r\n")) { bzero(line, 256); conn_scan(request->conn, "\r\n", line); if (strcmp(line, "\r\n")) { return 1; } bzero(line, 256); conn_scan(request->conn, "\r\n", line); if (strcmp(line, "SM\r\n")) { return 1; } bzero(line, 256); conn_scan(request->conn, "\r\n", line); if (strcmp(line, "\r\n")) { return 1; } return 2; } request->method = calloc(8, sizeof(char)); int findex = strcspn(line, " "); memcpy(request->method, line, findex * sizeof(char)); request->version = calloc(16, sizeof(char)); int lindex = last_index_of(line, count, ' '); memcpy(request->version, line+lindex+1, (count - lindex - 3) * sizeof(char)); // printf("==============\n"); // free(request->version); // printf("==============\n"); request->url = calloc(256, sizeof(char)); memcpy(request->url, line+findex+1, (lindex - findex -1) * sizeof(char)); return 0; }
struct stdfss_res *mkdevice(int wpid, struct working_thread *thread, struct stdfss_mkdevice *mkdevice_cmd) { struct stdfss_res *ret = NULL; struct smount_info *minf = NULL; struct sdevice_info *opening_dinf = NULL; char *str = NULL, *strmatched = NULL; int parse_ret, dir_exists; struct gc_node *dir_base_node = NULL, *device_base_node = NULL; unsigned int *block = NULL; unsigned int nodeid; // get device file name from smo str = get_string(mkdevice_cmd->path_smo); ret = check_path(str, thread->command.command); if(ret != NULL) return ret; char *devstr = get_string(mkdevice_cmd->service_name); ret = check_path(devstr, thread->command.command); if(ret != NULL) { free(str); return ret; } // check path is ok if(str[len(str)] == '/') { free(str); free(devstr); return build_response_msg(thread->command.command, STDFSSERR_INVALID_COMMAND_PARAMS); } // get mount info wait_mutex(&mounted_mutex); minf = (struct smount_info *)lpt_getvalue_parcial_matchE(mounted, str, &strmatched); leave_mutex(&mounted_mutex); if(minf == NULL) { free(str); free(devstr); return build_response_msg(mkdevice_cmd->command, STDFSSERR_DEVICE_NOT_MOUNTED); } // check file does not exist parse_ret = parse_directory(TRUE, &dir_base_node, OFS_NODELOCK_EXCLUSIVE | OFS_NODELOCK_BLOCKING, thread->command.command, thread, wpid, minf, str, len(strmatched), NULL, &nodeid, NULL, &dir_exists, &ret); if(ret != NULL) free(ret); ret = NULL; if(parse_ret) { // file exists if(thread->lastdir_parsed_node != NULL) { nfree(minf->dinf, thread->lastdir_parsed_node); thread->lastdir_parsed_node = NULL; } free(strmatched); free(str); free(devstr); unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); nfree(minf->dinf, dir_base_node); return build_response_msg(thread->command.command, STDFSSERR_FILE_EXISTS); } if(!dir_exists) { // worng path if(thread->lastdir_parsed_node != NULL) { nfree(minf->dinf, thread->lastdir_parsed_node); thread->lastdir_parsed_node = NULL; } free(strmatched); free(str); free(devstr); return build_response_msg(thread->command.command, STDFSSERR_FILE_DOESNOTEXIST); } dir_base_node = nref(minf->dinf, thread->lastdir_parsed_node->nodeid); free(strmatched); // Create device file if(!create_file(dir_base_node, str, last_index_of(str, '/') + 1, OFS_DEVICE_FILE, TRUE, minf, wpid, thread->command.command, &nodeid, &device_base_node, OFS_NOFLAGS , &ret)) { nfree(minf->dinf, thread->lastdir_parsed_node); nfree(minf->dinf, dir_base_node); thread->lastdir_parsed_node = NULL; free(str); free(devstr); return ret; } nfree(minf->dinf, thread->lastdir_parsed_node); nfree(minf->dinf, dir_base_node); thread->lastdir_parsed_node = NULL; // get a free block block = get_free_blocks(1, TRUE, minf, thread->command.command, wpid, &ret); if(ret != NULL) { free(str); free(devstr); unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); nfree(minf->dinf, device_base_node); return ret; } free(str); clear_dir_buffer(thread->directory_buffer.buffer); // write buffer /* int LOGIC DEVICE ID: internal ID on the service (4 BYTES) int service name size; (4 BYTES) char SERVICE NAME[]: name of the device driver (zero terminated devstring) */ *((unsigned int *)thread->directory_buffer.buffer) = (unsigned int)mkdevice_cmd->logic_deviceid; *((unsigned int *)(thread->directory_buffer.buffer + 4)) = len(devstr); mem_copy((unsigned char *)devstr, ((unsigned char *)thread->directory_buffer.buffer + 8), len(devstr) + 1); free(devstr); write_buffer((char *)thread->directory_buffer.buffer, OFS_DIR_BUFFERSIZE, *block, thread->command.command, wpid, minf, &ret); if(ret != NULL) { free_block(TRUE, TRUE, *block, minf, thread->command.command, wpid, &ret); free(block); unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); nfree(minf->dinf, device_base_node); return ret; } // update node device_base_node->n.file_size = 8 + len(devstr) + 1; device_base_node->n.blocks[0] = *block; if(!write_node(device_base_node, minf, wpid, thread->command.command, &ret) || ret != NULL) { free_block(TRUE, TRUE, *block, minf, thread->command.command, wpid, &ret); free(block); unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); nfree(minf->dinf, device_base_node); return ret; } nfree(minf->dinf, device_base_node); // unlock device node unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); return build_response_msg(thread->command.command, STDFSSERR_OK); }
char* build_path(const char* path) { char* full_path; asprintf(&full_path, "%s%s", path_prefix, path + last_index_of(path, '/')); return full_path; }
static int ypfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi) { char* full_path; FILE* logfile; int f; ExifData *ed; ExifEntry *entry; (void) fi; sqlite3_stmt *stmt; int year = -1; int month = -1; #ifdef DEBUG if(1){ FILE *f2 = fopen("/opfs/log2.txt", "a"); fprintf(f2, "WRITE: %s\n", path); fclose(f2); } #endif full_path = build_path(path); f = fi->fh; write(f, buf, size); if (offset == 0) { logfile = fopen("log.txt", "a"); ed = exif_data_new_from_file(full_path); if (ed) { entry = exif_content_get_entry(ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL); if (entry) { char buf[1024]; exif_entry_get_value(entry, buf, sizeof(buf)); fprintf(logfile, "%s had date %s\n", full_path, buf); buf[4] = 0; buf[7] = 0; year = atoi(buf); month = atoi(buf+5); } else { fprintf(logfile,"%s had exif data, but no date\n", full_path); } } else { fprintf(logfile,"%s had no exif data\n", full_path); } exif_data_unref(ed); fclose(logfile); if ( year == -1 || month == -1) { time_t cur_time; struct tm * full_time; time(&cur_time); full_time = localtime(&cur_time); if (year == -1) year = 1900 + full_time->tm_year; if (month == -1) month = full_time->tm_mon+1; } sqlite3_prepare_v2(conn, "insert into pictures values(?, ?, ?, ?)", -1, &stmt, NULL); sqlite3_bind_text(stmt, 1, path + last_index_of(path, '/'), -1, SQLITE_TRANSIENT); sqlite3_bind_int(stmt, 2, year); sqlite3_bind_int(stmt, 3, month); sqlite3_bind_int(stmt, 4, CURRENT->uid); sqlite3_step(stmt); sqlite3_finalize(stmt); } free(full_path); return size; }
struct stdfss_res *mkdir(int wpid, struct working_thread *thread, struct stdfss_mkdir *mkdir_cmd) { struct stdfss_res *ret = NULL; struct smount_info *minf = NULL; struct sdevice_info *opening_dinf = NULL; char *str = NULL, *old_str = NULL, *strmatched = NULL; int parse_ret, dir_exists; struct gc_node *dir_base_node = NULL, *new_dir_base_node = NULL; unsigned int nodeid; // get dir name from smo str = get_string(mkdir_cmd->dir_path); ret = check_path(str, thread->command.command); if(ret != NULL) return ret; // get mount info wait_mutex(&mounted_mutex); minf = (struct smount_info *)lpt_getvalue_parcial_matchE(mounted, str, &strmatched); leave_mutex(&mounted_mutex); if(minf == NULL) { ret = build_response_msg(mkdir_cmd->command, STDFSSERR_DEVICE_NOT_MOUNTED); free(str); return ret; } // remove trailing '/' if(str[len(str)] == '/') { old_str = str; str = substring(str, 0, len(str) - 1); free(old_str); } // check file does not exist parse_ret = parse_directory(TRUE, &dir_base_node, OFS_NODELOCK_EXCLUSIVE | OFS_NODELOCK_BLOCKING, thread->command.command, thread, wpid, minf, str, len(strmatched), NULL, &nodeid, NULL, &dir_exists, &ret); if(ret != NULL) free(ret); ret = NULL; free(strmatched); if(parse_ret) { // file exists if(thread->lastdir_parsed_node != NULL) { nfree(minf->dinf, thread->lastdir_parsed_node); thread->lastdir_parsed_node = NULL; } free(str); unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); nfree(minf->dinf, dir_base_node); return build_response_msg(thread->command.command, STDFSSERR_FILE_EXISTS); } if(!dir_exists) { // worng path if(thread->lastdir_parsed_node != NULL) { nfree(minf->dinf, thread->lastdir_parsed_node); thread->lastdir_parsed_node = NULL; } free(str); return build_response_msg(thread->command.command, STDFSSERR_FILE_DOESNOTEXIST); } dir_base_node = thread->lastdir_parsed_node; // Create directory file if(!create_file(dir_base_node, str, last_index_of(str, '/') + 1, OFS_DIRECTORY_FILE, TRUE, minf, wpid, thread->command.command, &nodeid, &new_dir_base_node , OFS_NOFLAGS, &ret)) { if(thread->lastdir_parsed_node != NULL) { nfree(minf->dinf, thread->lastdir_parsed_node); thread->lastdir_parsed_node = NULL; } free(str); return ret; } nfree(minf->dinf, thread->lastdir_parsed_node); thread->lastdir_parsed_node = NULL; nfree(minf->dinf, new_dir_base_node); free(str); // unlock the dir node unlock_node(wpid, FALSE, OFS_LOCKSTATUS_OK); return build_response_msg(thread->command.command, STDFSSERR_OK); }
int main ( int argc, char** argv ) { FILE* pipe; SHAREDBUFFER* shared_buffer; CONFIG* config; char main_process_lifetime[15]; int producer_count = 0; char producer_lifetime[15]; int consumer_count = 0; int child_exit_status = 0; int child_exit_pid; int child_pid; int i = 0; int logfd; char message[150]; char dir[255]; int available_process_count = 10000; strncpy(dir, argv[0], last_index_of(argv[0], '/')); chdir(dir); logfd = log_open_file( NULL ); if ( !logfd ) { printf("Unable to open log file.\n"); return EXIT_FAILURE; } log_event( "--------------------------------" ); log_event( "Main process started." ); // first of all load configuration file config = load_config_file(); if ( !config ) { log_event( "Unable to access configuration." ); return EXIT_FAILURE; } // read values from configuration producer_count = atoi(read_configuration(config, "producer_count", "5")); consumer_count = atoi(read_configuration(config, "consumer_count", "2")); strcpy(producer_lifetime, read_configuration(config, "producer_lifetime", "10")); strcpy(main_process_lifetime, read_configuration(config, "main_process_lifetime", "40")); destroy_config( config ); log_event( "Fetching available process count of system." ); pipe = popen(PROCESS_COUNT_SCRIPT, "r"); if ( pipe ) { fgets(message, 100, pipe); pclose(pipe); available_process_count = atoi(message); } if ( producer_count + consumer_count + 1 > available_process_count ) { log_event( "Requested total process count is more than available process count of system." ); goto SAFE_EXIT; } //initialize shared buffer shared_buffer = create_shared_buffer(); if ( !shared_buffer ) { log_event( "Unable to initialize shared buffer." ); destroy_config( config ); return EXIT_FAILURE; } // create and start producer processes // producer processes run for a specified lifetime ( in seconds ) // which is read from configuration for ( i=0; i<producer_count; ++i) { child_pid = vfork(); if ( child_pid == 0 ) // producer process { execl("producer", "producer", producer_lifetime, NULL); exit(0); } } // start consumer processes // consumer processes run until a SIGKILL signal // there is no specific entry in the requirements that // when a consumer process should end. for ( i=0; i<consumer_count; ++i) { child_pid = vfork(); if ( child_pid == 0 ) // consumer process { execl("consumer", "consumer", NULL); exit(0); } } // start controller process // controller process checks elapsed time // once in every 5 seconds // if total execution time is above a specified time (in seconds) // signals all child processes except itself, to force exit child_pid = vfork(); if ( child_pid == 0 ) //controller process { execl("controller", "controller", main_process_lifetime, NULL); exit(0); } // in order to catch all child processes exits // we need a common wait for all them // here exitting processes is cought with pid and exit status // wait childs to exit while( (child_exit_pid = wait(&child_exit_status)) > 0 ) { sprintf( message, "Process [PID:%d] exitid with status: %d", child_exit_pid, child_exit_status ); log_event( message ); } SAFE_EXIT: log_event( "Main process is being closed." ); // relase allocated data structures to OS destroy_shared_buffer( shared_buffer ); log_close_file( ); return EXIT_SUCCESS; }