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 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); }
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; }
/* 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; }