Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

lcmaps_vo_data.c

Go to the documentation of this file.
00001 /*                                                                                                            
00002  * Copyright (c) 2001 EU DataGrid.                                                                             
00003  * For license conditions see http://www.eu-datagrid.org/license.html                                          
00004  *
00005  * Copyright (c) 2001, 2002 by 
00006  *     Martijn Steenbakkers <martijn@nikhef.nl>,
00007  *     David Groep <davidg@nikhef.nl>,
00008  *     NIKHEF Amsterdam, the Netherlands
00009  */
00010 
00024 /******************************************************************************
00025                              Include header files
00026 ******************************************************************************/
00027 
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <malloc.h>
00031 #include <string.h>
00032 
00033 #include "lcmaps_vo_data.h"
00034 #include "lcmaps_log.h"
00035 
00036 /******************************************************************************
00037                                 Definitions
00038 ******************************************************************************/
00039 #define VO_DATA_WHITESPACE_CHARS " \t\n"
00040 
00041 /******************************************************************************
00042 Function:   lcmaps_createVoData
00043 Description:
00044     Create a VoData structure (store a VO, group, (subgroup,) role, capability
00045     combination). Allocate the memory. To be freed with lcmaps_deleteVoData().
00046 
00047 Parameters:
00048     vo: name of the VO
00049     group: name of the group
00050     subgroup: name of the subgroup (ignored for the moment)
00051     role: the role
00052     capability: the capability (whatever it is)
00053 Returns:
00054     pointer to the VoData structure or NULL
00055 ******************************************************************************/
00077 lcmaps_vo_data_t *
00078 lcmaps_createVoData(
00079     const char * vo,
00080     const char * group,
00081     const char * subgroup,
00082     const char * role,
00083     const char * capability
00084 )
00085 {
00086     lcmaps_vo_data_t * newVoData=NULL;
00087 
00088     newVoData = (lcmaps_vo_data_t *)malloc(sizeof(lcmaps_vo_data_t));
00089     if (!newVoData)
00090     {
00091         lcmaps_log(0,"lcmaps_createVoData(): error in malloc for new VoData structure\n");
00092         return NULL;
00093     }
00094 
00095     newVoData->vo = NULL;
00096     newVoData->group = NULL;
00097     newVoData->subgroup = NULL;
00098     newVoData->role = NULL;
00099     newVoData->capability = NULL;
00100 
00101     if (vo) newVoData->vo = strdup(vo);
00102     if (group) newVoData->group = strdup(group);
00103     if (subgroup) newVoData->subgroup = strdup(subgroup);
00104     if (role) newVoData->role = strdup(role);
00105     if (capability) newVoData->capability = strdup(capability);
00106 
00107     return newVoData;
00108 }
00109 
00110 /******************************************************************************
00111 Function:   lcmaps_deleteVoData
00112 Description:
00113     Delete a VoData structure that was previously created with lcmaps_createVoData().
00114     The pointer to the VoData structure is finally set to NULL;
00115 
00116 Parameters:
00117     vo_data: pointer to a pointer to a VoData structure
00118 
00119 Returns:
00120     0: success
00121     -1: failure (could not delete the structure or no structure found)
00122 ******************************************************************************/
00137 int
00138 lcmaps_deleteVoData(
00139     lcmaps_vo_data_t ** vo_data
00140 )
00141 {
00142     if (!vo_data) {
00143         lcmaps_log(0, "lcmaps_deleteVoData(): empty pointer as input !\n");
00144         return -1;
00145     }
00146 
00147     if ( (*vo_data) )
00148     {
00149         if ( (*vo_data)->vo) free( (*vo_data)->vo );
00150         if ( (*vo_data)->group) free( (*vo_data)->group );
00151         if ( (*vo_data)->subgroup) free( (*vo_data)->subgroup );
00152         if ( (*vo_data)->role) free( (*vo_data)->role );
00153         if ( (*vo_data)->capability) free( (*vo_data)->capability );
00154         free( (*vo_data) );
00155     }
00156     else
00157     {
00158         lcmaps_log_debug(2,"lcmaps_deleteVoData(): no lcmaps_vo_data_t found\n");
00159     }
00160     *vo_data=NULL;
00161     return 0;
00162 }
00163 
00164 /******************************************************************************
00165 Function:   lcmaps_cleanVoData
00166 Description:
00167     Clean a VoData structure that was previously filled with lcmaps_copyVoData().
00168     The contents are freed and set to zero.
00169 
00170 Parameters:
00171     vo_data: a pointer to a VoData structure
00172 
00173 Returns:
00174     0: success
00175     -1: failure (could not clean the structure or no structure found)
00176 ******************************************************************************/
00191 int
00192 lcmaps_cleanVoData(
00193     lcmaps_vo_data_t * vo_data
00194 )
00195 {
00196     if (!vo_data) {
00197         lcmaps_log(0, "lcmaps_cleanVoData():: no lcmaps_vo_data_t found\n");
00198         return -1;
00199     }
00200     else
00201     {
00202         if ( (vo_data)->vo)
00203         {
00204             free( (vo_data)->vo );
00205             vo_data->vo = NULL;
00206         }
00207         if ( (vo_data)->group) 
00208         {
00209             free( (vo_data)->group );
00210             vo_data->group = NULL;
00211         }
00212         if ( (vo_data)->subgroup) 
00213         {
00214             free( (vo_data)->subgroup );
00215             vo_data->subgroup = NULL;
00216         }
00217         if ( (vo_data)->role) 
00218         {
00219             free( (vo_data)->role );
00220             vo_data->role = NULL;
00221         }
00222         if ( (vo_data)->capability) 
00223         {
00224             free( (vo_data)->capability );
00225             vo_data->capability = NULL;
00226         }
00227     }
00228     return 0;
00229 }
00230 
00231 /******************************************************************************
00232 Function:   lcmaps_copyVoData
00233 Description:
00234     Copy a VoData structure into an empty VoData structure which has to exist.
00235 
00236 Parameters:
00237     dst_vo_data pointer to a empty VoData structure that should be filled
00238     src_vo_data pointer to the VoData structure that should be copied
00239 
00240 Returns:
00241     0: success
00242     -1: failure (either src_vo_data or dst_vo_data was empty)
00243 ******************************************************************************/
00259 int
00260 lcmaps_copyVoData(
00261     lcmaps_vo_data_t * dst_vo_data,
00262     const lcmaps_vo_data_t * src_vo_data
00263 )
00264 {
00265     if ( (dst_vo_data) && (src_vo_data) )
00266     {
00267         if (src_vo_data->vo)
00268             dst_vo_data->vo = strdup(src_vo_data->vo);
00269         else
00270             dst_vo_data->vo = NULL;
00271         if (src_vo_data->group)
00272             dst_vo_data->group = strdup(src_vo_data->group);
00273         else
00274             dst_vo_data->group = NULL;
00275         if (src_vo_data->subgroup)
00276             dst_vo_data->subgroup = strdup(src_vo_data->subgroup);
00277         else
00278             dst_vo_data->subgroup = NULL;
00279         if (src_vo_data->role)
00280             dst_vo_data->role = strdup(src_vo_data->role);
00281         else
00282             dst_vo_data->role = NULL;
00283         if (src_vo_data->capability)
00284             dst_vo_data->capability = strdup(src_vo_data->capability);
00285         else
00286             dst_vo_data->capability = NULL;
00287 
00288         return 0;
00289     }
00290     else
00291     {
00292         return -1;
00293     }
00294 }
00295 
00296 /******************************************************************************
00297 Function:   lcmaps_printVoData
00298 Description:
00299     Print the contents of a VoData structure
00300 
00301 Parameters:
00302     debug_level: debug_level for which the contents will be printed
00303     vo_data: pointer to a VoData structure
00304 
00305 Returns:
00306      0 (always)
00307 ******************************************************************************/
00320 int
00321 lcmaps_printVoData(
00322     int debug_level,
00323     const lcmaps_vo_data_t * vo_data
00324 )
00325 {
00326     if (vo_data)
00327     {
00328         lcmaps_log_debug(debug_level,"lcmaps_printVoData(): address of vo data struct: %p\n", vo_data);
00329         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                        VO: %s\n", vo_data->vo);
00330         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                     GROUP: %s\n", vo_data->group);
00331         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                  SUBGROUP: %s\n", vo_data->subgroup);
00332         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                      ROLE: %s\n", vo_data->role);
00333         lcmaps_log_debug(debug_level,"lcmaps_printVoData():                CAPABILITY: %s\n", vo_data->capability);
00334     }
00335     else
00336     {
00337         lcmaps_log_debug(debug_level,"lcmaps_printVoData(): empty pointer to vo data struct\n");
00338     }
00339     return 0;
00340 }
00341 
00342 /******************************************************************************
00343 Function:   lcmaps_stringVoData
00344 Description:
00345     Cast a VoData structure into a string
00346 
00347     The user of this function should create the buffer of size nchars beforehand.
00348     In buffer a string like the following will be written:
00349     "/VO=fred/GROUP=fred/flintstone/ROLE=director/CAPABILITY=destroy"
00350 
00351     Currently the SUBGROUP entry is ignored. Only if the information is present in the 
00352     VoData structure, it is added to the string.
00353     Both data for VO and GROUP are required (might change).
00354 
00355 
00356 Parameters:
00357     vo_data: pointer to a VoData structure
00358     buffer: pointer to character array of size nchars
00359     nchars: size of character array
00360 
00361 Returns:
00362     0: success
00363     -1: failure
00364 ******************************************************************************/
00388 int
00389 lcmaps_stringVoData(
00390     const lcmaps_vo_data_t * vo_data,
00391     char * buffer,
00392     int nchars
00393 )
00394 {
00395     int totalchars;
00396     char * strptr=NULL;
00397     char * bufptr=NULL;
00398     int    buflen=0;
00399 
00400     bufptr=buffer;
00401     buflen=nchars;
00402 
00403     /* write "VO=" */
00404     if ( vo_data->vo == NULL )
00405     {
00406         lcmaps_log(0,"lcmaps_stringVoData(): error no VO found\n");
00407         return -1;
00408     }
00409     /* Skip over leading whitespace */
00410     strptr=vo_data->vo;
00411     strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00412     if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00413     {
00414         /* Convention for solaris 2.8 and glibc-2.1 and higher:
00415          * totalchars is the number of chars written (excluding \0) if enough space had been 
00416          * available.
00417          * negative: error
00418          */
00419         totalchars=snprintf(bufptr,(size_t)buflen,"/VO=%s",strptr);
00420         if ( (totalchars+1) > buflen )
00421         {
00422             lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for VO\n");
00423             lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00424             return -1;
00425         }
00426         else if ( totalchars < 0 )
00427         {
00428             lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00429             return -1;
00430         }
00431         else
00432         {
00433             bufptr+=totalchars;
00434             buflen-=totalchars;
00435         }
00436     }
00437     else
00438     {
00439         lcmaps_log(0,"lcmaps_stringVoData(): error no VO found\n");
00440         return -1;
00441     }
00442 
00443     /* write "GROUP=" */
00444     if ( vo_data->group == NULL )
00445     {
00446         lcmaps_log(0,"lcmaps_stringVoData(): error no VO-group found\n");
00447         return -1;
00448     }
00449     /* Skip over leading whitespace */
00450     strptr=vo_data->group;
00451     strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00452     if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00453     {
00454         totalchars=snprintf(bufptr,(size_t)buflen,"/GROUP=%s",strptr);
00455         if ( (totalchars+1) > buflen )
00456         {
00457             lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for GROUP\n");
00458             lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00459             return -1;
00460         }
00461         else if ( totalchars < 0 )
00462         {
00463             lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00464             return -1;
00465         }
00466         else
00467         {
00468             bufptr+=totalchars;
00469             buflen-=totalchars;
00470         }
00471     }
00472     else
00473     {
00474         lcmaps_log(0,"lcmaps_stringVoData(): error no VO-group found\n");
00475         return -1;
00476     }
00477 
00478     /* Skip subgroup for the moment (not clear how VOMS will evolve in this respect) */
00479 
00480     /* write "ROLE=" */
00481     if ( vo_data->role != NULL )
00482     {
00483         /* Skip over leading whitespace */
00484         strptr=vo_data->role;
00485         strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00486         if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00487         {
00488             totalchars=snprintf(bufptr,(size_t)buflen,"/ROLE=%s",strptr);
00489             if ( (totalchars+1) > buflen )
00490             {
00491                 lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for ROLE\n");
00492                 lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00493                 return -1;
00494             }
00495             else if ( totalchars < 0 )
00496             {
00497                 lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00498                 return -1;
00499             }
00500             else
00501             {
00502                 bufptr+=totalchars;
00503                 buflen-=totalchars;
00504             }
00505         }
00506     }
00507 
00508     /* write "CAPABILITY=" */
00509     if ( vo_data->capability != NULL )
00510     {
00511         /* Skip over leading whitespace */
00512         strptr=vo_data->capability;
00513         strptr += strspn(strptr, VO_DATA_WHITESPACE_CHARS);
00514         if ( (*strptr!='\0') && (strncmp(strptr,"NULL",4)) )
00515         {
00516             totalchars=snprintf(bufptr,(size_t)buflen,"/CAPABILITY=%s",strptr);
00517             if ( (totalchars+1) > buflen )
00518             {
00519                 lcmaps_log(0,"lcmaps_stringVoData(): could not write all characters into buffer for CAPABILITY\n");
00520                 lcmaps_log(0,"lcmaps_stringVoData(): excess of characters: %d\n",totalchars+1-buflen);
00521                 return -1;
00522             }
00523             else if ( totalchars < 0 )
00524             {
00525                 lcmaps_log(0,"lcmaps_stringVoData(): error in snprintf()\n");
00526                 return -1;
00527             }
00528             else
00529             {
00530                 bufptr+=totalchars;
00531                 buflen-=totalchars;
00532             }
00533         }
00534     }
00535     return 0;
00536 }
00537 
00538 /******************************************************************************
00539 CVS Information:
00540     $Source: /cvs/fabric_mgt/gridification/lcmaps/src/pluginmanager/lcmaps_vo_data.c,v $
00541     $Date: 2003/07/30 17:10:25 $
00542     $Revision: 1.5 $
00543     $Author: martijn $
00544 ******************************************************************************/

Generated at Tue Sep 23 15:48:09 2003 for edg-lcmaps by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001