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 
00077 #include <errno.h>
00078 #include <sys/device.h>
00079 #include <string.h>
00080 
00081 #include <fs/fs.h>
00082 #include <unistd.h>
00083 #include <sys/stat.h>
00084 
00089 
00090 static int PathOperation(CONST char *path, int opcode)
00091 {
00092     NUTDEVICE *dev;
00093     char dev_name[9];
00094     uint8_t nidx;
00095     CONST char *nptr = path;
00096 
00097     /*
00098      * Extract device name.
00099      */
00100     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00101         dev_name[nidx] = *nptr++;
00102     }
00103     dev_name[nidx] = 0;
00104     nptr++;
00105 
00106     /*
00107      * Get device structure of registered device. In later releases we
00108      * try to open a file on a root device.
00109      */
00110     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00111         errno = ENOENT;
00112         return -1;
00113     }
00114     return (*dev->dev_ioctl) (dev, opcode, (void *) nptr);
00115 }
00116 
00131 int access(CONST char *path, int what)
00132 {
00133     struct stat s;
00134 
00135     if (stat(path, &s)) {
00136         return -1;
00137     }
00138     return 0;
00139 }
00140 
00150 long lseek(int fh, long pos, int whence)
00151 {
00152     //IOCTL_ARG3 args;
00153 
00154     //args.arg1 = (void *)fh;
00155     //args.arg2 = (void *)(unsigned int)pos;
00156     //args.arg3 = (void *)whence;
00157     //return (*dev->dev_ioctl) (dev, opcode, (void *)&args);
00158     return -1;
00159 }
00160 
00170 int rmdir(CONST char *path)
00171 {
00172     return PathOperation(path, FS_DIR_REMOVE);
00173 }
00174 
00180 int unlink(CONST char *path)
00181 {
00182     return PathOperation(path, FS_FILE_DELETE);
00183 }
00184 
00185 
00191 int stat(CONST char *path, struct stat *s)
00192 {
00193     NUTDEVICE *dev;
00194     char dev_name[9];
00195     uint8_t nidx;
00196     CONST char *nptr = path;
00197     FSCP_STATUS parms;
00198 
00199     /* Extract the device name. */
00200     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00201         dev_name[nidx] = *nptr++;
00202     }
00203     dev_name[nidx] = 0;
00204 
00205     /* Get device structure of registered device. */
00206     if ((dev = NutDeviceLookup(dev_name)) != 0) {
00207         if (*nptr == ':') {
00208             nptr++;
00209         }
00210         parms.par_path = nptr;
00211         parms.par_stp = s;
00212         return (*dev->dev_ioctl) (dev, FS_STATUS, (void *) &parms);
00213     }
00214     return -1;
00215 }
00216 
00222 int fstat(int fh, struct stat *s)
00223 {
00224     return -1;
00225 }
00226 
00232 int mkdir(CONST char *path, int mode)
00233 {
00234     return PathOperation(path, FS_DIR_CREATE);
00235 }
00236 
00249 int rename(CONST char *old_name, CONST char *new_name)
00250 {
00251     int rc = -1;
00252     NUTDEVICE *dev;
00253     char old_devname[9];
00254     char new_devname[9];
00255     uint8_t nidx;
00256     CONST char *nptr;
00257     FSCP_RENAME parms;   /* Structure used for renaming files. */
00258 
00259     /* Extract old file's device name. */
00260     nptr = old_name;
00261     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00262         old_devname[nidx] = *nptr;
00263     }
00264     old_devname[nidx] = 0;
00265 
00266     /* Make sure a colon follows the device name. */
00267     if (*nptr++ == ':') {
00268         /* Assign the old file's name to the file rename structure. */
00269         parms.par_old = nptr;
00270 
00271         /* Extract new device name. */
00272         nptr = new_name;
00273 
00274         for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00275             new_devname[nidx] = *nptr;
00276         }
00277         new_devname[nidx] = 0;
00278 
00279         /* Make sure a colon follows the device name. */
00280         if (*nptr++ == ':') {
00281             /* Assign the new file's name to the file rename structure. */
00282             parms.par_new = nptr;
00283 
00284             /* Make sure both device names are the same. */
00285             if (strcmp(new_devname, old_devname) == 0) {
00286                 /* Get device structure of registered device. */
00287                 if ((dev = NutDeviceLookup(old_devname)) == 0) {
00288                     errno = ENOENT;
00289                 } else {
00290                     rc = (*dev->dev_ioctl) (dev, FS_RENAME, (void *) &parms);
00291                 }
00292             }
00293         }
00294     }
00295     return rc;
00296 }
00297 

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