int share_file_cat(char *path, int pflags) { shstat st; shfs_t *tree; shfs_ino_t *file; shbuf_t *buff; char fpath[PATH_MAX+1]; unsigned char *data; size_t data_len; size_t of; int w_len; int err; tree = shfs_uri_init(path, 0, &file); if (!tree) return (SHERR_NOENT); err = shfs_fstat(file, &st); if (err) { shfs_free(&tree); return (err); } buff = shbuf_init(); err = shfs_read(file, buff); if (err) { shbuf_free(&buff); shfs_free(&tree); return (err); } of = 0; while (of < shbuf_size(buff)) { data_len = MIN((shbuf_size(buff) - of), 65536); data = shbuf_data(buff) + of; w_len = fwrite(data, sizeof(char), data_len, sharetool_fout); if (w_len < 0) { shbuf_free(&buff); shfs_free(&tree); return (-errno); } of += w_len; } shbuf_free(&buff); shfs_free(&tree); return (0); }
int install_sexe_userdata(sexe_t *S, char *tag) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; int is_new; k = shkey_str(tag); sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); memcpy(&S->pname, k, sizeof(S->pname)); shkey_free(&k); buff = shbuf_init(); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); is_new = shfs_read(fl, buff); udata = shjson_init(shbuf_size(buff) ? (char *)shbuf_data(buff) : NULL); shbuf_free(&buff); if (is_new) shjson_num_add(udata, "birth", shtimef(shtime())); sexe_table_set(S, udata); lua_setglobal(S, "userdata"); shjson_free(&udata); shfs_free(&fs); return (0); }
int shfs_ref_get(shfs_ino_t *file, shfs_t **ref_fs_p, shfs_ino_t **ref_p) { shfs_ino_t *ref; shfs_ino_t *parent; shpeer_t *peer; shfs_t *fs; shkey_t *hier; shbuf_t *buff; char path[SHFS_PATH_MAX]; int hier_cnt; int err; int i; *ref_p = NULL; *ref_fs_p = NULL; if (!file || !file->tree) return (SHERR_INVAL); buff = shbuf_init(); err = _shfs_ref_raw_read(file, buff); if (err) return (err); peer = (shpeer_t *)shbuf_data(buff); hier = (shkey_t *)(shbuf_data(buff) + sizeof(shpeer_t)); fs = shfs_init(peer); if (!fs) return (SHERR_IO); memset(path, 0, sizeof(path)); strcpy(path, "/"); ref = fs->fsbase_ino; for (i = SHFS_MAX_REFERENCE_HIERARCHY - 1; i >= 0; i--) { if (shkey_cmp(&hier[i], ashkey_blank())) continue; if (shkey_cmp(&hier[i], shfs_token(file->tree->fsbase_ino))) continue; ref = shfs_inode_load(ref, &hier[i]); if (!ref) { shfs_free(&fs); return (SHERR_NOENT); } if (shfs_type(ref) == SHINODE_DIRECTORY) strncat(path, "/", SHFS_PATH_MAX - strlen(path) - 1); strncat(path, shfs_filename(ref), SHFS_PATH_MAX - strlen(path) - 1); } shbuf_free(&buff); *ref_p = ref; *ref_fs_p = fs; return (0); }
int share_file_copy(char **args, int arg_cnt, int pflags) { struct stat st; shfs_t *dest_fs; shfs_t **src_fs; shfs_ino_t *dest_file; shfs_ino_t **src_file; shbuf_t *buff; char fpath[PATH_MAX+1]; unsigned char *data; size_t data_len; size_t of; int src_cnt; int w_len; int err; int i; if (arg_cnt < 1) return (SHERR_INVAL); src_file = NULL; src_fs = NULL; arg_cnt--; src_cnt = 0; if (!arg_cnt) { /* create faux substitute. */ /*DEBUG: */return (SHERR_INVAL); } else { dest_file = sharetool_file(args[arg_cnt], &dest_fs); src_file = (shfs_ino_t **)calloc(arg_cnt + 1, sizeof(shfs_ino_t *)); src_fs = (shfs_t **)calloc(arg_cnt + 1, sizeof(shfs_t *)); for (i = 1; i < arg_cnt; i++) { src_file[src_cnt] = sharetool_file(args[i], &src_fs[src_cnt]); err = shfs_fstat(src_file[src_cnt], &st); src_cnt++; if (err) goto done; } } for (i = 0; i < src_cnt; i++) { shfs_t *s_fs = src_fs[i]; err = shfs_file_copy(src_file[i], dest_file); shfs_free(&s_fs); if (err) goto done; } err = 0; done: if (src_file) free(src_file); if (src_fs) free(src_fs); return (err); }
int shpam_shadow_close(uint64_t uid, shadow_t **shadow_p, shseed_t **seed_p) { if (sh_p) { shadow_t *shadow = *shadow_p; *shadow_p = NULL; if (shadow) free(shadow); } if (seed_p) { shseed_t *seed = *seed_p; *seed_p = NULL; if (seed) free(seed); } shfs_free(&_shadow_fs); }
int shfs_ref_write(shfs_ino_t *file, shbuf_t *buff) { shfs_t *fs; shfs_ino_t *ref; int err; err = shfs_ref_get(file, &fs, &ref); if (err) return (err); err = shfs_write(ref, buff); shfs_free(&fs); if (err) return (err); return (0); }
int update_sexe_userdata(sexe_t *S) { SHFL *fl; shjson_t *udata; shfs_t *fs; shbuf_t *buff; shkey_t *k; char path[PATH_MAX+1]; char *str; int err; k = &S->pname; if (shkey_cmp(k, ashkey_blank())) { fprintf(stderr, "DEBUG: update_sexe_userdata: no app key\n"); return (0); /* blank */ } sprintf(path, "/sys/data/sexe/%s", shkey_hex(k)); lua_getglobal(S, "userdata"); udata = sexe_table_get(S); if (!udata) { fprintf(stderr, "DEBUG: update_sexe_userdata: no global 'userdata' variable.\n"); return (SHERR_INVAL); } str = shjson_print(udata); if (!str) { fprintf(stderr, "DEBUG: update_sexe_userdata: error encoding JSON.\n"); return (SHERR_INVAL); } shjson_free(&udata); buff = shbuf_init(); shbuf_catstr(buff, str); free(str); fs = shfs_init(NULL); fl = shfs_file_find(fs, path); err = shfs_write(fl, buff); shbuf_free(&buff); shfs_free(&fs); return (err); }
int share_file_copy(char **args, int arg_cnt, int pflags) { shfs_t *dest_fs; shfs_t *src_fs; shfs_ino_t *dest_file; shfs_ino_t *src_file; shbuf_t *buff; char fpath[PATH_MAX+1]; unsigned char *data; size_t data_len; size_t of; int total; int src_cnt; int w_len; int err; int i; if (arg_cnt < 2) return (SHERR_INVAL); arg_cnt--; dest_fs = shfs_uri_init(args[arg_cnt], O_CREAT, &dest_file); if (!dest_fs) return (SHERR_IO); total = 0; for (i = 1; i < arg_cnt; i++) { err = share_file_stat_recursive(dest_file, args[i], &total); if (err) break; } if (err) { shfs_free(&dest_fs); return (err); } if (total == 0) { /* no matches */ shfs_free(&dest_fs); return (SHERR_INVAL); } if (total > 1 && shfs_type(dest_file) != SHINODE_DIRECTORY) { /* cannot copy multiple files to a single file */ shfs_free(&dest_fs); return (SHERR_NOTDIR); } err = 0; for (i = 1; i < arg_cnt; i++) { err = share_file_copy_recursive(dest_file, args[i]); if (err) break; } if (err) { shfs_free(&dest_fs); return (err); } shfs_free(&dest_fs); return (0); }
int share_file_copy_recursive(SHFL *dest_file, char *fname) { shfs_dirent_t *ents; shfs_t *fs; SHFL **files; SHFL *dir; struct stat st; char spec_prefix[SHFS_PATH_MAX]; char spec_dir[SHFS_PATH_MAX]; char spec_fname[SHFS_PATH_MAX]; char path[SHFS_PATH_MAX]; char work_path[SHFS_PATH_MAX]; char buf[4096]; char *list_fname; char *ptr; int ent_nr; int err; int i; memset(spec_prefix, 0, sizeof(spec_prefix)); ptr = strstr(fname, ":/"); if (ptr) { ptr += 2; strncpy(spec_prefix, fname, MIN(sizeof(spec_prefix)-1, (ptr-fname))); fname += strlen(spec_prefix); } if (!strchr(fname, '/') || 0 == strncmp(fname, "./", 2)) { if (0 == strncmp(fname, "./", 2)) fname += 2; strcpy(spec_prefix, "file:/"); getcwd(buf, sizeof(buf)-1); strcat(buf, "/"); strcat(buf, fname); ptr = strrchr(buf, '/'); strncpy(spec_dir, buf + 1, strlen(buf) - strlen(ptr)); sprintf(spec_fname, "%s%s", spec_dir, ptr+1); list_fname = basename(spec_fname); } else { memset(spec_fname, 0, sizeof(spec_fname)); strncpy(spec_fname, fname, sizeof(spec_fname)-1); list_fname = basename(spec_fname); memset(spec_dir, 0, sizeof(spec_dir)); strncpy(spec_dir, fname, MIN(strlen(fname) - strlen(list_fname), sizeof(spec_dir)-1)); } sprintf(path, "%s%s", spec_prefix, spec_dir); fs = shfs_uri_init(path, 0, &dir); if (!*list_fname) { /* directory reference. */ share_file_copy_cb(dir, dest_file); } else { /* search files in directory */ err = shfs_list_cb(dir, list_fname, share_file_copy_cb, dest_file); if (err < 0) return (err); } shfs_free(&fs); return (0); }
int share_file_stat_recursive(SHFL *dest_file, char *fname, int *total_p) { shfs_dirent_t *ents; shfs_t *fs; SHFL **files; SHFL *dir; struct stat st; char spec_prefix[SHFS_PATH_MAX]; char spec_dir[SHFS_PATH_MAX]; char spec_fname[SHFS_PATH_MAX]; char path[SHFS_PATH_MAX]; char work_path[SHFS_PATH_MAX]; char *list_fname; char buf[4096]; char *ptr; int ent_nr; int err; int i; memset(spec_prefix, 0, sizeof(spec_prefix)); ptr = strstr(fname, ":/"); if (ptr) { ptr += 2; strncpy(spec_prefix, fname, MIN(sizeof(spec_prefix)-1, (ptr-fname))); fname += strlen(spec_prefix); } if (!strchr(fname, '/') || 0 == strncmp(fname, "./", 2)) { if (0 == strncmp(fname, "./", 2)) fname += 2; strcpy(spec_prefix, "file:/"); getcwd(buf, sizeof(buf)-1); strcat(buf, "/"); strcat(buf, fname); ptr = strrchr(buf, '/'); strncpy(spec_dir, buf + 1, strlen(buf) - strlen(ptr)); sprintf(spec_fname, "%s%s", spec_dir, ptr+1); list_fname = basename(spec_fname); } else { memset(spec_fname, 0, sizeof(spec_fname)); strncpy(spec_fname, fname, sizeof(spec_fname)-1); list_fname = basename(spec_fname); memset(spec_dir, 0, sizeof(spec_dir)); strncpy(spec_dir, fname, MIN(strlen(fname) - strlen(list_fname), sizeof(spec_dir)-1)); } fprintf(stderr, "DEBUG: spec_prefix(%s) spec_dir(%s) spec_fname(%s)\n", spec_prefix, spec_dir, spec_fname); sprintf(path, "%s%s", spec_prefix, spec_dir); fs = shfs_uri_init(path, 0, &dir); fprintf(stderr, "DEBUG: shfs_uri_init(%s)\n", path); if (!*list_fname) { /* directory reference. */ ent_nr = 1; } else { /* search files in directory */ err = 0; ent_nr = shfs_list(dir, list_fname, &ents); if (ent_nr <= 0) { fprintf(stderr, "DEBUG: share_file_stat_recursive: shfs_list('%s'): ent_nr = %d\n", list_fname, ent_nr); return (ent_nr); } err = SHERR_NOENT; files = (SHFL **)calloc(ent_nr+1, sizeof(SHFL *)); if (!files) return (SHERR_NOMEM); for (i = 0; i < ent_nr; i++) { ptr = strstr(spec_dir, ":/"); if (ptr) { sprintf(path, "%s%s", ptr+2, ents[i].d_name); } else { sprintf(path, "%s%s", spec_dir, ents[i].d_name); } err = shfs_stat(fs, path, &st); if (err) { fprintf(stderr, "DEBUG: share_file_stat_recursive: %d = shfs_stat('%s')\n", err, path); break; } } shfs_list_free(&ents); if (err) { return (err); } } *total_p = ent_nr; shfs_free(&fs); return (0); }
int share_file_copy_recursive(SHFL *dest_file, char *fname) { shfs_dirent_t *ents; shfs_t *fs; SHFL **files; SHFL *dir; struct stat st; char spec_prefix[SHFS_PATH_MAX]; char spec_dir[SHFS_PATH_MAX]; char spec_fname[SHFS_PATH_MAX]; char path[SHFS_PATH_MAX]; char work_path[SHFS_PATH_MAX]; char buf[4096]; char *list_fname; char *ptr; int ent_nr; int err; int i; memset(spec_prefix, 0, sizeof(spec_prefix)); ptr = strstr(fname, ":/"); if (ptr) { ptr += 2; strncpy(spec_prefix, fname, MIN(sizeof(spec_prefix)-1, (ptr-fname))); fname += strlen(spec_prefix); } if (!strchr(fname, '/') || 0 == strncmp(fname, "./", 2)) { if (0 == strncmp(fname, "./", 2)) fname += 2; strcpy(spec_prefix, "file:/"); getcwd(buf, sizeof(buf)-1); strcat(buf, "/"); strcat(buf, fname); ptr = strrchr(buf, '/'); strncpy(spec_dir, buf + 1, strlen(buf) - strlen(ptr)); sprintf(spec_fname, "%s%s", spec_dir, ptr+1); list_fname = basename(spec_fname); } else { memset(spec_fname, 0, sizeof(spec_fname)); strncpy(spec_fname, fname, sizeof(spec_fname)-1); list_fname = basename(spec_fname); memset(spec_dir, 0, sizeof(spec_dir)); strncpy(spec_dir, fname, MIN(strlen(fname) - strlen(list_fname), sizeof(spec_dir)-1)); } sprintf(path, "%s%s", spec_prefix, spec_dir); fprintf(stderr, "DEBUG: file_recursive: shfs_uri_init(%s)\n", path); fs = shfs_uri_init(path, 0, &dir); if (!*list_fname) { /* directory reference. */ ent_nr = 1; files = (SHFL **)calloc(ent_nr+1, sizeof(SHFL *)); if (!files) return (SHERR_NOMEM); files[0] = dir; } else { /* search files in directory */ err = 0; ent_nr = shfs_list(dir, list_fname, &ents); if (ent_nr <= 0) return (ent_nr); err = SHERR_NOENT; files = (SHFL **)calloc(ent_nr+1, sizeof(SHFL *)); if (!files) return (SHERR_NOMEM); for (i = 0; i < ent_nr; i++) { ptr = strstr(spec_dir, ":/"); if (ptr) { sprintf(path, "%s%s", ptr+2, ents[i].d_name); } else { sprintf(path, "%s%s", spec_dir, ents[i].d_name); } err = shfs_stat(fs, path, &st); if (err) break; files[i] = shfs_file_find(fs, path); } shfs_list_free(&ents); if (err) { free(files); return (err); } } #if 0 /* handle recursive hierarchy */ if ((run_flags & PFLAG_RECURSIVE)) { for (i = 0; i < ent_nr; i++) { if (shfs_type(files[i]) != SHINODE_DIRECTORY) continue; /* .. */ } } #endif for (i = 0; i < ent_nr; i++) { /* perform file copy */ err = shfs_file_copy(files[i], dest_file); if (err) { fprintf(sharetool_fout, "%s: error copying \"%s\" to \"%s\": %s [sherr %d].\n", process_path, shfs_filename(files[i]), shfs_filename(dest_file), sherrstr(err), err); return (err); } if (!(run_flags & PFLAG_QUIET) && (run_flags & PFLAG_VERBOSE)) { fprintf(sharetool_fout, "%s: %s \"%s\" copied to %s \"%s\".\n", process_path, shfs_type_str(shfs_type(files[i])), shfs_filename(files[i]), shfs_type_str(shfs_type(dest_file)), shfs_filename(dest_file)); } } free(files); shfs_free(&fs); return (0); }
int shfs_file_copy(shfs_ino_t *src_file, shfs_ino_t *dest_file) { shfs_t *ref_fs; shfs_ino_t *ref; shstat st; shbuf_t *buff; int err; if (!src_file || !dest_file) return (SHERR_INVAL); /* ensure there is something to copy */ err = shfs_fstat(src_file, &st); if (err) { fprintf(stderr, "DEBUG: shfs_file_copy: %d = shfs_fstat(src_file)\n", err); return (err); } if (shfs_type(dest_file) == SHINODE_DIRECTORY) { #if 0 /* extract tar archive */ if (shfs_format(dest_file) == SHINODE_BINARY && 0 == strcmp(shfs_meta_get(dest_file, "content.mime"), "application/x-tar")) { buff = shbuf_init(); err = shfs_read(src_file, buff); if (err) { shbuf_free(&buff); return (err); } err = shfs_unarch(buff, dest_file); shbuf_free(&buff); return (0); } #endif if (!(shfs_attr(src_file) & SHATTR_ARCH)) { if (IS_INODE_CONTAINER(shfs_type(src_file))) { dest_file = shfs_inode(dest_file, shfs_filename(src_file), shfs_type(src_file)); } else { dest_file = shfs_inode(dest_file, NULL, shfs_type(src_file)); } } } ref = NULL; ref_fs = NULL; if (shfs_format(dest_file) == SHINODE_REFERENCE) { /* apply operation to end-point inode. */ err = shfs_ref_get(dest_file, &ref_fs, &ref); if (err) { fprintf(stderr, "DEBUG: shfs_file_copy: %d = shfs_ref_get(dest_file)\n", err); return (err); } dest_file = ref; } if (shfs_format(dest_file) != SHINODE_EXTERNAL) { /* direct copy data content without conversion when applicable. */ switch (shfs_format(src_file)) { #if 0 case SHINODE_COMPRESS: err = shfs_zlib_copy(src_file, dest_file); if (err) return (err); return (0); #endif } } /* default case */ buff = shbuf_init(); err = shfs_read(src_file, buff); if (err) { fprintf(stderr, "DEBUG: shfs_file_copy: %d = shfs_read()\n", err); goto done; } err = shfs_write(dest_file, buff); shbuf_free(&buff); if (err) { fprintf(stderr, "DEBUG: shfs_file_copy: %d = shfs_write()\n", err); goto done; } /* success */ err = 0; done: shbuf_free(&buff); if (ref_fs) shfs_free(&ref_fs); return (err); }