uint64_t write_h_file(char *path_h, char *str_var, char *path_lib) { FILE *fh, *fl; struct stat buf; unsigned char *lib_data; int empty = 0; int n; if(!path_lib || stat(path_lib, &buf) != 0 || !(fl = fopen(path_lib, "r"))) { n = 0; empty = 1; } else n = buf.st_size; fh = fopen(path_h, "w"); if(!fh) { if(!empty) fclose(fl); return -1; } fprintf(fh, "#ifdef %s\n", str_var); if(empty) { fprintf(fh, "static char *lib_helper_data;\n"); } else { lib_data = calloc(n, sizeof(char)); copy_stream_to_buffer(fl, (char **) &lib_data, NULL); fprintf(fh, "static char lib_helper_data[%" PRIu64 "] = {\n", (uint64_t) n); int i, column; for(i = 0, column = 0; i < n; i++, column++) if(column == 10) { column = 0; fprintf(fh, "%u,\n", lib_data[i]); } else fprintf(fh, "%u,", lib_data[i]); fprintf(fh, "};\n"); free(lib_data); } fprintf(fh, "#else\n"); fprintf(fh, "static char *lib_helper_data;\n"); fprintf(fh, "#endif\n"); return n; }
int worker_main() { char line[MPI_QUEUE_LINE_MAX]; struct mpi_queue_operation *op; struct stat st; op = malloc(sizeof(*op)); MPI_Bcast(line, MPI_QUEUE_LINE_MAX, MPI_CHAR, 0, MPI_COMM_WORLD); if(strlen(line)) { if(stat(line, &st)) { debug(D_MPI, "Working directory (%s) does not exist\n", line); exit(1); } chdir(line); } while(!abort_flag) { char filename[MPI_QUEUE_LINE_MAX]; int mode, result; FILE *stream; MPI_Status mpi_status; memset(op, 0, sizeof(*op)); MPI_Recv(op, sizeof(*op), MPI_BYTE, 0, 0, MPI_COMM_WORLD, &mpi_status); switch(op->type) { case MPI_QUEUE_OP_WORK: op->buffer = malloc(op->buffer_length + 10); MPI_Recv(op->buffer, op->buffer_length, MPI_BYTE, 0, 0, MPI_COMM_WORLD, &mpi_status); op->buffer[op->buffer_length] = 0; strcat(op->buffer, " 2>&1"); debug(D_MPI, "%s", op->buffer); stream = popen(op->buffer, "r"); if(stream) { op->output_length = copy_stream_to_buffer(stream, &op->output_buffer); if(op->output_length < 0) op->output_length = 0; op->result = pclose(stream); } else { op->result = -1; op->output_length = 0; op->output_buffer = 0; } break; case MPI_QUEUE_OP_STAT: if(!stat(op->args, &st)) { op->result = 1; op->output_length = MPI_QUEUE_LINE_MAX; op->output_buffer = malloc(op->output_length); sprintf(op->output_buffer, "%lu %lu", (unsigned long int) st.st_size, (unsigned long int) st.st_mtime); } else { op->result = -1; op->output_length = MPI_QUEUE_LINE_MAX; op->output_buffer = malloc(op->output_length); sprintf(op->output_buffer, "0 0"); } break; case MPI_QUEUE_OP_UNLINK: result = remove(op->args); if(result != 0) { // 0 - succeeded; otherwise, failed op->result = -1; } else { op->result = 1; } break; case MPI_QUEUE_OP_MKDIR: if(sscanf(op->args, "%s %o", filename, &mode) == 2 && create_dir(filename, mode | 0700)) { op->result = 1; } else { op->result = -1; } break; // } else if(sscanf(line, "symlink %s %s", path, filename) == 2) { // } else if(sscanf(line, "put %s %lld %o", filename, &length, &mode) == 3) { // } else if(sscanf(line, "rget %s", filename) == 1) { // } else if(sscanf(line, "get %s", filename) == 1) { // } else if(sscanf(line, "thirdget %d %s %[^\n]", &mode, filename, path) == 3) { // } else if(sscanf(line, "thirdput %d %s %[^\n]", &mode, filename, path) == 3) { case MPI_QUEUE_OP_EXIT: free(op); MPI_Finalize(); return 0; break; default: abort_flag = 1; } if(abort_flag) break; if(op->buffer_length) { free(op->buffer); op->buffer = NULL; op->buffer_length = 0; } MPI_Send(op, sizeof(*op), MPI_BYTE, 0, 0, MPI_COMM_WORLD); if(op->output_length) { MPI_Send(op->output_buffer, op->output_length, MPI_BYTE, 0, 0, MPI_COMM_WORLD); free(op->output_buffer); } memset(op, 0, sizeof(*op)); } free(op); MPI_Finalize(); return 1; }