static int chirp_multi_update(const char *volume, const char *path, struct file_info *info, time_t stoptime) { char buffer[CHIRP_PATH_MAX * 2 + 2]; if(!chirp_multi_lpath(volume, path, info->lpath, stoptime)) return 0; sprintf(buffer, "%s\n%s\n", info->rhost, info->rpath); return chirp_reli_putfile_buffer(current_volume->host, info->lpath, buffer, 0700, strlen(buffer), stoptime); }
INT64_T chirp_multi_putfile_buffer(const char *volume, const char *path, const char *buffer, INT64_T mode, INT64_T length, time_t stoptime) { struct file_info info; struct chirp_file *file; if(!chirp_multi_lookup(volume, path, &info, stoptime)) { file = chirp_multi_create(volume, path, O_CREAT | O_TRUNC | O_WRONLY, mode, stoptime); if(!file) return -1; chirp_multi_close(file, stoptime); if(!chirp_multi_lookup(volume, path, &info, stoptime)) return -1; } return chirp_reli_putfile_buffer(info.rhost, info.rpath, buffer, mode, length, stoptime); }
INT64_T chirp_global_putfile_buffer(const char *host, const char *path, const char *buffer, INT64_T mode, INT64_T length, time_t stoptime) { if(is_multi_path(host)) { char mhost[CHIRP_PATH_MAX]; char mpath[CHIRP_PATH_MAX]; parse_multi_path(path, mhost, mpath); return chirp_multi_putfile_buffer(mhost, mpath, buffer, mode, length, stoptime); } else if(not_empty(path)) { return chirp_reli_putfile_buffer(host, path, buffer, mode, length, stoptime); } else if(not_empty(host)) { if(server_lookup(host, stoptime)) { errno = EISDIR; return -1; } else { errno = EACCES; return -1; } } else { errno = EACCES; return -1; } }
struct chirp_matrix *chirp_matrix_create(const char *host, const char *path, int width, int height, int element_size, int nhosts, time_t stoptime) { char host_file[CHIRP_LINE_MAX]; int result; unsigned int i; char **hosts; int nfiles = nhosts; while(1) { INT64_T n_row_per_file = height / nfiles; if(height % nfiles) n_row_per_file++; INT64_T file_size = n_row_per_file * width * element_size; if(file_size > GIGABYTE) { nfiles *= 2; continue; } else { break; } } char line[CHIRP_LINE_MAX * nfiles]; FILE *file = NULL; if(getenv("CHIRP_HOSTS")) { sprintf(host_file, "%s", getenv("CHIRP_HOSTS")); file = fopen(host_file, "r"); } if(!file) { if(getenv("HOME")) { sprintf(host_file, "%s/.chirp/hosts", getenv("HOME")); file = fopen(host_file, "r"); } } if(!file) { sprintf(host_file, "./chirp_hosts"); file = fopen(host_file, "r"); if(!file) { file = fopen(host_file, "w"); char hostname[CHIRP_LINE_MAX]; gethostname(hostname, CHIRP_LINE_MAX - 1); // get hostname, this may not have domain name, though! fprintf(file, "%s\n", hostname); fclose(file); file = fopen(host_file, "r"); } } if(!file) { debug(D_NOTICE | D_CHIRP, "matrix: could not open host list in %s: %s\n", host_file, strerror(errno)); errno = EINVAL; return 0; } hosts = malloc(sizeof(*hosts) * nhosts); for(i = 0; (int) i < nhosts; i++) { if(!fgets(line, sizeof(line), file)) { rewind(file); fgets(line, sizeof(line), file); } hosts[i] = strdup(line); int len = strlen(hosts[i]); hosts[i][len - 1] = '\0'; } fclose(file); sprintf(line, "%d\n%d\n%d\n%d\n%d\n", width, height, element_size, nhosts, nfiles); char datapath1[CHIRP_LINE_MAX]; char datapath2[CHIRP_LINE_MAX]; char datapath3[CHIRP_LINE_MAX]; char username[USERNAME_MAX]; char cookie[16]; username_get(username); string_cookie(cookie, sizeof(cookie)); sprintf(datapath1, "/%s", username); sprintf(datapath2, "/%s/matrixdata", username); sprintf(datapath3, "/%s/matrixdata/%s", username, cookie); for(i = 0; (int) i < nfiles; i++) { const char *datahost = hosts[i % nhosts]; result = chirp_reli_mkdir(datahost, datapath1, 0700, stoptime); result = chirp_reli_mkdir(datahost, datapath2, 0700, stoptime); result = chirp_reli_mkdir(datahost, datapath3, 0700, stoptime); sprintf(&line[strlen(line)], "%s %s/data.%d\n", datahost, datapath3, i); } for(i = 0; (int) i < nhosts; i++) { free(hosts[i]); } free(hosts); char metapath[CHIRP_LINE_MAX]; strcpy(metapath, path); result = chirp_reli_putfile_buffer(host, path, line, 0700, strlen(line), stoptime); if(result < 0) { for(i = 1; i < strlen(metapath); i++) if(metapath[i] == '/') { metapath[i] = '\0'; result = chirp_reli_mkdir(host, metapath, 0700, stoptime); if(result < 0 && errno != EEXIST) { debug(D_CHIRP, "matrix: could not build directory /chirp/%s/%s to create metadata file: %s\n", host, metapath, strerror(errno)); return 0; } metapath[i] = '/'; } result = chirp_reli_putfile_buffer(host, path, line, 0700, strlen(line), stoptime); if(result < 0) { debug(D_CHIRP, "matrix: could not create metadata file /chirp/%s/%s: %s\n", host, path, strerror(errno)); return 0; } } debug(D_CHIRP, "matrix: created matrix %s/%s -- now opening\n", host, path); return chirp_matrix_open(host, path, stoptime); }