root/branches/cu-security-branch/test/shared/test-common.c @ 8397

Revision 8397, 47.6 KB (checked in by nlmills, 3 years ago)

initial merge with Orange-Branch. much will be broken

Line 
1/*
2 * Copyright (c) Acxiom Corporation, 2005
3 *
4 * See COPYING in top-level directory
5 */
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/stat.h>
11#include <fcntl.h>
12#include <errno.h>
13#include <time.h>
14#include <limits.h>
15#include <string.h>
16#include <getopt.h>
17#include <assert.h>
18
19#include "test-common.h"
20#include "libgen.h"
21
22/** \file
23 * Implementation of the test-common wrapper functions.
24 */
25
26static char pvfsEXELocation[PATH_MAX]; /* Used to prefix the command line
27                                        * utilities for pvfs2 utils
28                                        */
29static void copy_pvfs2_to_stat(
30    const PVFS_sys_attr * attr,
31    struct stat         * fileStats);
32static void display_common_usage(char* exeName);
33 
34
35/**
36 * Sets a local char array containing the path to prefix all pvfs2 utilities
37 * \retval TEST_COMMON_SUCCESS Success
38 * \retval TEST_COMMON_FAIL Failure
39 */
40int set_util_path(const char * utility_path) /**< NULL terminated path. MUST end with "/" */
41{
42    int retval = 0;
43   
44    /* Validate we have enough space */
45    if (sizeof(pvfsEXELocation) < (strlen(utility_path)+1))
46    {
47        print_error("Unable to create pvfs2tab file: "
48                    "Specified pvfsEXELocation length (%d bytes) too short.\n"
49                    "pvfsEXELocation length needs to be at least %d bytes.\n",
50                    (int)sizeof(pvfsEXELocation), (int)strlen(utility_path));
51        return(TEST_COMMON_FAIL);
52    }
53
54    retval = snprintf(pvfsEXELocation, sizeof(pvfsEXELocation), "%s", utility_path);
55
56    if ((retval+1) > sizeof(pvfsEXELocation))  /* retval does not include NULL terminator */
57    {
58        print_error("Internal variable \"pvfsEXELocation\" too short.\n"
59                    "This array needs to be at least %d bytes in length.\n",
60                    retval+1);
61        return(TEST_COMMON_FAIL);
62    }
63    return(TEST_COMMON_SUCCESS);
64}
65
66/**
67 * creates the pvfs2tab file and sets the environment variable appropriately
68 * for you to use the PVFS2 API.
69 *
70 * \retval TEST_COMMON_SUCCESS Success
71 * \retval TEST_COMMON_FAIL Failure
72 */
73int create_pvfs2tab_file(
74    char* pvfs2tab_name, /**< Placeholder for file name of file created */
75    int len,             /**< Maximum length of pvfs2tab_name */
76    const int   port,    /**< Port number for PVFS2 file system */
77    const char* networkProto, /**< Network Protocol, i.e. tcp */
78    const char* hostname, /**< Hostname of main metaserver */
79    const char* fsname,  /**< file system name in pvfs2 configuration */
80    const char* fs_file) /**< fs_file as defined in fstab (mount point) */
81{
82    FILE * pvfs2tab = NULL;
83    char   buffer[512] = "";
84    char   fs_spec[PATH_MAX]= ""; /* fs_spec as defined in fstab (remote file system) */
85    int    retval = TEST_COMMON_SUCCESS;
86
87    retval = snprintf(fs_spec, sizeof(fs_spec), "%s://%s:%d/%s",
88                      networkProto,
89                      hostname,
90                      port,
91                      fsname);
92
93    if ((retval+1) > sizeof(fs_spec))  /* ret does not include NULL terminator */
94    {
95        print_error("Internal variable \"fs_spec\" too short.\n"
96                    "This array needs to be at least %d bytes in length.\n",
97                    retval+1);
98        return(TEST_COMMON_FAIL);
99    }
100
101
102    /* Construct unique name for pvfstab file (use the pid to make it unique) */
103    retval = snprintf(buffer, sizeof(buffer), "/usr/tmp/pvfs2tab_%d", getpid());
104    if ((retval+1) > sizeof(buffer))  /* retval does not include NULL terminator */
105    {
106        print_error("Internal variable \"buffer\" too short.\n"
107                    "This array needs to be at least %d bytes in length.\n",
108                    retval+1);
109        return(TEST_COMMON_FAIL);
110    }
111   
112    if (len < (strlen(buffer)+1))
113    {
114        print_error("Unable to create pvfs2tab file: "
115                    "Specified buffer length (%d bytes) too short.\n"
116                    "Buffer length needs to be at least %d bytes.\n",
117                    len, (int)strlen(buffer));
118        return(TEST_COMMON_FAIL);
119    }
120   
121    strncpy(pvfs2tab_name, buffer, len);
122
123    pvfs2tab = fopen(pvfs2tab_name, "w");
124    if (pvfs2tab == NULL)
125    {
126        print_error("Could not open pvfs2tab file [%s]: %s\n",
127                    pvfs2tab_name, strerror(errno));
128        return(TEST_COMMON_FAIL);
129    }
130
131    fprintf(pvfs2tab, "%s %s pvfs2 default,noauto 0 0\n", fs_spec, fs_file); 
132   
133    fclose(pvfs2tab);
134
135    /* The PVFS2TAB_FILE environment variable must be set to use this file */
136    if (setenv("PVFS2TAB_FILE", pvfs2tab_name, 1))
137    {
138        print_error("Unable to set PVFS2TAB_FILE environment variable.\n");
139        return(TEST_COMMON_FAIL);
140    }
141    return(TEST_COMMON_SUCCESS);
142}
143
144/**
145 * Deletes the pvfstab file and un-sets the environment variable PVFS2TAB_FILE
146 *
147 * \retval TEST_COMMON_SUCCESS Success
148 * \retval TEST_COMMON_FAIL Failure
149 */
150int destroy_pvfs2tab_file(const char * pvfs2tab_name) /**< name of file to delete */
151{
152    if(unlink(pvfs2tab_name))
153    {
154        print_error("Unable to unlink [%s].\n"
155                    "\tError Number = [%d]\n"
156                    "\tError Desc   = [%s]\n",
157                    pvfs2tab_name,
158                    errno,
159                    strerror(errno));
160        return(TEST_COMMON_FAIL);
161    }
162
163    unsetenv("PVFS2TAB_FILE");
164    return(TEST_COMMON_SUCCESS);
165}
166
167/** Initializes the PVFS2 API 
168 *
169 * \retval TEST_COMMON_SUCCESS Success
170 * \retval TEST_COMMON_FAIL Failure
171 */
172int initialize(
173    const int use_pvfs2_lib, /**< determines use of pvfs2 library  */
174    const int verbose)       /**< Turns on verbose prints if set to non-zero value */
175{
176    int ret = 0;
177
178    if(verbose) { printf("Initializing PVFS2 API\n"); }
179   
180    if(use_pvfs2_lib)
181    {
182        ret = PVFS_util_init_defaults();
183        if(ret < 0)
184        {
185            PVFS_perror("PVFS_util_init_defaults", ret);
186            print_error("Unable to initialize PVFS2 API\n");
187            return(TEST_COMMON_FAIL);
188        }
189    }
190    return(TEST_COMMON_SUCCESS);
191}
192
193/**
194 * \note initialize must be called prior to this function. This function will
195 *       fill in the cur_fs and relativeName on success
196 *
197 * \retval TEST_COMMON_SUCCESS Success (PVFS2 file)
198 * \retval TEST_COMMON_FAIL Failure (NOT PVFS2 file)
199 */
200int is_pvfs2(
201    const char * fileName,         /**< Null terminated file name */
202    PVFS_fs_id * cur_fs,           /**< container for return value for fs_id */
203    char       * relativeName,     /**< container for return value for relative path */
204    const int    relativeNameSize, /**< size of relativeName */
205    const int    use_pvfs2_lib,    /**< determines use of pvfs2 library */
206    const int    verbose)          /**< Turns on verbose prints if set to non-zero value */
207{
208    int ret=0;
209
210    if(verbose) { printf("Checking to see if [%s] is on a pvfs2 file system\n", fileName); }
211   
212    if(use_pvfs2_lib)
213    {
214        ret = PVFS_util_resolve(fileName,
215                                cur_fs,
216                                relativeName,
217                                relativeNameSize);
218        if(ret < 0)
219        {
220            PVFS_perror("PVFS_util_resolve", ret);
221            print_error("Unable to deteremine if [%s] resides on PVFS2\n", fileName);
222            return(TEST_COMMON_FAIL);
223        }
224    }
225
226    return(TEST_COMMON_SUCCESS);
227}
228
229/**
230 * \retval TEST_COMMON_SUCCESS Success
231 * \retval TEST_COMMON_FAIL Failure
232 */
233int finalize(int use_pvfs2_lib) /**< determines use of pvfs2 library */
234{
235    int ret;
236
237    if(use_pvfs2_lib)
238    {
239        ret = PVFS_sys_finalize();
240        if(ret < 0)
241        {
242            PVFS_perror("PVFS_sys_finalize", ret);
243            print_error("Unable to finalize PVFS2 API\n");
244            return(TEST_COMMON_FAIL);
245        }
246    }
247    return(TEST_COMMON_SUCCESS);
248}
249
250/**
251 * \retval  0  SUCCESS
252 * \retval  errno FAILURE using VFS layer
253 * \retval  -ENODATA FAILURE using PVFS2 API
254 */
255int close_file(
256    struct file_ref *stFileRef, /**< File descriptor (or handle) for an open file */
257    const int use_pvfs2_lib,    /**< determines use of pvfs2 library */
258    const int verbose)          /**< Turns on verbose prints if set to non-zero value */
259{
260    int ret=0;
261   
262    if(verbose)
263    {
264        if(use_pvfs2_lib)
265        {
266            printf("\tClosing [%lld]\n", lld(stFileRef->handle));
267        }
268        else
269        {
270            printf("\tClosing [%d]\n", stFileRef->fd);
271        }
272    }
273
274    if(use_pvfs2_lib)
275    {
276        printf("close_file not implemented for PVFS2 API\n");
277        return(-ENODATA);
278    }
279    else
280    {
281        ret = close(stFileRef->fd);
282        if(ret != 0)
283        {
284            ret = errno; /* Save the error number */
285            if(verbose)
286            {
287                print_error("\tError closing file descriptor [%d].\n"   
288                            "\t\tError Number = [%d]\n"
289                            "\t\tError Desc   = [%s]\n",
290                            stFileRef->fd,
291                            errno,
292                            strerror(errno));
293            }
294        }
295        stFileRef->fd = 0;
296    }
297    return(ret);
298}
299
300/**
301 * \retval TEST_COMMON_SUCCESS Success
302 * \retval TEST_COMMON_FAIL Failure
303 */
304int stat_file(
305    const char  * fileName,      /**< File Name */
306    struct stat * fileStats,     /**< stat structure to place results from stat call */
307    const int     followLink,    /**< Determines whether to stat link or link target */
308    const int     use_pvfs2_lib, /**< determines use of pvfs2 library */
309    const int     verbose)       /**< Turns on verbose prints if set to non-zero value */
310{
311   int  ret=0;
312   char szPvfsPath[PVFS_NAME_MAX] = "";
313   PVFS_sysresp_lookup  lk_response;
314   PVFS_object_ref      ref;
315   PVFS_sysresp_getattr getattr_response;
316   PVFS_credentials     credentials;
317   PVFS_fs_id           fs_id;
318 
319    if(verbose) { printf("\tPerforming stat on [%s]\n", fileName); }
320   
321    if(use_pvfs2_lib)
322    {
323        ret = PVFS_util_resolve(fileName,
324                                &fs_id,
325                                szPvfsPath,
326                                sizeof(szPvfsPath));
327
328        if (ret < 0)
329        {
330            print_error("Error: could not find file system for [%s] in pvfstab\n", fileName);
331            return(TEST_COMMON_FAIL);
332        }
333
334        PVFS_util_gen_credentials(&credentials);
335 
336        if(followLink)
337        {
338            ret = PVFS_sys_lookup(fs_id,
339                                  szPvfsPath,
340                                  &credentials,
341                                  &lk_response,
342                                  PVFS2_LOOKUP_LINK_FOLLOW,
343                                  NULL);
344        }
345        else
346        {
347            ret = PVFS_sys_lookup(fs_id,
348                                  szPvfsPath,
349                                  &credentials,
350                                  &lk_response,
351                                  PVFS2_LOOKUP_LINK_NO_FOLLOW,
352                                  NULL);
353        }
354   
355        if(ret < 0)
356        {
357            PVFS_perror("PVFS_sys_lookup", ret);
358            return(TEST_COMMON_FAIL);
359        }
360     
361        ref.handle = lk_response.ref.handle;
362        ref.fs_id  = fs_id;
363     
364        ret = PVFS_sys_getattr(ref,
365                               PVFS_ATTR_SYS_ALL,
366                               &credentials,
367                               &getattr_response,
368                               NULL);
369
370        if(ret < 0)
371        {                         
372            PVFS_perror("PVFS_sys_getattr", ret);
373            return(TEST_COMMON_FAIL);
374        }
375        copy_pvfs2_to_stat(&getattr_response.attr, fileStats);
376    }
377    else
378    {
379        if(followLink)
380        {
381            ret = stat(fileName, fileStats);
382        }
383        else
384        {
385            ret = lstat(fileName, fileStats);
386        }
387
388        if( ret  != 0)
389        {
390            if(verbose)
391            {
392                print_error("\tError performing stat on [%s].\n"   
393                            "\t\tError Number = [%d]\n"
394                            "\t\tError Desc   = [%s]\n",
395                            fileName,
396                            errno,
397                            strerror(errno));
398            }
399            return(TEST_COMMON_FAIL);
400        }
401    }
402    return(TEST_COMMON_SUCCESS); 
403}
404
405/**
406 * \retval TEST_COMMON_SUCCESS Success
407 * \retval TEST_COMMON_FAIL Failure
408 */
409int open_file(
410    const char      * fileName,    /**< File Name */
411    const int         accessFlags, /**< open access flags */
412    const int         mode,        /**< open mode flags */
413    const int         use_pvfs2_lib,   /**< determines use of pvfs2 library */
414    const int         verbose,     /**< Turns on verbose prints if set to non-zero value */
415    const int         followLink,  /**< Follow symbolic link to target? */
416    struct file_ref * pstFileRef)  /**< File descriptor (or handle) for an open file */
417{
418    int ret=0;
419   
420    if(verbose) { printf("\tOpening [%s]\n", fileName); }
421   
422    if(use_pvfs2_lib)
423    {
424        ret = pvfs2_open(fileName,
425                         accessFlags,
426                         mode,
427                         verbose,
428                         followLink,
429                         pstFileRef);
430
431        if(ret != TEST_COMMON_SUCCESS)
432        {
433            print_error("Unable to open [%s]\n", fileName);
434            return(TEST_COMMON_FAIL); 
435        }     
436    }
437    else
438    {
439        pstFileRef->fd = open(fileName, accessFlags, mode);
440    }
441
442
443    if(pstFileRef->handle == -1 || pstFileRef->fd == -1)
444    {
445        return(TEST_COMMON_FAIL);
446    }
447    return(TEST_COMMON_SUCCESS);
448}
449
450/**
451 * \retval TEST_COMMON_SUCCESS Success
452 * \retval TEST_COMMON_FAIL Failure
453 */
454int create_file(
455    const char * fileName,  /**< File Name */
456    const int    mode,      /**< directory permissions */
457    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
458    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
459{
460    int  ret=0;
461    char szPvfsPath[PVFS_NAME_MAX] = "";
462    PVFS_fs_id fs_id;
463    PVFS_credentials credentials;
464    struct file_ref stFileRef;
465   
466    if(verbose) { printf("\tCreating [%s] using mode [%o]\n", fileName, mode); }
467   
468    if(use_pvfs2_lib)
469    {
470        ret = PVFS_util_resolve(fileName,
471                                &fs_id,
472                                szPvfsPath,
473                                sizeof(szPvfsPath));
474
475        if (ret < 0)
476        {
477            print_error("Error: could not find file system for [%s] in pvfstab\n", fileName);
478            return(TEST_COMMON_FAIL);
479        }
480
481        PVFS_util_gen_credentials(&credentials);
482
483        ret = pvfs2_create_file(szPvfsPath,
484                                fs_id,
485                                &credentials,
486                                mode,
487                                verbose,
488                                &stFileRef);
489
490        if (ret < 0)
491        {
492            print_error("Error: could not create test file [%s]\n", fileName);
493            return(TEST_COMMON_FAIL);
494        }
495    }
496    else
497    {
498        stFileRef.fd = open(fileName, (O_WRONLY|O_CREAT|O_EXCL), mode);
499        if(stFileRef.fd < 0)
500        {
501            print_error("Error: could not create test file [%s]: %s\n",
502                        fileName, strerror(errno));
503            return(TEST_COMMON_FAIL);
504        }
505
506        ret = close(stFileRef.fd);
507        if(ret < 0)
508        {
509            print_error("Error: could not close test file [%s]: %s\n",
510                        fileName, strerror(errno));
511            return(TEST_COMMON_FAIL);
512        }
513    }
514    return(TEST_COMMON_SUCCESS);
515}
516
517/**
518 * \retval  0  SUCCESS
519 * \retval  errno FAILURE using VFS layer
520 * \retval  -ENODATA FAILURE using PVFS2 API
521 */
522int create_directory(
523    const char * directory, /**< Directory Name */
524    const int    mode,      /**< directory permissions */
525    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
526    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
527{
528    int  ret=0;
529    char cmd[PATH_MAX] = "";
530   
531    if(verbose) { printf("\tCreating [%s] using mode [%o]:\n", directory, mode); }
532   
533    if(use_pvfs2_lib)
534    {
535        if(verbose)
536        {
537            snprintf(cmd, sizeof(cmd), "%spvfs2-mkdir -m %o %s", pvfsEXELocation, mode, directory);
538        }
539        else
540        {
541            /* Make sure nothing prints to STDOUT/STDERR if verbose mode is off */
542            snprintf(cmd, sizeof(cmd), "%spvfs2-mkdir -m %o %s >/dev/null 2>&1", pvfsEXELocation, mode, directory);
543        }
544        ret = system(cmd);
545        if(ret != 0)
546        {
547            ret = -ENODATA; /* Set to a generic return code, since errno not
548                             * appropriate for command line utility */
549            if(verbose) 
550            {
551                print_error("\tUnable to create [%s] using mode [%o]\n", directory, mode);
552            }
553        }
554    }
555    else
556    {
557        ret = mkdir(directory, mode);
558        if(ret != 0)
559        {
560            ret = errno; /* Save errno for return */
561            if(verbose) 
562            {
563                print_error("\tUnable to create [%s] using mode [%o]\n"
564                            "\t\tError Number = [%d]\n"
565                            "\t\tError Desc   = [%s]\n",
566                            directory,
567                            mode,
568                            errno,
569                            strerror(errno));
570            }
571        }
572    }
573
574   return ret;
575}
576
577/**
578 * \retval  0  SUCCESS
579 * \retval  errno FAILURE using VFS layer
580 * \retval  -ENODATA FAILURE using PVFS2 API
581 */
582int remove_directory(
583    const char * directory, /**< Directory Name */
584    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
585    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
586{
587    int  ret=0;
588    char cmd[PATH_MAX] = "";
589   
590    if(verbose) { printf("\tRemoving [%s]\n", directory); }
591   
592    if(use_pvfs2_lib)
593    {
594        if(verbose)
595        {
596            snprintf(cmd, sizeof(cmd), "%spvfs2-rm %s", pvfsEXELocation, directory);
597        }
598        else
599        {
600            snprintf(cmd, sizeof(cmd), "%spvfs2-rm %s  >/dev/null 2>&1", pvfsEXELocation, directory);
601        }
602
603        ret = system(cmd);
604        if(ret != 0)
605        {
606            ret = -ENODATA; /* Save the error number */
607            if(verbose)
608            {
609                print_error("\tUnable to remove [%s]\n", directory);
610            }
611        }
612    }
613    else
614    {
615        ret = rmdir(directory);
616        if(ret != 0)
617        {
618            ret = errno; /* Save the error number */
619            if(verbose)
620            {
621                print_error("\tUnable to remove [%s]\n"
622                            "\t\tError Number = [%d]\n"
623                            "\t\tError Desc   = [%s]\n",
624                            directory,
625                            errno,
626                            strerror(errno));
627            }
628        }
629    }
630    return ret;
631}
632
633/**
634 * \retval  0  SUCCESS
635 * \retval  errno FAILURE using VFS layer
636 * \retval  -ENODATA FAILURE using PVFS2 API
637 */
638int remove_symlink(
639    const char * linkName,  /**< Symlink Name */
640    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
641    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
642{
643    /* the remove_file() function should work fine on symbolic links */
644    return (remove_file(linkName, use_pvfs2_lib, verbose));
645}
646
647
648/**
649 * \retval  0  SUCCESS
650 * \retval  errno FAILURE using VFS layer
651 * \retval  -ENODATA FAILURE using PVFS2 API
652 */
653int remove_file(
654    const char * fileName,  /**< File Name */
655    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
656    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
657{
658    int  ret=0;
659    char cmd[PATH_MAX] = "";
660   
661    if(verbose) { printf("\tRemoving [%s]\n", fileName); }
662   
663    if(use_pvfs2_lib)
664    {
665        if(verbose)
666        {
667            snprintf(cmd, sizeof(cmd), "%spvfs2-rm %s", pvfsEXELocation, fileName);
668        }
669        else
670        {
671            snprintf(cmd, sizeof(cmd), "%spvfs2-rm %s >/dev/null 2>&1", pvfsEXELocation, fileName);
672        }
673        ret = system(cmd);
674        if(ret != 0)
675        {
676            ret = -ENODATA;
677            if(verbose) { print_error("\tUnable to remove [%s]\n", fileName); }
678        }
679    }
680    else
681    {
682        ret = unlink(fileName);
683        if(ret != 0)
684        {
685            ret = errno;
686            if(verbose)
687            {
688                print_error("\tUnable to remove [%s]\n"
689                            "\t\tError Number = [%d]\n"
690                            "\t\tError Desc   = [%s]\n",
691                            fileName,
692                            errno,
693                            strerror(errno));
694            }
695        }
696    }
697    return(ret);
698}
699
700/**
701 * \retval  0  SUCCESS
702 * \retval  errno FAILURE using VFS layer
703 * \retval  -ENODATA FAILURE using PVFS2 API
704 */
705int change_mode(
706    const char * fileName,  /**< Target Name */
707    const int    mode,      /**< Mode to set  */
708          int*   error_code, /**< Not implemented, should be return code from call */
709    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
710    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
711{
712    int  ret=0;
713    char cmd[PATH_MAX] = "";
714   
715    if(verbose) { printf("\tChanging mode on [%s] to [%o]\n", fileName, mode); }
716   
717    if(use_pvfs2_lib)
718    {
719        if(verbose)
720        {
721            snprintf(cmd, sizeof(cmd), "%spvfs2-chmod %o %s",
722                     pvfsEXELocation, mode, fileName);
723        }
724        else
725        {
726            snprintf(cmd, sizeof(cmd), "%spvfs2-chmod %o %s >/dev/null 2>&1",
727                     pvfsEXELocation, mode, fileName);
728        }
729        ret = system(cmd);
730        if(ret != 0)
731        {
732            ret = -ENODATA;
733            if(verbose)
734            {
735                print_error("\tUnable to chmod [%s] to [%o]\n", fileName, mode);
736            }
737        }
738    }
739    else
740    {
741        ret = chmod(fileName, mode);
742        if(ret != 0)
743        {
744            ret = errno; /* Save the error number */
745            if(verbose)
746            {
747                print_error("\tUnable to chmod [%s] to [%o]\n"
748                            "\t\tError Number = [%d]\n"
749                            "\t\tError Desc   = [%s]\n",
750                            fileName,
751                            mode,
752                            errno,
753                            strerror(errno));
754            }
755        }
756    }
757    return(ret);
758}
759
760/**
761 * \retval TEST_COMMON_SUCCESS Success
762 * \retval TEST_COMMON_FAIL Failure
763 */
764int change_owner(
765    const char * fileName,  /**< Target Name */
766    const char * ownerName, /**< Owner Name to change to */ 
767    const uid_t  owner_id,  /**< The uid of the new owner  */     
768    const char * groupName, /**< group Name to change to */
769    const gid_t  group_id,  /**< The uid of the new group */
770    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
771    const int    verbose)   /**< Turns on verbose prints if set to non-zero value */
772{
773    int  ret=0;
774    char cmd[PATH_MAX] = "";
775   
776    if(verbose)
777    {
778        if(use_pvfs2_lib)
779        {
780            printf("\tChanging owner on [%s] to [%s].[%s]\n", fileName, ownerName, groupName);
781        }
782        else
783        {
784            printf("\tChanging owner on [%s] to [%d].[%d]\n", fileName, owner_id, group_id);
785        }
786    }
787   
788    if(use_pvfs2_lib)
789    {
790        /* Determine if we need to use sudo to change owner/group */
791        if(geteuid() != owner_id ||
792           geteuid() != group_id)
793        {
794            if(verbose)
795            {
796                snprintf(cmd, sizeof(cmd), "sudo %spvfs2-chown %s %s %s", 
797                         pvfsEXELocation, ownerName, groupName, fileName);
798            }
799            else
800            {
801                snprintf(cmd, sizeof(cmd), "sudo %spvfs2-chown %s %s %s >/dev/null 2>&1", 
802                         pvfsEXELocation, ownerName, groupName, fileName);
803            }
804        }
805        else
806        {
807            if(verbose)
808            {
809                snprintf(cmd, sizeof(cmd), "%spvfs2-chown %s %s %s",
810                         pvfsEXELocation, ownerName, groupName, fileName);
811            }
812            else
813            {
814                snprintf(cmd, sizeof(cmd), "%spvfs2-chown %s %s %s >/dev/null 2>&1",
815                         pvfsEXELocation, ownerName, groupName, fileName);
816            }
817        }
818    }
819    else
820    {
821   
822        /* Determine if we need to use sudo to change owner/group */
823        if(geteuid() != owner_id &&
824           geteuid() != group_id)
825        {
826            if(verbose)
827            {
828                snprintf(cmd, sizeof(cmd), "sudo chown %s:%s %s",
829                         ownerName, groupName, fileName);
830            }
831            else
832            {
833                snprintf(cmd, sizeof(cmd), "sudo chown %s:%s %s >/dev/null 2>&1",
834                         ownerName, groupName, fileName);
835            }
836        }
837        else
838        {
839            if(verbose)
840            {
841                snprintf(cmd, sizeof(cmd), "chown %s:%s %s",
842                         ownerName, groupName, fileName);
843            }
844            else
845            {
846                snprintf(cmd, sizeof(cmd), "chown %s:%s %s >/dev/null 2>&1",
847                         ownerName, groupName, fileName);
848            }
849        }
850    }
851 
852    ret = system(cmd);
853    if(ret != 0)
854    {
855        if(use_pvfs2_lib)
856        {
857            if(verbose)
858            {
859                print_error("\tUnable to change owner of [%s] to [%s].[%s]\n"
860                            "\t\tError Number = [%d]\n"
861                            "\t\tError Desc   = [%s]\n",
862                            fileName,
863                            ownerName,
864                            groupName,
865                            errno,
866                            strerror(errno));
867            }
868        }
869        else
870        {
871            if(verbose)
872            {
873                print_error("\tUnable to change owner of [%s] to [%d]\n"
874                            "\t\tError Number = [%d]\n"
875                            "\t\tError Desc   = [%s]\n",
876                            fileName,
877                            owner_id,
878                            errno,
879                            strerror(errno));
880            }         
881        }
882        return(TEST_COMMON_FAIL);
883    }     
884    return(TEST_COMMON_SUCCESS);
885}
886
887/**
888 * \retval  0  SUCCESS
889 * \retval  errno FAILURE using VFS layer
890 * \retval  -ENODATA FAILURE using PVFS2 API
891 */
892int change_group(
893    const char * fileName,  /**< Target Name */
894    const uid_t  group_id,  /**< The gid of the new group */
895    const int    use_pvfs2_lib, /**< determines use of pvfs2 library */
896    const int    verbose)   /**< Turns on verbose prints if set to non-zero value*/
897{
898    int ret=0;
899   
900    if(verbose) { printf("\tChanging group on [%s] to [%o]\n", fileName, group_id); }
901   
902    if(use_pvfs2_lib)
903    {
904        if(verbose)
905        {
906            printf("\tchange_group not appropriate for PVFS2 API. call change_owner\n");
907        }
908        return(-ENODATA);
909    }
910    else
911    {
912        ret = chown(fileName, -1, group_id);
913
914        if(ret == -1)
915        {
916            ret = errno; /* save the error number */
917            if(verbose)
918            {
919                print_error("\tUnable to change group of [%s] to [%d]\n"
920                            "\t\tError Number = [%d]\n"
921                            "\t\tError Desc   = [%s]\n",
922                            fileName,
923                            group_id,
924                            errno,
925                            strerror(errno));
926            }
927        }     
928    }
929 
930    return ret;     
931}
932
933void print_stats(
934    const struct stat stats, /**< Structure contains file stats */
935    const int verbose)       /**< Turns on verbose prints if set to non-zero value */
936{
937    char a_time[100], m_time[100], c_time[100];
938    struct passwd * user;
939    struct group  * gid;
940
941    if(verbose)
942    {
943        snprintf(a_time, sizeof(a_time), "%s", ctime(&stats.st_atime));
944        snprintf(m_time, sizeof(m_time), "%s", ctime(&stats.st_mtime));
945        snprintf(c_time, sizeof(c_time), "%s", ctime(&stats.st_ctime));
946     
947        a_time[strlen(a_time)-1] = 0;
948        m_time[strlen(m_time)-1] = 0;
949        c_time[strlen(c_time)-1] = 0;
950     
951        user  = getpwuid(stats.st_uid);
952        gid   = getgrgid(stats.st_gid);
953     
954        printf("\t st_size    = %llu\n",     llu(stats.st_size));
955        printf("\t st_ino     = %llu\n",     llu(stats.st_ino));
956        printf("\t atime      = %lu (%s)\n", stats.st_atime, a_time);
957        printf("\t mtime      = %lu (%s)\n", stats.st_mtime, m_time);
958        printf("\t ctime      = %lu (%s)\n", stats.st_ctime, c_time);
959        printf("\t st_mode    = 0%o\n",      stats.st_mode);
960        printf("\t st_uid     = %d (%s)\n",  stats.st_uid, user->pw_name);
961        printf("\t st_gid     = %d (%s)\n",  stats.st_gid, gid->gr_name);
962    }
963}
964
965/**
966 * \retval  0  SUCCESS
967 * \retval  -errno FAILURE using PVFS2 API
968 */
969int pvfs2_open(
970    const char * fileName, /**< File Name         */
971    const int accessFlags, /**< open LDAP access flags */
972    const int mode,        /**< open LDAP mode flags   */
973    const int verbose,     /**< Turns on verbose prints if set to non-zero value */
974    const int followLink,  /**< Follow symbolic link to target */
975    struct file_ref * pstFileRef)  /**< File descriptor (or handle) for an open file */
976{
977    int                  ret=0;
978    char                 szPvfsPath[PVFS_NAME_MAX] = "";
979    PVFS_fs_id           fs_id;
980    PVFS_credentials     credentials;
981    PVFS_sysresp_lookup  resp_lookup;
982
983    /* Initialize memory */
984    memset(&fs_id,        0, sizeof(fs_id));
985    memset(&credentials,  0, sizeof(credentials));
986    memset(&resp_lookup,  0, sizeof(resp_lookup));
987
988    ret = PVFS_util_resolve(fileName,
989                            &fs_id,
990                            szPvfsPath,
991                            sizeof(szPvfsPath));
992
993    if (ret < 0)
994    {
995        print_error("Error: could not find file system for [%s] in pvfstab\n", fileName);
996        return(ret);
997    }
998
999    PVFS_util_gen_credentials(&credentials);
1000
1001    if(followLink)
1002    {
1003        ret = PVFS_sys_lookup(fs_id,
1004                              szPvfsPath,
1005                              &credentials,
1006                              &resp_lookup,
1007                              PVFS2_LOOKUP_LINK_FOLLOW,
1008                              NULL);
1009    }
1010    else
1011    {
1012        ret = PVFS_sys_lookup(fs_id,
1013                              szPvfsPath,
1014                              &credentials,
1015                              &resp_lookup,
1016                              PVFS2_LOOKUP_LINK_NO_FOLLOW,
1017                              NULL);
1018    }
1019
1020    if( (ret < 0) &&
1021        (ret != -PVFS_ENOENT))
1022    {
1023        PVFS_perror("PVFS_sys_lookup", ret);
1024        return(ret);
1025    }
1026   
1027    /* If the file doesn't exist, and the O_CREAT flag is specified */
1028    if( (ret == -PVFS_ENOENT) &&
1029        (accessFlags & O_CREAT))
1030    {
1031        ret = pvfs2_create_file(szPvfsPath,
1032                                fs_id,
1033                                &credentials,
1034                                mode,
1035                                verbose,
1036                                pstFileRef);
1037
1038        if(ret != 0)
1039        {
1040            print_error("Unable to create [%s]\n", fileName);
1041            return(ret);
1042        }                             
1043    }
1044
1045    return ret;
1046}
1047
1048
1049/**
1050 * \retval  0  SUCCESS
1051 * \retval  -errno FAILURE using PVFS2 API
1052 */
1053int pvfs2_create_file(const char             * fileName,    /**< File Name */
1054                      const PVFS_fs_id         fs_id,       /**< PVFS2 files sytem ID for the fileName parm */
1055                      const PVFS_credentials * credentials, /**< Struct with user/group permissions for operation */
1056                      const int                mode,        /**< open mode flags */
1057                      const int                verbose,     /**< Turns on verbose prints if set to non-zero value */
1058                      struct file_ref        * pstFileRef)  /**< File descriptor (or handle) for an open file*/
1059{
1060    int                 ret=0;
1061    char                szParentDir[PVFS_NAME_MAX] = "";
1062    char                szBaseName[PVFS_NAME_MAX]  = "";
1063    char              * parentDirectory            = NULL;
1064    char              * baseName                   = NULL;
1065    PVFS_sysresp_create resp_create;
1066    PVFS_object_ref     parent_ref;
1067    PVFS_sys_attr       attr;
1068    PVFS_sysresp_lookup resp_lookup;
1069
1070    memset(&resp_create, 0, sizeof(resp_create));
1071    memset(&resp_lookup, 0, sizeof(resp_lookup));
1072    memset(&parent_ref,  0, sizeof(parent_ref));
1073    memset(&attr,        0, sizeof(attr));
1074   
1075    attr.owner = credentials->uid;
1076    attr.group = credentials->gid;
1077    attr.perms = PVFS_util_translate_mode(mode,0);
1078    attr.atime = time(NULL);
1079    attr.mtime = attr.atime;
1080    attr.ctime = attr.atime;
1081    attr.mask  = PVFS_ATTR_SYS_ALL_SETABLE; /* All things setable after file
1082                                            * creation
1083                                            */
1084
1085    /* Copy the file name into structures to be passed to dirname and basename
1086     * These calls change the parameter, so we don't want to mess with original
1087     */
1088    strcpy(szParentDir, fileName);
1089    strcpy(szBaseName,  fileName);
1090   
1091    parentDirectory = dirname(szParentDir);
1092    baseName  = basename(szBaseName);
1093
1094    ret = PVFS_sys_lookup(fs_id,
1095                          parentDirectory,
1096                          (PVFS_credentials *) credentials,
1097                          &resp_lookup,
1098                          PVFS2_LOOKUP_LINK_FOLLOW,
1099                          NULL);
1100 
1101    if (ret < 0)
1102    {
1103        PVFS_perror("PVFS_sys_lookup", ret);
1104        return(ret);
1105    }
1106   
1107    parent_ref.handle = resp_lookup.ref.handle;
1108    parent_ref.fs_id  = fs_id;
1109
1110    /* Copy the file name into structures to be passed to dirname and basename
1111     * These calls change the parameter, so we don't want to mess with original
1112     */
1113    strcpy(szParentDir, fileName);
1114    strcpy(szBaseName,  fileName);
1115   
1116    parentDirectory = dirname(szParentDir);
1117    baseName  = basename(szBaseName);
1118   
1119    ret = PVFS_sys_create(baseName,
1120                          parent_ref,     /* handle & fs_id of parent  */
1121                          attr,
1122                          (PVFS_credentials *) credentials,
1123                          NULL,           /* Accept default distribution for fs */
1124                          &resp_create,
1125                          NULL,
1126                          NULL);
1127
1128    if (ret < 0)
1129    {
1130        PVFS_perror("PVFS_sys_create", ret);
1131        return(ret);
1132    }
1133
1134    pstFileRef->handle = resp_create.ref.handle;
1135   
1136    return(ret);
1137}
1138
1139void copy_pvfs2_to_stat(const PVFS_sys_attr * attr,
1140                        struct stat         * fileStats)
1141{
1142    /* We blindly say we have all the data in the attr struct without checking
1143    * for validity against the masks
1144    */
1145    memcpy(&fileStats->st_atime, &attr->atime, sizeof(fileStats->st_atime));
1146    memcpy(&fileStats->st_mtime, &attr->mtime, sizeof(fileStats->st_mtime));
1147    memcpy(&fileStats->st_ctime, &attr->ctime, sizeof(fileStats->st_ctime));
1148    memcpy(&fileStats->st_uid,   &attr->owner, sizeof(fileStats->st_uid));
1149    memcpy(&fileStats->st_gid,   &attr->group, sizeof(fileStats->st_gid));
1150    memcpy(&fileStats->st_mode,  &attr->perms, sizeof(fileStats->st_mode));   
1151}
1152
1153/**
1154 * \retval  0  SUCCESS
1155 * \retval  -1 Generic FAILURE
1156 * \retval  -errno FAILURE using PVFS2 API
1157 */
1158int lookup_parent(char             * filename,    /**< File Name */
1159                  PVFS_fs_id         fs_id,       /**< PVFS2 files sytem ID for the fileName parm */
1160                  PVFS_credentials * credentials, /**< Struct with user/group permissions for operation */
1161                  PVFS_handle      * handle,      /**< PVFS2 handle  */
1162                  int                verbose)     /**< Turns on verbose prints if set to non-zero value */
1163{
1164    int                 ret=0;
1165    char                szSegment[PVFS_SEGMENT_MAX] = "";
1166    PVFS_sysresp_lookup resp_look;
1167
1168    memset(&resp_look, 0, sizeof(PVFS_sysresp_lookup));
1169
1170    if ( get_base_dir(filename, szSegment, sizeof(szSegment)) )
1171    {
1172        if (filename[0] != '/' && verbose)
1173        {
1174            print_error("Invalid filename [%s] (no leading '/')\n", filename);
1175        }
1176        *handle = PVFS_HANDLE_NULL;
1177        return TEST_COMMON_FAIL;
1178    }
1179
1180    ret = PVFS_sys_lookup(fs_id,
1181                          szSegment,
1182                          credentials,
1183                          &resp_look,
1184                          PVFS2_LOOKUP_LINK_FOLLOW,
1185                          NULL);
1186    if (ret < 0)
1187    {
1188        if(verbose)
1189        {
1190            print_error("Lookup failed on [%s]\n", szSegment);
1191        }
1192        *handle = PVFS_HANDLE_NULL;
1193        return(ret);
1194    }
1195
1196    *handle = resp_look.ref.handle;
1197    return 0;
1198}
1199
1200/**
1201 * \retval TEST_COMMON_SUCCESS Success
1202 * \retval TEST_COMMON_FAIL     Failure
1203 * \note
1204 * Example inputs and outputs/return values:
1205 *
1206 * pathname: /tmp         - out_base_dir: /         - returns  0
1207 * pathname: /tmp/foo     - out_base_dir: /tmp      - returns  0
1208 * pathname: /tmp/foo/bar - out_base_dir: /tmp/foo  - returns  0
1209 *
1210 * invalid pathname input examples:
1211 * pathname: /            - out_base_dir: undefined - returns -1
1212 * pathname: NULL         - out_base_dir: undefined - returns -1
1213 * pathname: foo          - out_base_dir: undefined - returns -1
1214 */
1215int get_base_dir(
1216    char * pathName,    /**< absolute path name */
1217    char * baseDir,     /**< pointer to memory to place discovered base directory */
1218    int    baseDirSize) /**< maximum size base directory could be */
1219{
1220    int    ret = TEST_COMMON_SUCCESS,
1221           len = 0;
1222    char * start = NULL,   
1223         * end   = NULL;
1224
1225    /* Validate parameters */
1226    if (pathName && baseDir && baseDirSize)
1227    {
1228        if ( (strcmp(pathName,"/") == 0) ||
1229             (pathName[0]          != '/') )
1230        {
1231            return TEST_COMMON_FAIL;
1232        }
1233
1234        start = pathName;
1235        end = (char *)(pathName + strlen(pathName));
1236
1237        while( end              &&
1238               (end > start) &&
1239               (*(--end) != '/') );
1240
1241        /*
1242          get rid of trailing slash unless we're handling
1243          the case where parent is the root directory
1244          (in root dir case, len == 1)
1245        */
1246        len = ++end - start;
1247        if (len != 1)
1248        {
1249            len--;
1250        }
1251        if (len < baseDirSize)
1252        {
1253            memcpy(baseDir, start, len);
1254            baseDir[len] = '\0';
1255        }
1256    }
1257    else
1258    {
1259        ret = TEST_COMMON_FAIL;   
1260    }
1261    return ret;
1262}
1263
1264/**
1265 * \retval TEST_COMMON_SUCCESS Success
1266 * \retval TEST_COMMON_FAIL Failure
1267 * \note
1268 * Example inputs and outputs/return values:                     
1269 *                                                               
1270 * pathname: /tmp/foo     - out_base_dir: foo       - returns  0
1271 * pathname: /tmp/foo/bar - out_base_dir: bar       - returns  0
1272 *                                                               
1273 * invalid pathname input examples:                             
1274 * pathname: /            - out_base_dir: undefined - returns -1
1275 * pathname: NULL         - out_base_dir: undefined - returns -1
1276 * pathname: foo          - out_base_dir: undefined - returns -1
1277 *
1278 */
1279int remove_base_dir(
1280    char * pathName,    /**< absolute path name */
1281    char * baseDir,     /**< pointer to memory to place discovered base directory */
1282    int    baseDirSize) /**< maximum size base directory could be */
1283{
1284    int    len       = 0;
1285    char * start  = NULL,   
1286         * end    = NULL,
1287         * endRef = NULL;
1288
1289    /* Validate parameters */
1290    if (pathName && baseDir && baseDirSize)
1291    {
1292        if ( (strcmp(pathName, "/") == 0) ||
1293             (pathName[0]           != '/') )
1294        {
1295            return TEST_COMMON_FAIL;
1296        }
1297
1298        start = pathName;
1299        end = (char *) (pathName + strlen(pathName));
1300        endRef = end;
1301
1302        while( end             && 
1303              (end > start) &&
1304              (*(--end) != '/') );
1305
1306        len = endRef - ++end;
1307        if (len < baseDirSize)
1308        {
1309            memcpy(baseDir, end, len);
1310            baseDir[len] = '\0';
1311        }
1312    }
1313    else
1314    {
1315        return TEST_COMMON_FAIL;
1316    }
1317   
1318    return TEST_COMMON_SUCCESS;
1319}
1320
1321/**
1322 * \retval  0  SUCCESS
1323 * \retval  errno FAILURE using VFS layer
1324 * \retval  -ENODATA FAILURE using PVFS2 API
1325*/
1326int create_symlink(
1327    const char * linkName,   /**< The link name to create */
1328    const char * linkTarget, /**< The string the link will contain */
1329    const int    use_pvfs2_lib,  /**< determines use of pvfs2 library */
1330    const int    verbose)    /**< Turns on verbose prints if set to non-zero value */
1331{
1332    int  ret=0;
1333    char cmd[PATH_MAX] = "";
1334   
1335    if(verbose) { printf("\tCreating symlink [%s] to [%s]:\n", linkName, linkTarget); }
1336   
1337    if(use_pvfs2_lib)
1338    {
1339        if(verbose)
1340        {
1341            snprintf(cmd, sizeof(cmd), "%spvfs2-ln -s %s %s",
1342                     pvfsEXELocation, linkTarget, linkName);
1343        }
1344        else
1345        {
1346            snprintf(cmd, sizeof(cmd), "%spvfs2-ln -s %s %s >/dev/null 2>&1",
1347                     pvfsEXELocation, linkTarget, linkName);
1348        }
1349        ret = system(cmd);
1350        if(ret != 0)
1351        {
1352            ret = -ENODATA;
1353            if(verbose)
1354            {
1355                print_error("\tUnable to create symlink [%s] to [%s]\n",
1356                            linkName,
1357                            linkTarget);     
1358            }
1359        }
1360    }
1361    else
1362    {
1363        ret = symlink(linkTarget, linkName);
1364        if(ret != 0)
1365        {
1366            ret = errno;
1367            if(verbose)
1368            {
1369                print_error("\tUnable to create symlink [%s] to [%s]\n"
1370                            "\t\tError Number = [%d]\n"
1371                            "\t\tError Desc   = [%s]\n",
1372                            linkName,
1373                            linkTarget,
1374                            errno,
1375                            strerror(errno));     
1376            }
1377        }
1378    }
1379    return ret;
1380}
1381
1382/**
1383 * Parses command line options for the most common arguments
1384 * /retval TEST_COMMON_SUCCESS Indicates successfull return
1385 * /retval TEST_COMMON_FAIL    Indicates failure return
1386 */
1387int parse_common_args(
1388    int argc,                    /**< Number of arguments */
1389    char** argv,                 /**< argument list */
1390    struct common_options* opts) /**< structure to hold the common arguments */
1391{
1392    int ret          = 0;
1393    int option_index = 0;
1394    char *cur_option = NULL;
1395
1396    static struct option long_opts[] =
1397    {
1398        {"help",0,0,0},
1399        {"directory",1,0,0},
1400        {"use-lib",0,0,0},
1401        {"print-results",0,0,0},
1402        {"verbose",0,0,0},
1403        {"network-proto",1,0,0},
1404        {"port",1,0,0},
1405        {"fs-name",1,0,0},
1406        {"hostname",1,0,0},
1407        {"exe-path",1,0,0},
1408        {0,0,0,0}
1409    };
1410
1411    memset(opts, 0, sizeof(struct common_options));
1412
1413    while((ret = getopt_long_only(argc, argv, "", long_opts, &option_index)) != -1)
1414    {
1415        switch (ret)
1416        {
1417            case 0:
1418                 cur_option = (char*)long_opts[option_index].name;
1419   
1420                 if(strcmp("help", cur_option) == 0)
1421                 {
1422                     display_common_usage(argv[0]);
1423                     return(TEST_COMMON_FAIL);
1424                 }
1425                 if(strcmp("verbose", cur_option) == 0)
1426                 {
1427                     opts->verbose      = 1;
1428                     opts->printResults = 1;
1429                 }
1430                 if(strcmp("print-results", cur_option) == 0)
1431                 {
1432                     opts->printResults = 1;
1433                 }
1434                 if(strcmp("directory", cur_option) == 0)
1435                 {
1436                     opts->directory = (char*) malloc(strlen(optarg)+1);
1437                     assert(opts->directory);
1438                     strcpy(opts->directory, optarg);
1439                 }
1440                 if(strcmp("hostname", cur_option) == 0)
1441                 {
1442                     opts->hostname = (char*) malloc(strlen(optarg)+1);
1443                     assert(opts->hostname);
1444                     strcpy(opts->hostname, optarg);
1445                 }
1446                 if(strcmp("fs-name", cur_option) == 0)
1447                 {
1448                     opts->fsname = (char*) malloc(strlen(optarg)+1);
1449                     assert(opts->fsname);
1450                     strcpy(opts->fsname, optarg);
1451                 }
1452                 if(strcmp("network-proto", cur_option) == 0)
1453                 {
1454                     opts->networkProto = (char*) malloc(strlen(optarg)+1);
1455                     assert(opts->networkProto);
1456                     strcpy(opts->networkProto, optarg);
1457                 }
1458                 if(strcmp("exe-path", cur_option) == 0)
1459                 {
1460                     opts->exePath = (char*) malloc(strlen(optarg)+1);
1461                     assert(opts->exePath);
1462                     strcpy(opts->exePath, optarg);
1463                 }
1464                 if(strcmp("port", cur_option) == 0)
1465                 {
1466                    opts->port = atoi(optarg);
1467                 }
1468                 if(strcmp("use-lib", cur_option) == 0)
1469                 {
1470                     opts->use_pvfs2_lib = 1;
1471                 }
1472                 break;
1473        }
1474    }
1475   
1476    if(opts->directory == NULL)
1477    {
1478        display_common_usage(argv[0]);
1479        return(TEST_COMMON_FAIL);
1480    }
1481
1482    /* defaults values for options */
1483    if(opts->fsname == NULL)
1484    {
1485        opts->fsname = (char *) malloc(strlen("pvfs2-fs")+1);
1486        strncpy(opts->fsname, "pvfs2-fs", strlen("pvfs2-fs"));
1487    }
1488    if(opts->port == 0)
1489    {
1490        opts->port=3334;
1491    }
1492    if(opts->networkProto == NULL)
1493    {
1494        opts->networkProto= (char *) malloc(strlen("tcp")+1);
1495        strncpy(opts->networkProto, "tcp", strlen("tcp"));
1496    }
1497    if(opts->exePath != NULL)
1498    {
1499        strcat(opts->exePath, "/"); /* Make sure it ends with "/" */
1500        ret = set_util_path(opts->exePath);
1501       
1502        if(ret != TEST_COMMON_SUCCESS)
1503        {
1504            print_error("Unable to set executable path location to [%s].\n", opts->exePath);
1505            exit(1);
1506        }
1507    }
1508
1509    if(opts->verbose)
1510    {
1511        printf("Common Options: "
1512               "\t--directory      = [%s]\n"
1513               "\t--use-lib        = [%d]\n"
1514               "\t--print-results  = [%d]\n"
1515               "\t--verbose        = [%d]\n"
1516               "\t--network-proto  = [%s]\n"
1517               "\t--port           = [%d]\n"
1518               "\t--fs-name        = [%s]\n"
1519               "\t--hostname       = [%s]\n"
1520               "\t--exe-path       = [%s]\n",
1521               (opts->directory==NULL)?"NULL":opts->directory,
1522               opts->use_pvfs2_lib,
1523               opts->printResults,
1524               opts->verbose,
1525               opts->networkProto,
1526               opts->port,
1527               opts->fsname,
1528               opts->hostname,
1529               (opts->exePath==NULL)?"NULL":opts->exePath);
1530    }
1531    return(TEST_COMMON_SUCCESS);
1532}
1533
1534/** Displays common argument lists */
1535void display_common_usage(char* exeName)
1536{
1537    fprintf(stderr, "%s:\n", exeName);
1538    fprintf(stderr, "COMMON mandatory arguments:\n"
1539        "  --directory     : name of file/directory to open\n");
1540    fprintf(stderr, "COMMON optional arguments:\n"
1541        "  --help          : prints help\n"
1542        "  --verbose       : turns on verbose output\n"
1543        "  --print-results : Always prints results (PASS or FAIL)\n"
1544        "  --use-lib       : uses the pvfs2 API instead of the kernel module\n"
1545        "  --hostname      : PVFS2 metaserver\n"
1546        "  --fs-name       : PVFS2 filesystem name\n"
1547        "  --network-proto : PVFS2 network protocol to use. Defaults to tcp\n"
1548        "  --port          : PVFS2 port for filesystem. Defaults to 3334\n"
1549        "  --exe-path      : Path where PVFS2 utilities reside\n");
1550}
1551/*
1552 * Local variables:
1553 *  c-indent-level: 4
1554 *  c-basic-offset: 4
1555 * End:
1556 *
1557 * vim: ts=8 sts=4 sw=4 expandtab
1558 */
1559
Note: See TracBrowser for help on using the browser.