dfs.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * File : dfs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2005-02-22 Bernard The first version.
  13. * 2010-07-16
  14. */
  15. #include <dfs.h>
  16. #include <dfs_fs.h>
  17. #include <dfs_config.h>
  18. #include <dfs_file.h>
  19. /* Global variables */
  20. const struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
  21. struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
  22. /* device filesystem lock */
  23. static struct rt_mutex fslock;
  24. #ifdef DFS_USING_WORKDIR
  25. char working_directory[DFS_PATH_MAX];
  26. #endif
  27. #ifdef DFS_USING_STDIO
  28. struct dfs_fd fd_table[3 + DFS_FD_MAX];
  29. #else
  30. struct dfs_fd fd_table[DFS_FD_MAX];
  31. #endif
  32. /**
  33. * this function will initialize device file system.
  34. */
  35. void dfs_init()
  36. {
  37. /* clear filesystem operations table */
  38. rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
  39. /* clear filesystem table */
  40. rt_memset(filesystem_table, 0, sizeof(filesystem_table));
  41. /* clean fd table */
  42. rt_memset(fd_table, 0, sizeof(fd_table));
  43. /* create device filesystem lock */
  44. rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
  45. #ifdef DFS_USING_WORKDIR
  46. /* set current working directory */
  47. rt_memset(working_directory, 0, sizeof(working_directory));
  48. working_directory[0] = '/';
  49. #endif
  50. }
  51. /**
  52. * this function will lock device file system.
  53. *
  54. * note: please don't invoke it on ISR.
  55. */
  56. void dfs_lock()
  57. {
  58. rt_err_t result;
  59. result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
  60. RT_ASSERT(result == RT_EOK);
  61. }
  62. /**
  63. * this function will lock device file system.
  64. *
  65. * note: please don't invoke it on ISR.
  66. */
  67. void dfs_unlock()
  68. {
  69. rt_mutex_release(&fslock);
  70. }
  71. /**
  72. * this function will allocate a file descriptor.
  73. *
  74. * @return -1 on failed or the allocated file descriptor.
  75. */
  76. int fd_new(void)
  77. {
  78. struct dfs_fd* d;
  79. int idx;
  80. /* lock filesystem */
  81. dfs_lock();
  82. /* find an empty fd entry */
  83. #ifdef DFS_USING_STDIO
  84. for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++);
  85. #else
  86. for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
  87. #endif
  88. /* can't find an empty fd entry */
  89. #ifdef DFS_USING_STDIO
  90. if (idx == DFS_FD_MAX + 3)
  91. #else
  92. if (idx == DFS_FD_MAX)
  93. #endif
  94. {
  95. idx = -1;
  96. goto __result;
  97. }
  98. d = &(fd_table[idx]);
  99. d->ref_count = 1;
  100. __result:
  101. dfs_unlock();
  102. return idx;
  103. }
  104. /**
  105. * this function will return a file descriptor structure according to file
  106. * descriptor.
  107. *
  108. * @return NULL on on this file descriptor or the file descriptor structure
  109. * pointer.
  110. */
  111. struct dfs_fd* fd_get(int fd)
  112. {
  113. struct dfs_fd* d;
  114. #ifdef DFS_USING_STDIO
  115. if ( fd < 3 || fd > DFS_FD_MAX + 3) return RT_NULL;
  116. #else
  117. if ( fd < 0 || fd > DFS_FD_MAX ) return RT_NULL;
  118. #endif
  119. dfs_lock();
  120. d = &fd_table[fd];
  121. /* increase the reference count */
  122. d->ref_count ++;
  123. dfs_unlock();
  124. return d;
  125. }
  126. /**
  127. * this function will put the file descriptor.
  128. */
  129. void fd_put(struct dfs_fd* fd)
  130. {
  131. dfs_lock();
  132. fd->ref_count --;
  133. /* clear this fd entry */
  134. if ( fd->ref_count == 0 )
  135. {
  136. rt_memset(fd, 0, sizeof(struct dfs_fd));
  137. }
  138. dfs_unlock();
  139. };
  140. /**
  141. * this function will return whether this file has been opend.
  142. *
  143. * @param pathname the file path name.
  144. *
  145. * @return 0 on file has been open, -1 on not open.
  146. */
  147. int fd_is_open(const char* pathname)
  148. {
  149. char *fullpath;
  150. unsigned int index;
  151. struct dfs_filesystem* fs;
  152. struct dfs_fd* fd;
  153. fullpath = dfs_normalize_path(RT_NULL, pathname);
  154. if (fullpath != RT_NULL)
  155. {
  156. char *mountpath;
  157. fs = dfs_filesystem_lookup(fullpath);
  158. if (fs == RT_NULL)
  159. {
  160. /* can't find mounted file system */
  161. rt_free(fullpath);
  162. return -1;
  163. }
  164. /* get file path name under mounted file system */
  165. if (fs->path[0] == '/' && fs->path[1] == '\0')
  166. mountpath = fullpath;
  167. else mountpath = fullpath + strlen(fs->path);
  168. dfs_lock();
  169. for (index = 0; index < DFS_FD_MAX; index++)
  170. {
  171. fd = &(fd_table[index]);
  172. if (fd->fs == RT_NULL) continue;
  173. if (fd->fs == fs &&
  174. strcmp(fd->path, mountpath) == 0)
  175. {
  176. /* found file in file descriptor table */
  177. rt_free(fullpath);
  178. dfs_unlock();
  179. return 0;
  180. }
  181. }
  182. dfs_unlock();
  183. rt_free(fullpath);
  184. }
  185. return -1;
  186. }
  187. /**
  188. * this function will return a sub-path name under directory.
  189. *
  190. * @param directory the parent directory.
  191. * @param filename the filename.
  192. *
  193. * @return the subdir pointer in filename
  194. */
  195. const char* dfs_subdir(const char* directory, const char* filename)
  196. {
  197. const char* dir;
  198. if (strlen(directory) == strlen(filename)) /* it's a same path */
  199. return RT_NULL;
  200. dir = filename + strlen(directory);
  201. if ((*dir != '/') && (dir != filename))
  202. {
  203. dir --;
  204. }
  205. return dir;
  206. }
  207. /**
  208. * this function will normalize a path according to specified parent directory and file name.
  209. *
  210. * @param directory the parent path
  211. * @param filename the file name
  212. *
  213. * @return the built full file path (absoluted path)
  214. */
  215. char* dfs_normalize_path(const char* directory, const char* filename)
  216. {
  217. char *fullpath;
  218. char *dst0, *dst, *src;
  219. /* check parameters */
  220. RT_ASSERT(filename != RT_NULL);
  221. #ifdef DFS_USING_WORKDIR
  222. if (directory == NULL) /* shall use working directory */
  223. directory = &working_directory[0];
  224. #else
  225. if ((directory == NULL) && (filename[0] != '/'))
  226. {
  227. return RT_NULL;
  228. }
  229. #endif
  230. if (filename[0] != '/') /* it's a absolute path, use it directly */
  231. {
  232. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  233. /* join path and file name */
  234. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  235. "%s/%s", directory, filename);
  236. }
  237. else
  238. {
  239. fullpath = rt_strdup(filename); /* copy string */
  240. }
  241. src = fullpath;
  242. dst = fullpath;
  243. while (1)
  244. {
  245. char c = *src;
  246. if (c == '.')
  247. {
  248. if (!src[1]) src ++; /* '.' and ends */
  249. else if (src[1] == '/')
  250. {
  251. /* './' case */
  252. src += 2;
  253. while ((*src == '/') && (*src != '\0')) src ++;
  254. continue;
  255. }
  256. else if (src[1] == '.')
  257. {
  258. if (!src[2])
  259. {
  260. /* '..' and ends case */
  261. src += 2;
  262. goto up_one;
  263. }
  264. else if (src[2] == '/')
  265. {
  266. /* '../' case */
  267. src += 3;
  268. while ((*src == '/') && (*src != '\0')) src ++;
  269. goto up_one;
  270. }
  271. }
  272. }
  273. /* copy up the next '/' and erase all '/' */
  274. while ((c = *src++) != '\0' && c != '/') *dst ++ = c;
  275. if (c == '/')
  276. {
  277. *dst ++ = '/';
  278. while (c == '/') c = *src++;
  279. src --;
  280. }
  281. else if (!c) break;
  282. continue;
  283. up_one:
  284. dst --;
  285. if (dst < dst0) { rt_free(fullpath); return NULL;}
  286. while (dst0 < dst && dst[-1] != '/') dst --;
  287. }
  288. *dst = '\0';
  289. return fullpath;
  290. }