append.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. ** File: append.c
  7. ** Description: Testing File writes where PR_APPEND was used on open
  8. **
  9. ** append attempts to verify that a file opened with PR_APPEND
  10. ** will always append to the end of file, regardless where the
  11. ** current file pointer is positioned. To do this, PR_Seek() is
  12. ** called before each write with the position set to beginning of
  13. ** file. Subsequent writes should always append.
  14. ** The file is read back, summing the integer data written to the
  15. ** file. If the expected result is equal, the test passes.
  16. **
  17. ** See BugSplat: 4090
  18. */
  19. #include "plgetopt.h"
  20. #include "nspr.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. PRIntn debug = 0;
  24. PRIntn verbose = 0;
  25. PRBool failedAlready = PR_FALSE;
  26. const PRInt32 addedBytes = 1000;
  27. const PRInt32 buf = 1; /* constant written to fd, addedBytes times */
  28. PRInt32 inBuf; /* read it back into here */
  29. int main(int argc, char **argv)
  30. {
  31. PRStatus rc;
  32. PRInt32 rv;
  33. PRFileDesc *fd;
  34. PRIntn i;
  35. PRInt32 sum = 0;
  36. { /* Get command line options */
  37. PLOptStatus os;
  38. PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
  39. while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  40. {
  41. if (PL_OPT_BAD == os) {
  42. continue;
  43. }
  44. switch (opt->option)
  45. {
  46. case 'd': /* debug */
  47. debug = 1;
  48. break;
  49. case 'v': /* verbose */
  50. verbose = 1;
  51. break;
  52. default:
  53. break;
  54. }
  55. }
  56. PL_DestroyOptState(opt);
  57. } /* end block "Get command line options" */
  58. /* ---------------------------------------------------------------------- */
  59. fd = PR_Open( "./tmp-nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 );
  60. if ( NULL == fd ) {
  61. if (debug) {
  62. printf("PR_Open() failed for writing: %d\n", PR_GetError());
  63. }
  64. failedAlready = PR_TRUE;
  65. goto Finished;
  66. }
  67. for ( i = 0; i < addedBytes ; i++ ) {
  68. rv = PR_Write( fd, &buf, sizeof(buf));
  69. if ( sizeof(buf) != rv ) {
  70. if (debug) {
  71. printf("PR_Write() failed: %d\n", PR_GetError());
  72. }
  73. failedAlready = PR_TRUE;
  74. goto Finished;
  75. }
  76. rv = PR_Seek( fd, 0, PR_SEEK_SET );
  77. if ( -1 == rv ) {
  78. if (debug) {
  79. printf("PR_Seek() failed: %d\n", PR_GetError());
  80. }
  81. failedAlready = PR_TRUE;
  82. goto Finished;
  83. }
  84. }
  85. rc = PR_Close( fd );
  86. if ( PR_FAILURE == rc ) {
  87. if (debug) {
  88. printf("PR_Close() failed after writing: %d\n", PR_GetError());
  89. }
  90. failedAlready = PR_TRUE;
  91. goto Finished;
  92. }
  93. /* ---------------------------------------------------------------------- */
  94. fd = PR_Open( "./tmp-nsprAppend", PR_RDONLY, 0 );
  95. if ( NULL == fd ) {
  96. if (debug) {
  97. printf("PR_Open() failed for reading: %d\n", PR_GetError());
  98. }
  99. failedAlready = PR_TRUE;
  100. goto Finished;
  101. }
  102. for ( i = 0; i < addedBytes ; i++ ) {
  103. rv = PR_Read( fd, &inBuf, sizeof(inBuf));
  104. if ( sizeof(inBuf) != rv) {
  105. if (debug) {
  106. printf("PR_Write() failed: %d\n", PR_GetError());
  107. }
  108. failedAlready = PR_TRUE;
  109. goto Finished;
  110. }
  111. sum += inBuf;
  112. }
  113. rc = PR_Close( fd );
  114. if ( PR_FAILURE == rc ) {
  115. if (debug) {
  116. printf("PR_Close() failed after reading: %d\n", PR_GetError());
  117. }
  118. failedAlready = PR_TRUE;
  119. goto Finished;
  120. }
  121. if ( sum != addedBytes ) {
  122. if (debug) {
  123. printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum);
  124. }
  125. failedAlready = PR_TRUE;
  126. goto Finished;
  127. }
  128. /* ---------------------------------------------------------------------- */
  129. Finished:
  130. if (debug || verbose) {
  131. printf("%s\n", (failedAlready)? "FAILED" : "PASSED" );
  132. }
  133. return( (failedAlready)? 1 : 0 );
  134. } /* main() */
  135. /* append.c */