| 1 | /* |
|---|
| 2 | * (C) 2004 Clemson University and The University of Chicago |
|---|
| 3 | * |
|---|
| 4 | * See COPYING in top-level directory. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #include <unistd.h> |
|---|
| 8 | #include <stdio.h> |
|---|
| 9 | #include <errno.h> |
|---|
| 10 | #include <string.h> |
|---|
| 11 | #include <sys/stat.h> |
|---|
| 12 | #include <fcntl.h> |
|---|
| 13 | #include <sys/time.h> |
|---|
| 14 | #include <time.h> |
|---|
| 15 | #include <stdlib.h> |
|---|
| 16 | |
|---|
| 17 | #include "pvfs2.h" |
|---|
| 18 | #include "str-utils.h" |
|---|
| 19 | #include "pint-sysint-utils.h" |
|---|
| 20 | |
|---|
| 21 | #ifndef PVFS2_VERSION |
|---|
| 22 | #define PVFS2_VERSION "Unknown" |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #define VALBUFSZ 1024 |
|---|
| 26 | |
|---|
| 27 | #ifndef NUM_KEYS_PER_REQUEST |
|---|
| 28 | #define NUM_KEYS_PER_REQUEST 10 |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | /* optional parameters, filled in by parse_args() */ |
|---|
| 32 | struct options |
|---|
| 33 | { |
|---|
| 34 | int nkey; |
|---|
| 35 | PVFS_ds_keyval *key; |
|---|
| 36 | int target_count; |
|---|
| 37 | char** destfiles; |
|---|
| 38 | }; |
|---|
| 39 | |
|---|
| 40 | static struct options* parse_args(int argc, char* argv[]); |
|---|
| 41 | int pvfs2_listeattr (int nkey, PVFS_ds_keyval *key_p, |
|---|
| 42 | char *destfile); |
|---|
| 43 | static void usage(int argc, char** argv); |
|---|
| 44 | int check_perm(char c); |
|---|
| 45 | |
|---|
| 46 | int main(int argc, char **argv) |
|---|
| 47 | { |
|---|
| 48 | int ret = 0; |
|---|
| 49 | struct options* user_opts = NULL; |
|---|
| 50 | int i, k; |
|---|
| 51 | |
|---|
| 52 | /* look at command line arguments */ |
|---|
| 53 | user_opts = parse_args(argc, argv); |
|---|
| 54 | if(!user_opts) |
|---|
| 55 | { |
|---|
| 56 | fprintf(stderr, "Error: failed to parse " |
|---|
| 57 | "command line arguments.\n"); |
|---|
| 58 | return(-1); |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | ret = PVFS_util_init_defaults(); |
|---|
| 62 | if(ret < 0) |
|---|
| 63 | { |
|---|
| 64 | PVFS_perror("PVFS_util_init_defaults", ret); |
|---|
| 65 | return(-1); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | /* |
|---|
| 69 | * for each file the user specified |
|---|
| 70 | * for each file the user specified |
|---|
| 71 | */ |
|---|
| 72 | for (i = 0; i < user_opts->target_count; i++) { |
|---|
| 73 | ret = pvfs2_listeattr(user_opts->nkey, user_opts->key, user_opts->destfiles[i]); |
|---|
| 74 | if (ret < 0) { |
|---|
| 75 | printf("listeattr returned error code - exiting\n"); |
|---|
| 76 | break; |
|---|
| 77 | } |
|---|
| 78 | printf("listeattr returned %d keys\n", ret); |
|---|
| 79 | for (k = 0; k < (user_opts->nkey ? ret : 0); k++) { |
|---|
| 80 | printf("key[%d]:%s\n",k, |
|---|
| 81 | (char *)user_opts->key[k].buffer); |
|---|
| 82 | } |
|---|
| 83 | /* TODO: need to free the request descriptions */ |
|---|
| 84 | } |
|---|
| 85 | PVFS_sys_finalize(); |
|---|
| 86 | return(ret); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /* pvfs2_listeattr() |
|---|
| 90 | * |
|---|
| 91 | * Retrieves the list of keys for a particular object! |
|---|
| 92 | * returns # of keys read successfully on success and negative one on failure |
|---|
| 93 | */ |
|---|
| 94 | int pvfs2_listeattr(int nkey, PVFS_ds_keyval *key_p, |
|---|
| 95 | char *destfile) |
|---|
| 96 | { |
|---|
| 97 | int ret = -1; |
|---|
| 98 | char str_buf[PVFS_NAME_MAX] = {0}; |
|---|
| 99 | char pvfs_path[PVFS_NAME_MAX] = {0}; |
|---|
| 100 | PVFS_fs_id cur_fs; |
|---|
| 101 | PVFS_sysresp_lookup resp_lookup; |
|---|
| 102 | PVFS_sysresp_listeattr resp_listeattr; |
|---|
| 103 | PVFS_object_ref parent_ref; |
|---|
| 104 | PVFS_credentials credentials; |
|---|
| 105 | PVFS_ds_position token = PVFS_ITERATE_START; |
|---|
| 106 | |
|---|
| 107 | /* translate local path into pvfs2 relative path */ |
|---|
| 108 | ret = PVFS_util_resolve(destfile,&cur_fs, pvfs_path, PVFS_NAME_MAX); |
|---|
| 109 | if(ret < 0) |
|---|
| 110 | { |
|---|
| 111 | PVFS_perror("PVFS_util_resolve", ret); |
|---|
| 112 | return -1; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | PVFS_util_gen_credentials(&credentials); |
|---|
| 116 | |
|---|
| 117 | /* this if-else statement just pulls apart the pathname into its |
|---|
| 118 | * parts....I think...this should be a function somewhere |
|---|
| 119 | */ |
|---|
| 120 | if (strcmp(pvfs_path,"/") == 0) |
|---|
| 121 | { |
|---|
| 122 | memset(&resp_lookup, 0, sizeof(PVFS_sysresp_lookup)); |
|---|
| 123 | ret = PVFS_sys_lookup(cur_fs, pvfs_path, |
|---|
| 124 | &credentials, &resp_lookup, |
|---|
| 125 | PVFS2_LOOKUP_LINK_FOLLOW, NULL); |
|---|
| 126 | if (ret < 0) |
|---|
| 127 | { |
|---|
| 128 | PVFS_perror("PVFS_sys_lookup", ret); |
|---|
| 129 | return -1; |
|---|
| 130 | } |
|---|
| 131 | parent_ref.handle = resp_lookup.ref.handle; |
|---|
| 132 | parent_ref.fs_id = resp_lookup.ref.fs_id; |
|---|
| 133 | } |
|---|
| 134 | else |
|---|
| 135 | { |
|---|
| 136 | /* get the absolute path on the pvfs2 file system */ |
|---|
| 137 | if (PINT_remove_base_dir(pvfs_path,str_buf,PVFS_NAME_MAX)) |
|---|
| 138 | { |
|---|
| 139 | if (pvfs_path[0] != '/') |
|---|
| 140 | { |
|---|
| 141 | fprintf(stderr, "Error: poorly formatted path.\n"); |
|---|
| 142 | } |
|---|
| 143 | fprintf(stderr, "Error: cannot retrieve entry name for " |
|---|
| 144 | "creation on %s\n",pvfs_path); |
|---|
| 145 | return -1; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | ret = PINT_lookup_parent(pvfs_path, cur_fs, &credentials, |
|---|
| 149 | &parent_ref.handle); |
|---|
| 150 | if(ret < 0) |
|---|
| 151 | { |
|---|
| 152 | PVFS_perror("PINT_lookup_parent", ret); |
|---|
| 153 | return -1; |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | { |
|---|
| 157 | parent_ref.fs_id = cur_fs; |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | memset(&resp_lookup, 0, sizeof(PVFS_sysresp_lookup)); |
|---|
| 161 | |
|---|
| 162 | ret = PVFS_sys_ref_lookup(parent_ref.fs_id, str_buf, |
|---|
| 163 | parent_ref, &credentials, &resp_lookup, |
|---|
| 164 | PVFS2_LOOKUP_LINK_NO_FOLLOW, NULL); |
|---|
| 165 | if (ret != 0) |
|---|
| 166 | { |
|---|
| 167 | fprintf(stderr, "Target '%s' does not exist!\n", str_buf); |
|---|
| 168 | return -1; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /* list extended attribute */ |
|---|
| 172 | resp_listeattr.key_array = key_p; |
|---|
| 173 | ret = PVFS_sys_listeattr(resp_lookup.ref, |
|---|
| 174 | token, nkey, &credentials, &resp_listeattr, NULL); |
|---|
| 175 | if (ret < 0) |
|---|
| 176 | { |
|---|
| 177 | PVFS_perror("PVFS_sys_listeattr failed with errcode", ret); |
|---|
| 178 | return(-1); |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | return resp_listeattr.nkey; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | /* parse_args() |
|---|
| 186 | * |
|---|
| 187 | * parses command line arguments |
|---|
| 188 | * |
|---|
| 189 | * returns pointer to options structure on success, NULL on failure |
|---|
| 190 | */ |
|---|
| 191 | static struct options* parse_args(int argc, char* argv[]) |
|---|
| 192 | { |
|---|
| 193 | /* getopt stuff */ |
|---|
| 194 | extern char* optarg; |
|---|
| 195 | extern int optind, opterr, optopt; |
|---|
| 196 | char flags[] = "n:v"; |
|---|
| 197 | int one_opt = 0; |
|---|
| 198 | int i, k; |
|---|
| 199 | |
|---|
| 200 | struct options* tmp_opts = NULL; |
|---|
| 201 | |
|---|
| 202 | /* create storage for the command line options */ |
|---|
| 203 | tmp_opts = (struct options*)malloc(sizeof(struct options)); |
|---|
| 204 | if(!tmp_opts){ |
|---|
| 205 | return(NULL); |
|---|
| 206 | } |
|---|
| 207 | memset(tmp_opts, 0, sizeof(struct options)); |
|---|
| 208 | |
|---|
| 209 | /* fill in defaults */ |
|---|
| 210 | tmp_opts->key = NULL; |
|---|
| 211 | tmp_opts->nkey = 0; |
|---|
| 212 | tmp_opts->target_count = 0; |
|---|
| 213 | tmp_opts->destfiles = NULL; |
|---|
| 214 | |
|---|
| 215 | if (argc < 3) { |
|---|
| 216 | usage(argc,argv); |
|---|
| 217 | exit(0); |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /* look at command line arguments */ |
|---|
| 221 | while((one_opt = getopt(argc, argv, flags)) != EOF) |
|---|
| 222 | { |
|---|
| 223 | switch(one_opt){ |
|---|
| 224 | case('n'): |
|---|
| 225 | tmp_opts->nkey = atoi(optarg); |
|---|
| 226 | break; |
|---|
| 227 | case('v'): |
|---|
| 228 | printf("%s\n", PVFS2_VERSION); |
|---|
| 229 | exit(0); |
|---|
| 230 | case('?'): |
|---|
| 231 | printf("?\n"); |
|---|
| 232 | usage(argc, argv); |
|---|
| 233 | exit(EXIT_FAILURE); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | /* now malloc space for the keys */ |
|---|
| 237 | tmp_opts->key = (PVFS_ds_keyval *) |
|---|
| 238 | malloc(sizeof(PVFS_ds_keyval) * tmp_opts->nkey); |
|---|
| 239 | /* now re-run the list and copy them in */ |
|---|
| 240 | for (k = 0; k < tmp_opts->nkey; k++) |
|---|
| 241 | { |
|---|
| 242 | tmp_opts->key[k].buffer_sz = 256; |
|---|
| 243 | tmp_opts->key[k].buffer = |
|---|
| 244 | (char *)malloc(sizeof(char)*tmp_opts->key[k].buffer_sz); |
|---|
| 245 | } |
|---|
| 246 | tmp_opts->target_count = argc-optind; |
|---|
| 247 | tmp_opts->destfiles=(char **)malloc(sizeof(char *)*(tmp_opts->target_count)); |
|---|
| 248 | for (i = 0; i < tmp_opts->target_count; i++) { |
|---|
| 249 | char *cur_arg_str = argv[optind+i]; |
|---|
| 250 | int length = strlen(cur_arg_str); |
|---|
| 251 | tmp_opts->destfiles[i] = (char *)malloc(sizeof(char)*(length+1)); |
|---|
| 252 | strncpy(tmp_opts->destfiles[i],cur_arg_str,length+1); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | return(tmp_opts); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | |
|---|
| 259 | static void usage(int argc, char** argv) |
|---|
| 260 | { |
|---|
| 261 | fprintf(stderr,"Usage: %s [-v] -n <# of keys> filename(s)\n",argv[0]); |
|---|
| 262 | fprintf(stderr," -v - print program version and terminate.\n"); |
|---|
| 263 | return; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /* |
|---|
| 267 | * Local variables: |
|---|
| 268 | * c-indent-level: 4 |
|---|
| 269 | * c-basic-offset: 4 |
|---|
| 270 | * End: |
|---|
| 271 | * |
|---|
| 272 | * vim: ts=8 sts=4 sw=4 expandtab |
|---|
| 273 | */ |
|---|
| 274 | |
|---|