pathops.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004-2007 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Copyright (c) 1991
00005  *      The Regents of the University of California.  All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  *
00011  * 1. Redistributions of source code must retain the above copyright
00012  *    notice, this list of conditions and the following disclaimer.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. Neither the name of the copyright holders nor the names of
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00024  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00027  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00028  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00030  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031  * SUCH DAMAGE.
00032  *
00033  * For additional information see http://www.ethernut.de/
00034  *
00035  */
00036 
00073 #include <errno.h>
00074 #include <sys/device.h>
00075 #include <string.h>
00076 
00077 #include <fs/fs.h>
00078 #include <unistd.h>
00079 #include <sys/stat.h>
00080 
00085 
00086 static int PathOperation(CONST char *path, int opcode)
00087 {
00088     NUTDEVICE *dev;
00089     char dev_name[9];
00090     uint8_t nidx;
00091     CONST char *nptr = path;
00092 
00093     /*
00094      * Extract device name.
00095      */
00096     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00097         dev_name[nidx] = *nptr++;
00098     }
00099     dev_name[nidx] = 0;
00100     nptr++;
00101 
00102     /*
00103      * Get device structure of registered device. In later releases we
00104      * try to open a file on a root device.
00105      */
00106     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00107         errno = ENOENT;
00108         return -1;
00109     }
00110     return (*dev->dev_ioctl) (dev, opcode, (void *) nptr);
00111 }
00112 
00127 int access(CONST char *path, int what)
00128 {
00129     struct stat s;
00130 
00131     if (stat(path, &s)) {
00132         return -1;
00133     }
00134     return 0;
00135 }
00136 
00146 long lseek(int fh, long pos, int whence)
00147 {
00148     //IOCTL_ARG3 args;
00149 
00150     //args.arg1 = (void *)fh;
00151     //args.arg2 = (void *)(u_int)pos;
00152     //args.arg3 = (void *)whence;
00153     //return (*dev->dev_ioctl) (dev, opcode, (void *)&args);
00154     return -1;
00155 }
00156 
00166 int rmdir(CONST char *path)
00167 {
00168     return PathOperation(path, FS_DIR_REMOVE);
00169 }
00170 
00176 int unlink(CONST char *path)
00177 {
00178     return PathOperation(path, FS_FILE_DELETE);
00179 }
00180 
00181 
00187 int stat(CONST char *path, struct stat *s)
00188 {
00189     NUTDEVICE *dev;
00190     char dev_name[9];
00191     uint8_t nidx;
00192     CONST char *nptr = path;
00193     FSCP_STATUS parms;
00194 
00195     /* Extract the device name. */
00196     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00197         dev_name[nidx] = *nptr++;
00198     }
00199     dev_name[nidx] = 0;
00200 
00201     /* Get device structure of registered device. */
00202     if ((dev = NutDeviceLookup(dev_name)) != 0) {
00203         if (*nptr == ':') {
00204             nptr++;
00205         }
00206         parms.par_path = nptr;
00207         parms.par_stp = s;
00208         return (*dev->dev_ioctl) (dev, FS_STATUS, (void *) &parms);
00209     }
00210     return -1;
00211 }
00212 
00218 int fstat(int fh, struct stat *s)
00219 {
00220     return -1;
00221 }
00222 
00228 int mkdir(CONST char *path, int mode)
00229 {
00230     return PathOperation(path, FS_DIR_CREATE);
00231 }
00232 
00245 int rename(CONST char *old_name, CONST char *new_name)
00246 {
00247     int rc = -1;
00248     NUTDEVICE *dev;
00249     char old_devname[9];
00250     char new_devname[9];
00251     uint8_t nidx;
00252     CONST char *nptr;
00253     FSCP_RENAME parms;   /* Structure used for renaming files. */
00254 
00255     /* Extract old file's device name. */
00256     nptr = old_name;
00257     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00258         old_devname[nidx] = *nptr;
00259     }
00260     old_devname[nidx] = 0;
00261 
00262     /* Make sure a colon follows the device name. */
00263     if (*nptr++ == ':') {
00264         /* Assign the old file's name to the file rename structure. */
00265         parms.par_old = nptr;
00266 
00267         /* Extract new device name. */
00268         nptr = new_name;
00269 
00270         for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00271             new_devname[nidx] = *nptr;
00272         }
00273         new_devname[nidx] = 0;
00274 
00275         /* Make sure a colon follows the device name. */
00276         if (*nptr++ == ':') {
00277             /* Assign the new file's name to the file rename structure. */
00278             parms.par_new = nptr;
00279 
00280             /* Make sure both device names are the same. */
00281             if (strcmp(new_devname, old_devname) == 0) {
00282                 /* Get device structure of registered device. */
00283                 if ((dev = NutDeviceLookup(old_devname)) == 0) {
00284                     errno = ENOENT;
00285                 } else {
00286                     rc = (*dev->dev_ioctl) (dev, FS_RENAME, (void *) &parms);
00287                 }
00288             }
00289         }
00290     }
00291     return rc;
00292 }
00293 

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/