Back to index

im-sdk  12.3.91
iiimcf_file_operation.c
Go to the documentation of this file.
00001 /*
00002   ns.c
00003 */
00004 
00005 #ifdef HAVE_CONFIG_H
00006 #include <config.h>
00007 #endif
00008 
00009 #ifdef HAVE_STDLIB_H
00010 #include <stdlib.h>
00011 #endif
00012 #ifdef HAVE_STRING_H
00013 #include <string.h>
00014 #endif
00015 #ifdef HAVE_MEMORY_H
00016 #include <memory.h>
00017 #endif
00018 
00019 #include <stdio.h>
00020 #include <limits.h>
00021 #include <dlfcn.h>
00022 #include <sys/types.h>
00023 #include <sys/param.h>
00024 #include <fcntl.h>
00025 #include <sys/stat.h>
00026 #include <sys/unistd.h>
00027 #include "iiimcfint.h"
00028 #include <EIMIL.h>
00029 #include <locale.h>
00030 #include <langinfo.h>
00031 
00032 #include <sys/errno.h>
00033 extern int errno;
00034 
00035 #include <dirent.h>
00036 #include "csconv.h"
00037 #define CSC_OPEN_LOCALE "csconv_open_locale"
00038 #define CSC_OPEN        "csconv_open"
00039 #define CSC_CONV        "csconv"
00040 #define CSC_CLOSE       "csconv_close"
00041 
00042 typedef csconv_t        (* csc_open_locale_t)(const char *, const char *, const char *);
00043 typedef csconv_t        (* csc_open_t)(const char *, const char *);
00044 typedef size_t          (* csc_conv_t)(csconv_t, const char **, size_t *, char **, size_t *);
00045 typedef int             (* csc_close_t)(csconv_t);
00046 
00047 static void *                   csc_handle;
00048 static csc_open_locale_t        csc_open_locale;
00049 static csc_open_t               csc_open;
00050 static csc_conv_t               csc_conv;
00051 static csc_close_t              csc_close;
00052 
00053 static char *current_locale = 0;
00054 static Bool skip_native_to_utf8_conv = False;
00055 
00056 static void
00057 dlopen_csconv()
00058 {
00059     csc_handle = dlopen(CSC_PATH, RTLD_LAZY);
00060     if (NULL == csc_handle) {
00061         csc_handle = (void *)(-1);
00062         return;
00063     }
00064 
00065     csc_open_locale = (csc_open_locale_t)dlsym(csc_handle, CSC_OPEN_LOCALE);
00066     csc_open = (csc_open_t)dlsym(csc_handle, CSC_OPEN);
00067     csc_conv = (csc_conv_t)dlsym(csc_handle, CSC_CONV);
00068     csc_close = (csc_close_t)dlsym(csc_handle, CSC_CLOSE);
00069 
00070     if ((NULL == csc_open_locale) || (NULL == csc_open) ||
00071        (NULL == csc_conv) || (NULL == csc_close)) {
00072         dlclose(csc_handle);
00073         csc_handle = (void *)(-1);
00074         return;
00075     }
00076 }
00077 
00078 int
00079 iiimcf_convert_NativeToUTF8(
00080     char *encoding, 
00081     char *from_buf,
00082     size_t from_left,
00083     char **to_buf, 
00084     size_t *to_left
00085 )
00086 {
00087     char *dst;
00088     size_t ret = 0;
00089     static csconv_t csconv_cd = NULL;
00090 
00091     if (current_locale == NULL) {
00092        current_locale = (char*)strdup(setlocale(LC_CTYPE, NULL));
00093     }
00094 
00095     do {
00096        if (((void *)(-1) == csc_handle) || ((csconv_t)(-1) == csconv_cd)) {
00097            continue;
00098        }
00099        if (NULL == csc_handle) {
00100            dlopen_csconv();
00101            if ((void *)(-1) == csc_handle) {
00102               continue;
00103            }
00104        }
00105        if (NULL == csconv_cd) {
00106            csconv_cd = (csc_open_locale)(current_locale, "UTF-8", encoding);
00107            if ((csconv_t)(-1) == csconv_cd) {
00108               continue;
00109            }
00110        }
00111 
00112        dst = *((char **)to_buf);
00113 
00114        ret = csc_conv(csconv_cd,
00115                      (const char **)&from_buf, &from_left,
00116                      &dst, to_left);
00117        return ret;
00118     } while (0);
00119 
00120     return ret;
00121 }
00122 
00123 IIIMF_status
00124 iiimcf_perform_file_operation(
00125     IIIMCF_handle_rec *ph,
00126     IIIMP_message *pmes        
00127 )
00128 {
00129     IIIMP_file_operation_v *pfop = &pmes->v.file_operation;
00130 
00131     ASSERT(pmes->opcode == IM_FILE_OPERATION);
00132 
00133     switch(pfop->type) {
00134       case IIIMP_FILE_OPERATION_TYPE_OPEN:
00135        return iiimcf_open(ph, (IIIMP_open *)pfop->value);
00136 
00137       case IIIMP_FILE_OPERATION_TYPE_READ:
00138        return iiimcf_read(ph, (IIIMP_read *)pfop->value);
00139 
00140       case IIIMP_FILE_OPERATION_TYPE_WRITE:
00141        return iiimcf_write(ph, (IIIMP_write *)pfop->value);
00142 
00143       case IIIMP_FILE_OPERATION_TYPE_CLOSE:
00144        return iiimcf_close(ph, (IIIMP_close *)pfop->value);
00145 
00146       case IIIMP_FILE_OPERATION_TYPE_OPENDIR:
00147        return iiimcf_opendir(ph, (IIIMP_opendir *)pfop->value);
00148 
00149       case IIIMP_FILE_OPERATION_TYPE_CLOSEDIR:
00150        return iiimcf_closedir(ph, (IIIMP_closedir *)pfop->value);
00151 
00152       case IIIMP_FILE_OPERATION_TYPE_MKDIR:
00153        return iiimcf_mkdir(ph, (IIIMP_mkdir *)pfop->value);
00154 
00155       case IIIMP_FILE_OPERATION_TYPE_RMDIR:
00156        return iiimcf_rmdir(ph, (IIIMP_rmdir *)pfop->value);
00157 
00158       case IIIMP_FILE_OPERATION_TYPE_SYMLINK:
00159        return iiimcf_symlink(ph, (IIIMP_symlink *)pfop->value);
00160 
00161       case IIIMP_FILE_OPERATION_TYPE_STAT:
00162        return iiimcf_stat(ph, (IIIMP_stat *)pfop->value);
00163 
00164       case IIIMP_FILE_OPERATION_TYPE_LSTAT:
00165        return iiimcf_lstat(ph, (IIIMP_lstat *)pfop->value);
00166 
00167       case IIIMP_FILE_OPERATION_TYPE_FSTAT:
00168        return iiimcf_fstat(ph, (IIIMP_fstat *)pfop->value);
00169 
00170       case IIIMP_FILE_OPERATION_TYPE_CREAT:
00171        return iiimcf_creat(ph, (IIIMP_creat *)pfop->value);
00172 
00173       case IIIMP_FILE_OPERATION_TYPE_LSEEK:
00174        return iiimcf_lseek(ph, (IIIMP_lseek *)pfop->value);
00175 
00176       case IIIMP_FILE_OPERATION_TYPE_UNLINK:
00177        return iiimcf_unlink(ph, (IIIMP_unlink *)pfop->value);
00178 
00179       case IIIMP_FILE_OPERATION_TYPE_RENAME:
00180        return iiimcf_rename(ph, (IIIMP_rename *)pfop->value);
00181 
00182       case IIIMP_FILE_OPERATION_TYPE_FCNTL:
00183        return iiimcf_fcntl(ph, (IIIMP_fcntl *)pfop->value);
00184 
00185       case IIIMP_FILE_OPERATION_TYPE_TRUNCATE:
00186        return iiimcf_truncate(ph, (IIIMP_truncate *)pfop->value);
00187 
00188       case IIIMP_FILE_OPERATION_TYPE_FTRUNCATE:
00189        return iiimcf_ftruncate(ph, (IIIMP_ftruncate *)pfop->value);
00190 
00191     }
00192 
00193     return IIIMF_STATUS_FAIL;
00194 }
00195 
00196 IIIMF_status
00197 iiimcf_open(
00198     IIIMCF_handle_rec *ph,
00199     IIIMP_open *po        
00200 )
00201 {
00202     IIIMF_status st_r = IIIMF_STATUS_SUCCESS;
00203     IIIMP_open_reply *por;
00204     IIIMP_message *preply;
00205     int ns_count;
00206     char path[PATH_MAX];
00207     int i;
00208 
00209     errno = 0;
00210 
00211     ns_count = ph->num_of_ns;
00212 
00213     if (!ns_count) {
00214        ph->pns = (IIIMCF_ns *) calloc(10, sizeof(IIIMCF_ns));
00215     } else if (ns_count > 10) {
00216        ph->pns = (IIIMCF_ns *) realloc((IIIMCF_ns *)ph->pns, (ns_count + 1) * sizeof(IIIMCF_ns));
00217     }
00218     if (!ph->pns) return IIIMF_STATUS_MALLOC;
00219 
00220     ph->pns[ns_count].ns_id = po->ns_id;
00221 
00222     ph->pns[ns_count].filename = (char *) calloc((po->filename->len + 1), sizeof(char));
00223     for(i=0; i<po->filename->len; i++) {
00224        ph->pns[ns_count].filename[i] = po->filename->ptr[i];
00225     }
00226     ph->pns[ns_count].filename[i] = '\0';
00227     (void) memset((char *)path, '\0', sizeof(path));
00228     if (strstr(ph->pns[ns_count].filename, ".iiim")) {
00229        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, ph->pns[ns_count].filename);
00230     } else {
00231        snprintf(path, sizeof(path), "%s",ph->pns[ns_count].filename);
00232     }
00233     check_pathname(path);
00234 
00235     ph->pns[ns_count].nsc_fd = open(path, po->flags);
00236     if (ph->pns[ns_count].nsc_fd < 0) {
00237         por = iiimp_open_reply_new(ph->data_s, ph->pns[ns_count].nsc_fd, errno);
00238        preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_OPEN_REPLY, por); 
00239        st_r = iiimcf_send_message(ph, preply, 1);
00240        return st_r; 
00241     }
00242     ph->num_of_ns = ns_count + 1;
00243 
00244     por = iiimp_open_reply_new(ph->data_s, po->ns_id, errno);
00245     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_OPEN_REPLY, por); 
00246     st_r = iiimcf_send_message(ph, preply, 1);
00247 
00248     return st_r;
00249 }
00250 
00251 IIIMF_status
00252 iiimcf_read(
00253     IIIMCF_handle_rec *ph,
00254     IIIMP_read *pr        
00255 )
00256 {
00257     IIIMP_read_reply *prr;
00258     IIIMP_message *preply;
00259     int i;
00260     void *ptr;
00261     int nread = -1;
00262 
00263     errno = 0;
00264 
00265     for (i=0; i<ph->num_of_ns; i++) {
00266        if (ph->pns[i].ns_id == pr->ns_id) {
00267            ptr = (char *) calloc(pr->size + 1, sizeof(char));
00268            nread = read(ph->pns[i].nsc_fd, ptr, pr->size);
00269             prr = iiimp_read_reply_new(ph->data_s, pr->ns_id, nread, errno, ptr); 
00270            preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_READ_REPLY, prr);
00271            return iiimcf_send_message(ph, preply, 1);
00272        }
00273     } 
00274     return IIIMF_STATUS_FAIL;
00275 }
00276 
00277 IIIMF_status
00278 iiimcf_write(
00279     IIIMCF_handle_rec *ph,
00280     IIIMP_write *pw        
00281 )
00282 {
00283     int i;
00284     IIIMP_write_reply *pwr;
00285     IIIMP_message *preply;
00286     errno = 0;
00287     int nwrite = -1;
00288 
00289     for (i=0; i<ph->num_of_ns; i++) {
00290        if (ph->pns[i].ns_id == pw->ns_id) {
00291            nwrite = write(ph->pns[i].nsc_fd, pw->object, pw->size);
00292            pwr = iiimp_write_reply_new(ph->data_s, pw->ns_id, nwrite, errno); 
00293             preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_WRITE_REPLY, pwr);
00294            return iiimcf_send_message(ph, preply, 1);
00295        }
00296     } 
00297     return IIIMF_STATUS_FAIL;
00298 }
00299 
00300 IIIMF_status
00301 iiimcf_close(
00302     IIIMCF_handle_rec *ph,
00303     IIIMP_close *pc        
00304 )
00305 {
00306     int i, ret;
00307     IIIMP_close_reply *iclr;
00308     IIIMP_message *preply;
00309     errno = 0;
00310 
00311     for (i=0; i<ph->num_of_ns; i++) {
00312        if (ph->pns[i].ns_id == pc->ns_id) {
00313            if ((ret = close(ph->pns[i].nsc_fd)) < 0) {
00314                 iclr = iiimp_close_reply_new(ph->data_s, ret, errno);
00315                 preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_CLOSE_REPLY, iclr);
00316               return iiimcf_send_message(ph, preply, 1);
00317             }
00318        }
00319     } 
00320     iclr = iiimp_close_reply_new(ph->data_s, pc->ns_id, errno);
00321     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_CLOSE_REPLY, iclr);
00322     return iiimcf_send_message(ph, preply, 1);
00323 }
00324 
00325 IIIMF_status
00326 iiimcf_opendir(
00327     IIIMCF_handle_rec *ph,
00328     IIIMP_opendir *pod
00329 )
00330 {
00331     int i, ns_count, nitems, data_len;
00332     IIIMP_message *preply;
00333     IIIMP_opendir_reply *podr;
00334     char path[PATH_MAX];
00335     char buf[PATH_MAX];
00336     struct dirent *entry;
00337     IIIMP_utf8string *pstart, *pcurr, *pstr;
00338     char *to_utf8;
00339     size_t to_len;
00340     char *encoding;
00341     errno = 0;
00342 
00343     pstart = pcurr = pstr = NULL;
00344 
00345     ns_count = ph->num_of_ns;
00346 
00347     if (!ns_count) {
00348         ph->pns = (IIIMCF_ns *) calloc(10, sizeof(IIIMCF_ns));
00349     } else if (ns_count > 10) {
00350         ph->pns = (IIIMCF_ns *) realloc((IIIMCF_ns *)ph->pns, (ns_count + 1) * sizeof(IIIMCF_ns));
00351     }
00352     if (!ph->pns) return IIIMF_STATUS_MALLOC;
00353 
00354     ph->pns[ns_count].ns_id = pod->ns_id;
00355 
00356     ph->pns[ns_count].dirname = (char *) calloc((pod->dirname->len + 1), sizeof(char));
00357     for (i=0; i<pod->dirname->len; i++) {
00358         ph->pns[ns_count].dirname[i] = pod->dirname->ptr[i];
00359     }
00360     ph->pns[ns_count].dirname[i] = '\0';
00361     (void) memset((char *)path, '\0', sizeof(path));
00362     if (strstr(ph->pns[ns_count].dirname, ".iiim")) {
00363        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, ph->pns[ns_count].dirname);
00364     } else {
00365        snprintf(path, sizeof(path), "%s",ph->pns[ns_count].dirname);
00366     }
00367     check_pathname(path);
00368 
00369     ph->pns[ns_count].nsc_dirp = (DIR *)NULL;
00370     ph->pns[ns_count].nsc_dirp = opendir(path);
00371     if (ph->pns[ns_count].nsc_dirp == NULL) {
00372        podr = iiimp_opendir_reply_new(ph->data_s, 0, errno, 0, NULL);
00373        preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_OPENDIR_REPLY, podr);
00374        return iiimcf_send_message(ph, preply, 1);
00375     }   
00376     i = 0;
00377     if (current_locale == NULL) {
00378        current_locale = (char *)strdup(setlocale(LC_CTYPE, NULL));
00379     }
00380     encoding = nl_langinfo(CODESET);
00381     if (!strcasecmp(encoding, "UTF-8") || !strcasecmp(encoding, "UTF8")) {
00382        skip_native_to_utf8_conv = True;
00383     }
00384 
00385     while ((entry = readdir(ph->pns[ns_count].nsc_dirp)) != NULL) {
00386        if (skip_native_to_utf8_conv) {
00387            (void) memset((char *)buf, '\0', sizeof(buf));
00388            snprintf(buf, sizeof(buf), "%s",entry->d_name);
00389            pstr = iiimp_utf8string_new(ph->data_s, entry->d_reclen, buf);
00390        }else {
00391            to_utf8 = (char *)calloc(1, sizeof(buf));
00392            to_len = PATH_MAX;
00393            (void) memset((char *)buf, '\0', sizeof(buf));
00394            snprintf(buf, sizeof(buf), "%s",entry->d_name);
00395            iiimcf_convert_NativeToUTF8(encoding, buf, entry->d_reclen, &to_utf8, &to_len);
00396            data_len = PATH_MAX - to_len;
00397            pstr = iiimp_utf8string_new(ph->data_s, data_len, to_utf8);
00398            free(to_utf8);
00399        }
00400        if (!pstr) {
00401            podr = iiimp_opendir_reply_new(ph->data_s, pod->ns_id, errno, 0, NULL);
00402            preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_OPENDIR_REPLY, podr);
00403            return iiimcf_send_message(ph, preply, 1);
00404        }
00405        if (!pstart) pstart = pstr;
00406        else pcurr->next = pstr;
00407        pcurr = pstr;
00408        i++;
00409     }
00410     nitems = i;
00411     podr = iiimp_opendir_reply_new(ph->data_s, pod->ns_id, errno, nitems, pstart);
00412     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_OPENDIR_REPLY, podr);
00413     return iiimcf_send_message(ph, preply, 1);
00414 }
00415 
00416 IIIMF_status
00417 iiimcf_closedir(
00418     IIIMCF_handle_rec *ph,
00419     IIIMP_closedir *pcd
00420 )
00421 {
00422     int i, ret;
00423     IIIMP_message *preply;
00424     IIIMP_closedir_reply *pcdr;
00425     errno = 0;
00426 
00427     for (i=0; i<ph->num_of_ns; i++) {
00428         if (ph->pns[i].ns_id == pcd->ns_id) {
00429             if ((ret = closedir(ph->pns[i].nsc_dirp)) < 0) {
00430                 pcdr = iiimp_closedir_reply_new(ph->data_s, ret, errno);
00431                 preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_CLOSEDIR_REPLY, pcdr);
00432               return iiimcf_send_message(ph, preply, 1);
00433             }
00434         }
00435     }
00436     pcdr = iiimp_closedir_reply_new(ph->data_s, pcd->ns_id, errno);
00437     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_CLOSEDIR_REPLY, pcdr);
00438     return iiimcf_send_message(ph, preply, 1);
00439 }
00440 
00441 IIIMF_status
00442 iiimcf_mkdir(
00443     IIIMCF_handle_rec *ph,
00444     IIIMP_mkdir *pm        
00445 )
00446 {
00447     int i, ret;
00448     IIIMP_message *preply;
00449     IIIMP_mkdir_reply *pmr;
00450     char path[PATH_MAX];
00451     char *dirname;
00452     errno = 0;
00453 
00454     dirname = (char *)calloc((pm->path->len + 1), sizeof(char));
00455     for (i=0; i<pm->path->len; i++) {
00456         dirname[i] = pm->path->ptr[i];
00457     }
00458     dirname[i] = '\0';
00459     (void) memset((char *)path, '\0', sizeof(path));
00460     if (strstr(dirname, ".iiim")) {
00461        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00462     } else {
00463        snprintf(path, sizeof(path), "%s",dirname);
00464     }
00465     check_pathname(path);
00466     free(dirname);
00467 
00468     if ((ret = mkdir(path, pm->mode)) < 0) {
00469        pmr = iiimp_mkdir_reply_new(ph->data_s, ret, errno); 
00470         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_MKDIR_REPLY, pmr);
00471        return iiimcf_send_message(ph, preply, 1);
00472     }
00473     pmr = iiimp_mkdir_reply_new(ph->data_s, pm->ns_id, errno); 
00474     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_MKDIR_REPLY, pmr);
00475     return iiimcf_send_message(ph, preply, 1);
00476 }
00477 
00478 IIIMF_status
00479 iiimcf_rmdir(
00480     IIIMCF_handle_rec *ph,
00481     IIIMP_rmdir *pr        
00482 )
00483 {
00484     int i, ret;
00485     IIIMP_message *preply;
00486     IIIMP_rmdir_reply *prr;
00487     char path[PATH_MAX];
00488     char *dirname;
00489     errno = 0;
00490 
00491     dirname = (char *)calloc((pr->path->len + 1), sizeof(char));
00492     for (i=0; i<pr->path->len; i++) {
00493         dirname[i] = pr->path->ptr[i];
00494     }
00495     dirname[i] = '\0';
00496     (void) memset((char *)path, '\0', sizeof(path));
00497     if (strstr(dirname, ".iiim")) {
00498        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00499     } else {
00500        snprintf(path, sizeof(path), "%s",dirname);
00501     }
00502     check_pathname(path);
00503     free(dirname);
00504     
00505     if ((ret = rmdir(path)) < 0) {
00506         prr = iiimp_rmdir_reply_new(ph->data_s, ret, errno);
00507         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RMDIR_REPLY, prr);
00508         return iiimcf_send_message(ph, preply, 1);
00509     }
00510     prr = iiimp_rmdir_reply_new(ph->data_s, pr->ns_id, errno);
00511     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RMDIR_REPLY, prr);
00512     return iiimcf_send_message(ph, preply, 1);
00513 }
00514 
00515 IIIMF_status
00516 iiimcf_symlink(
00517     IIIMCF_handle_rec *ph,
00518     IIIMP_symlink *ps        
00519 )
00520 {
00521     int i, ret;
00522     IIIMP_message *preply;
00523     IIIMP_symlink_reply *psr;
00524     char dest_path[PATH_MAX];
00525     char src_path[PATH_MAX];
00526     char *dest;
00527     char *src;
00528     errno = 0;
00529 
00530     dest = (char *)calloc((ps->dest->len + 1), sizeof(char));
00531     for (i=0; i<ps->dest->len; i++) {
00532         dest[i] = ps->dest->ptr[i];
00533     }
00534     dest[i] = '\0';
00535     (void) memset((char *)dest_path, '\0', sizeof(dest_path));
00536     if (strstr(dest, ".iiim")) {
00537        snprintf(dest_path, sizeof(dest_path), "%s/%s",ph->penv->home_dir, dest);
00538     } else {
00539        snprintf(dest_path, sizeof(dest_path), "%s",dest);
00540     }
00541     check_pathname(dest_path);
00542 
00543     src = (char *)calloc((ps->src->len + 1), sizeof(char));
00544     for (i=0; i<ps->src->len; i++) {
00545         src[i] = ps->src->ptr[i];
00546     }
00547     src[i] = '\0';
00548     (void) memset((char *)src_path, '\0', sizeof(src_path));
00549     if (strstr(src, ".iiim")) {
00550        snprintf(src_path, sizeof(src_path), "%s/%s",ph->penv->home_dir, src);
00551     } else {
00552        snprintf(src_path, sizeof(src_path), "%s",src);
00553     }
00554     check_pathname(src_path);
00555     free(dest);
00556     free(src);
00557 
00558     if ((ret = symlink(dest_path, src_path)) < 0) {
00559         psr = iiimp_symlink_reply_new(ph->data_s, ret, errno);
00560         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_SYMLINK_REPLY, psr);
00561         return iiimcf_send_message(ph, preply, 1);
00562     }
00563     psr = iiimp_symlink_reply_new(ph->data_s, ps->ns_id, errno);
00564     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_SYMLINK_REPLY, psr);
00565     return iiimcf_send_message(ph, preply, 1);
00566 }
00567 
00568 IIIMF_status
00569 iiimcf_stat(
00570     IIIMCF_handle_rec *ph,
00571     IIIMP_stat *ps         
00572 )   
00573 {   
00574     int i, ret;
00575     IIIMP_message *preply;
00576     IIIMP_stat_reply *psr;
00577     char path[PATH_MAX];
00578     char *dirname; 
00579     struct stat buf;
00580     iiimp_stat_struct *im_buf;
00581     errno = 0;
00582     
00583     dirname = (char *)calloc((ps->path->len + 1), sizeof(char));
00584 
00585     for (i=0; i<ps->path->len; i++) {
00586         dirname[i] = ps->path->ptr[i];
00587     }
00588     dirname[i] = '\0';
00589 
00590     (void) memset((char *)path, '\0', sizeof(path));
00591     if (strstr(dirname, ".iiim")) {
00592        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00593     } else {
00594         snprintf(path, sizeof(path), "%s",dirname);
00595     }
00596     check_pathname(path);
00597     free(dirname);
00598 
00599     if ((ret = stat(path, &buf)) < 0) {
00600         psr = iiimp_stat_reply_new(ph->data_s, ret, errno, (iiimp_stat_struct *)NULL);
00601         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_STAT_REPLY, psr);
00602         return iiimcf_send_message(ph, preply, 1);
00603     }
00604     im_buf = (iiimp_stat_struct *) calloc(1, sizeof(iiimp_stat_struct));
00605 
00606     im_buf->s_mode = buf.st_mode;
00607     im_buf->s_nlink = buf.st_nlink;
00608     im_buf->s_uid = buf.st_uid;
00609     im_buf->s_gid = buf.st_gid;
00610     im_buf->s_atime = buf.st_atime;
00611     im_buf->s_mtime = buf.st_mtime;
00612     im_buf->s_ctime = buf.st_ctime;
00613     im_buf->s_blksize = buf.st_blksize;
00614 
00615     im_buf->s_dev = buf.st_dev;
00616     im_buf->s_rdev = buf.st_rdev;
00617     im_buf->s_ino = buf.st_ino;
00618     im_buf->s_size = buf.st_size;
00619     im_buf->s_blocks = buf.st_blocks;
00620 
00621     psr = iiimp_stat_reply_new(ph->data_s, ps->ns_id, errno, im_buf);
00622     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_STAT_REPLY, psr);
00623     return iiimcf_send_message(ph, preply, 1);
00624 }
00625 
00626 IIIMF_status
00627 iiimcf_lstat(
00628     IIIMCF_handle_rec *ph,
00629     IIIMP_lstat *pl
00630 )
00631 {
00632     int i, ret;
00633     IIIMP_message *preply;
00634     IIIMP_lstat_reply *plr;
00635     char path[PATH_MAX];
00636     char *dirname;
00637     struct stat buf;
00638     iiimp_stat_struct *im_buf;
00639     errno = 0;
00640 
00641     dirname = (char *)calloc((pl->path->len + 1), sizeof(char));
00642 
00643     for (i=0; i<pl->path->len; i++) {
00644         dirname[i] = pl->path->ptr[i];
00645     }
00646     dirname[i] = '\0';
00647 
00648     (void) memset((char *)path, '\0', sizeof(path));
00649     if (strstr(dirname, ".iiim")) {
00650        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00651     } else {
00652         snprintf(path, sizeof(path), "%s",dirname);
00653     }
00654     check_pathname(path);
00655     free(dirname);
00656 
00657     if ((ret = lstat(path, &buf)) < 0) {
00658         plr = iiimp_lstat_reply_new(ph->data_s, ret, errno, (iiimp_stat_struct *)NULL);
00659         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_LSTAT_REPLY, plr);
00660         return iiimcf_send_message(ph, preply, 1);
00661     }
00662 
00663     im_buf = (iiimp_stat_struct *) calloc(1, sizeof(iiimp_stat_struct));
00664 
00665     im_buf->s_mode = buf.st_mode;
00666     im_buf->s_nlink = buf.st_nlink;
00667     im_buf->s_uid = buf.st_uid;
00668     im_buf->s_gid = buf.st_gid;
00669     im_buf->s_atime = buf.st_atime;
00670     im_buf->s_mtime = buf.st_mtime;
00671     im_buf->s_ctime = buf.st_ctime;
00672     im_buf->s_blksize = buf.st_blksize;
00673 
00674     im_buf->s_dev = buf.st_dev;
00675     im_buf->s_rdev = buf.st_rdev;
00676     im_buf->s_ino = buf.st_ino;
00677     im_buf->s_size = buf.st_size;
00678     im_buf->s_blocks = buf.st_blocks;
00679 
00680     plr = iiimp_lstat_reply_new(ph->data_s, pl->ns_id, errno, im_buf);
00681     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_LSTAT_REPLY, plr);
00682     return iiimcf_send_message(ph, preply, 1);
00683 }
00684 
00685 IIIMF_status
00686 iiimcf_fstat(
00687     IIIMCF_handle_rec *ph,
00688     IIIMP_fstat *pf
00689 )
00690 {
00691     int i, ret;
00692     IIIMP_message *preply;
00693     IIIMP_fstat_reply *pfr;
00694     struct stat buf;
00695     iiimp_stat_struct *im_buf;
00696     errno = 0;
00697 
00698     for (i=0; i<ph->num_of_ns; i++) {
00699        if (ph->pns[i].ns_id == pf->ns_id) {
00700            if ((ret = fstat(ph->pns[i].nsc_fd, &buf)) < 0) {
00701               pfr = iiimp_fstat_reply_new(ph->data_s, ret, errno, (iiimp_stat_struct *)NULL);
00702               preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_FSTAT_REPLY, pfr);
00703               return iiimcf_send_message(ph, preply, 1);
00704            } 
00705            im_buf = (iiimp_stat_struct *) calloc(1, sizeof(iiimp_stat_struct));
00706            im_buf->s_mode = buf.st_mode;
00707            im_buf->s_nlink = buf.st_nlink;
00708            im_buf->s_uid = buf.st_uid;
00709            im_buf->s_gid = buf.st_gid;
00710            im_buf->s_atime = buf.st_atime;
00711            im_buf->s_mtime = buf.st_mtime;
00712            im_buf->s_ctime = buf.st_ctime;
00713            im_buf->s_blksize = buf.st_blksize;
00714     
00715            im_buf->s_dev = buf.st_dev;
00716            im_buf->s_rdev = buf.st_rdev;
00717            im_buf->s_ino = buf.st_ino;
00718            im_buf->s_size = buf.st_size;
00719            im_buf->s_blocks = buf.st_blocks;
00720     
00721            pfr = iiimp_fstat_reply_new(ph->data_s, pf->ns_id, errno, im_buf);
00722            preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_FSTAT_REPLY, pfr);
00723            return iiimcf_send_message(ph, preply, 1);
00724        }
00725     }
00726     return IIIMF_STATUS_FAIL;
00727 }
00728 
00729 IIIMF_status
00730 iiimcf_creat(
00731     IIIMCF_handle_rec *ph,
00732     IIIMP_creat *pc        
00733 )
00734 {
00735     int i, ret;
00736     IIIMP_message *preply;
00737     IIIMP_creat_reply *pcr;
00738     char path[PATH_MAX];
00739     char *dirname;
00740     errno = 0;
00741 
00742     dirname = (char *)calloc((pc->path->len + 1), sizeof(char));
00743     for (i=0; i<pc->path->len; i++) {
00744         dirname[i] = pc->path->ptr[i];
00745     }
00746     dirname[i] = '\0';
00747     (void) memset((char *)path, '\0', sizeof(path));
00748     snprintf(path, sizeof(path), "%s",dirname);
00749     if (strstr(dirname, ".iiim")) {
00750        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00751     } else {
00752        snprintf(path, sizeof(path), "%s",dirname);
00753     }
00754     check_pathname(path);
00755     free(dirname);
00756 
00757     if ((ret = creat(path, pc->mode)) < 0) {
00758         pcr = iiimp_creat_reply_new(ph->data_s, ret, errno);
00759         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_CREAT_REPLY, pcr);
00760         return iiimcf_send_message(ph, preply, 1);
00761     }
00762     pcr = iiimp_creat_reply_new(ph->data_s, pc->ns_id, errno);
00763     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_CREAT_REPLY, pcr);
00764     return iiimcf_send_message(ph, preply, 1);
00765 }
00766 
00767 IIIMF_status
00768 iiimcf_lseek(
00769     IIIMCF_handle_rec *ph,
00770     IIIMP_lseek *pl
00771 )
00772 {
00773     int i;
00774     off_t pos = (off_t) -1;
00775     IIIMP_message *preply;
00776     IIIMP_lseek_reply *plr;
00777     errno = 0;
00778 
00779     for (i=0; i<ph->num_of_ns; i++) {
00780        if (ph->pns[i].ns_id == pl->ns_id) {
00781            pos = lseek(ph->pns[i].nsc_fd, pl->offset, pl->whence);
00782             if (pos < 0) {
00783               plr = iiimp_lseek_reply_new(ph->data_s, pl->ns_id, errno, pos);
00784               preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_LSEEK_REPLY, plr);
00785               return iiimcf_send_message(ph, preply, 1);
00786             }
00787            break;
00788        }
00789     }
00790     plr = iiimp_lseek_reply_new(ph->data_s, pl->ns_id, errno, pos);
00791     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_LSEEK_REPLY, plr);
00792     return iiimcf_send_message(ph, preply, 1);
00793 }
00794 
00795 IIIMF_status
00796 iiimcf_unlink(
00797     IIIMCF_handle_rec *ph,
00798     IIIMP_unlink *pu        
00799 )
00800 {
00801     int i, ret;
00802     IIIMP_message *preply;
00803     IIIMP_unlink_reply *pur;
00804     char path[PATH_MAX];
00805     char *dirname;
00806     errno = 0;
00807 
00808     dirname = (char *)calloc((pu->path->len + 1), sizeof(char));
00809     for (i=0; i<pu->path->len; i++) {
00810         dirname[i] = pu->path->ptr[i];
00811     }
00812     dirname[i] = '\0';
00813     (void) memset((char *)path, '\0', sizeof(path));
00814     if (strstr(dirname, ".iiim")) {
00815        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00816     } else {
00817        snprintf(path, sizeof(path), "%s",dirname);
00818     }
00819     check_pathname(path);
00820     free(dirname);
00821 
00822     if ((ret = unlink(path)) < 0) {
00823         pur = iiimp_unlink_reply_new(ph->data_s, ret, errno);
00824         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_UNLINK_REPLY, pur);
00825         return iiimcf_send_message(ph, preply, 1);
00826     }
00827     pur = iiimp_unlink_reply_new(ph->data_s, pu->ns_id, errno);
00828     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_UNLINK_REPLY, pur);
00829     return iiimcf_send_message(ph, preply, 1);
00830 }
00831 
00832 IIIMF_status
00833 iiimcf_rename(
00834     IIIMCF_handle_rec *ph,
00835     IIIMP_rename *pr        
00836 )
00837 {
00838     int i, ret;
00839     IIIMP_message *preply;
00840     IIIMP_rename_reply *prr;
00841     char old_buf[PATH_MAX];
00842     char new_buf[PATH_MAX];
00843     char *old_name;
00844     char *new_name;
00845     errno = 0;
00846 
00847     old_name = (char *)calloc((pr->old_name->len + 1), sizeof(char));
00848     for (i=0; i<pr->old_name->len; i++) {
00849         old_name[i] = pr->old_name->ptr[i];
00850     }
00851     old_name[i] = '\0';
00852     (void) memset((char *)old_buf, '\0', sizeof(old_buf));
00853     if (strstr(old_name, ".iiim")) {
00854        snprintf(old_buf, sizeof(old_buf), "%s/%s",ph->penv->home_dir, old_name);
00855     } else {
00856        snprintf(old_buf, sizeof(old_buf), "%s",old_name);
00857     }
00858     check_pathname(old_buf);
00859 
00860     new_name = (char *)calloc((pr->new_name->len + 1), sizeof(char));
00861     for (i=0; i<pr->new_name->len; i++) {
00862         new_name[i] = pr->new_name->ptr[i];
00863     }
00864     new_name[i] = '\0';
00865     (void) memset((char *)new_buf, '\0', sizeof(new_buf));
00866     snprintf(new_buf, sizeof(new_buf), "%s",new_name);
00867     if (strstr(new_name, ".iiim")) {
00868        snprintf(new_buf, sizeof(new_buf), "%s/%s",ph->penv->home_dir, new_name);
00869     } else {
00870        snprintf(new_buf, sizeof(new_buf), "%s",new_name);
00871     }
00872     check_pathname(new_buf);
00873     free(old_name);
00874     free(new_name);
00875 
00876     if ((ret = rename(old_buf, new_buf)) < 0) {
00877         prr = iiimp_rename_reply_new(ph->data_s, ret, errno);
00878         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY, prr);
00879         return iiimcf_send_message(ph, preply, 1);
00880     }
00881 
00882     prr = iiimp_rename_reply_new(ph->data_s, pr->ns_id, errno);
00883     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY, prr);
00884     return iiimcf_send_message(ph, preply, 1);
00885 }
00886 
00887 IIIMF_status
00888 iiimcf_fcntl(
00889     IIIMCF_handle_rec *ph,
00890     IIIMP_fcntl *pf        
00891 )
00892 {
00893     int i, ret = 0;
00894     IIIMP_message *preply;
00895     IIIMP_fcntl_reply *pfr;
00896     errno = 0;
00897     
00898     for (i=0; i<ph->num_of_ns; i++) {
00899        if (ph->pns[i].ns_id == pf->ns_id) {
00900            if ((ret = fcntl(ph->pns[i].nsc_fd, pf->cmd, pf->arg)) < 0) {
00901               pfr = iiimp_fcntl_reply_new(ph->data_s, pf->ns_id, errno, ret);
00902               preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY, pfr);
00903               return iiimcf_send_message(ph, preply, 1);
00904            }
00905            break;
00906        }
00907     }
00908     pfr = iiimp_fcntl_reply_new(ph->data_s, pf->ns_id, errno, ret);
00909     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY, pfr);
00910     return iiimcf_send_message(ph, preply, 1);
00911 }
00912 
00913 IIIMF_status
00914 iiimcf_truncate(
00915     IIIMCF_handle_rec *ph,
00916     IIIMP_truncate *pt        
00917 )
00918 {
00919     int i, ret;
00920     IIIMP_message *preply;
00921     IIIMP_truncate_reply *ptr;
00922     char path[PATH_MAX];
00923     char *dirname;
00924     errno = 0;
00925 
00926     dirname = (char *)calloc((pt->path->len + 1), sizeof(char));
00927     for (i=0; i<pt->path->len; i++) {
00928         dirname[i] = pt->path->ptr[i];
00929     }
00930     dirname[i] = '\0';
00931     (void) memset((char *)path, '\0', sizeof(path));
00932     if (strstr(dirname, ".iiim")) {
00933        snprintf(path, sizeof(path), "%s/%s",ph->penv->home_dir, dirname);
00934     } else {
00935        snprintf(path, sizeof(path), "%s",dirname);
00936     }
00937     check_pathname(path);
00938     free(dirname);
00939 
00940     if ((ret = truncate(path, pt->length)) < 0) {
00941         ptr = iiimp_truncate_reply_new(ph->data_s, ret, errno);
00942         preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY, ptr);
00943         return iiimcf_send_message(ph, preply, 1);
00944     }
00945     ptr = iiimp_truncate_reply_new(ph->data_s, ret, errno);
00946     preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_RENAME_REPLY, ptr);
00947     return iiimcf_send_message(ph, preply, 1);
00948 }
00949 
00950 IIIMF_status
00951 iiimcf_ftruncate(
00952     IIIMCF_handle_rec *ph,
00953     IIIMP_ftruncate *pf
00954 )
00955 {
00956     int i, ret;
00957     IIIMP_message *preply;
00958     IIIMP_ftruncate_reply *pfr;
00959     errno = 0;
00960 
00961     for (i=0; i<ph->num_of_ns; i++) {
00962        if (ph->pns[i].ns_id == pf->ns_id) {
00963            if ((ret = ftruncate(ph->pns[i].nsc_fd, pf->length)) < 0) {
00964               pfr = iiimp_ftruncate_reply_new(ph->data_s, ret, errno);
00965               preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_FTRUNCATE_REPLY, pfr);
00966               return iiimcf_send_message(ph, preply, 1);
00967            }
00968            pfr = iiimp_ftruncate_reply_new(ph->data_s, ret, errno);
00969            preply = iiimp_file_operation_reply_new(ph->data_s, ph->im_id, IIIMP_FILE_OPERATION_TYPE_FTRUNCATE_REPLY, pfr);
00970            return iiimcf_send_message(ph, preply, 1);
00971        }
00972     }
00973     return IIIMF_STATUS_FAIL;
00974 }
00975 
00976 /* Local Variables: */
00977 /* c-file-style: "iiim-project" */
00978 /* End: */