Nut/OS  4.10.3
API Reference
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 
00080 // unix emulation requires unistd.h as first include
00081 #include <unistd.h>
00082 
00083 #include <errno.h>
00084 #include <sys/device.h>
00085 #include <string.h>
00086 
00087 #include <fs/fs.h>
00088 #include <sys/stat.h>
00089 
00094 
00095 static int PathOperation(CONST char *path, int opcode)
00096 {
00097     NUTDEVICE *dev;
00098     char dev_name[9];
00099     uint8_t nidx;
00100     CONST char *nptr = path;
00101 
00102     /*
00103      * Extract device name.
00104      */
00105     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00106         dev_name[nidx] = *nptr++;
00107     }
00108     dev_name[nidx] = 0;
00109     nptr++;
00110 
00111     /*
00112      * Get device structure of registered device. In later releases we
00113      * try to open a file on a root device.
00114      */
00115     if ((dev = NutDeviceLookup(dev_name)) == 0) {
00116         errno = ENOENT;
00117         return -1;
00118     }
00119     return (*dev->dev_ioctl) (dev, opcode, (void *) nptr);
00120 }
00121 
00136 int access(CONST char *path, int what)
00137 {
00138     struct stat s;
00139 
00140     if (stat(path, &s)) {
00141         return -1;
00142     }
00143     return 0;
00144 }
00145 
00155 long lseek(int fh, long pos, int whence)
00156 {
00157     //IOCTL_ARG3 args;
00158 
00159     //args.arg1 = (void *)fh;
00160     //args.arg2 = (void *)(unsigned int)pos;
00161     //args.arg3 = (void *)whence;
00162     //return (*dev->dev_ioctl) (dev, opcode, (void *)&args);
00163     return -1;
00164 }
00165 
00175 int rmdir(CONST char *path)
00176 {
00177     return PathOperation(path, FS_DIR_REMOVE);
00178 }
00179 
00185 int unlink(CONST char *path)
00186 {
00187     return PathOperation(path, FS_FILE_DELETE);
00188 }
00189 
00190 
00196 int stat(CONST char *path, struct stat *s)
00197 {
00198     NUTDEVICE *dev;
00199     char dev_name[9];
00200     uint8_t nidx;
00201     CONST char *nptr = path;
00202     FSCP_STATUS parms;
00203 
00204     /* Extract the device name. */
00205     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
00206         dev_name[nidx] = *nptr++;
00207     }
00208     dev_name[nidx] = 0;
00209 
00210     /* Get device structure of registered device. */
00211     if ((dev = NutDeviceLookup(dev_name)) != 0) {
00212         if (*nptr == ':') {
00213             nptr++;
00214         }
00215         parms.par_path = nptr;
00216         parms.par_stp = s;
00217         return (*dev->dev_ioctl) (dev, FS_STATUS, (void *) &parms);
00218     }
00219     return -1;
00220 }
00221 
00227 int fstat(int fh, struct stat *s)
00228 {
00229     return -1;
00230 }
00231 
00237 int mkdir(CONST char *path, int mode)
00238 {
00239     return PathOperation(path, FS_DIR_CREATE);
00240 }
00241 
00254 int rename(CONST char *old_name, CONST char *new_name)
00255 {
00256     int rc = -1;
00257     NUTDEVICE *dev;
00258     char old_devname[9];
00259     char new_devname[9];
00260     uint8_t nidx;
00261     CONST char *nptr;
00262     FSCP_RENAME parms;   /* Structure used for renaming files. */
00263 
00264     /* Extract old file's device name. */
00265     nptr = old_name;
00266     for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00267         old_devname[nidx] = *nptr;
00268     }
00269     old_devname[nidx] = 0;
00270 
00271     /* Make sure a colon follows the device name. */
00272     if (*nptr++ == ':') {
00273         /* Assign the old file's name to the file rename structure. */
00274         parms.par_old = nptr;
00275 
00276         /* Extract new device name. */
00277         nptr = new_name;
00278 
00279         for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
00280             new_devname[nidx] = *nptr;
00281         }
00282         new_devname[nidx] = 0;
00283 
00284         /* Make sure a colon follows the device name. */
00285         if (*nptr++ == ':') {
00286             /* Assign the new file's name to the file rename structure. */
00287             parms.par_new = nptr;
00288 
00289             /* Make sure both device names are the same. */
00290             if (strcmp(new_devname, old_devname) == 0) {
00291                 /* Get device structure of registered device. */
00292                 if ((dev = NutDeviceLookup(old_devname)) == 0) {
00293                     errno = ENOENT;
00294                 } else {
00295                     rc = (*dev->dev_ioctl) (dev, FS_RENAME, (void *) &parms);
00296                 }
00297             }
00298         }
00299     }
00300     return rc;
00301 }
00302