nfs-ganesha 1.4

PrintFh.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  *
00004  * Copyright CEA/DAM/DIF  (2008)
00005  * contributeur : Philippe DENIEL   philippe.deniel@cea.fr
00006  *                Thomas LEIBOVICI  thomas.leibovici@cea.fr
00007  *
00008  *
00009  * This program is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 3 of the License, or (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00022  *
00023  * ---------------------------------------*/
00024 
00034 #ifdef HAVE_CONFIG_H
00035 #include "config.h"
00036 #endif
00037 
00038 #include "nfs_init.h"
00039 #include "fsal.h"
00040 #include <stdio.h>
00041 #include <string.h>
00042 #include <pthread.h>
00043 #include <signal.h>             /* for sigaction */
00044 #include <errno.h>
00045 #include <ctype.h>
00046 
00047 #define CMD_BUFFER_SIZE 1024
00048 #define DEFAULT_CONFIG_FILE "/etc/ganesha/"FS_NAME".ganesha.nfsd.conf"
00049 
00050 short HashFileID4(u_int64_t fileid4);
00051 time_t ServerBootTime;
00052 
00053 char ganesha_exec_path[MAXPATHLEN];     /* Just because the symbol is required to compile */
00054 
00055 /* determine buffer type and display it */
00056 void print_buffer(caddr_t buffer, size_t sz_returned)
00057 {
00058   unsigned int i;
00059   int ascii, numeric, hexa;
00060 
00061   /* print the value */
00062   if(sz_returned == 0)
00063     {
00064       printf("(empty)\n");
00065       return;
00066     }
00067 
00068   /* ascii, numeric or hexa ? */
00069   ascii = numeric = hexa = FALSE;
00070 
00071   /* is it ascii ? */
00072   if(strlen(buffer) == sz_returned - 1 || strlen(buffer) == sz_returned)
00073     {
00074       char *str = buffer;
00075       int tmp_is_ascii = TRUE;
00076 
00077       for(i = 0; i < strlen(str); i++)
00078         {
00079           if(!isprint(str[i]) && !isspace(str[i]))
00080             {
00081               tmp_is_ascii = FALSE;
00082               break;
00083             }
00084         }
00085       if(tmp_is_ascii)
00086         ascii = TRUE;
00087     }
00088 
00089   /* is it numeric ? */
00090   if(!ascii)
00091     {
00092       if(sz_returned == 1 || sz_returned == 2 || sz_returned == 4 || sz_returned == 8)
00093         numeric = TRUE;
00094       else
00095         hexa = TRUE;
00096     }
00097 
00098   if(ascii)
00099     {
00100       printf("%s\n", buffer);
00101     }
00102   else if(numeric)
00103     {
00104       if(sz_returned == 1)
00105         printf("%hhu\n", buffer[0]);
00106       else if(sz_returned == 2)
00107         printf("%hu\n", *((unsigned short *)buffer));
00108       else if(sz_returned == 4)
00109         printf("%u\n", *((unsigned int *)buffer));
00110       else if(sz_returned == 8)
00111         printf("%llu\n", *((unsigned long long *)buffer));
00112       else
00113         {
00114           for(i = 0; i < sz_returned; i += 8)
00115             {
00116               unsigned long long *p64 = (unsigned long long *)(buffer + i);
00117               if(i == 0)
00118                 printf("%llu", *p64);
00119               else
00120                 printf(".%llu", *p64);
00121             }
00122           printf("\n");
00123         }
00124     }
00125   else if(hexa)                 /* hexa */
00126     {
00127       printf("0x");
00128       for(i = 0; i < sz_returned; i++)
00129         {
00130           unsigned char val = buffer[i];
00131           printf("%hhX", val);
00132         }
00133       printf("\n");
00134 
00135     }
00136 
00137   return;
00138 }                               /* print_buffer */
00139 
00140 int main(int argc, char *argv[])
00141 {
00142   int c;
00143   int exportid = 0;
00144   char argpath[FSAL_MAX_PATH_LEN];
00145   char filepath[FSAL_MAX_PATH_LEN];
00146   fsal_path_t fsal_path ;
00147   char exec_name[MAXPATHLEN];
00148   char *tempo_exec_name = NULL;
00149   fsal_op_context_t fsal_op_context;
00150   fsal_export_context_t fsal_export_context;
00151   exportlist_t *pexportlist = NULL;
00152   exportlist_t *pexport = NULL;
00153   nfs_start_info_t nfs_start_info;
00154   fsal_status_t fsal_status;
00155   path_str_t fsal_path_lib[NB_AVAILABLE_FSAL];
00156 
00157   fsal_handle_t fsal_handle;
00158   fsal_path_t export_path = FSAL_PATH_INITIALIZER;
00159   FILE * file = NULL ;
00160 
00161   char buff[2*MAXPATHLEN] ;
00162 
00163   char options[] = "h@f:i:p:";
00164   char usage[] = "%s [-h][-f <cfg_path>] {-v 2|3|4 <NFS_FileHandle> | -i <inum>}\n"
00165       "   -h               : prints this help\n"
00166       "   -f <config_file> : sets the ganesha configuration file to be used\n"
00167       "   -p <path>        : FSAL path to the related object\n" 
00168       "   -i <exportid>    : export id to be used for this path\n" ;
00169 
00170   /* Set the server's boot time and epoch */
00171   ServerBootTime = time(NULL);
00172   ServerEpoch    = ServerBootTime;
00173 
00174   SetDefaultLogging("STDERR");
00175 
00176   /* What is the executable file's name */
00177   if((tempo_exec_name = strrchr(argv[0], '/')) != NULL)
00178     strcpy((char *)exec_name, tempo_exec_name + 1);
00179 
00180   strncpy(config_path, DEFAULT_CONFIG_FILE, MAXPATHLEN);
00181 
00182   /* now parsing options with getopt */
00183   while((c = getopt(argc, argv, options)) != EOF)
00184     {
00185       switch (c)
00186         {
00187         case '@':
00188           printf("%s compiled on %s at %s\n", exec_name, __DATE__, __TIME__);
00189           printf("Git HEAD = %s\n", _GIT_HEAD_COMMIT ) ;
00190           printf("Git Describe = %s\n", _GIT_DESCRIBE ) ;
00191           exit(0);
00192           break;
00193 
00194         case 'h':
00195           printf(usage, exec_name);
00196           exit(0);
00197           break;
00198 
00199         case 'f':
00200           strncpy(config_path, optarg, MAXPATHLEN);
00201           break;
00202 
00203         case 'i':
00204           if(sscanf(optarg, "%u", &exportid) != 1)
00205             {
00206               fprintf(stderr, "Invalid object_id %s (base-10 integer expected)\n",
00207                       optarg);
00208               exit(1);
00209             }
00210           break;
00211 
00212         case 'p':
00213           strncpy( argpath, optarg, MAXPATHLEN ) ;
00214           break ;
00215 
00216         case '?':
00217           printf("Unknown option: %c\n", optopt);
00218           printf(usage, exec_name);
00219           exit(1);
00220         }
00221     }
00222 
00223   /* initialize memory and logging */
00224 
00225   nfs_prereq_init("convert_fh", "localhost", NIV_MAJ, "/dev/tty");
00226 
00227   /* Load the FSAL library (if needed) */
00228   if(!FSAL_LoadLibrary((char *)fsal_path_lib))  
00229     {
00230       fprintf(stderr, "NFS MAIN: Could not load FSAL dynamic library %s", (char *)fsal_path_lib[0]);
00231       exit(1);
00232     }
00233 
00234   /* Get the FSAL functions */
00235   FSAL_LoadFunctions();
00236 
00237   /* Get the FSAL consts */
00238   FSAL_LoadConsts();
00239 
00240   /* initialize default parameters */
00241 
00242   nfs_set_param_default();
00243 
00244   /* parse configuration file */
00245 
00246   if(nfs_set_param_from_conf(&nfs_start_info))
00247     {
00248       fprintf(stderr, "Error parsing configuration file '%s'", config_path);
00249       exit(1);
00250     }
00251 
00252   /* check parameters consitency */
00253 
00254   if(nfs_check_param_consistency())
00255     {
00256       fprintf(stderr, "Inconsistent parameters found");
00257       exit(1);
00258     }
00259 
00260   if(!nfs_param.pexportlist)
00261     {
00262       fprintf(stderr, "No export entries found in configuration file !!!\n");
00263       return -1;
00264     }
00265 
00266   pexportlist = nfs_param.pexportlist;
00267 
00268   /* not initialization is needed for converting fileid to path in datacache */
00269   fsal_status = FSAL_Init(&nfs_param.fsal_param);
00270   if(FSAL_IS_ERROR(fsal_status))
00271     {
00272       /* Failed init */
00273       fprintf(stderr, "FSAL library could not be initialized, major=%d minor=%d\n",
00274               fsal_status.major, fsal_status.minor);
00275       exit(1);
00276     }
00277 
00278   if((pexport = nfs_Get_export_by_id(pexportlist, exportid)) == NULL)
00279     {
00280       fprintf(stderr, "NFS FH has exportid %u which is invalid....\n", exportid);
00281       exit(1);
00282     }
00283 
00284   /* INITIALIZING A CLIENT CONTEXT FOR FSAL */
00285 
00286   FSAL_str2path(pexport->fullpath, MAXPATHLEN, &export_path);
00287 
00288   if(FSAL_IS_ERROR
00289      (fsal_status =
00290       FSAL_BuildExportContext(&fsal_export_context, &export_path,
00291                               pexport->FS_specific)))
00292     {
00293       fprintf(stderr, "Error in FSAL_BuildExportContext, major=%u, minor=%u\n",
00294               fsal_status.major, fsal_status.minor);
00295       exit(1);
00296     }
00297 
00298   fsal_status = FSAL_InitClientContext(&fsal_op_context);
00299   if(FSAL_IS_ERROR(fsal_status))
00300     {
00301       /* Failed init */
00302       fprintf(stderr, "Could not init client context... major=%d minor=%d\n",
00303               fsal_status.major, fsal_status.minor);
00304       exit(1);
00305     }
00306 
00307   fsal_status = FSAL_GetClientContext(&fsal_op_context,
00308                                       &fsal_export_context, 0, 0, NULL, 0);
00309 
00310   if(FSAL_IS_ERROR(fsal_status))
00311     {
00312       /* Failed init */
00313       fprintf(stderr, "Could not get cred for uid=%d gid=%d, major=%d minor=%d\n",
00314               getuid(), getgid(), fsal_status.major, fsal_status.minor);
00315       exit(1);
00316     }
00317 
00318   if( ( file = fopen( argpath, "r" ) ) == NULL )
00319     {
00320        fprintf( stderr, "Can't open input file %s\n", argpath ) ;
00321        exit( 1 ) ;
00322     }
00323 
00324   while( fgets( filepath, FSAL_MAX_PATH_LEN, file ) )
00325    { 
00326      /* Chomp */
00327      filepath[strlen(filepath)-1] = '\0'  ;
00328 
00329      if(FSAL_IS_ERROR( FSAL_str2path( filepath, FSAL_MAX_PATH_LEN, &fsal_path)))
00330        {
00331          /* Failed init */
00332          fprintf( stderr, "Could not convert string '%s' to valid fsal_path\n", filepath ) ;
00333          exit(1);
00334        }
00335 
00336      if(FSAL_IS_ERROR((fsal_status = FSAL_lookupPath(&fsal_path, &fsal_op_context, &fsal_handle, NULL))))
00337        {
00338          /* Failed init */
00339          fprintf(stderr, "Could not look up path %s\n", filepath ) ;
00340          continue ;
00341        }
00342 
00343        
00344       snprintHandle(buff, 2 * sizeof(fsal_handle_t) + 1, &fsal_handle);
00345       printf( "%s %s\n", filepath, buff ) ;
00346        
00347    }
00348    
00349   fclose( file ) ;
00350 
00351   exit(0);
00352 }