| 1 | /* |
|---|
| 2 | * (C) 2001 Clemson University and The University of Chicago |
|---|
| 3 | * |
|---|
| 4 | * See COPYING in top-level directory. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #include <stdio.h> |
|---|
| 8 | #include <stdlib.h> |
|---|
| 9 | #include <client.h> |
|---|
| 10 | #include <string.h> |
|---|
| 11 | #ifndef WIN32 |
|---|
| 12 | #include <unistd.h> |
|---|
| 13 | #endif |
|---|
| 14 | #include <sys/types.h> |
|---|
| 15 | #include "pvfs2-util.h" |
|---|
| 16 | #include "pvfs2-internal.h" |
|---|
| 17 | |
|---|
| 18 | #ifdef WIN32 |
|---|
| 19 | #define snprintf _snprintf |
|---|
| 20 | #endif |
|---|
| 21 | |
|---|
| 22 | /* TODO: this can be larger after system interface readdir logic |
|---|
| 23 | * is in place to break up large readdirs into multiple operations |
|---|
| 24 | */ |
|---|
| 25 | #define MAX_NUM_DIRENTS 32 |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | void print_at_depth(char *name, int depth); |
|---|
| 29 | int is_directory(PVFS_handle handle, PVFS_fs_id fs_id); |
|---|
| 30 | int directory_walk(PVFS_fs_id cur_fs, |
|---|
| 31 | char *start_dir, char *base_dir, int depth); |
|---|
| 32 | |
|---|
| 33 | void print_at_depth(char *name, int depth) |
|---|
| 34 | { |
|---|
| 35 | /* we ignore depth for now */ |
|---|
| 36 | if (name) |
|---|
| 37 | { |
|---|
| 38 | printf("**** %s\n",name); |
|---|
| 39 | } |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | /* |
|---|
| 43 | returns -1 on error; 0 if handle is not a directory; |
|---|
| 44 | 1 otherwise |
|---|
| 45 | */ |
|---|
| 46 | int is_directory(PVFS_handle handle, PVFS_fs_id fs_id) |
|---|
| 47 | { |
|---|
| 48 | PVFS_object_ref pinode_refn; |
|---|
| 49 | uint32_t attrmask; |
|---|
| 50 | PVFS_credentials credentials; |
|---|
| 51 | PVFS_sysresp_getattr getattr_response; |
|---|
| 52 | |
|---|
| 53 | memset(&getattr_response,0,sizeof(PVFS_sysresp_getattr)); |
|---|
| 54 | |
|---|
| 55 | pinode_refn.handle = handle; |
|---|
| 56 | pinode_refn.fs_id = fs_id; |
|---|
| 57 | attrmask = PVFS_ATTR_SYS_ALL_NOSIZE; |
|---|
| 58 | |
|---|
| 59 | PVFS_util_gen_credentials(&credentials); |
|---|
| 60 | if (PVFS_sys_getattr(pinode_refn, attrmask, |
|---|
| 61 | &credentials, &getattr_response, NULL)) |
|---|
| 62 | { |
|---|
| 63 | fprintf(stderr,"Failed to get attributes on handle 0x%08llx " |
|---|
| 64 | "(fs_id is %d)\n",llu(handle),fs_id); |
|---|
| 65 | return -1; |
|---|
| 66 | } |
|---|
| 67 | return ((getattr_response.attr.objtype == PVFS_TYPE_DIRECTORY) ? 1 : 0); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | int directory_walk(PVFS_fs_id cur_fs, |
|---|
| 71 | char *start_dir, char *base_dir, int depth) |
|---|
| 72 | { |
|---|
| 73 | int i = 0; |
|---|
| 74 | int is_dir = 0; |
|---|
| 75 | char *cur_file = (char *)0; |
|---|
| 76 | PVFS_handle cur_handle; |
|---|
| 77 | PVFS_sysresp_lookup lk_response; |
|---|
| 78 | PVFS_sysresp_readdir rd_response; |
|---|
| 79 | char full_path[PVFS_NAME_MAX] = {0}; |
|---|
| 80 | char* name; |
|---|
| 81 | PVFS_credentials credentials; |
|---|
| 82 | PVFS_object_ref pinode_refn; |
|---|
| 83 | PVFS_ds_position token; |
|---|
| 84 | int pvfs_dirent_incount; |
|---|
| 85 | |
|---|
| 86 | gossip_debug(GOSSIP_CLIENT_DEBUG, "DIRECTORY WALK CALLED WITH " |
|---|
| 87 | "base %s | %s\n",base_dir,start_dir); |
|---|
| 88 | |
|---|
| 89 | memset(&lk_response,0,sizeof(PVFS_sysresp_lookup)); |
|---|
| 90 | |
|---|
| 91 | if (base_dir) |
|---|
| 92 | { |
|---|
| 93 | strncpy(full_path,base_dir,PVFS_NAME_MAX); |
|---|
| 94 | if (strlen(base_dir) > 1) |
|---|
| 95 | { |
|---|
| 96 | strcat(full_path,"/"); |
|---|
| 97 | } |
|---|
| 98 | strncat(full_path,start_dir,PVFS_NAME_MAX); |
|---|
| 99 | } |
|---|
| 100 | else |
|---|
| 101 | { |
|---|
| 102 | strcpy(full_path,start_dir); |
|---|
| 103 | } |
|---|
| 104 | name = full_path; |
|---|
| 105 | |
|---|
| 106 | PVFS_util_gen_credentials(&credentials); |
|---|
| 107 | if (PVFS_sys_lookup(cur_fs, name, &credentials, |
|---|
| 108 | &lk_response, PVFS2_LOOKUP_LINK_FOLLOW, NULL)) |
|---|
| 109 | { |
|---|
| 110 | fprintf(stderr,"Failed to lookup %s on fs_id %d!\n", |
|---|
| 111 | name,cur_fs); |
|---|
| 112 | return 1; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | print_at_depth(name,depth); |
|---|
| 116 | |
|---|
| 117 | pinode_refn.handle = lk_response.ref.handle; |
|---|
| 118 | pinode_refn.fs_id = cur_fs; |
|---|
| 119 | token = 0; |
|---|
| 120 | pvfs_dirent_incount = MAX_NUM_DIRENTS; |
|---|
| 121 | |
|---|
| 122 | do |
|---|
| 123 | { |
|---|
| 124 | memset(&rd_response,0,sizeof(PVFS_sysresp_readdir)); |
|---|
| 125 | if (PVFS_sys_readdir(pinode_refn, |
|---|
| 126 | (!token ? PVFS_READDIR_START : token), |
|---|
| 127 | pvfs_dirent_incount, |
|---|
| 128 | &credentials, &rd_response, NULL)) |
|---|
| 129 | { |
|---|
| 130 | fprintf(stderr,"Failed to perform readdir operation\n"); |
|---|
| 131 | return 1; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | if (!rd_response.pvfs_dirent_outcount) |
|---|
| 135 | { |
|---|
| 136 | gossip_debug(GOSSIP_CLIENT_DEBUG,"No files found.\n"); |
|---|
| 137 | return 0; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | gossip_debug(GOSSIP_CLIENT_DEBUG, "%d files found.\n", |
|---|
| 141 | rd_response.pvfs_dirent_outcount); |
|---|
| 142 | for(i = 0; i < rd_response.pvfs_dirent_outcount; i++) |
|---|
| 143 | { |
|---|
| 144 | cur_file = rd_response.dirent_array[i].d_name; |
|---|
| 145 | cur_handle = rd_response.dirent_array[i].handle; |
|---|
| 146 | |
|---|
| 147 | gossip_debug(GOSSIP_CLIENT_DEBUG,"Got handle %llu\n", |
|---|
| 148 | llu(cur_handle)); |
|---|
| 149 | |
|---|
| 150 | is_dir = is_directory(cur_handle, |
|---|
| 151 | cur_fs); |
|---|
| 152 | switch(is_dir) |
|---|
| 153 | { |
|---|
| 154 | case -1: |
|---|
| 155 | /* if we had an error, warn */ |
|---|
| 156 | gossip_err("Failed to get attributes. Skipping " |
|---|
| 157 | "file %s\n", cur_file); |
|---|
| 158 | break; |
|---|
| 159 | case 0: |
|---|
| 160 | /* if we have a normal file, print it */ |
|---|
| 161 | { |
|---|
| 162 | char buf[PVFS_NAME_MAX] = {0}; |
|---|
| 163 | snprintf(buf,PVFS_NAME_MAX,"%s/%s", |
|---|
| 164 | ((strcmp(full_path,"/")) ? |
|---|
| 165 | full_path : ""),cur_file); |
|---|
| 166 | print_at_depth(buf,depth); |
|---|
| 167 | } |
|---|
| 168 | break; |
|---|
| 169 | case 1: |
|---|
| 170 | /* if we have a dir, recurse */ |
|---|
| 171 | if (directory_walk(cur_fs, cur_file, |
|---|
| 172 | full_path,depth+1)) |
|---|
| 173 | { |
|---|
| 174 | fprintf(stderr,"Failed directory walk at " |
|---|
| 175 | "depth %d\n", depth+1); |
|---|
| 176 | return 1; |
|---|
| 177 | } |
|---|
| 178 | break; |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | token += rd_response.pvfs_dirent_outcount; |
|---|
| 183 | if (rd_response.pvfs_dirent_outcount) |
|---|
| 184 | free(rd_response.dirent_array); |
|---|
| 185 | |
|---|
| 186 | } while(rd_response.pvfs_dirent_outcount != 0); |
|---|
| 187 | |
|---|
| 188 | return 0; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | int main(int argc, char **argv) |
|---|
| 193 | { |
|---|
| 194 | int go_twice = 0; |
|---|
| 195 | PVFS_fs_id cur_fs; |
|---|
| 196 | int ret = -1; |
|---|
| 197 | |
|---|
| 198 | if (argc != 2) |
|---|
| 199 | { |
|---|
| 200 | if (argc == 3) |
|---|
| 201 | { |
|---|
| 202 | go_twice = atoi(argv[2]); |
|---|
| 203 | if (go_twice != 1) |
|---|
| 204 | { |
|---|
| 205 | goto usage; |
|---|
| 206 | } |
|---|
| 207 | goto start_find; |
|---|
| 208 | } |
|---|
| 209 | usage: |
|---|
| 210 | fprintf(stderr,"usage: %s <starting dir> [ 1 ]\n",argv[0]); |
|---|
| 211 | fprintf(stderr,"This is not a full featured version of FIND(1L)\n"); |
|---|
| 212 | fprintf(stderr,"If the 3rd argument is a '1', find runs twice.\n"); |
|---|
| 213 | fprintf(stderr," (useful for lookup/dcache testing).\n"); |
|---|
| 214 | return 1; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | start_find: |
|---|
| 218 | |
|---|
| 219 | ret = PVFS_util_init_defaults(); |
|---|
| 220 | if (ret < 0) |
|---|
| 221 | { |
|---|
| 222 | PVFS_perror("PVFS_util_init_defaults", ret); |
|---|
| 223 | return (-1); |
|---|
| 224 | } |
|---|
| 225 | ret = PVFS_util_get_default_fsid(&cur_fs); |
|---|
| 226 | if (ret < 0) |
|---|
| 227 | { |
|---|
| 228 | PVFS_perror("PVFS_util_get_default_fsid", ret); |
|---|
| 229 | return (-1); |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | if (directory_walk(cur_fs, argv[1],NULL,0)) |
|---|
| 233 | { |
|---|
| 234 | fprintf(stderr,"Failed to do directory walk\n"); |
|---|
| 235 | return 1; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | if (go_twice) |
|---|
| 239 | { |
|---|
| 240 | if (directory_walk(cur_fs, argv[1],NULL,0)) |
|---|
| 241 | { |
|---|
| 242 | fprintf(stderr,"Failed to do directory walk (2nd iteration)\n"); |
|---|
| 243 | return 1; |
|---|
| 244 | } |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | if (PVFS_sys_finalize()) |
|---|
| 248 | { |
|---|
| 249 | fprintf(stderr,"Failed to finalize PVFS\n"); |
|---|
| 250 | return 1; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | return 0; |
|---|
| 254 | } |
|---|