romfs_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. /****************************************************************************
  2. * apps/examples/romfs/romfs_main.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /* Mount the ROMFS image, Verify that it contains the
  21. * following:
  22. *
  23. * testdir
  24. * |---------- [drwxr-xr-x 4096] adir
  25. * | |------ [-rw-r--r-- 21] anotherfile.txt
  26. * | |------ [drwxr-xr-x 4096] subdir
  27. * | | `-- [-rw-r--r-- 21] subdirfile.txt
  28. * | `------ [-rw-r--r-- 25] yafile.txt
  29. * |---------- [-rw-r--r-- 15] afile.txt
  30. * |---------- [-rw-r--r-- 21] hfile
  31. * `---------- [lrwxrwxrwx 11] ldir -> adir/subdir
  32. *
  33. * testdir/ldir is a soft-link and should not be detectable.
  34. * hfile is a hardlink to subdirfile and should be identical
  35. */
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <sys/types.h>
  41. #include <sys/mount.h>
  42. #include <sys/stat.h>
  43. #include <sys/mman.h>
  44. #include <sys/boardctl.h>
  45. #include <inttypes.h>
  46. #include <stdbool.h>
  47. #include <stdint.h>
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #include <unistd.h>
  51. #include <string.h>
  52. #include <fcntl.h>
  53. #include <dirent.h>
  54. #include <errno.h>
  55. #include <nuttx/drivers/ramdisk.h>
  56. #include "romfs_testdir.h"
  57. /****************************************************************************
  58. * Pre-processor Definitions
  59. ****************************************************************************/
  60. /* Configuration settings */
  61. #ifndef CONFIG_EXAMPLES_ROMFS_RAMDEVNO
  62. # define CONFIG_EXAMPLES_ROMFS_RAMDEVNO 1
  63. #endif
  64. #ifndef CONFIG_EXAMPLES_ROMFS_SECTORSIZE
  65. # define CONFIG_EXAMPLES_ROMFS_SECTORSIZE 64
  66. #endif
  67. #ifndef CONFIG_EXAMPLES_ROMFS_MOUNTPOINT
  68. # define CONFIG_EXAMPLES_ROMFS_MOUNTPOINT "/usr/local/share"
  69. #endif
  70. #ifdef CONFIG_DISABLE_MOUNTPOINT
  71. # error "Mountpoint support is disabled"
  72. #endif
  73. #ifndef CONFIG_FS_ROMFS
  74. # error "ROMFS support not enabled"
  75. #endif
  76. #define NSECTORS(b) (((b)+CONFIG_EXAMPLES_ROMFS_SECTORSIZE-1)/CONFIG_EXAMPLES_ROMFS_SECTORSIZE)
  77. #define STR_RAMDEVNO(m) #m
  78. #define MKMOUNT_DEVNAME(m) "/dev/ram" STR_RAMDEVNO(m)
  79. #define MOUNT_DEVNAME MKMOUNT_DEVNAME(CONFIG_EXAMPLES_ROMFS_RAMDEVNO)
  80. #define SCRATCHBUFFER_SIZE 1024
  81. /* Test directory stuff */
  82. #define WRITABLE_MODE (S_IWOTH|S_IWGRP|S_IWUSR)
  83. #define READABLE_MODE (S_IROTH|S_IRGRP|S_IRUSR)
  84. #define EXECUTABLE_MODE (S_IXOTH|S_IXGRP|S_IXUSR)
  85. #define DIRECTORY_MODE (S_IFDIR|READABLE_MODE|EXECUTABLE_MODE)
  86. #define FILE_MODE (S_IFREG|READABLE_MODE)
  87. /****************************************************************************
  88. * Private Types
  89. ****************************************************************************/
  90. struct node_s
  91. {
  92. struct node_s *peer; /* Next node in this directory */
  93. bool directory; /* True: directory */
  94. bool found; /* True: found and verified */
  95. const char *name; /* Node name */
  96. mode_t mode; /* Expected permissions */
  97. size_t size; /* Expected size */
  98. union
  99. {
  100. const char *filecontent; /* Context of text file */
  101. struct node_s *child; /* Subdirectory start */
  102. } u;
  103. };
  104. /****************************************************************************
  105. * Private Data
  106. ****************************************************************************/
  107. static const char g_afilecontent[] = "This is a file\n";
  108. static const char g_anotherfilecontent[] = "This is another file\n";
  109. static const char g_yafilecontent[] = "This is yet another file\n";
  110. static const char g_subdirfilecontent[] = "File in subdirectory\n";
  111. #define g_hfilecontent g_subdirfilecontent
  112. static struct node_s g_adir;
  113. static struct node_s g_afile;
  114. static struct node_s g_ldir;
  115. static struct node_s g_hfile;
  116. static struct node_s g_anotherfile;
  117. static struct node_s g_subdir;
  118. static struct node_s g_yafile;
  119. static struct node_s g_subdirfile;
  120. static int g_nerrors = 0;
  121. static char g_scratchbuffer[SCRATCHBUFFER_SIZE];
  122. /****************************************************************************
  123. * Private Functions
  124. ****************************************************************************/
  125. /****************************************************************************
  126. * Name: connectem
  127. ****************************************************************************/
  128. static void connectem(void)
  129. {
  130. g_adir.peer = &g_afile;
  131. g_adir.directory = true;
  132. g_adir.found = false;
  133. g_adir.name = "adir";
  134. g_adir.mode = DIRECTORY_MODE;
  135. g_adir.size = 0;
  136. g_adir.u.child = &g_anotherfile;
  137. g_afile.peer = &g_ldir;
  138. g_afile.directory = false;
  139. g_afile.found = false;
  140. g_afile.name = "afile.txt";
  141. g_afile.mode = FILE_MODE;
  142. g_afile.size = strlen(g_afilecontent);
  143. g_afile.u.filecontent = g_afilecontent;
  144. g_ldir.peer = &g_hfile;
  145. g_ldir.directory = true;
  146. g_ldir.found = false;
  147. g_ldir.name = "ldir";
  148. g_ldir.mode = DIRECTORY_MODE;
  149. g_ldir.size = 0;
  150. g_ldir.u.child = &g_subdirfile;
  151. g_hfile.peer = NULL;
  152. g_hfile.directory = false; /* Actually a hard link */
  153. g_hfile.found = false;
  154. g_hfile.name = "hfile";
  155. g_hfile.mode = FILE_MODE;
  156. g_hfile.size = strlen(g_hfilecontent);
  157. g_hfile.u.filecontent = g_hfilecontent;
  158. g_anotherfile.peer = &g_yafile;
  159. g_anotherfile.directory = false;
  160. g_anotherfile.found = false;
  161. g_anotherfile.name = "anotherfile.txt";
  162. g_anotherfile.mode = FILE_MODE;
  163. g_anotherfile.size = strlen(g_anotherfilecontent);
  164. g_anotherfile.u.filecontent = g_anotherfilecontent;
  165. g_yafile.peer = &g_subdir;
  166. g_yafile.directory = false;
  167. g_yafile.found = false;
  168. g_yafile.name = "yafile.txt";
  169. g_yafile.mode = FILE_MODE;
  170. g_yafile.size = strlen(g_yafilecontent);
  171. g_yafile.u.filecontent = g_yafilecontent;
  172. g_subdir.peer = NULL;
  173. g_subdir.directory = true;
  174. g_subdir.found = false;
  175. g_subdir.name = "subdir";
  176. g_subdir.mode = DIRECTORY_MODE;
  177. g_subdir.size = 0;
  178. g_subdir.u.child = &g_subdirfile;
  179. g_subdirfile.peer = NULL;
  180. g_subdirfile.directory = false;
  181. g_subdirfile.found = false;
  182. g_subdirfile.name = "subdirfile.txt";
  183. g_subdirfile.mode = FILE_MODE;
  184. g_subdirfile.size = strlen(g_subdirfilecontent);
  185. g_subdirfile.u.filecontent = g_subdirfilecontent;
  186. }
  187. /****************************************************************************
  188. * Name: findindirectory
  189. ****************************************************************************/
  190. static struct node_s *findindirectory(struct node_s *entry, const char *name)
  191. {
  192. for (; entry; entry = entry->peer)
  193. {
  194. if (!entry->found && strcmp(entry->name, name) == 0)
  195. {
  196. entry->found = true;
  197. return entry;
  198. }
  199. }
  200. return NULL;
  201. }
  202. /****************************************************************************
  203. * Name: checkattributes
  204. ****************************************************************************/
  205. static void checkattributes(const char *path, mode_t mode, size_t size)
  206. {
  207. struct stat buf;
  208. int ret;
  209. ret = stat(path, &buf);
  210. if (ret != 0)
  211. {
  212. printf(" -- ERROR: Failed to stat %s: %d\n", path, errno);
  213. g_nerrors++;
  214. return;
  215. }
  216. if (mode != buf.st_mode)
  217. {
  218. printf(" -- ERROR: Expected mode %08x, got %08x\n", mode,
  219. buf.st_mode);
  220. g_nerrors++;
  221. }
  222. if (size != buf.st_size)
  223. {
  224. printf(" -- ERROR: Expected size %zu, got %ju\n", size,
  225. (uintmax_t)buf.st_size);
  226. g_nerrors++;
  227. }
  228. }
  229. /****************************************************************************
  230. * Name: checkfile
  231. ****************************************************************************/
  232. static void checkfile(const char *path, struct node_s *node)
  233. {
  234. ssize_t nbytesread;
  235. char *filedata;
  236. int fd;
  237. /* Open the file */
  238. fd = open(path, O_RDONLY);
  239. if (fd < 0)
  240. {
  241. printf(" -- ERROR: Failed to open %s: %d\n", path, errno);
  242. g_nerrors++;
  243. return;
  244. }
  245. /* Read and verify the file contents */
  246. nbytesread = read(fd, g_scratchbuffer, SCRATCHBUFFER_SIZE);
  247. if (nbytesread < 0)
  248. {
  249. printf(" -- ERROR: Failed to read from %s: %d\n", path, errno);
  250. g_nerrors++;
  251. }
  252. else if (nbytesread != node->size)
  253. {
  254. printf(" -- ERROR: Read %ld bytes, expected %lu\n",
  255. (long)nbytesread, (unsigned long)node->size);
  256. g_nerrors++;
  257. }
  258. else if (memcmp(g_scratchbuffer, node->u.filecontent, node->size) != 0)
  259. {
  260. g_scratchbuffer[nbytesread] = '\0';
  261. printf(" -- ERROR: File content read does not match expectation:\n");
  262. printf(" -- Read: [%s]\n", g_scratchbuffer);
  263. printf(" -- Expected: [%s]\n", node->u.filecontent);
  264. g_nerrors++;
  265. }
  266. /* Memory map and verify the file contents */
  267. filedata = (char *)mmap(NULL, node->size, PROT_READ, MAP_SHARED | MAP_FILE,
  268. fd, 0);
  269. if (!filedata || filedata == (char *)MAP_FAILED)
  270. {
  271. printf(" -- ERROR: mmap of %s failed: %d\n", path, errno);
  272. g_nerrors++;
  273. }
  274. else
  275. {
  276. if (memcmp(filedata, node->u.filecontent, node->size) != 0)
  277. {
  278. memcpy(g_scratchbuffer, filedata, node->size);
  279. g_scratchbuffer[node->size] = '\0';
  280. printf(" -- ERROR: Mapped file content read does not match "
  281. "expectation:\n");
  282. printf(" -- Memory: [%s]\n", filedata);
  283. printf(" -- Expected: [%s]\n", node->u.filecontent);
  284. g_nerrors++;
  285. }
  286. munmap(filedata, node->size);
  287. }
  288. /* Close the file */
  289. if (close(fd) != OK)
  290. {
  291. printf(" -- ERROR: Failed to close %s: %d\n", path, errno);
  292. g_nerrors++;
  293. }
  294. }
  295. /****************************************************************************
  296. * Name: readdirectories
  297. ****************************************************************************/
  298. static void readdirectories(const char *path, struct node_s *entry)
  299. {
  300. DIR *dirp;
  301. struct node_s *node;
  302. struct dirent *direntry;
  303. char *fullpath;
  304. printf("Traversing directory: %s\n", path);
  305. dirp = opendir(path);
  306. if (!dirp)
  307. {
  308. printf(" ERROR opendir(\"%s\") failed: %d\n", path, errno);
  309. g_nerrors++;
  310. return;
  311. }
  312. for (direntry = readdir(dirp); direntry; direntry = readdir(dirp))
  313. {
  314. if (strcmp(direntry->d_name, ".") == 0 ||
  315. strcmp(direntry->d_name, "..") == 0)
  316. {
  317. printf(" Skipping %s\n", direntry->d_name);
  318. continue;
  319. }
  320. node = findindirectory(entry, direntry->d_name);
  321. if (!node)
  322. {
  323. printf(" ERROR: No node found for %s\n", direntry->d_name);
  324. g_nerrors++;
  325. continue;
  326. }
  327. /* Get the full path to the entry */
  328. sprintf(g_scratchbuffer, "%s/%s", path, direntry->d_name);
  329. fullpath = strdup(g_scratchbuffer);
  330. if (DIRENT_ISDIRECTORY(direntry->d_type))
  331. {
  332. printf(" DIRECTORY: %s/\n", fullpath);
  333. if (!node->directory)
  334. {
  335. printf(" -- ERROR: Expected type directory\n");
  336. g_nerrors++;
  337. }
  338. else
  339. {
  340. checkattributes(fullpath, node->mode, 0);
  341. readdirectories(fullpath, node->u.child);
  342. printf("Continuing directory: %s\n", path);
  343. }
  344. }
  345. else if (!DIRENT_ISLINK(direntry->d_type))
  346. {
  347. printf(" FILE: %s/\n", fullpath);
  348. if (node->directory)
  349. {
  350. printf(" -- ERROR: Expected type file\n");
  351. g_nerrors++;
  352. }
  353. else
  354. {
  355. checkattributes(fullpath, node->mode, node->size);
  356. checkfile(fullpath, node);
  357. }
  358. }
  359. free(fullpath);
  360. }
  361. closedir(dirp);
  362. }
  363. /****************************************************************************
  364. * Name: checkdirectories
  365. ****************************************************************************/
  366. static void checkdirectories(struct node_s *entry)
  367. {
  368. for (; entry; entry = entry->peer)
  369. {
  370. if (!entry->found)
  371. {
  372. printf("ERROR: %s never found\n", entry->name);
  373. g_nerrors++;
  374. }
  375. if (entry->directory)
  376. {
  377. checkdirectories(entry->u.child);
  378. }
  379. }
  380. }
  381. /****************************************************************************
  382. * Public Functions
  383. ****************************************************************************/
  384. /****************************************************************************
  385. * Name: romfs_main
  386. ****************************************************************************/
  387. int main(int argc, FAR char *argv[])
  388. {
  389. int ret;
  390. struct boardioc_romdisk_s desc;
  391. /* Create a RAM disk for the test */
  392. desc.minor = CONFIG_EXAMPLES_ROMFS_RAMDEVNO; /* Minor device number of the ROM disk. */
  393. desc.nsectors = NSECTORS(testdir_img_len); /* The number of sectors in the ROM disk */
  394. desc.sectsize = CONFIG_EXAMPLES_ROMFS_SECTORSIZE; /* The size of one sector in bytes */
  395. desc.image = (FAR uint8_t *)testdir_img; /* File system image */
  396. ret = boardctl(BOARDIOC_ROMDISK, (uintptr_t)&desc);
  397. if (ret < 0)
  398. {
  399. printf("ERROR: Failed to create RAM disk: %s\n", strerror(errno));
  400. return 1;
  401. }
  402. /* Mount the test file system */
  403. printf("Mounting ROMFS filesystem at target=%s with source=%s\n",
  404. CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, MOUNT_DEVNAME);
  405. ret = mount(MOUNT_DEVNAME, CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, "romfs",
  406. MS_RDONLY, NULL);
  407. if (ret < 0)
  408. {
  409. printf("ERROR: Mount failed: %s\n", strerror(errno));
  410. return 1;
  411. }
  412. /* Perform the test */
  413. connectem();
  414. readdirectories(CONFIG_EXAMPLES_ROMFS_MOUNTPOINT, &g_adir);
  415. checkdirectories(&g_adir);
  416. if (g_nerrors)
  417. {
  418. printf("Finished with %d errors\n", g_nerrors);
  419. return g_nerrors;
  420. }
  421. printf("PASSED\n");
  422. return 0;
  423. }