dfs_win32.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * File : rtthread.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2012, 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. * 2012-11-27 prife the first version
  13. * 2013-03-03 aozima add dfs_win32_stat st_mtime support.
  14. */
  15. #include <rtthread.h>
  16. #include <dfs_fs.h>
  17. #include <dfs_def.h>
  18. #include <rtdevice.h>
  19. //#include "dfs_win32.h"
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <WinError.h>
  26. #include <windows.h>
  27. #if defined(__MINGW32__) && defined(_NO_OLDNAMES)
  28. #define O_RDONLY _O_RDONLY
  29. #define O_WRONLY _O_WRONLY
  30. #define O_RDWR _O_RDWR
  31. #define O_ACCMODE _O_ACCMODE
  32. #define O_APPEND _O_APPEND
  33. #define O_CREAT _O_CREAT
  34. #define O_TRUNC _O_TRUNC
  35. #define O_EXCL _O_EXCL
  36. #define O_TEXT _O_TEXT
  37. #define O_BINARY _O_BINARY
  38. #define O_TEMPORARY _O_TEMPORARY
  39. #define O_NOINHERIT _O_NOINHERIT
  40. #define O_SEQUENTIAL _O_SEQUENTIAL
  41. #define O_RANDOM _O_RANDOM
  42. #endif
  43. /*
  44. * RT-Thread DFS Interface for win-directory as an disk device
  45. */
  46. #define FILE_PATH_MAX 256 /* the longest file path */
  47. #define WIN32_DIRDISK_ROOT "." /* "F:\\Project\\svn\\rtt\\trunk\\bsp\\simulator_test" */
  48. typedef struct {
  49. HANDLE handle;
  50. char *start;
  51. char *end;
  52. char *curr;
  53. struct _finddata_t finddata;
  54. } WINDIR;
  55. /* There are so many error codes in windows, you'd better google for details.
  56. * google "System Error Codes (Windows)"
  57. * http://msdn.microsoft.com/ZH-CN/library/windows/desktop/ms681381(v=vs.85).aspx
  58. */
  59. struct _errcode_map
  60. {
  61. rt_uint16_t dfserr;
  62. rt_uint16_t win32err;
  63. };
  64. static const struct _errcode_map errcode_table[] =
  65. {
  66. {DFS_STATUS_ENOENT, ERROR_FILE_NOT_FOUND },
  67. {DFS_STATUS_ENOENT, ERROR_PATH_NOT_FOUND },
  68. {DFS_STATUS_EEXIST, ERROR_FILE_EXISTS },
  69. {DFS_STATUS_EEXIST, ERROR_ALREADY_EXISTS },
  70. {DFS_STATUS_ENOTEMPTY, ERROR_DIR_NOT_EMPTY },
  71. {DFS_STATUS_EBUSY, ERROR_PATH_BUSY },
  72. {DFS_STATUS_EINVAL, ERROR_ACCESS_DENIED },
  73. #if 0 /* TODO: MORE NEED BE ADDED */
  74. {DFS_STATUS_EISDIR, ERROR_FILE_EXISTS },
  75. {DFS_STATUS_ENOTDIR, ERROR_FILE_EXISTS },
  76. {DFS_STATUS_EBADF, ERROR_FILE_EXISTS },
  77. {DFS_STATUS_EBUSY, ERROR_FILE_EXISTS },
  78. {DFS_STATUS_ENOMEM, ERROR_FILE_EXISTS },
  79. {DFS_STATUS_ENOSPC, ERROR_FILE_EXISTS },
  80. #endif
  81. };
  82. static int win32_result_to_dfs(DWORD res)
  83. {
  84. int i;
  85. int err = 0;
  86. for (i = 0; i < sizeof(errcode_table) / sizeof(struct _errcode_map); i++)
  87. {
  88. if (errcode_table[i].win32err == (res & 0xFFFF))
  89. {
  90. err = -errcode_table[i].dfserr;
  91. return err;
  92. }
  93. }
  94. /* unknown error */
  95. rt_kprintf("dfs win32 error not supported yet: %d\n", res);
  96. return -1;
  97. }
  98. static int dfs_win32_mount(
  99. struct dfs_filesystem *fs,
  100. unsigned long rwflag,
  101. const void *data)
  102. {
  103. return 0;
  104. }
  105. static int dfs_win32_unmount(struct dfs_filesystem *fs)
  106. {
  107. return 0;
  108. }
  109. static int dfs_win32_mkfs(rt_device_t devid)
  110. {
  111. return -DFS_STATUS_ENOSYS;
  112. }
  113. static int dfs_win32_statfs(struct dfs_filesystem *fs,
  114. struct statfs *buf)
  115. {
  116. return -DFS_STATUS_ENOSYS;
  117. }
  118. static char *winpath_dirdup(char *des, const char *src)
  119. {
  120. char *path;
  121. int i = 0;
  122. path = rt_malloc(FILE_PATH_MAX);
  123. if (path == RT_NULL)
  124. return RT_NULL;
  125. strcpy(path, des);
  126. strcat(path, src);
  127. while (1)
  128. {
  129. if (path[i] == 0)
  130. break;
  131. if (path[i] == '/')
  132. path[i] = '\\';
  133. i++;
  134. }
  135. return path;
  136. }
  137. /* This function can convert the path in rt-thread/dfs to the path in windows */
  138. char * dfs_win32_dirdup(char * path)
  139. {
  140. char * file_path;
  141. file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
  142. return file_path;
  143. }
  144. static int dfs_win32_open(struct dfs_fd *file)
  145. {
  146. int fd;
  147. int oflag, mode;
  148. char *file_path;
  149. int res;
  150. oflag = file->flags;
  151. if (oflag & DFS_O_DIRECTORY) /* operations about dir */
  152. {
  153. WINDIR *wdirp;
  154. HANDLE handle;
  155. int len;
  156. file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, file->path);
  157. if (oflag & DFS_O_CREAT) /* create a dir*/
  158. {
  159. res = CreateDirectory(file_path, NULL);
  160. if (res == 0)
  161. {
  162. rt_free(file_path);
  163. return win32_result_to_dfs(GetLastError());
  164. }
  165. }
  166. len = strlen(file_path);
  167. if (file_path[len - 1] != '\\')
  168. {
  169. file_path[len] = '\\';
  170. file_path[len + 1] = 0;
  171. }
  172. strcat(file_path, "*.*");
  173. /* _findfirst will get '.' */
  174. /* save this pointer,will used by dfs_win32_getdents*/
  175. wdirp = rt_malloc(sizeof(WINDIR));
  176. RT_ASSERT(wdirp != NULL);
  177. if ((handle = _findfirst(file_path, &wdirp->finddata)) == -1)
  178. {
  179. rt_free(wdirp);
  180. rt_free(file_path);
  181. goto __err;
  182. }
  183. len = strlen(wdirp->finddata.name) + 1;
  184. wdirp->handle = handle;
  185. //wdirp->nfiles = 1;
  186. wdirp->start = malloc(len); //not rt_malloc!
  187. wdirp->end = wdirp->curr = wdirp->start;
  188. wdirp->end += len;
  189. strncpy(wdirp->curr, wdirp->finddata.name, len);
  190. file->data = (void *)wdirp;
  191. rt_free(file_path);
  192. return DFS_STATUS_OK;
  193. }
  194. /* regular file operations */
  195. mode = O_BINARY;
  196. if (oflag & DFS_O_RDONLY) mode |= O_RDONLY;
  197. if (oflag & DFS_O_WRONLY) mode |= O_WRONLY;
  198. if (oflag & DFS_O_RDWR) mode |= O_RDWR;
  199. /* Opens the file, if it is existing. If not, a new file is created. */
  200. if (oflag & DFS_O_CREAT) mode |= O_CREAT;
  201. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  202. if (oflag & DFS_O_TRUNC) mode |= O_TRUNC;
  203. /* Creates a new file. The function fails if the file is already existing. */
  204. if (oflag & DFS_O_EXCL) mode |= O_EXCL;
  205. file_path = winpath_dirdup(WIN32_DIRDISK_ROOT, file->path);
  206. fd = _open(file_path, mode, 0x0100 | 0x0080); /* _S_IREAD | _S_IWRITE */
  207. rt_free(file_path);
  208. if (fd < 0)
  209. goto __err;
  210. /* save this pointer, it will be used when calling read(), write(),
  211. * flush(), seek(), and will be free when calling close()*/
  212. file->data = (void *)fd;
  213. file->pos = 0;
  214. file->size = _lseek(fd, 0, SEEK_END);
  215. if (oflag & DFS_O_APPEND)
  216. {
  217. file->pos = file->size;
  218. }
  219. else
  220. _lseek(fd, 0, SEEK_SET);
  221. return 0;
  222. __err:
  223. res = GetLastError();
  224. return win32_result_to_dfs(res);
  225. }
  226. static int dfs_win32_close(struct dfs_fd *file)
  227. {
  228. if (file->flags & DFS_O_DIRECTORY)
  229. {
  230. WINDIR *wdirp = (WINDIR*)(file->data);
  231. RT_ASSERT(wdirp != RT_NULL);
  232. if (_findclose((intptr_t)wdirp->handle) == 0) {
  233. free(wdirp->start); //NOTE: here we don't use rt_free!
  234. rt_free(wdirp);
  235. return 0;
  236. }
  237. }
  238. else /* regular file operations */
  239. {
  240. if (_close((int)(file->data)) == 0)
  241. return 0;
  242. }
  243. return win32_result_to_dfs(GetLastError());
  244. }
  245. static int dfs_win32_ioctl(struct dfs_fd *file, int cmd, void *args)
  246. {
  247. return -DFS_STATUS_ENOSYS;
  248. }
  249. static int dfs_win32_read(struct dfs_fd *file, void *buf, rt_size_t len)
  250. {
  251. int fd;
  252. int char_read;
  253. fd = (int)(file->data);
  254. char_read = _read(fd, buf, len);
  255. if (char_read < 0)
  256. return win32_result_to_dfs(GetLastError());
  257. /* update position */
  258. file->pos = _lseek(fd, 0, SEEK_CUR);
  259. return char_read;
  260. }
  261. static int dfs_win32_write(struct dfs_fd *file,
  262. const void *buf,
  263. rt_size_t len)
  264. {
  265. int fd;
  266. int char_write;
  267. fd = (int)(file->data);
  268. char_write = _write(fd, buf, len);
  269. if (char_write < 0)
  270. return win32_result_to_dfs(GetLastError());
  271. /* update position */
  272. file->pos = _lseek(fd, 0, SEEK_CUR);
  273. return char_write;
  274. }
  275. static int dfs_win32_flush(struct dfs_fd *file)
  276. {
  277. return 0;
  278. }
  279. static int dfs_win32_seek(struct dfs_fd *file,
  280. rt_off_t offset)
  281. {
  282. int result;
  283. /* set offset as current offset */
  284. if (file->type == FT_DIRECTORY)
  285. {
  286. WINDIR* wdirp = (WINDIR*)(file->data);
  287. RT_ASSERT(wdirp != RT_NULL);
  288. wdirp->curr = wdirp->start + offset;
  289. return offset;
  290. }
  291. else //file->type == FT_REGULAR)
  292. {
  293. result = _lseek((int)(file->data), offset, SEEK_SET);
  294. if (result >= 0)
  295. return offset;
  296. else
  297. return win32_result_to_dfs(GetLastError());
  298. }
  299. }
  300. /* return the size of struct dirent*/
  301. static int dfs_win32_getdents(
  302. struct dfs_fd *file,
  303. struct dirent *dirp,
  304. rt_uint32_t count)
  305. {
  306. WINDIR *wdirp;
  307. struct dirent *d = dirp;
  308. int result;
  309. /* make integer count */
  310. if (count / sizeof(struct dirent) != 1)
  311. return -DFS_STATUS_EINVAL;
  312. wdirp = (WINDIR*)(file->data);
  313. RT_ASSERT(wdirp != RT_NULL);
  314. if (wdirp->curr == NULL) //no more entries in this directory
  315. return 0;
  316. /* get the current entry */
  317. if (wdirp->finddata.attrib & _A_SUBDIR)
  318. d->d_type = DFS_DT_DIR;
  319. else
  320. d->d_type = DFS_DT_REG;
  321. d->d_namlen = strlen(wdirp->curr);
  322. strncpy(d->d_name, wdirp->curr, DFS_PATH_MAX);
  323. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  324. wdirp->curr += (strlen(wdirp->curr) + 1);
  325. file->pos = wdirp->curr - wdirp->start + sizeof(struct dirent);//NOTE!
  326. /* now set up for the next call to readdir */
  327. if (wdirp->curr >= wdirp->end)
  328. {
  329. if (_findnext(wdirp->handle, &wdirp->finddata) == 0)
  330. {
  331. char* old_start = wdirp->start;
  332. long name_len = strlen(wdirp->finddata.name) + 1;
  333. wdirp->start = realloc(wdirp->start, wdirp->end - wdirp->start + name_len);
  334. wdirp->curr = wdirp->start + (wdirp->curr - old_start);
  335. wdirp->end = wdirp->curr + name_len;
  336. strcpy(wdirp->curr, wdirp->finddata.name);
  337. }
  338. else
  339. {
  340. if ((result = GetLastError()) == ERROR_NO_MORE_FILES)
  341. wdirp->curr = NULL;
  342. else
  343. return win32_result_to_dfs(result);
  344. }
  345. }
  346. return sizeof(struct dirent);
  347. }
  348. static int dfs_win32_unlink(struct dfs_filesystem *fs, const char *path)
  349. {
  350. int result;
  351. char *fp;
  352. fp = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
  353. if (fp == RT_NULL)
  354. {
  355. rt_kprintf("out of memory.\n");
  356. return -DFS_STATUS_ENOMEM;
  357. }
  358. result = GetFileAttributes(fp);
  359. if (result == INVALID_FILE_ATTRIBUTES)
  360. goto __err;
  361. if (result & FILE_ATTRIBUTE_DIRECTORY)//winnt.h
  362. {
  363. if (RemoveDirectory(fp) == RT_FALSE)
  364. goto __err;
  365. }
  366. else //(result & FILE_ATTRIBUTE_NORMAL)
  367. {
  368. if (_unlink(fp) < 0)
  369. goto __err;
  370. }
  371. rt_free(fp);
  372. return 0;
  373. __err:
  374. rt_free(fp);
  375. return win32_result_to_dfs(GetLastError());
  376. }
  377. static int dfs_win32_rename(
  378. struct dfs_filesystem *fs,
  379. const char *oldpath,
  380. const char *newpath)
  381. {
  382. int result;
  383. char *op, *np;
  384. op = winpath_dirdup(WIN32_DIRDISK_ROOT, oldpath);
  385. np = winpath_dirdup(WIN32_DIRDISK_ROOT, newpath);
  386. if (op == RT_NULL || np == RT_NULL)
  387. {
  388. rt_kprintf("out of memory.\n");
  389. return -DFS_STATUS_ENOMEM;
  390. }
  391. /* If the function fails, the return value is zero. */
  392. result = MoveFile(op, np);
  393. rt_free(op);
  394. rt_free(np);
  395. if (result == 0)
  396. return win32_result_to_dfs(GetLastError());
  397. return 0;
  398. }
  399. static int dfs_win32_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  400. {
  401. WIN32_FIND_DATA fileInfo;
  402. HANDLE hFind;
  403. char *fp;
  404. fp = winpath_dirdup(WIN32_DIRDISK_ROOT, path);
  405. if (fp == RT_NULL)
  406. {
  407. rt_kprintf("out of memory.\n");
  408. return -DFS_STATUS_ENOMEM;
  409. }
  410. hFind = FindFirstFile(fp, &fileInfo);
  411. rt_free(fp);
  412. if (hFind == INVALID_HANDLE_VALUE)
  413. goto __err;
  414. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  415. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  416. /* convert file info to dfs stat structure */
  417. if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  418. {
  419. st->st_mode &= ~DFS_S_IFREG;
  420. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  421. }
  422. st->st_dev = 0;
  423. st->st_size = fileInfo.nFileSizeLow;
  424. /* get st_mtime. */
  425. {
  426. LARGE_INTEGER time_tmp;
  427. time_tmp.LowPart = fileInfo.ftLastWriteTime.dwLowDateTime;
  428. time_tmp.HighPart = fileInfo.ftLastWriteTime.dwHighDateTime;
  429. /* removes the diff between 1970 and 1601. */
  430. time_tmp.QuadPart -= 11644473600000 * 10000;
  431. /* converts back from 100-nanoseconds to seconds. */
  432. time_tmp.QuadPart /= 10UL * 1000 * 1000;
  433. st->st_mtime = time_tmp.QuadPart;
  434. }
  435. st->st_blksize = 0;
  436. FindClose(hFind);
  437. return 0;
  438. __err:
  439. return win32_result_to_dfs(GetLastError());
  440. }
  441. static const struct dfs_filesystem_operation dfs_win32_ops =
  442. {
  443. "wdir", /* file system type: dir */
  444. DFS_FS_FLAG_DEFAULT,
  445. dfs_win32_mount,
  446. dfs_win32_unmount,
  447. dfs_win32_mkfs,
  448. dfs_win32_statfs,
  449. dfs_win32_open,
  450. dfs_win32_close,
  451. dfs_win32_ioctl,
  452. dfs_win32_read,
  453. dfs_win32_write,
  454. dfs_win32_flush,
  455. dfs_win32_seek,
  456. dfs_win32_getdents,
  457. dfs_win32_unlink,
  458. dfs_win32_stat,
  459. dfs_win32_rename,
  460. };
  461. int dfs_win32_init(void)
  462. {
  463. /* register uffs file system */
  464. dfs_register(&dfs_win32_ops);
  465. return 0;
  466. }
  467. static rt_err_t nop_init(rt_device_t dev)
  468. {
  469. return RT_EOK;
  470. }
  471. static rt_err_t nop_open(rt_device_t dev, rt_uint16_t oflag)
  472. {
  473. return RT_EOK;
  474. }
  475. static rt_err_t nop_close(rt_device_t dev)
  476. {
  477. return RT_EOK;
  478. }
  479. static rt_size_t nop_read(rt_device_t dev,
  480. rt_off_t pos,
  481. void *buffer,
  482. rt_size_t size)
  483. {
  484. return size;
  485. }
  486. static rt_size_t nop_write(rt_device_t dev,
  487. rt_off_t pos,
  488. const void *buffer,
  489. rt_size_t size)
  490. {
  491. return size;
  492. }
  493. static rt_err_t nop_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  494. {
  495. return RT_EOK;
  496. }
  497. static struct rt_device win_sharedir_dev;
  498. rt_err_t rt_win_sharedir_init(const char *name)
  499. {
  500. rt_device_t dev;
  501. dev = &win_sharedir_dev;
  502. RT_ASSERT(dev != RT_NULL);
  503. /* set device class and generic device interface */
  504. dev->type = RT_Device_Class_Block;
  505. dev->init = nop_init;
  506. dev->open = nop_open;
  507. dev->read = nop_read;
  508. dev->write = nop_write;
  509. dev->close = nop_close;
  510. dev->control = nop_control;
  511. dev->rx_indicate = RT_NULL;
  512. dev->tx_complete = RT_NULL;
  513. /* register to RT-Thread device system */
  514. return rt_device_register(dev, name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  515. }