int main(int argc,char **argv) { int ret = -1; char str_buf[256] = {0}; char *filename = (char *)0; PVFS_fs_id cur_fs; char* entry_name; PVFS_object_ref parent_refn; PVFS_credentials credentials; if (argc != 2) { printf("usage: %s file_to_remove\n", argv[0]); return 1; } filename = argv[1]; ret = PVFS_util_init_defaults(); if (ret < 0) { PVFS_perror("PVFS_util_init_defaults", ret); return (-1); } ret = PVFS_util_get_default_fsid(&cur_fs); if (ret < 0) { PVFS_perror("PVFS_util_get_default_fsid", ret); return (-1); } if (PINT_remove_base_dir(filename,str_buf,256)) { if (filename[0] != '/') { printf("You forgot the leading '/'\n"); } printf("Cannot retrieve entry name for creation on %s\n", filename); return(-1); } printf("File to be removed is %s\n",str_buf); entry_name = str_buf; PVFS_util_gen_credentials(&credentials); ret = PINT_lookup_parent(filename, cur_fs, &credentials, &parent_refn.handle); if(ret < 0) { PVFS_perror("PVFS_util_lookup_parent", ret); return(-1); } parent_refn.fs_id = cur_fs; ret = PVFS_sys_remove(entry_name, parent_refn, &credentials, NULL); if (ret < 0) { PVFS_perror("remove failed ", ret); return(-1); } printf("===================================\n"); printf("file named %s has been removed.\n", filename); //close it down ret = PVFS_sys_finalize(); if (ret < 0) { printf("finalizing sysint failed with errcode = %d\n", ret); return (-1); } return(0); }
int generic_open(file_object *obj, PVFS_credentials *credentials, int nr_datafiles, PVFS_size strip_size, char *srcname, int open_type) { struct stat stat_buf; PVFS_sysresp_lookup resp_lookup; PVFS_sysresp_getattr resp_getattr; PVFS_sysresp_create resp_create; PVFS_object_ref parent_ref; PVFS_sys_dist *new_dist; int ret = -1; char *entry_name; /* name of the pvfs2 file */ char str_buf[PVFS_NAME_MAX]; /* basename of pvfs2 file */ if (obj->fs_type == UNIX_FILE) { memset(&stat_buf, 0, sizeof(struct stat)); stat(obj->u.ufs.path, &stat_buf); if (open_type == OPEN_SRC) { if (S_ISDIR(stat_buf.st_mode)) { fprintf(stderr, "Source cannot be a directory\n"); return(-1); } obj->u.ufs.fd = open(obj->u.ufs.path, O_RDONLY); obj->u.ufs.mode = (int)stat_buf.st_mode; } else { if (S_ISDIR(stat_buf.st_mode)) { if (srcname) { strncat(obj->u.ufs.path, basename(srcname), NAME_MAX); } else { fprintf(stderr, "cannot find name for " "destination. giving up\n"); return(-1); } } obj->u.ufs.fd = open(obj->u.ufs.path, O_WRONLY|O_CREAT|O_LARGEFILE|O_TRUNC,0666); } if (obj->u.ufs.fd < 0) { perror("open"); fprintf(stderr, "could not open %s\n", obj->u.ufs.path); return (-1); } } else { entry_name = str_buf; /* it's a PVFS2 file */ if (strcmp(obj->u.pvfs2.pvfs2_path, "/") == 0) { /* special case: PVFS2 root file system, so stuff the end of * srcfile onto pvfs2_path */ char *segp = NULL, *prev_segp = NULL; void *segstate = NULL; /* can only perform this special case if we know srcname */ if (srcname == NULL) { fprintf(stderr, "unable to guess filename in " "toplevel PVFS2\n"); return -1; } memset(&resp_lookup, 0, sizeof(PVFS_sysresp_lookup)); ret = PVFS_sys_lookup(obj->u.pvfs2.fs_id, obj->u.pvfs2.pvfs2_path, credentials, &resp_lookup, PVFS2_LOOKUP_LINK_FOLLOW, hints); if (ret < 0) { PVFS_perror("PVFS_sys_lookup", ret); return (-1); } parent_ref.handle = resp_lookup.ref.handle; parent_ref.fs_id = resp_lookup.ref.fs_id; while (!PINT_string_next_segment(srcname, &segp, &segstate)) { prev_segp = segp; } entry_name = prev_segp; /* see... points to basename of srcname */ } else /* given either a pvfs2 directory or a pvfs2 file */ { /* get the absolute path on the pvfs2 file system */ /*parent_ref.fs_id = obj->pvfs2.fs_id; */ if (PINT_remove_base_dir(obj->u.pvfs2.pvfs2_path,str_buf, PVFS_NAME_MAX)) { if(obj->u.pvfs2.pvfs2_path[0] != '/') { fprintf(stderr, "Error: poorly formatted path.\n"); } fprintf(stderr, "Error: cannot retrieve entry name for " "creation on %s\n", obj->u.pvfs2.user_path); return(-1); } ret = PINT_lookup_parent(obj->u.pvfs2.pvfs2_path, obj->u.pvfs2.fs_id, credentials, &parent_ref.handle); if (ret < 0) { PVFS_perror("PVFS_util_lookup_parent", ret); return (-1); } else /* parent lookup succeeded. if the pvfs2 path is just a directory, use basename of src for the new file */ { int len = strlen(obj->u.pvfs2.pvfs2_path); if (obj->u.pvfs2.pvfs2_path[len - 1] == '/') { char *segp = NULL, *prev_segp = NULL; void *segstate = NULL; if (srcname == NULL) { fprintf(stderr, "unable to guess filename\n"); return(-1); } while (!PINT_string_next_segment(srcname, &segp, &segstate)) { prev_segp = segp; } strncat(obj->u.pvfs2.pvfs2_path, prev_segp, PVFS_NAME_MAX); entry_name = prev_segp; } parent_ref.fs_id = obj->u.pvfs2.fs_id; } } memset(&resp_lookup, 0, sizeof(PVFS_sysresp_lookup)); ret = PVFS_sys_ref_lookup(parent_ref.fs_id, entry_name, parent_ref, credentials, &resp_lookup, PVFS2_LOOKUP_LINK_FOLLOW, hints); if ((ret == 0) && (open_type == OPEN_SRC)) { memset(&resp_getattr, 0, sizeof(PVFS_sysresp_getattr)); ret = PVFS_sys_getattr(resp_lookup.ref, PVFS_ATTR_SYS_ALL_NOHINT, credentials, &resp_getattr, hints); if (ret) { fprintf(stderr, "Failed to do pvfs2 getattr on %s\n", entry_name); return -1; } if (resp_getattr.attr.objtype == PVFS_TYPE_SYMLINK) { free(resp_getattr.attr.link_target); resp_getattr.attr.link_target = NULL; } obj->u.pvfs2.perms = resp_getattr.attr.perms; memcpy(&obj->u.pvfs2.attr, &resp_getattr.attr, sizeof(PVFS_sys_attr)); obj->u.pvfs2.attr.mask = PVFS_ATTR_SYS_ALL_SETABLE; } /* at this point, we have looked up the file in the parent directory. * . If we found something, and we are the SRC, then we're done. * . We will maintain the semantic of pvfs2-import and refuse to * overwrite existing PVFS2 files, so if we found something, and we * are the DEST, then that's an error. * . Otherwise, we found nothing and we will create the destination. */ if (open_type == OPEN_SRC) { if (ret == 0) { obj->u.pvfs2.ref = resp_lookup.ref; return 0; } else { PVFS_perror("PVFS_sys_ref_lookup", ret); return (ret); } } if (open_type == OPEN_DEST) { if (ret == 0) { obj->u.pvfs2.ref = resp_lookup.ref; return 0; } else { memset(&stat_buf, 0, sizeof(struct stat)); /* preserve permissions doing a unix => pvfs2 copy */ stat(srcname, &stat_buf); make_attribs(&(obj->u.pvfs2.attr), credentials, nr_datafiles, (int)stat_buf.st_mode); if (strip_size > 0) { new_dist = PVFS_sys_dist_lookup("simple_stripe"); ret = PVFS_sys_dist_setparam(new_dist, "strip_size", &strip_size); if (ret < 0) { PVFS_perror("PVFS_sys_dist_setparam", ret); return -1; } } else { new_dist=NULL; } ret = PVFS_sys_create(entry_name, parent_ref, obj->u.pvfs2.attr, credentials, new_dist, &resp_create, NULL, hints); if (ret < 0) { PVFS_perror("PVFS_sys_create", ret); return -1; } obj->u.pvfs2.ref = resp_create.ref; } } } return 0; }
int do_list( char *full_path, char *start, int fs_id, struct options *opts) { int i = 0, printed_dot_info = 0; int ret = -1; int pvfs_dirent_incount; char *name = NULL, *cur_file = NULL; PVFS_handle cur_handle; PVFS_sysresp_lookup lk_response; PVFS_sysresp_readdirplus rdplus_response; PVFS_sysresp_getattr getattr_response; PVFS_credentials credentials; PVFS_object_ref ref; PVFS_ds_position token; uint64_t dir_version = 0; double begin = 0., end; subdir *current, *head = NULL, *tail = NULL; name = start; memset(&lk_response,0,sizeof(PVFS_sysresp_lookup)); PVFS_util_gen_credentials(&credentials); if (opts->list_recursive || opts->num_starts > 1) { printf("%s%s:\n",full_path,start); } ret = PVFS_sys_lookup(fs_id, name, &credentials, &lk_response, PVFS2_LOOKUP_LINK_NO_FOLLOW, NULL); if(ret < 0) { PVFS_perror("PVFS_sys_lookup", ret); return -1; } ref.handle = lk_response.ref.handle; ref.fs_id = fs_id; pvfs_dirent_incount = MAX_NUM_DIRENTS; memset(&getattr_response,0,sizeof(PVFS_sysresp_getattr)); if (PVFS_sys_getattr(ref, PVFS_ATTR_SYS_ALL, &credentials, &getattr_response, NULL) == 0) { if ((getattr_response.attr.objtype == PVFS_TYPE_METAFILE) || (getattr_response.attr.objtype == PVFS_TYPE_SYMLINK) || ((getattr_response.attr.objtype == PVFS_TYPE_DIRECTORY) && (opts->list_directory))) { char segment[128] = {0}; PVFS_sysresp_getparent getparent_resp; PINT_remove_base_dir(name, segment, 128); if (strcmp(segment,"") == 0) { snprintf(segment,128,"/"); } if (getattr_response.attr.objtype == PVFS_TYPE_DIRECTORY) { if (PVFS_sys_getparent(ref.fs_id, name, &credentials, &getparent_resp, NULL) == 0) { print_dot_and_dot_dot_info_if_required( getparent_resp.parent_ref); } } if (opts->list_long) { print_entry_attr(ref.handle, segment, &getattr_response.attr, opts); } else { print_entry(segment, ref.handle, ref.fs_id, NULL, 0, opts); } return 0; } } if (do_timing) begin = Wtime(); token = 0; do { memset(&rdplus_response, 0, sizeof(PVFS_sysresp_readdirplus)); ret = PVFS_sys_readdirplus( ref, (!token ? PVFS_READDIR_START : token), pvfs_dirent_incount, &credentials, (opts->list_long) ? PVFS_ATTR_SYS_ALL : PVFS_ATTR_SYS_ALL_NOSIZE, &rdplus_response, NULL); if(ret < 0) { PVFS_perror("PVFS_sys_readdir", ret); return -1; } if (dir_version == 0) { dir_version = rdplus_response.directory_version; } else if (opts->list_verbose) { if (dir_version != rdplus_response.directory_version) { fprintf(stderr, "*** directory changed! listing may " "not be correct\n"); dir_version = rdplus_response.directory_version; } } if (!printed_dot_info) { /* the list_all option prints files starting with .; the almost_all option skips the '.', '..' printing */ print_dot_and_dot_dot_info_if_required(ref); printed_dot_info = 1; } for(i = 0; i < rdplus_response.pvfs_dirent_outcount; i++) { cur_file = rdplus_response.dirent_array[i].d_name; cur_handle = rdplus_response.dirent_array[i].handle; print_entry(cur_file, cur_handle, fs_id, &rdplus_response.attr_array[i], rdplus_response.stat_err_array[i], opts); PVFS_sys_attr *attr = &rdplus_response.attr_array[i]; if(attr->objtype == PVFS_TYPE_DIRECTORY && opts->list_recursive) { int path_len = strlen(start) + strlen(cur_file) + 1; current = (subdir *) malloc(sizeof(subdir)); /* Prevent duplicate slashes in path */ if(start[strlen(start)-1] == '/') { current->path = (char *) malloc(path_len); snprintf(current->path,path_len,"%s%s",start,cur_file); } else { current->path = (char *) malloc(path_len + 1); snprintf(current->path,path_len+1,"%s/%s",start,cur_file); } /* Update linked list of subdirectories to recurse */ current->next = NULL; if(!head) { head = current; tail = current; } else { tail->next = current; tail = current; } } } token = rdplus_response.token; if (rdplus_response.pvfs_dirent_outcount) { free(rdplus_response.dirent_array); rdplus_response.dirent_array = NULL; free(rdplus_response.stat_err_array); rdplus_response.stat_err_array = NULL; for (i = 0; i < rdplus_response.pvfs_dirent_outcount; i++) { if (rdplus_response.attr_array) { PVFS_util_release_sys_attr(&rdplus_response.attr_array[i]); } } free(rdplus_response.attr_array); rdplus_response.attr_array = NULL; } } while(rdplus_response.pvfs_dirent_outcount == pvfs_dirent_incount); if (do_timing) { end = Wtime(); printf("PVFS_sys_readdirplus took %g msecs\n", (end - begin)); } if (rdplus_response.pvfs_dirent_outcount) { free(rdplus_response.dirent_array); rdplus_response.dirent_array = NULL; free(rdplus_response.stat_err_array); rdplus_response.stat_err_array = NULL; for (i = 0; i < rdplus_response.pvfs_dirent_outcount; i++) { if (rdplus_response.attr_array) { PVFS_util_release_sys_attr(&rdplus_response.attr_array[i]); } } free(rdplus_response.attr_array); rdplus_response.attr_array = NULL; } if (opts->list_recursive) { current = head; while(current) { printf("\n"); do_list(full_path,current->path,fs_id,opts); current = current->next; free(head->path); free(head); head = current; } } return 0; }
int main(int argc, char **argv) { int ret = -1; char str_buf[256] = {0}; char *filename = (char *)0; PVFS_fs_id cur_fs; PVFS_sysresp_symlink resp_sym; char* entry_name = NULL; char *target = NULL; PVFS_object_ref parent_refn; PVFS_sys_attr attr; PVFS_credentials credentials; if (argc != 3) { fprintf(stderr,"Usage: %s filename target\n",argv[0]); return ret; } filename = argv[1]; target = argv[2]; ret = PVFS_util_init_defaults(); if (ret < 0) { PVFS_perror("PVFS_util_init_defaults", ret); return (-1); } ret = PVFS_util_get_default_fsid(&cur_fs); if (ret < 0) { PVFS_perror("PVFS_util_get_default_fsid", ret); return (-1); } if (PINT_remove_base_dir(filename,str_buf,256)) { if (filename[0] != '/') { printf("You forgot the leading '/'\n"); } printf("Cannot retrieve link name for creation on %s\n", filename); return(-1); } printf("Link to be created is %s\n",str_buf); memset(&resp_sym, 0, sizeof(PVFS_sysresp_symlink)); PVFS_util_gen_credentials(&credentials); entry_name = str_buf; attr.mask = PVFS_ATTR_SYS_ALL_SETABLE; attr.owner = credentials.uid; attr.group = credentials.gid; attr.perms = 1877; attr.atime = attr.ctime = attr.mtime = time(NULL); ret = PINT_lookup_parent(filename, cur_fs, &credentials, &parent_refn.handle); if(ret < 0) { PVFS_perror("PVFS_util_lookup_parent", ret); return(-1); } parent_refn.fs_id = cur_fs; ret = PVFS_sys_symlink(entry_name, parent_refn, target, attr, &credentials, &resp_sym, NULL); if (ret < 0) { printf("symlink failed with errcode = %d\n", ret); return(-1); } printf("--symlink--\n"); printf("Handle: %lld\n", lld(resp_sym.ref.handle)); ret = PVFS_sys_finalize(); if (ret < 0) { printf("finalizing sysint failed with errcode = %d\n", ret); return (-1); } return(0); }
/* pvfs2_geteattr() * * changes the mode of the given file to the given permissions * * returns zero on success and negative one on failure */ int pvfs2_geteattr(int nkey, PVFS_ds_keyval *key_p, PVFS_ds_keyval *val_p, char *destfile) { int ret = -1; char str_buf[PVFS_NAME_MAX] = {0}; char pvfs_path[PVFS_NAME_MAX] = {0}; PVFS_fs_id cur_fs; PVFS_sysresp_lookup resp_lookup; PVFS_sysresp_geteattr resp_geteattr; PVFS_object_ref parent_ref; PVFS_credentials credentials; /* translate local path into pvfs2 relative path */ ret = PVFS_util_resolve(destfile,&cur_fs, pvfs_path, PVFS_NAME_MAX); if(ret < 0) { PVFS_perror("PVFS_util_resolve", ret); return -1; } PVFS_util_gen_credentials(&credentials); /* this if-else statement just pulls apart the pathname into its * parts....I think...this should be a function somewhere */ if (strcmp(pvfs_path,"/") == 0) { memset(&resp_lookup, 0, sizeof(PVFS_sysresp_lookup)); ret = PVFS_sys_lookup(cur_fs, pvfs_path, &credentials, &resp_lookup, PVFS2_LOOKUP_LINK_FOLLOW, NULL); if (ret < 0) { PVFS_perror("PVFS_sys_lookup", ret); return -1; } parent_ref.handle = resp_lookup.ref.handle; parent_ref.fs_id = resp_lookup.ref.fs_id; } else { /* get the absolute path on the pvfs2 file system */ if (PINT_remove_base_dir(pvfs_path,str_buf,PVFS_NAME_MAX)) { if (pvfs_path[0] != '/') { fprintf(stderr, "Error: poorly formatted path.\n"); } fprintf(stderr, "Error: cannot retrieve entry name for " "creation on %s\n",pvfs_path); return -1; } ret = PINT_lookup_parent(pvfs_path, cur_fs, &credentials, &parent_ref.handle); if(ret < 0) { PVFS_perror("PINT_lookup_parent", ret); return -1; } else { parent_ref.fs_id = cur_fs; } } memset(&resp_lookup, 0, sizeof(PVFS_sysresp_lookup)); ret = PVFS_sys_ref_lookup(parent_ref.fs_id, str_buf, parent_ref, &credentials, &resp_lookup, PVFS2_LOOKUP_LINK_NO_FOLLOW, NULL); if (ret != 0) { fprintf(stderr, "Target '%s' does not exist!\n", str_buf); return -1; } /* get extended attribute */ resp_geteattr.val_array = val_p; ret = PVFS_sys_geteattr_list(resp_lookup.ref, &credentials, nkey, key_p, &resp_geteattr, NULL); if (ret < 0) { PVFS_perror("PVFS_sys_geteattr failed with errcode", ret); return(-1); } return 0; }