dfs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * File : dfs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2005-02-22 Bernard The first version.
  23. */
  24. #include <dfs.h>
  25. #include <dfs_fs.h>
  26. #include <dfs_file.h>
  27. /* Global variables */
  28. const struct dfs_filesystem_operation *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
  29. struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
  30. /* device filesystem lock */
  31. static struct rt_mutex fslock;
  32. #ifdef DFS_USING_WORKDIR
  33. char working_directory[DFS_PATH_MAX] = {"/"};
  34. #endif
  35. #ifdef DFS_USING_STDIO
  36. struct dfs_fd fd_table[3 + DFS_FD_MAX];
  37. #else
  38. struct dfs_fd fd_table[DFS_FD_MAX];
  39. #endif
  40. /**
  41. * @addtogroup DFS
  42. */
  43. /*@{*/
  44. /**
  45. * this function will initialize device file system.
  46. */
  47. int dfs_init(void)
  48. {
  49. /* clear filesystem operations table */
  50. rt_memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
  51. /* clear filesystem table */
  52. rt_memset(filesystem_table, 0, sizeof(filesystem_table));
  53. /* clean fd table */
  54. rt_memset(fd_table, 0, sizeof(fd_table));
  55. /* create device filesystem lock */
  56. rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
  57. #ifdef DFS_USING_WORKDIR
  58. /* set current working directory */
  59. rt_memset(working_directory, 0, sizeof(working_directory));
  60. working_directory[0] = '/';
  61. #endif
  62. return 0;
  63. }
  64. INIT_COMPONENT_EXPORT(dfs_init);
  65. /**
  66. * this function will lock device file system.
  67. *
  68. * @note please don't invoke it on ISR.
  69. */
  70. void dfs_lock(void)
  71. {
  72. rt_err_t result;
  73. result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
  74. if (result != RT_EOK)
  75. {
  76. RT_ASSERT(0);
  77. }
  78. }
  79. /**
  80. * this function will lock device file system.
  81. *
  82. * @note please don't invoke it on ISR.
  83. */
  84. void dfs_unlock(void)
  85. {
  86. rt_mutex_release(&fslock);
  87. }
  88. /**
  89. * @ingroup Fd
  90. * This function will allocate a file descriptor.
  91. *
  92. * @return -1 on failed or the allocated file descriptor.
  93. */
  94. int fd_new(void)
  95. {
  96. struct dfs_fd *d;
  97. int idx;
  98. /* lock filesystem */
  99. dfs_lock();
  100. /* find an empty fd entry */
  101. #ifdef DFS_USING_STDIO
  102. for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++);
  103. #else
  104. for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
  105. #endif
  106. /* can't find an empty fd entry */
  107. #ifdef DFS_USING_STDIO
  108. if (idx == DFS_FD_MAX + 3)
  109. #else
  110. if (idx == DFS_FD_MAX)
  111. #endif
  112. {
  113. idx = -1;
  114. goto __result;
  115. }
  116. d = &(fd_table[idx]);
  117. d->ref_count = 1;
  118. d->magic = DFS_FD_MAGIC;
  119. __result:
  120. dfs_unlock();
  121. return idx;
  122. }
  123. /**
  124. * @ingroup Fd
  125. *
  126. * This function will return a file descriptor structure according to file
  127. * descriptor.
  128. *
  129. * @return NULL on on this file descriptor or the file descriptor structure
  130. * pointer.
  131. */
  132. struct dfs_fd *fd_get(int fd)
  133. {
  134. struct dfs_fd *d;
  135. #ifdef DFS_USING_STDIO
  136. if (fd < 3 || fd >= DFS_FD_MAX + 3)
  137. return RT_NULL;
  138. #else
  139. if (fd < 0 || fd >= DFS_FD_MAX)
  140. return RT_NULL;
  141. #endif
  142. dfs_lock();
  143. d = &fd_table[fd];
  144. /* check dfs_fd valid or not */
  145. if (d->magic != DFS_FD_MAGIC)
  146. {
  147. dfs_unlock();
  148. return RT_NULL;
  149. }
  150. /* increase the reference count */
  151. d->ref_count ++;
  152. dfs_unlock();
  153. return d;
  154. }
  155. /**
  156. * @ingroup Fd
  157. *
  158. * This function will put the file descriptor.
  159. */
  160. void fd_put(struct dfs_fd *fd)
  161. {
  162. RT_ASSERT(fd != RT_NULL);
  163. dfs_lock();
  164. fd->ref_count --;
  165. /* clear this fd entry */
  166. if (fd->ref_count == 0)
  167. {
  168. rt_memset(fd, 0, sizeof(struct dfs_fd));
  169. }
  170. dfs_unlock();
  171. };
  172. /**
  173. * @ingroup Fd
  174. *
  175. * This function will return whether this file has been opend.
  176. *
  177. * @param pathname the file path name.
  178. *
  179. * @return 0 on file has been open successfully, -1 on open failed.
  180. */
  181. int fd_is_open(const char *pathname)
  182. {
  183. char *fullpath;
  184. unsigned int index;
  185. struct dfs_filesystem *fs;
  186. struct dfs_fd *fd;
  187. fullpath = dfs_normalize_path(RT_NULL, pathname);
  188. if (fullpath != RT_NULL)
  189. {
  190. char *mountpath;
  191. fs = dfs_filesystem_lookup(fullpath);
  192. if (fs == RT_NULL)
  193. {
  194. /* can't find mounted file system */
  195. rt_free(fullpath);
  196. return -1;
  197. }
  198. /* get file path name under mounted file system */
  199. if (fs->path[0] == '/' && fs->path[1] == '\0')
  200. mountpath = fullpath;
  201. else
  202. mountpath = fullpath + strlen(fs->path);
  203. dfs_lock();
  204. #ifdef DFS_USING_STDIO
  205. for (index = 3; index < DFS_FD_MAX+3; index++)
  206. #else
  207. for (index = 0; index < DFS_FD_MAX; index++)
  208. #endif
  209. {
  210. fd = &(fd_table[index]);
  211. if (fd->fs == RT_NULL)
  212. continue;
  213. if (fd->fs == fs && strcmp(fd->path, mountpath) == 0)
  214. {
  215. /* found file in file descriptor table */
  216. rt_free(fullpath);
  217. dfs_unlock();
  218. return 0;
  219. }
  220. }
  221. dfs_unlock();
  222. rt_free(fullpath);
  223. }
  224. return -1;
  225. }
  226. /**
  227. * this function will return a sub-path name under directory.
  228. *
  229. * @param directory the parent directory.
  230. * @param filename the filename.
  231. *
  232. * @return the subdir pointer in filename
  233. */
  234. const char *dfs_subdir(const char *directory, const char *filename)
  235. {
  236. const char *dir;
  237. if (strlen(directory) == strlen(filename)) /* it's a same path */
  238. return RT_NULL;
  239. dir = filename + strlen(directory);
  240. if ((*dir != '/') && (dir != filename))
  241. {
  242. dir --;
  243. }
  244. return dir;
  245. }
  246. RTM_EXPORT(dfs_subdir);
  247. /**
  248. * this function will normalize a path according to specified parent directory
  249. * and file name.
  250. *
  251. * @param directory the parent path
  252. * @param filename the file name
  253. *
  254. * @return the built full file path (absolute path)
  255. */
  256. char *dfs_normalize_path(const char *directory, const char *filename)
  257. {
  258. char *fullpath;
  259. char *dst0, *dst, *src;
  260. /* check parameters */
  261. RT_ASSERT(filename != RT_NULL);
  262. #ifdef DFS_USING_WORKDIR
  263. if (directory == RT_NULL) /* shall use working directory */
  264. directory = &working_directory[0];
  265. #else
  266. if ((directory == RT_NULL) && (filename[0] != '/'))
  267. {
  268. rt_kprintf(NO_WORKING_DIR);
  269. return RT_NULL;
  270. }
  271. #endif
  272. if (filename[0] != '/') /* it's a absolute path, use it directly */
  273. {
  274. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  275. if (fullpath == RT_NULL)
  276. return RT_NULL;
  277. /* join path and file name */
  278. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  279. "%s/%s", directory, filename);
  280. }
  281. else
  282. {
  283. fullpath = rt_strdup(filename); /* copy string */
  284. if (fullpath == RT_NULL)
  285. return RT_NULL;
  286. }
  287. src = fullpath;
  288. dst = fullpath;
  289. dst0 = dst;
  290. while (1)
  291. {
  292. char c = *src;
  293. if (c == '.')
  294. {
  295. if (!src[1]) src ++; /* '.' and ends */
  296. else if (src[1] == '/')
  297. {
  298. /* './' case */
  299. src += 2;
  300. while ((*src == '/') && (*src != '\0'))
  301. src ++;
  302. continue;
  303. }
  304. else if (src[1] == '.')
  305. {
  306. if (!src[2])
  307. {
  308. /* '..' and ends case */
  309. src += 2;
  310. goto up_one;
  311. }
  312. else if (src[2] == '/')
  313. {
  314. /* '../' case */
  315. src += 3;
  316. while ((*src == '/') && (*src != '\0'))
  317. src ++;
  318. goto up_one;
  319. }
  320. }
  321. }
  322. /* copy up the next '/' and erase all '/' */
  323. while ((c = *src++) != '\0' && c != '/')
  324. *dst ++ = c;
  325. if (c == '/')
  326. {
  327. *dst ++ = '/';
  328. while (c == '/')
  329. c = *src++;
  330. src --;
  331. }
  332. else if (!c)
  333. break;
  334. continue;
  335. up_one:
  336. dst --;
  337. if (dst < dst0)
  338. {
  339. rt_free(fullpath);
  340. return RT_NULL;
  341. }
  342. while (dst0 < dst && dst[-1] != '/')
  343. dst --;
  344. }
  345. *dst = '\0';
  346. /* remove '/' in the end of path if exist */
  347. dst --;
  348. if ((dst != fullpath) && (*dst == '/'))
  349. *dst = '\0';
  350. return fullpath;
  351. }
  352. RTM_EXPORT(dfs_normalize_path);
  353. /*@}*/