Back to index

im-sdk  12.3.91
fop_file.c
Go to the documentation of this file.
00001 /*
00002  * fop_file.c: iml file operation - ``file'' scheme
00003  */
00004 
00005 #include <stdlib.h>
00006 #include <string.h>
00007 #include <stdio.h>
00008 #include <stdarg.h>
00009 #include <unistd.h>
00010 #include <fcntl.h>
00011 #include <errno.h>
00012 #include <dirent.h>
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <ulimit.h>
00016 #include <utime.h>
00017 #include <sys/time.h>
00018 #include <sys/uio.h>
00019 
00020 #include "fop.h"
00021 
00022 
00023 typedef struct {
00024     char *           name;
00025     void *           function;
00026 } fop_name_function_t;
00027 
00028 
00029 typedef union {
00030     int              fd;
00031     DIR *     dirp;
00032 } fop_data_t;
00033 
00034 
00035 static fop_data_t *
00036 fop_open(
00037     fopc_t *         fopc,
00038     const char *     path,
00039     int                     oflag,
00040     mode_t           mode
00041 )
00042 {
00043     int                     fd;
00044     fop_data_t *     data;
00045 
00046     fd = open(path, oflag, mode);
00047     if (0 <= fd) {
00048        data = (fop_data_t *)malloc(sizeof (fop_data_t));
00049        if (NULL == data) {
00050            close(fd);
00051            errno = EAGAIN;
00052            return NULL;
00053        }
00054        data->fd = fd;
00055        return data;
00056     } else {
00057        return NULL;
00058     }
00059 }
00060 
00061 
00062 static int
00063 fop_close(
00064     fopc_t *         fopc,
00065     fop_data_t *     data
00066 )
00067 {
00068     int              fd;
00069 
00070     if (NULL == data) {
00071        errno = EBADF;
00072        return -1;
00073     }
00074 
00075     fd = data->fd;
00076     free(data);
00077 
00078     return close(fd);
00079 }
00080 
00081 
00082 static fop_data_t *
00083 fop_creat(
00084     fopc_t *         fopc,
00085     const char *     path,
00086     mode_t           mode
00087 )
00088 {
00089     int                     fd;
00090     fop_data_t *     data;
00091 
00092     fd = creat(path, mode);
00093     if (0 <= fd) {
00094        data = (fop_data_t *)malloc(sizeof (fop_data_t));
00095        if (NULL == data) {
00096            close(fd);
00097            errno = EAGAIN;
00098            return NULL;
00099        }
00100        data->fd = fd;
00101        return data;
00102     } else {
00103        return NULL;
00104     }
00105 }
00106 
00107 
00108 static size_t
00109 fop_read(
00110     fopc_t *         fopc,
00111     fop_data_t *     data,
00112     void *           buf,
00113     size_t           nbyte
00114 )
00115 {
00116     if (NULL == data) {
00117        errno = EBADF;
00118        return -1;
00119     }
00120 
00121     return read(data->fd, buf, nbyte);
00122 }
00123 
00124 
00125 #if defined(fop_not_defined)
00126 static int
00127 fop_readv(
00128     fopc_t *                fopc,
00129     fop_data_t *            data,
00130     const struct iovec *    iov,
00131     int                            iovcnt
00132 )
00133 {
00134     if (NULL == data) {
00135        errno = EBADF;
00136        return -1;
00137     }
00138 
00139     return readv(data->fd, iov, iovcnt);
00140 }
00141 #endif /* fop_not_defined */
00142 
00143 
00144 static size_t
00145 fop_write(
00146     fopc_t *         fopc,
00147     fop_data_t *     data,
00148     const void *     buf,
00149     size_t           nbyte
00150 )
00151 {
00152     if (NULL == data) {
00153        errno = EBADF;
00154        return -1;
00155     }
00156 
00157     return write(data->fd, buf, nbyte);
00158 }
00159 
00160 
00161 #if defined(fop_not_defined)
00162 static int
00163 fop_writev(
00164     fopc_t *                fopc,
00165     fop_data_t *            data,
00166     const struct iovec *    iov,
00167     int                            iovcnt
00168 )
00169 {
00170     if (NULL == data) {
00171        errno = EBADF;
00172        return -1;
00173     }
00174 
00175     return writev(data->fd, iov, iovcnt);
00176 }
00177 #endif /* fop_not_defined */
00178 
00179 
00180 static off_t
00181 fop_lseek(
00182     fopc_t *         fopc,
00183     fop_data_t *     data,
00184     off_t            offset,
00185     int                     whence
00186 )
00187 {
00188     if (NULL == data) {
00189        errno = EBADF;
00190        return -1;
00191     }
00192 
00193     return lseek(data->fd, offset, whence);
00194 }
00195 
00196 
00197 static int
00198 fop_stat(
00199     fopc_t *         fopc,
00200     const char *     path,
00201     struct stat *    buf
00202 )
00203 {
00204     return stat(path, buf);
00205 }
00206 
00207 
00208 static int
00209 fop_lstat(
00210     fopc_t *         fopc,
00211     const char *     path,
00212     struct stat *    buf
00213 )
00214 {
00215     return lstat(path, buf);
00216 }
00217 
00218 
00219 static int
00220 fop_fstat(
00221     fopc_t *         fopc,
00222     fop_data_t *     data,
00223     struct stat *    buf
00224 )
00225 {
00226     if (NULL == data) {
00227        errno = EBADF;
00228        return -1;
00229     }
00230 
00231     return fstat(data->fd, buf);
00232 }
00233 
00234 
00235 static int
00236 fop_symlink(
00237     fopc_t *         fopc,
00238     const char *     name1,
00239     const char *     name2
00240 )
00241 {
00242     return symlink(name1, name2);
00243 }
00244 
00245 
00246 static int
00247 fop_readlink(
00248     fopc_t *         fopc,
00249     const char *     path,
00250     char *           buf,
00251     size_t           bufsiz
00252 )
00253 {
00254     return readlink(path, buf, bufsiz);
00255 }
00256 
00257 
00258 static int
00259 fop_link(
00260     fopc_t *         fopc,
00261     const char *     existing,
00262     const char *     new
00263 )
00264 {
00265     return link(existing, new);
00266 }
00267 
00268 
00269 static int
00270 fop_unlink(
00271     fopc_t *         fopc,
00272     const char *     path
00273 )
00274 {
00275     return unlink(path);
00276 }
00277 
00278 
00279 static int
00280 fop_rename(
00281     fopc_t *         fopc,
00282     const char *     old,
00283     const char *     new
00284 )
00285 {
00286     return rename(old, new);
00287 }
00288 
00289 
00290 static int
00291 fop_fcntl(
00292     fopc_t *         fopc,
00293     fop_data_t *     data,
00294     int                     cmd,
00295     ...
00296 )
00297 {
00298     va_list          ap;
00299     int                     arg_int;
00300     struct flock *   arg_flock;
00301 #if defined(F_FREESP64) || defined(F_GETLK64) || \
00302     defined(F_SETLK64) || defined(F_SETLKW64)
00303     struct flock64 * arg_flock64;
00304 #endif /* F_FREESP64 || F_GETLK64 || F_SETLK64 || F_SETLKW64 */
00305     int                     rv;
00306 
00307     if (NULL == data) {
00308        errno = EBADF;
00309        return -1;
00310     }
00311 
00312     va_start(ap, cmd);
00313     switch(cmd) {
00314     case F_DUPFD:
00315 #if defined(F_DUP2FD)
00316     case F_DUP2FD:
00317 #endif /* F_DUP2FD */
00318     case F_SETFD:
00319     case F_SETFL:
00320     case F_SETOWN:
00321        arg_int = va_arg(ap, int);
00322        rv = fcntl(data->fd, cmd, arg_int);
00323        break;
00324 #if defined(F_FREESP)
00325     case F_FREESP:
00326 #endif /* F_FREESP */
00327     case F_GETLK:
00328     case F_SETLK:
00329     case F_SETLKW:
00330        arg_flock = va_arg(ap, struct flock *);
00331        rv = fcntl(data->fd, cmd, arg_flock);
00332        break;
00333 #if __WORDSIZE != 64 && !defined(__USE_FILE_OFFSET64)
00334        /* When __WORDSIZE is 64 or __USE_FILE_OFFSET64 is defined,
00335           F_*64 has similar number to F_*(non-64) */
00336 #if defined(F_FREESP64)
00337     case F_FREESP64:
00338 #endif /* F_FREESP64 */
00339 #if defined(F_GETLK64)
00340     case F_GETLK64:
00341 #endif /* F_GETLK64 */
00342 #if defined(F_SETLK64)
00343     case F_SETLK64:
00344 #endif /* F_SETLK64 */
00345 #if defined(F_SETLKW64)
00346     case F_SETLKW64:
00347 #endif /* F_SETLKW64 */
00348 
00349 #if defined(F_FREESP64) || defined(F_GETLK64) || defined(F_SETLK64) || defined(F_SETLKW64)
00350        arg_flock64 = va_arg(ap, struct flock64 *);
00351        rv = fcntl(data->fd, cmd, arg_flock64);
00352        break;
00353 #endif /* F_FREESP64 || F_GETLK64 || F_SETLK64 || F_SETLKW64 */
00354 #endif
00355     default:
00356        rv = fcntl(data->fd, cmd, 0);
00357        break;
00358     }
00359     va_end(ap);
00360 
00361     return rv;
00362 }
00363 
00364 
00365 static int
00366 fop_truncate(
00367     fopc_t *         fopc,
00368     const char *     path,
00369     off_t            length
00370 )
00371 {
00372     return truncate(path, length);
00373 }
00374 
00375 
00376 static int
00377 fop_ftruncate(
00378     fopc_t *         fopc,
00379     fop_data_t *     data,
00380     off_t            length
00381 )
00382 {
00383     if (NULL == data) {
00384        errno = EBADF;
00385        return -1;
00386     }
00387 
00388     return ftruncate(data->fd, length);
00389 }
00390 
00391 
00392 static int
00393 fop_mkdir(
00394     fopc_t *         fopc,
00395     const char *     path,
00396     mode_t           mode
00397 )
00398 {
00399     return mkdir(path, mode);
00400 }
00401 
00402 
00403 static int
00404 fop_rmdir(
00405     fopc_t *         fopc,
00406     const char *     path
00407 )
00408 {
00409     return rmdir(path);
00410 }
00411 
00412 
00413 static fop_data_t *
00414 fop_opendir(
00415     fopc_t *         fopc,
00416     const char *     dirname
00417 )
00418 {
00419     DIR *            dirp;
00420     fop_data_t *     data;
00421 
00422     dirp = opendir(dirname);
00423     if (NULL != dirp) {
00424        data = (fop_data_t *)malloc(sizeof (fop_data_t));
00425        if (NULL == data) {
00426            closedir(dirp);
00427            errno = EAGAIN;
00428            return NULL;
00429        }
00430        data->dirp = dirp;
00431        return data;
00432     } else {
00433        return NULL;
00434     }
00435 
00436 }
00437 
00438 
00439 static struct dirent *
00440 fop_readdir(
00441     fopc_t *         fopc,
00442     fop_data_t *     data
00443 )
00444 {
00445     if (NULL == data) {
00446        errno = EBADF;
00447        return NULL;
00448     }
00449 
00450     return readdir(data->dirp);
00451 }
00452 
00453 
00454 static int
00455 fop_closedir(
00456     fopc_t *         fopc,
00457     fop_data_t *     data
00458 )
00459 {
00460     DIR *     dirp;
00461 
00462     if (NULL == data) {
00463        errno = EBADF;
00464        return -1;
00465     }
00466 
00467     dirp = data->dirp;
00468     free(data);
00469 
00470     return closedir(dirp);
00471 }
00472 
00473 
00474 static void
00475 fop_rewinddir(
00476     fopc_t *         fopc,
00477     fop_data_t *     data
00478 )
00479 {
00480     if (NULL == data) {
00481        errno = EBADF;
00482        return;
00483     }
00484 
00485     rewinddir(data->dirp);
00486     return;
00487 }
00488 
00489 
00490 static void
00491 fop_seekdir(
00492     fopc_t *         fopc,
00493     fop_data_t *     data,
00494     long int         loc
00495 )
00496 {
00497     if (NULL == data) {
00498        errno = EBADF;
00499        return;
00500     }
00501 
00502     seekdir(data->dirp, loc);
00503     return;
00504 }
00505 
00506 
00507 static long int
00508 fop_telldir(
00509     fopc_t *         fopc,
00510     fop_data_t *     data
00511 )
00512 {
00513     if (NULL == data) {
00514        errno = EBADF;
00515        return 0;
00516     }
00517 
00518     return telldir(data->dirp);
00519 }
00520 
00521 
00522 static int
00523 fop_access(
00524     fopc_t *         fopc,
00525     const char *     path,
00526     int                     amode
00527 )
00528 {
00529     return access(path, amode);
00530 }
00531 
00532 
00533 static int
00534 fop_chmod(
00535     fopc_t *         fopc,
00536     const char *     path,
00537     mode_t           mode
00538 )
00539 {
00540     return chmod(path, mode);
00541 }
00542 
00543 
00544 static int
00545 fop_fchmod(
00546     fopc_t *         fopc,
00547     fop_data_t *     data,
00548     mode_t           mode
00549 )
00550 {
00551     if (NULL == data) {
00552        errno = EBADF;
00553        return -1;
00554     }
00555 
00556     return fchmod(data->fd, mode);
00557 }
00558 
00559 
00560 static int
00561 fop_chown(
00562     fopc_t *         fopc,
00563     const char *     path,
00564     uid_t            owner,
00565     gid_t            group
00566 )
00567 {
00568     return chown(path, owner, group);
00569 }
00570 
00571 
00572 #if defined(fop_not_defined)
00573 static int
00574 fop_lchown(
00575     fopc_t *         fopc,
00576     const char *     path,
00577     uid_t            owner,
00578     gid_t            group
00579 )
00580 {
00581     return lchown(path, owner, group);
00582 }
00583 #endif /* fop_not_defined */
00584 
00585 
00586 static int
00587 fop_fchown(
00588     fopc_t *         fopc,
00589     fop_data_t *     data,
00590     uid_t            owner,
00591     gid_t            group
00592 )
00593 {
00594     if (NULL == data) {
00595        errno = EBADF;
00596        return -1;
00597     }
00598 
00599     return fchown(data->fd, owner, group);
00600 }
00601 
00602 
00603 #if defined(fop_not_defined)
00604 static long
00605 fop_pathconf(
00606     fopc_t *         fopc,
00607     const char *     path,
00608     int                     name
00609 )
00610 {
00611     return pathconf(path, name);
00612 }
00613 #endif /* fop_not_defined */
00614 
00615 
00616 #if defined(fop_not_defined)
00617 static long
00618 fop_fpathconf(
00619     fopc_t *         fopc,
00620     fop_data_t *     data,
00621     int                     name
00622 )
00623 {
00624     if (NULL == data) {
00625        errno = EBADF;
00626        return -1;
00627     }
00628 
00629     return fpathconf(data->fd, name);
00630 }
00631 #endif /* fop_not_defined */
00632 
00633 
00634 static int
00635 fop_utime(
00636     fopc_t *                fopc,
00637     const char *            path,
00638     const struct utimbuf *  times
00639 )
00640 {
00641     return utime(path, times);
00642 }
00643 
00644 
00645 static int
00646 fop_utimes(
00647     fopc_t *                fopc,
00648     const char *            path,
00649     const struct timeval *  times
00650 )
00651 {
00652     return utimes(path, times);
00653 }
00654 
00655 
00656 static fop_name_function_t fop_name_function_list[] = {
00657        {"close",     (void *)fop_close},
00658        {"creat",     (void *)fop_creat},
00659        {"ftruncate", (void *)fop_ftruncate},
00660        {"lseek",     (void *)fop_lseek},
00661        {"open",      (void *)fop_open},
00662        {"read",      (void *)fop_read},
00663        {"truncate",  (void *)fop_truncate},
00664        {"write",     (void *)fop_write},
00665 
00666        {"link",      (void *)fop_link},
00667        {"readlink",  (void *)fop_readlink},
00668        {"rename",    (void *)fop_rename},
00669        {"symlink",   (void *)fop_symlink},
00670        {"unlink",    (void *)fop_unlink},
00671 
00672        {"chmod",     (void *)fop_chmod},
00673        {"chown",     (void *)fop_chown},
00674        {"fchmod",    (void *)fop_fchmod},
00675        {"fchown",    (void *)fop_fchown},
00676 #if defined(fop_not_defined)
00677        {"lchown",    (void *)fop_lchown},
00678 #endif /* fop_not_defined */
00679 
00680        {"utime",     (void *)fop_utime},
00681        {"utimes",    (void *)fop_utimes},
00682 
00683        {"fstat",     (void *)fop_fstat},
00684        {"lstat",     (void *)fop_lstat},
00685        {"stat",      (void *)fop_stat},
00686        {"access",    (void *)fop_access},
00687 
00688        {"closedir",  (void *)fop_closedir},
00689        {"opendir",   (void *)fop_opendir},
00690        {"readdir",   (void *)fop_readdir},
00691        {"rewinddir", (void *)fop_rewinddir},
00692        {"seekdir",   (void *)fop_seekdir},
00693        {"telldir",   (void *)fop_telldir},
00694 
00695        {"mkdir",     (void *)fop_mkdir},
00696        {"rmdir",     (void *)fop_rmdir},
00697 
00698        {"fcntl",     (void *)fop_fcntl},
00699 
00700 #if defined(fop_not_defined)
00701        {"fpathconf", (void *)fop_fpathconf},
00702        {"pathconf",  (void *)fop_pathconf},
00703        {"readv",     (void *)fop_readv},
00704        {"writev",    (void *)fop_writev},
00705 #endif /* fop_not_defined */
00706 
00707        {NULL,        (void *)NULL},
00708 };
00709 
00710 
00711 void *
00712 fop_scheme_file_get_function(
00713     const char *     name
00714 )
00715 {
00716     fop_name_function_t *   nf;
00717 
00718     for (nf = fop_name_function_list; NULL != nf->name; nf += 1) {
00719        if (0 == strcmp(nf->name, name)) return nf->function;
00720     }
00721 
00722     return NULL;
00723 }
00724 
00725 
00726 /* Local Variables: */
00727 /* c-file-style: "iiim-project" */
00728 /* End: */