File.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include "pal.h"
  4. #include "pal_debug.h"
  5. #include "api.h"
  6. int main (int argc, char ** argv, char ** envp)
  7. {
  8. char buffer1[41], buffer2[41];
  9. int ret;
  10. /* test regular file opening */
  11. PAL_HANDLE file1 = DkStreamOpen("file:file_exist.tmp",
  12. PAL_ACCESS_RDWR, 0, 0, 0);
  13. if (file1) {
  14. pal_printf("File Open Test 1 OK\n");
  15. /* test file read */
  16. ret = DkStreamRead(file1, 0, 40, buffer1, NULL, 0);
  17. if (ret > 0) {
  18. buffer1[ret] = 0;
  19. pal_printf("Read Test 1 (0th - 40th): %s\n", buffer1);
  20. }
  21. ret = DkStreamRead(file1, 0, 40, buffer1, NULL, 0);
  22. if (ret > 0) {
  23. buffer1[ret] = 0;
  24. pal_printf("Read Test 2 (0th - 40th): %s\n", buffer1);
  25. }
  26. ret = DkStreamRead(file1, 200, 40, buffer2, NULL, 0);
  27. if (ret > 0) {
  28. buffer2[ret] = 0;
  29. pal_printf("Read Test 3 (200th - 240th): %s\n", buffer2);
  30. }
  31. /* test file attribute query */
  32. PAL_STREAM_ATTR attr1;
  33. if (DkStreamAttributesQuerybyHandle(file1, &attr1))
  34. pal_printf("Query by Handle: type = %d, size = %d\n",
  35. attr1.handle_type, attr1.pending_size);
  36. /* test file map */
  37. void * mem1 = (void *) DkStreamMap(file1, NULL, PAL_PROT_READ, 0,
  38. attr1.pending_size);
  39. if (mem1) {
  40. memcpy(buffer1, mem1, 40);
  41. buffer1[40] = 0;
  42. pal_printf("Map Test 1 (0th - 40th): %s\n", buffer1);
  43. memcpy(buffer2, mem1 + 200, 40);
  44. buffer2[40] = 0;
  45. pal_printf("Map Test 2 (200th - 240th): %s\n", buffer2);
  46. DkStreamUnmap(mem1, attr1.pending_size);
  47. }
  48. DkObjectClose(file1);
  49. }
  50. PAL_HANDLE file2 = DkStreamOpen("file:./file_exist.tmp",
  51. PAL_ACCESS_RDWR, 0, 0, 0);
  52. if (file2) {
  53. pal_printf("File Open Test 2 OK\n");
  54. DkObjectClose(file2);
  55. }
  56. PAL_HANDLE file3 = DkStreamOpen("file:../regression/file_exist.tmp",
  57. PAL_ACCESS_RDWR, 0, 0, 0);
  58. if (file3) {
  59. pal_printf("File Open Test 3 OK\n");
  60. DkObjectClose(file3);
  61. }
  62. PAL_STREAM_ATTR attr2;
  63. if (DkStreamAttributesQuery("file:file_exist.tmp", &attr2))
  64. pal_printf("Query: type = %d, size = %d\n",
  65. attr2.handle_type, attr2.pending_size);
  66. /* test regular file creation */
  67. PAL_HANDLE file4 = DkStreamOpen("file:file_nonexist.tmp",
  68. PAL_ACCESS_RDWR,
  69. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  70. PAL_CREAT_TRY|PAL_CREAT_ALWAYS, 0);
  71. if (file4)
  72. pal_printf("File Creation Test 1 OK\n");
  73. PAL_HANDLE file5 = DkStreamOpen("file:file_nonexist.tmp",
  74. PAL_ACCESS_RDWR,
  75. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  76. PAL_CREAT_TRY|PAL_CREAT_ALWAYS, 0);
  77. if (file5) {
  78. DkObjectClose(file5);
  79. } else {
  80. pal_printf("File Creation Test 2 OK\n");
  81. }
  82. PAL_HANDLE file6 = DkStreamOpen("file:file_nonexist.tmp",
  83. PAL_ACCESS_RDWR,
  84. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  85. PAL_CREAT_TRY, 0);
  86. if (file6) {
  87. pal_printf("File Creation Test 3 OK\n");
  88. DkObjectClose(file6);
  89. }
  90. if (file4) {
  91. /* test file writing */
  92. ret = DkStreamWrite(file4, 0, 40, buffer1, NULL);
  93. if (ret < 0)
  94. goto fail_writing;
  95. ret = DkStreamWrite(file4, 0, 40, buffer2, NULL);
  96. if (ret < 0)
  97. goto fail_writing;
  98. ret = DkStreamWrite(file4, 200, 40, buffer1, NULL);
  99. if (ret < 0)
  100. goto fail_writing;
  101. /* test file truncate */
  102. DkStreamSetLength(file4, pal_control.alloc_align);
  103. fail_writing:
  104. DkObjectClose(file4);
  105. }
  106. PAL_HANDLE file7 = DkStreamOpen("file:file_delete.tmp",
  107. PAL_ACCESS_RDONLY, 0, 0, 0);
  108. if (file7) {
  109. DkStreamDelete(file7, 0);
  110. DkObjectClose(file7);
  111. }
  112. return 0;
  113. }