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 
00070 #include <errno.h>
00071 #include <sys/device.h>
00072 #include <string.h>
00073 
00074 #include <fs/fs.h>
00075 #include <unistd.h>
00076 #include <sys/stat.h>
00077 
00082 
00083 static int PathOperation(CONST char *path, int opcode)
00084 {
00085     NUTDEVICE *dev;
00086     char dev_name[9];
00087     u_char nidx;
00088     CONST char *nptr = path;
00089 
00090     /*
00091      * Extract device name.
00092      */
00093     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00094         dev_name[nidx] = *nptr++;
00095     }
00096     dev_name[nidx] = 0;
00097     nptr++;
00098 
00099     /*
00100      * Get device structure of registered device. In later releases we
00101      * try to open a file on a root device.
00102      */
00103     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00104         errno = ENOENT;
00105         return -1;
00106     }
00107     return (*dev->dev_ioctl) (dev, opcode, (void *) nptr);
00108 }
00109 
00124 int access(CONST char *path, int what)
00125 {
00126     struct stat s;
00127 
00128     if (stat(path, &s)) {
00129         return -1;
00130     }
00131     return 0;
00132 }
00133 
00143 long lseek(int fh, long pos, int whence)
00144 {
00145     //IOCTL_ARG3 args;
00146 
00147     //args.arg1 = (void *)fh;
00148     //args.arg2 = (void *)(u_int)pos;
00149     //args.arg3 = (void *)whence;
00150     //return (*dev->dev_ioctl) (dev, opcode, (void *)&args);
00151     return -1;
00152 }
00153 
00163 int rmdir(CONST char *path)
00164 {
00165     return PathOperation(path, FS_DIR_REMOVE);
00166 }
00167 
00173 int unlink(CONST char *path)
00174 {
00175     return PathOperation(path, FS_FILE_DELETE);
00176 }
00177 
00178 
00184 int stat(CONST char *path, struct stat *s)
00185 {
00186     NUTDEVICE *dev;
00187     char dev_name[9];
00188     u_char nidx;
00189     CONST char *nptr = path;
00190     FSCP_STATUS parms;
00191 
00192     /* Extract the device name. */
00193     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00194         dev_name[nidx] = *nptr++;
00195     }
00196     dev_name[nidx] = 0;
00197 
00198     /* Get device structure of registered device. */
00199     if ((dev = NutDeviceLookup(dev_name)) != 0) {
00200         if (*nptr == ':') {
00201             nptr++;
00202         }
00203         parms.par_path = nptr;
00204         parms.par_stp = s;
00205         return (*dev->dev_ioctl) (dev, FS_STATUS, (void *) &parms);
00206     }
00207     return -1;
00208 }
00209 
00215 int fstat(int fh, struct stat *s)
00216 {
00217     return -1;
00218 }
00219 
00225 int mkdir(CONST char *path, int mode)
00226 {
00227     return PathOperation(path, FS_DIR_CREATE);
00228 }
00229 
00242 int rename(CONST char *old_name, CONST char *new_name)
00243 {
00244     int rc = -1;
00245     NUTDEVICE *dev;
00246     char old_devname[9];
00247     char new_devname[9];
00248     u_char nidx;
00249     CONST char *nptr;
00250     FSCP_RENAME parms;   /* Structure used for renaming files. */
00251 
00252     /* Extract old file's device name. */
00253     nptr = old_name;
00254     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00255         old_devname[nidx] = *nptr;
00256     }
00257     old_devname[nidx] = 0;
00258 
00259     /* Make sure a colon follows the device name. */
00260     if (*nptr++ == ':') {
00261         /* Assign the old file's name to the file rename structure. */
00262         parms.par_old = nptr;
00263 
00264         /* Extract new device name. */
00265         nptr = new_name;
00266 
00267         for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00268             new_devname[nidx] = *nptr;
00269         }
00270         new_devname[nidx] = 0;
00271 
00272         /* Make sure a colon follows the device name. */
00273         if (*nptr++ == ':') {
00274             /* Assign the new file's name to the file rename structure. */
00275             parms.par_new = nptr;
00276 
00277             /* Make sure both device names are the same. */
00278             if (strcmp(new_devname, old_devname) == 0) {
00279                 /* Get device structure of registered device. */
00280                 if ((dev = NutDeviceLookup(old_devname)) == 0) {
00281                     errno = ENOENT;
00282                 } else {
00283                     rc = (*dev->dev_ioctl) (dev, FS_RENAME, (void *) &parms);
00284                 }
00285             }
00286         }
00287     }
00288     return rc;
00289 }
00290 

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