nfs-ganesha 1.4

fsal_strings.c

Go to the documentation of this file.
00001 /*
00002  * vim:expandtab:shiftwidth=8:tabstop=8:
00003  */
00004 
00013 #ifdef HAVE_CONFIG_H
00014 #include "config.h"
00015 #endif
00016 
00017 #include "fsal.h"
00018 #include "fsal_internal.h"
00019 #include <string.h>
00020 
00045 fsal_status_t FSAL_str2name(const char *string, /* IN */
00046                             fsal_mdsize_t in_str_maxlen,        /* IN */
00047                             fsal_name_t * name  /* OUT */
00048     )
00049 {
00050 
00051   unsigned int i;
00052   char *output;
00053 
00054   /* sanity checks */
00055   if(!name || !string)
00056     ReturnCode(ERR_FSAL_FAULT, 0);
00057 
00058   output = (*name).name;
00059 
00060   /* computes input strlen */
00061   for(i = 0; (i < in_str_maxlen) && (string[i] != '\0'); i++) ;
00062 
00063   /* the value of i doesn't include terminating '\0',
00064    * thus, FSAL_MAX_NAME_LEN is excluded.
00065    */
00066   if(i >= FSAL_MAX_NAME_LEN)
00067     ReturnCode(ERR_FSAL_NAMETOOLONG, 0);
00068 
00069   (*name).len = i;
00070 
00071   /* copies the string */
00072   for(i = 0; i < (*name).len; i++)
00073     {
00074       output[i] = string[i];
00075     }
00076   /* sets the terminating \0 */
00077   output[i] = '\0';
00078 
00079   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00080 
00081 }
00082 
00099 fsal_status_t FSAL_str2path(char *string,       /* IN */
00100                             fsal_mdsize_t in_str_maxlen,        /* IN */
00101                             fsal_path_t * p_path        /* OUT */
00102     )
00103 {
00104 
00105   unsigned int i;
00106   char *output;
00107 
00108   /* sanity checks */
00109   if(!p_path || !string)
00110     ReturnCode(ERR_FSAL_FAULT, 0);
00111 
00112   output = p_path->path;
00113 
00114   /* computes input strlen */
00115   for(i = 0; (i < in_str_maxlen) && (string[i] != '\0'); i++) ;
00116 
00117   /* the value of i doesn't include terminating '\0',
00118    * thus, FSAL_MAX_PATH_LEN is excluded.
00119    */
00120   if(i >= FSAL_MAX_PATH_LEN)
00121     ReturnCode(ERR_FSAL_NAMETOOLONG, 0);
00122 
00123   p_path->len = i;
00124 
00125   /* copies the string */
00126   for(i = 0; i < p_path->len; i++)
00127     {
00128       output[i] = string[i];
00129     }
00130   /* sets the terminating \0 */
00131   output[i] = '\0';
00132 
00133   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00134 
00135 }
00136 
00153 fsal_status_t FSAL_name2str(fsal_name_t * p_name,       /* IN */
00154                             char *string,       /* OUT */
00155                             fsal_mdsize_t out_str_maxlen        /* IN */
00156     )
00157 {
00158 
00159   unsigned int i;
00160   char *input;
00161 
00162   /* sanity checks */
00163   if(!string || !p_name)
00164     ReturnCode(ERR_FSAL_FAULT, 0);
00165 
00166   input = p_name->name;
00167 
00168   i = p_name->len;
00169 
00170   /* the value of i doesn't include terminating '\0',
00171    * thus, last value is excluded.
00172    */
00173   if(i >= out_str_maxlen)
00174     ReturnCode(ERR_FSAL_TOOSMALL, 0);
00175 
00176   /* copies the value */
00177   strncpy(string, input, out_str_maxlen);
00178 
00179   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00180 }
00181 
00198 fsal_status_t FSAL_path2str(fsal_path_t * p_path,       /* IN */
00199                             char *string,       /* OUT */
00200                             fsal_mdsize_t out_str_maxlen        /* IN */
00201     )
00202 {
00203 
00204   unsigned int i;
00205   char *input;
00206 
00207   /* sanity checks */
00208   if(!string || !p_path)
00209     ReturnCode(ERR_FSAL_FAULT, 0);
00210 
00211   input = p_path->path;
00212 
00213   i = p_path->len;
00214 
00215   /* the value of i doesn't include terminating '\0',
00216    * thus, last value is excluded.
00217    */
00218   if(i >= out_str_maxlen)
00219     ReturnCode(ERR_FSAL_TOOSMALL, 0);
00220 
00221   /* copies the value */
00222   strncpy(string, input, out_str_maxlen);
00223 
00224   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00225 }
00226 
00233 int FSAL_namecmp(fsal_name_t * p_name1, fsal_name_t * p_name2)
00234 {
00235 
00236   return (strncmp(p_name1->name, p_name2->name, FSAL_MAX_NAME_LEN));
00237 
00238 }
00239 
00246 int FSAL_pathcmp(fsal_path_t * p_path1, fsal_path_t * p_path2)
00247 {
00248 
00249   return (strncmp(p_path1->path, p_path2->path, FSAL_MAX_PATH_LEN));
00250 
00251 }
00252 
00261 fsal_status_t FSAL_namecpy(fsal_name_t * p_tgt_name, fsal_name_t * p_src_name)
00262 {
00263 
00264   char *output;
00265 
00266   /* sanity check */
00267   if(!p_tgt_name || !p_src_name)
00268     ReturnCode(ERR_FSAL_FAULT, 0);
00269 
00270   output = p_tgt_name->name;
00271 
00272   /* copy */
00273   strncpy(output, p_src_name->name, FSAL_MAX_NAME_LEN);
00274 
00275   p_tgt_name->len = p_src_name->len;
00276 
00277   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00278 
00279 }
00280 
00289 fsal_status_t FSAL_pathcpy(fsal_path_t * p_tgt_path, fsal_path_t * p_src_path)
00290 {
00291 
00292   char *output;
00293 
00294   /* sanity check */
00295   if(!p_tgt_path || !p_src_path)
00296     ReturnCode(ERR_FSAL_FAULT, 0);
00297 
00298   output = p_tgt_path->path;
00299 
00300   /* copy */
00301   strncpy(output, p_src_path->path, FSAL_MAX_PATH_LEN);
00302 
00303   p_tgt_path->len = p_src_path->len;
00304 
00305   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00306 
00307 }
00308 
00313 fsal_status_t FSAL_buffdesc2name(fsal_buffdesc_t * in_buf, fsal_name_t * out_name)
00314 {
00315 
00316   if(!in_buf || !out_name)
00317     ReturnCode(ERR_FSAL_FAULT, 0);
00318 
00319   return FSAL_str2name(in_buf->pointer, in_buf->len, out_name);
00320 
00321 }
00322 
00327 fsal_status_t FSAL_buffdesc2path(fsal_buffdesc_t * in_buf, fsal_path_t * out_path)
00328 {
00329 
00330   if(!in_buf || !out_path)
00331     ReturnCode(ERR_FSAL_FAULT, 0);
00332 
00333   return FSAL_str2path(in_buf->pointer, in_buf->len, out_path);
00334 
00335 }
00336 
00351 fsal_status_t FSAL_path2buffdesc(fsal_path_t * in_path, fsal_buffdesc_t * out_buff)
00352 {
00353 
00354   if(!out_buff || !in_path)
00355     ReturnCode(ERR_FSAL_FAULT, 0);
00356 
00357   out_buff->pointer = in_path->path;
00358   out_buff->len = in_path->len;
00359 
00360   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00361 
00362 }
00363 
00378 fsal_status_t FSAL_name2buffdesc(fsal_name_t * in_name, fsal_buffdesc_t * out_buff)
00379 {
00380 
00381   if(!out_buff || !in_name)
00382     ReturnCode(ERR_FSAL_FAULT, 0);
00383 
00384   out_buff->pointer = in_name->name;
00385   out_buff->len = in_name->len;
00386 
00387   ReturnCode(ERR_FSAL_NO_ERROR, 0);
00388 
00389 }
00390 
00391 /* @} */