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 
00067 #include <errno.h>
00068 #include <sys/device.h>
00069 #include <string.h>
00070 
00071 #include <fs/fs.h>
00072 #include <unistd.h>
00073 #include <sys/stat.h>
00074 
00079 
00080 static int PathOperation(CONST char *path, int opcode)
00081 {
00082     NUTDEVICE *dev;
00083     char dev_name[9];
00084     u_char nidx;
00085     CONST char *nptr = path;
00086 
00087     /*
00088      * Extract device name.
00089      */
00090     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00091         dev_name[nidx] = *nptr++;
00092     }
00093     dev_name[nidx] = 0;
00094     nptr++;
00095 
00096     /*
00097      * Get device structure of registered device. In later releases we
00098      * try to open a file on a root device.
00099      */
00100     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00101         errno = ENOENT;
00102         return -1;
00103     }
00104     return (*dev->dev_ioctl) (dev, opcode, (void *) nptr);
00105 }
00106 
00115 int access(CONST char *path, int what)
00116 {
00117     return -1;
00118 }
00119 
00129 long lseek(int fh, long pos, int whence)
00130 {
00131     //IOCTL_ARG3 args;
00132 
00133     //args.arg1 = (void *)fh;
00134     //args.arg2 = (void *)(u_int)pos;
00135     //args.arg3 = (void *)whence;
00136     //return (*dev->dev_ioctl) (dev, opcode, (void *)&args);
00137     return -1;
00138 }
00139 
00149 int rmdir(CONST char *path)
00150 {
00151     return PathOperation(path, FS_DIR_REMOVE);
00152 }
00153 
00159 int unlink(CONST char *path)
00160 {
00161     return PathOperation(path, FS_FILE_DELETE);
00162 }
00163 
00164 
00170 int stat(CONST char *path, struct stat *s)
00171 {
00172     NUTDEVICE *dev;
00173     char dev_name[9];
00174     u_char nidx;
00175     CONST char *nptr = path;
00176     FSCP_STATUS parms;
00177 
00178     /* Extract the device name. */
00179     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00180         dev_name[nidx] = *nptr++;
00181     }
00182     dev_name[nidx] = 0;
00183 
00184     /* Get device structure of registered device. */
00185     if ((dev = NutDeviceLookup(dev_name)) != 0) {
00186         if (*nptr == ':') {
00187             nptr++;
00188         }
00189         parms.par_path = nptr;
00190         parms.par_stp = s;
00191         return (*dev->dev_ioctl) (dev, FS_STATUS, (void *) &parms);
00192     }
00193     return -1;
00194 }
00195 
00201 int fstat(int fh, struct stat *s)
00202 {
00203     return -1;
00204 }
00205 
00211 int mkdir(CONST char *path, int mode)
00212 {
00213     return PathOperation(path, FS_DIR_CREATE);
00214 }
00215 
00228 int rename(CONST char *old_name, CONST char *new_name)
00229 {
00230     int rc = -1;
00231     NUTDEVICE *dev;
00232     char old_devname[9];
00233     char new_devname[9];
00234     u_char nidx;
00235     CONST char *nptr;
00236     FSCP_RENAME parms;   /* Structure used for renaming files. */
00237 
00238     /* Extract old file's device name. */
00239     nptr = old_name;
00240     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00241         old_devname[nidx] = *nptr;
00242     }
00243     old_devname[nidx] = 0;
00244 
00245     /* Make sure a colon follows the device name. */
00246     if (*nptr++ == ':') {
00247         /* Assign the old file's name to the file rename structure. */
00248         parms.par_old = nptr;
00249 
00250         /* Extract new device name. */
00251         nptr = new_name;
00252 
00253         for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00254             new_devname[nidx] = *nptr;
00255         }
00256         new_devname[nidx] = 0;
00257 
00258         /* Make sure a colon follows the device name. */
00259         if (*nptr++ == ':') {
00260             /* Assign the new file's name to the file rename structure. */
00261             parms.par_new = nptr;
00262 
00263             /* Make sure both device names are the same. */
00264             if (strcmp(new_devname, old_devname) == 0) {
00265                 /* Get device structure of registered device. */
00266                 if ((dev = NutDeviceLookup(old_devname)) == 0) {
00267                     errno = ENOENT;
00268                 } else {
00269                     rc = (*dev->dev_ioctl) (dev, FS_RENAME, (void *) &parms);
00270                 }
00271             }
00272         }
00273     }
00274     return rc;
00275 }
00276 

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