root/trunk/test/correctness/pts/test-path-lookup.c @ 5338

Revision 5338, 17.1 KB (checked in by vilayann, 7 years ago)

Added the hindexed test program to the source tree...

Line 
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 <time.h>
10#include "mpi.h"
11#include "pts.h"
12#include "pvfs-helper.h"
13#include "pvfs2-util.h"
14#include "test-path-lookup.h"
15#include "pvfs2-req-proto.h"
16#include "pvfs2-internal.h"
17
18#define GENERATE_FILENAME(fname, max_len, f, i, r, slash) \
19do {                                                      \
20snprintf(fname, max_len, (slash ? "/%s%dr%d" : "%s%dr%d"),\
21          f, i, r);                                       \
22} while (0);
23
24#define RELATIVE_SYMLINK_NAME "rl"
25#define ABSOLUTE_SYMLINK_NAME "al"
26
27static int build_nested_path(
28    int levels, char *format, int rank, int test_symlinks)
29{
30    int ret = -1, i = 0, stored_error = 0;
31    char cur_filename[64] = {0}, tmp_buf[PVFS_NAME_MAX] = {0};
32    PVFS_fs_id cur_fs_id = 0;
33    PVFS_sys_attr attr;
34    PVFS_credentials credentials;
35    PVFS_sysresp_lookup lookup_resp;
36    PVFS_sysresp_mkdir mkdir_resp;
37    PVFS_sysresp_symlink symlink_resp;
38    char PATH_LOOKUP_BASE_DIR[64] = {0};
39    PVFS_object_ref root_refn = {0,0}, parent_refn = {0,0}, base_refn = {0,0};
40    PVFS_object_ref *newdir_refns = NULL;
41    PVFS_object_ref *lookup_refns = NULL;
42    PVFS_object_ref *rsymlink_refns = NULL;
43    PVFS_object_ref *asymlink_refns = NULL;
44    char **absolute_paths = NULL;
45
46    PVFS_util_gen_credentials(&credentials);
47
48    if (levels && format)
49    {
50        snprintf(PATH_LOOKUP_BASE_DIR, 64, "d%sr%d",
51                 format, rank);
52
53        cur_fs_id = pvfs_helper.fs_id;
54
55        /* look up the root handle */
56        ret = PVFS_sys_lookup(
57            cur_fs_id, "/", &credentials, &lookup_resp,
58            PVFS2_LOOKUP_LINK_NO_FOLLOW);
59        if (ret < 0)
60        {
61            fprintf(stderr," *** lookup failed on root directory\n");
62            return ret;
63        }
64
65        root_refn = lookup_resp.ref;
66        fprintf(stderr,"Got Root Handle %llu on fs %d\n",
67                llu(root_refn.handle), root_refn.fs_id);
68
69        attr.mask = PVFS_ATTR_SYS_ALL_SETABLE;
70        attr.owner = credentials.uid;
71        attr.group = credentials.gid;
72        attr.perms = 1877;
73        attr.atime = attr.ctime = attr.mtime = time(NULL);
74
75        /* make the top-level base directory */
76        fprintf(stderr," Creating base directory %s under %llu, %d\n",
77                PATH_LOOKUP_BASE_DIR, llu(root_refn.handle),
78                root_refn.fs_id);
79        ret = PVFS_sys_mkdir(PATH_LOOKUP_BASE_DIR, root_refn,
80                             attr, &credentials, &mkdir_resp);
81        if (ret < 0)
82        {
83            fprintf(stderr," PVFS_sys_mkdir failed to create "
84                    "base directory %s\n", PATH_LOOKUP_BASE_DIR);
85            goto cleanup;
86        }
87        base_refn = mkdir_resp.ref;
88
89        newdir_refns = (PVFS_object_ref *)malloc(
90            (levels * sizeof(PVFS_object_ref)));
91        lookup_refns = (PVFS_object_ref *)malloc(
92            (levels * sizeof(PVFS_object_ref)));
93        absolute_paths = (char **)malloc(
94            (levels * PVFS_NAME_MAX * sizeof(char)));
95        if (!newdir_refns || !lookup_refns | !absolute_paths)
96        {
97            fprintf(stderr," failed to allocate reference arrays\n");
98            goto cleanup;
99        }
100
101        for(i = 0; i < levels; i++)
102        {
103            parent_refn = mkdir_resp.ref;
104
105            GENERATE_FILENAME(cur_filename, 64, format, i, rank, 0);
106            fprintf(stderr,"  Creating directory %s under %llu, %d\n",
107                    cur_filename, llu(parent_refn.handle),
108                    parent_refn.fs_id);
109
110            ret = PVFS_sys_mkdir(cur_filename, parent_refn, attr,
111                                 &credentials, &mkdir_resp);
112            if (ret < 0)
113            {
114                fprintf(stderr," PVFS_sys_mkdir failed to create "
115                        "the directory %s\n", cur_filename);
116                goto cleanup;
117            }
118            fprintf(stderr, "Got handle %llu\n",
119                    llu(mkdir_resp.ref.handle));
120
121            /* grab refn of newly created directory */
122            newdir_refns[i] = mkdir_resp.ref;
123        }
124
125        /* generate the absolute path names */
126        snprintf(tmp_buf, PVFS_NAME_MAX, "/%s", PATH_LOOKUP_BASE_DIR);
127        for(i = 0; i < levels; i++)
128        {
129            GENERATE_FILENAME(cur_filename, 64, format, i, rank, 1);
130            strncat(tmp_buf, cur_filename, PVFS_NAME_MAX);
131            absolute_paths[i] = strdup(tmp_buf);
132            if (strlen(absolute_paths[i]) > PVFS_NAME_MAX)
133            {
134                fprintf(stderr," Generated pathname is too long to "
135                        "be a valid PVFS2 path name\n");
136                fprintf(stderr,"%s", absolute_paths[i]);
137                goto cleanup;
138            }
139        }
140
141        /* for each directory just created, do a lookup on them */
142        parent_refn = base_refn;
143        for(i = 0; i < levels; i++)
144        {
145            GENERATE_FILENAME(cur_filename, 64, format, i, rank, 0);
146            fprintf(stderr,"Looking up path %d [RELATIVE] \t\t... ", i);
147#if 0
148            fprintf(stderr,
149                    " - Looking up relative path %s under %llu, %d\n",
150                    cur_filename, llu(parent_refn.handle),
151                    parent_refn.fs_id);
152#endif
153            /* first do a relative lookup */
154            ret = PVFS_sys_ref_lookup(
155                parent_refn.fs_id, cur_filename,
156                parent_refn, &credentials,
157                &lookup_resp, PVFS2_LOOKUP_LINK_NO_FOLLOW);
158            if (ret < 0)
159            {
160                fprintf(stderr,"\nPVFS_sys_ref_lookup failed\n");
161                goto cleanup;
162            }
163            else
164            {
165                fprintf(stderr,"OK\n");
166            }
167
168            /* grab refn of looked up directory */
169            lookup_refns[i] = lookup_resp.ref;
170
171            /* then do an absolute path lookup */
172            fprintf(stderr,"Looking up path %d [ABSOLUTE] \t\t... ", i);
173#if 0
174            fprintf(stderr," - Looking up absolute path:\n%s\n",
175                    absolute_paths[i]);
176#endif
177            ret = PVFS_sys_lookup(cur_fs_id, absolute_paths[i],
178                                  &credentials, &lookup_resp,
179                                  PVFS2_LOOKUP_LINK_NO_FOLLOW);
180            if (ret < 0)
181            {
182                fprintf(stderr,"\nPVFS_sys_lookup failed\n");
183                goto cleanup;
184            }
185            else
186            {
187                fprintf(stderr,"OK\n");
188            }
189
190            /*
191              assert that the ref lookup and the absolute
192              lookup yielded the same result
193            */
194            if ((lookup_refns[i].fs_id !=
195                 lookup_resp.ref.fs_id) ||
196                (lookup_refns[i].handle !=
197                 lookup_resp.ref.handle))
198            {
199                fprintf(stderr,"! PVFS_sys_ref_lookup and "
200                        "PVFS_sys_lookup returned different results\n"
201                        "\twhen they SHOULD BE THE SAME (%llu != %llu)!\n",
202                        llu(lookup_refns[i].handle),
203                        llu(lookup_resp.ref.handle));
204                goto cleanup;
205            }
206            parent_refn = lookup_resp.ref;
207        }
208        ret = 0;
209    }
210
211    if (!test_symlinks)
212    {
213        goto cleanup;
214    }
215
216    /*
217      generate both relative and absolute symlinks for
218      most of the nested directories created
219    */
220    rsymlink_refns = (PVFS_object_ref *)malloc(
221        (levels * sizeof(PVFS_object_ref)));
222    asymlink_refns = (PVFS_object_ref *)malloc(
223        (levels * sizeof(PVFS_object_ref)));
224    for(i = 0; i < levels; i++)
225    {
226        parent_refn = ((i == 0) ? base_refn : lookup_refns[i - 1]);
227        GENERATE_FILENAME(cur_filename, 64, format, i, rank, 0);
228        fprintf(stderr, "Generating relative symlink %s "
229                "in %llu,%d to point at %s\n", RELATIVE_SYMLINK_NAME,
230                llu(parent_refn.handle),
231                parent_refn.fs_id, cur_filename);
232        ret = PVFS_sys_symlink(
233            RELATIVE_SYMLINK_NAME, parent_refn, cur_filename,
234            attr, &credentials, &symlink_resp);
235        if (ret < 0)
236        {
237            PVFS_perror("Failed to create symlink ", ret);
238            goto symlink_cleanup;
239        }
240        fprintf(stderr, "Got handle %llu\n", llu(symlink_resp.ref.handle));
241
242        /* stash the newly created relative symlink references created */
243        rsymlink_refns[i] = symlink_resp.ref;
244
245        fprintf(stderr, "Generating absolute symlink %s "
246                "in %llu,%d to point at %s\n", ABSOLUTE_SYMLINK_NAME,
247                llu(parent_refn.handle),
248                parent_refn.fs_id, absolute_paths[i]);
249        ret = PVFS_sys_symlink(
250            ABSOLUTE_SYMLINK_NAME, parent_refn, absolute_paths[i],
251            attr, &credentials, &symlink_resp);
252        if (ret < 0)
253        {
254            PVFS_perror("Failed to create symlink ", ret);
255            goto symlink_cleanup;
256        }
257        fprintf(stderr, "Got handle %llu\n", llu(symlink_resp.ref.handle));
258
259        /* stash the newly created absolute symlink references created */
260        asymlink_refns[i] = symlink_resp.ref;
261    }
262
263    for(i = 0; i < levels; i++)
264    {
265        /*
266          do absolute path lookups with simple symlink substitutions
267          that should resolve to known targets at this point
268        */
269        if (i > 0)
270        {
271            snprintf(tmp_buf, PVFS_NAME_MAX, "%s/%s",
272                     absolute_paths[i - 1], RELATIVE_SYMLINK_NAME);
273            /*
274              a lookup on tmp_buf should resolve exactly
275              to rsymlink_refns[i] if not followed, and
276              lookup_refns[i] if followed.  make sure!
277            */
278            fprintf(stderr,"Looking up rsymlink %d [UNFOLLOWED] "
279                    "\t\t... ", i);
280            ret = PVFS_sys_lookup(cur_fs_id, tmp_buf,
281                                  &credentials, &lookup_resp,
282                                  PVFS2_LOOKUP_LINK_NO_FOLLOW);
283            if (ret < 0)
284            {
285                fprintf(stderr,"\nPVFS_sys_lookup failed\n");
286                goto symlink_cleanup;
287            }
288
289            if ((lookup_resp.ref.handle !=
290                 rsymlink_refns[i].handle) ||
291                (lookup_resp.ref.fs_id !=
292                 rsymlink_refns[i].fs_id))
293            {
294                fprintf(stderr,"\nSymlink %s resolved to %llu "
295                        "but should have resolved to %llu\n", tmp_buf,
296                        llu(lookup_resp.ref.handle),
297                        llu(rsymlink_refns[i].handle));
298                goto symlink_cleanup;
299            }
300            else
301            {
302                fprintf(stderr,"OK\n");
303            }
304
305            fprintf(stderr,"Looking up rsymlink %d [  FOLLOWED] "
306                    "\t\t... ", i);
307            ret = PVFS_sys_lookup(cur_fs_id, tmp_buf,
308                                  &credentials, &lookup_resp,
309                                  PVFS2_LOOKUP_LINK_FOLLOW);
310            if (ret < 0)
311            {
312                fprintf(stderr,"\nPVFS_sys_lookup failed\n");
313                goto symlink_cleanup;
314            }
315
316            if ((lookup_resp.ref.handle !=
317                 lookup_refns[i].handle) ||
318                (lookup_resp.ref.fs_id !=
319                 lookup_refns[i].fs_id))
320            {
321                fprintf(stderr,"\nSymlink %s resolved to %llu "
322                        "but should have resolved to %llu\n", tmp_buf,
323                        llu(lookup_resp.ref.handle),
324                        llu(lookup_refns[i].handle));
325                goto symlink_cleanup;
326            }
327            else
328            {
329                fprintf(stderr,"OK\n");
330            }
331
332            snprintf(tmp_buf, PVFS_NAME_MAX, "%s/%s",
333                     absolute_paths[i - 1], ABSOLUTE_SYMLINK_NAME);
334            /*
335              a lookup on tmp_buf should resolve exactly
336              to asymlink_refns[i] if not followed, and
337              lookup_refns[i] if followed.  make sure!
338            */
339            fprintf(stderr,"Looking up asymlink %d [UNFOLLOWED] "
340                    "\t\t... ", i);
341            ret = PVFS_sys_lookup(cur_fs_id, tmp_buf,
342                                  &credentials, &lookup_resp,
343                                  PVFS2_LOOKUP_LINK_NO_FOLLOW);
344            if (ret < 0)
345            {
346                fprintf(stderr,"\nPVFS_sys_lookup failed\n");
347                goto symlink_cleanup;
348            }
349
350            if ((lookup_resp.ref.handle !=
351                 asymlink_refns[i].handle) ||
352                (lookup_resp.ref.fs_id !=
353                 asymlink_refns[i].fs_id))
354            {
355                fprintf(stderr,"\nSymlink %s resolved to %llu "
356                        "but should have resolved to %llu\n", tmp_buf,
357                        llu(lookup_resp.ref.handle),
358                        llu(asymlink_refns[i].handle));
359                goto symlink_cleanup;
360            }
361            else
362            {
363                fprintf(stderr,"OK\n");
364            }
365
366            fprintf(stderr,"Looking up asymlink %d [  FOLLOWED] "
367                    "\t\t... ", i);
368            ret = PVFS_sys_lookup(cur_fs_id, tmp_buf,
369                                  &credentials, &lookup_resp,
370                                  PVFS2_LOOKUP_LINK_FOLLOW);
371            if (ret < 0)
372            {
373                fprintf(stderr,"\nPVFS_sys_lookup failed\n");
374                goto symlink_cleanup;
375            }
376
377            if ((lookup_resp.ref.handle !=
378                 lookup_refns[i].handle) ||
379                (lookup_resp.ref.fs_id !=
380                 lookup_refns[i].fs_id))
381            {
382                fprintf(stderr,"\nSymlink %s resolved to %llu "
383                        "but should have resolved to %llu\n", tmp_buf,
384                        llu(lookup_resp.ref.handle),
385                        llu(lookup_refns[i].handle));
386                goto symlink_cleanup;
387            }
388            else
389            {
390                fprintf(stderr,"OK\n");
391            }
392        }
393    }
394
395  symlink_cleanup:
396    if (rsymlink_refns)
397    {
398        free(rsymlink_refns);
399    }
400    if (asymlink_refns)
401    {
402        free(asymlink_refns);
403    }
404
405  cleanup:
406    stored_error = ret;
407
408    if (absolute_paths)
409    {
410        for(i = (levels - 1); i > -1; i--)
411        {
412            parent_refn = ((i == 0) ? base_refn : lookup_refns[i - 1]);
413            if (parent_refn.handle == PVFS_HANDLE_NULL)
414            {
415                fprintf(stderr, "Error performing clean up. "
416                        "Sorry, we're busted ... aborting\n");
417                break;
418            }
419            GENERATE_FILENAME(cur_filename, 64, format, i, rank, 0);
420            fprintf(stderr,"Removing path %s under %llu,%d \t\t... ",
421                    cur_filename, llu(parent_refn.handle),
422                    parent_refn.fs_id);
423            ret = PVFS_sys_remove(cur_filename, parent_refn, &credentials);
424            fprintf(stderr, "%s\n", ((ret < 0) ? "FAILED" : "DONE"));
425            if (ret)
426            {
427                PVFS_perror("\nPath removal status: ", ret);
428            }
429
430            if (test_symlinks)
431            {
432                fprintf(stderr,"Removing rlink %s under %llu,%d \t\t... ",
433                        RELATIVE_SYMLINK_NAME, llu(parent_refn.handle),
434                        parent_refn.fs_id);
435                ret = PVFS_sys_remove(RELATIVE_SYMLINK_NAME,
436                                      parent_refn, &credentials);
437                fprintf(stderr, "%s\n", ((ret < 0) ? "FAILED" : "DONE"));
438                if (ret)
439                {
440                    PVFS_perror("\nPath removal status: ", ret);
441                }
442
443                fprintf(stderr,"Removing alink %s under %llu,%d \t\t... ",
444                        ABSOLUTE_SYMLINK_NAME, llu(parent_refn.handle),
445                        parent_refn.fs_id);
446                ret = PVFS_sys_remove(ABSOLUTE_SYMLINK_NAME,
447                                      parent_refn, &credentials);
448                fprintf(stderr, "%s\n", ((ret < 0) ? "FAILED" : "DONE"));
449                if (ret)
450                {
451                    PVFS_perror("\nPath removal status: ", ret);
452                }
453            }
454        }
455        free(absolute_paths);
456    }
457    ret = PVFS_sys_remove(PATH_LOOKUP_BASE_DIR, root_refn, &credentials);
458    if (ret)
459    {
460        PVFS_perror("Top-level Path removal error ", ret);
461    }
462    if (newdir_refns)
463    {
464        free(newdir_refns);
465    }
466    if (lookup_refns)
467    {
468        free(lookup_refns);
469    }
470    return stored_error;
471}
472
473int test_path_lookup(MPI_Comm *comm __unused, int rank, char *buf __unused, void *params __unused)
474{
475    int ret = -1;
476    char *format_prefix1 = "pt-0";
477    char *format_prefix2 = "st0";
478
479    if (!pvfs_helper.initialized && initialize_sysint())
480    {
481        fprintf(stderr, "initialize_sysint failed\n");
482        return ret;
483    }
484
485    ret = build_nested_path(5, format_prefix1, rank, 0);
486    if (ret)
487    {
488        fprintf(stderr,"(1) Failed to build nested path\n");
489        goto error_exit;
490    }
491
492    ret = build_nested_path(13, format_prefix2, rank, 1);
493    if (ret)
494    {
495        fprintf(stderr,"(2) Failed to build nested path\n");
496        goto error_exit;
497    }
498
499  error_exit:
500    return ret;
501}
502
503/*
504 * Local variables:
505 *  c-indent-level: 4
506 *  c-basic-offset: 4
507 * End:
508 *
509 * vim: ts=8 sts=4 sw=4 expandtab
510 */
Note: See TracBrowser for help on using the browser.