readwrite.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-02-10 Bernard first version
  9. * 2020-04-12 Jianjia Ma add msh cmd
  10. */
  11. #include <rtthread.h>
  12. #include <dfs_file.h>
  13. #include <unistd.h>
  14. #include <stdio.h>
  15. #include <sys/stat.h>
  16. #include <sys/statfs.h>
  17. #define TEST_DATA_LEN 120
  18. /* file read write test */
  19. void readwrite(const char* filename)
  20. {
  21. int fd;
  22. int index, length;
  23. char* test_data;
  24. char* buffer;
  25. int block_size = TEST_DATA_LEN;
  26. /* open with write only & create */
  27. fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0);
  28. if (fd < 0)
  29. {
  30. rt_kprintf("open file for write failed\n");
  31. return;
  32. }
  33. test_data = rt_malloc(block_size);
  34. if (test_data == RT_NULL)
  35. {
  36. rt_kprintf("no memory\n");
  37. close(fd);
  38. return;
  39. }
  40. buffer = rt_malloc(block_size);
  41. if (buffer == RT_NULL)
  42. {
  43. rt_kprintf("no memory\n");
  44. close(fd);
  45. rt_free(test_data);
  46. return;
  47. }
  48. /* prepare some data */
  49. for (index = 0; index < block_size; index ++)
  50. {
  51. test_data[index] = index + 27;
  52. }
  53. /* write to file */
  54. length = write(fd, test_data, block_size);
  55. if (length != block_size)
  56. {
  57. rt_kprintf("write data failed\n");
  58. close(fd);
  59. goto __exit;
  60. }
  61. /* close file */
  62. close(fd);
  63. /* reopen the file with append to the end */
  64. fd = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0);
  65. if (fd < 0)
  66. {
  67. rt_kprintf("open file for append write failed\n");
  68. goto __exit;;
  69. }
  70. length = write(fd, test_data, block_size);
  71. if (length != block_size)
  72. {
  73. rt_kprintf("append write data failed\n");
  74. close(fd);
  75. goto __exit;
  76. }
  77. /* close the file */
  78. close(fd);
  79. /* open the file for data validation. */
  80. fd = open(filename, O_RDONLY, 0);
  81. if (fd < 0)
  82. {
  83. rt_kprintf("check: open file for read failed\n");
  84. goto __exit;
  85. }
  86. /* read the data (should be the data written by the first time ) */
  87. length = read(fd, buffer, block_size);
  88. if (length != block_size)
  89. {
  90. rt_kprintf("check: read file failed\n");
  91. close(fd);
  92. goto __exit;
  93. }
  94. /* validate */
  95. for (index = 0; index < block_size; index ++)
  96. {
  97. if (test_data[index] != buffer[index])
  98. {
  99. rt_kprintf("check: check data failed at %d\n", index);
  100. close(fd);
  101. goto __exit;
  102. }
  103. }
  104. /* read the data (should be the second time data) */
  105. length = read(fd, buffer, block_size);
  106. if (length != block_size)
  107. {
  108. rt_kprintf("check: read file failed\n");
  109. close(fd);
  110. goto __exit;
  111. }
  112. /* validate */
  113. for (index = 0; index < block_size; index ++)
  114. {
  115. if (test_data[index] != buffer[index])
  116. {
  117. rt_kprintf("check: check data failed at %d\n", index);
  118. close(fd);
  119. goto __exit;
  120. }
  121. }
  122. /* close the file */
  123. close(fd);
  124. /* print result */
  125. rt_kprintf("read/write test successful!\n");
  126. __exit:
  127. rt_free(test_data);
  128. rt_free(buffer);
  129. }
  130. #ifdef RT_USING_FINSH
  131. #include <finsh.h>
  132. /* export to finsh */
  133. FINSH_FUNCTION_EXPORT(readwrite, perform file read and write test);
  134. static void cmd_readwrite(int argc, char *argv[])
  135. {
  136. char* filename;
  137. if(argc == 2)
  138. {
  139. filename = argv[1];
  140. }
  141. else
  142. {
  143. rt_kprintf("Usage: readwrite [file_path]\n");
  144. return;
  145. }
  146. readwrite(filename);
  147. }
  148. MSH_CMD_EXPORT_ALIAS(cmd_readwrite, readwrite, perform file read and write test);
  149. #endif /* RT_USING_FINSH */