pathops.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (C) 2004-2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Copyright (c) 1991
  5. * The Regents of the University of California. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. */
  36. /*!
  37. * \file fs/pathops.c
  38. * \brief File system path operations.
  39. *
  40. * \verbatim
  41. *
  42. * $Log$
  43. * Revision 1.10 2009/03/05 22:02:26 freckle
  44. * unix emulation requires unistd.h as first include
  45. *
  46. * Revision 1.9 2009/01/17 11:26:46 haraldkipp
  47. * Getting rid of two remaining BSD types in favor of stdint.
  48. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  49. *
  50. * Revision 1.8 2008/08/11 06:59:42 haraldkipp
  51. * BSD types replaced by stdint types (feature request #1282721).
  52. *
  53. * Revision 1.7 2008/04/01 10:16:02 haraldkipp
  54. * Implemented access() function.
  55. *
  56. * Revision 1.6 2007/08/29 13:34:13 haraldkipp
  57. * Added function for renaming files (contrib by ZACK).
  58. *
  59. * Revision 1.5 2006/10/08 16:48:09 haraldkipp
  60. * Documentation fixed
  61. *
  62. * Revision 1.4 2006/10/05 17:22:35 haraldkipp
  63. * Fixes bug #1281167. Thanks to Jukka Holappa.
  64. *
  65. * Revision 1.3 2006/03/16 15:25:24 haraldkipp
  66. * Changed human readable strings from u_char to char to stop GCC 4 from
  67. * nagging about signedness.
  68. *
  69. * Revision 1.2 2006/01/05 16:52:23 haraldkipp
  70. * The argument for the FS_STATUS ioctl now uses an individual structure.
  71. *
  72. * Revision 1.1 2005/02/05 20:35:21 haraldkipp
  73. * Peanut added
  74. *
  75. *
  76. * \endverbatim
  77. */
  78. // unix emulation requires unistd.h as first include
  79. #include <unistd.h>
  80. #include <errno.h>
  81. #include <sys/device.h>
  82. #include <string.h>
  83. #include <unistd.h>
  84. #include <fs/fs.h>
  85. #include <sys/stat.h>
  86. /*!
  87. * \addtogroup xgFS
  88. */
  89. /*@{*/
  90. static int PathOperation(const char *path, int opcode)
  91. {
  92. NUTDEVICE *dev;
  93. char dev_name[9];
  94. uint8_t nidx;
  95. const char *nptr = path;
  96. /*
  97. * Extract device name.
  98. */
  99. for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
  100. dev_name[nidx] = *nptr++;
  101. }
  102. dev_name[nidx] = 0;
  103. nptr++;
  104. /*
  105. * Get device structure of registered device. In later releases we
  106. * try to open a file on a root device.
  107. */
  108. if ((dev = NutDeviceLookup(dev_name)) == 0) {
  109. errno = ENOENT;
  110. return -1;
  111. }
  112. return (*dev->dev_ioctl) (dev, opcode, (void *) nptr);
  113. }
  114. /*!
  115. * \brief Check the accessibility of a file.
  116. *
  117. * \param path Pathname of the file to check.
  118. * \param what Access permission to check. Set to F_OK for existence
  119. * check or any of the following values or'ed:
  120. * - R_OK checks read permission
  121. * - W_OK checks write permission
  122. * - X_OK checks execute permission
  123. *
  124. * \return 0 on success, otherwise -1 is returned.
  125. *
  126. * \note Access permissions are not supported by all file systems.
  127. */
  128. int access(const char *path, int what)
  129. {
  130. struct stat s;
  131. if (stat(path, &s)) {
  132. return -1;
  133. }
  134. return 0;
  135. }
  136. /*!
  137. * \brief Reposition a file pointer.
  138. *
  139. * \param fh Handle of an open file.
  140. * \param pos Positioning value.
  141. * \param whence Positioning directive.
  142. *
  143. * \return Always -1 due to missing implementation.
  144. */
  145. long lseek(int fh, long pos, int whence)
  146. {
  147. //IOCTL_ARG3 args;
  148. //args.arg1 = (void *)fh;
  149. //args.arg2 = (void *)(unsigned int)pos;
  150. //args.arg3 = (void *)whence;
  151. //return (*dev->dev_ioctl) (dev, opcode, (void *)&args);
  152. return -1;
  153. }
  154. /*!
  155. * \brief Remove a directory.
  156. *
  157. * \param path Pathname of the directory. Must be the full pathname
  158. * including the device, because Nut/OS doesn't support
  159. * relative paths.
  160. *
  161. * \return 0 if the remove succeeds, otherwise -1 is returned.
  162. */
  163. int rmdir(const char *path)
  164. {
  165. return PathOperation(path, FS_DIR_REMOVE);
  166. }
  167. /*!
  168. * \brief Remove a file entry.
  169. *
  170. * \return 0 if the remove succeeds, otherwise -1 is returned.
  171. */
  172. int unlink(const char *path)
  173. {
  174. return PathOperation(path, FS_FILE_DELETE);
  175. }
  176. /*!
  177. * \brief Get information about a specified file.
  178. *
  179. * \return 0 if the query succeeds, otherwise -1 is returned.
  180. */
  181. int stat(const char *path, struct stat *s)
  182. {
  183. NUTDEVICE *dev;
  184. char dev_name[9];
  185. uint8_t nidx;
  186. const char *nptr = path;
  187. FSCP_STATUS parms;
  188. /* Extract the device name. */
  189. for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++) {
  190. dev_name[nidx] = *nptr++;
  191. }
  192. dev_name[nidx] = 0;
  193. /* Get device structure of registered device. */
  194. if ((dev = NutDeviceLookup(dev_name)) != 0) {
  195. if (*nptr == ':') {
  196. nptr++;
  197. }
  198. parms.par_path = nptr;
  199. parms.par_stp = s;
  200. return (*dev->dev_ioctl) (dev, FS_STATUS, (void *) &parms);
  201. }
  202. return -1;
  203. }
  204. /*!
  205. * \brief Get information about an opened file.
  206. *
  207. * \return Always -1 due to missing implementation.
  208. */
  209. int fstat(int fh, struct stat *s)
  210. {
  211. return -1;
  212. }
  213. /*!
  214. * \brief Create a directory entry.
  215. *
  216. * \return 0 on success, otherwise -1 is returned.
  217. */
  218. int mkdir(const char *path, int mode)
  219. {
  220. return PathOperation(path, FS_DIR_CREATE);
  221. }
  222. /*!
  223. * \brief Rename a file.
  224. *
  225. * New and old filename must contain the name of a registered device,
  226. * followed by a colon and a filename. Moving a file from one device
  227. * to another is not supported.
  228. *
  229. * \param old_name Pathname of an existing file.
  230. * \param new_name New pathname.
  231. *
  232. * \return 0 for success or -1 to indicate an error.
  233. */
  234. int rename(const char *old_name, const char *new_name)
  235. {
  236. int rc = -1;
  237. NUTDEVICE *dev;
  238. char old_devname[9];
  239. char new_devname[9];
  240. uint8_t nidx;
  241. const char *nptr;
  242. FSCP_RENAME parms; /* Structure used for renaming files. */
  243. /* Extract old file's device name. */
  244. nptr = old_name;
  245. for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
  246. old_devname[nidx] = *nptr;
  247. }
  248. old_devname[nidx] = 0;
  249. /* Make sure a colon follows the device name. */
  250. if (*nptr++ == ':') {
  251. /* Assign the old file's name to the file rename structure. */
  252. parms.par_old = nptr;
  253. /* Extract new device name. */
  254. nptr = new_name;
  255. for (nidx = 0; *nptr && *nptr != ':' && nidx < 8; nidx++, nptr++) {
  256. new_devname[nidx] = *nptr;
  257. }
  258. new_devname[nidx] = 0;
  259. /* Make sure a colon follows the device name. */
  260. if (*nptr++ == ':') {
  261. /* Assign the new file's name to the file rename structure. */
  262. parms.par_new = nptr;
  263. /* Make sure both device names are the same. */
  264. if (strcmp(new_devname, old_devname) == 0) {
  265. /* Get device structure of registered device. */
  266. if ((dev = NutDeviceLookup(old_devname)) == 0) {
  267. errno = ENOENT;
  268. } else {
  269. rc = (*dev->dev_ioctl) (dev, FS_RENAME, (void *) &parms);
  270. }
  271. }
  272. }
  273. }
  274. return rc;
  275. }
  276. /*@}*/