ftrunc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (C) 2007 Nokia Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. *
  18. * Author: Adrian Hunter
  19. */
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdint.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <errno.h>
  29. #include "tests.h"
  30. #define WRITE_BUFFER_SIZE 32768
  31. static void ftrunc(void)
  32. {
  33. int fd, i;
  34. pid_t pid;
  35. ssize_t written;
  36. int64_t remains;
  37. size_t block;
  38. const char *file_name;
  39. off_t actual;
  40. char buf[WRITE_BUFFER_SIZE];
  41. file_name = "ftrunc_test_file";
  42. fd = open(file_name, O_CREAT | O_WRONLY,
  43. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
  44. CHECK(fd != -1);
  45. pid = getpid();
  46. srand(pid);
  47. for (i = 0; i < WRITE_BUFFER_SIZE;++i)
  48. buf[i] = rand();
  49. remains = tests_size_parameter;
  50. actual = 0;
  51. while (remains > 0) {
  52. if (remains > WRITE_BUFFER_SIZE)
  53. block = WRITE_BUFFER_SIZE;
  54. else
  55. block = remains;
  56. written = write(fd, buf, block);
  57. if (written <= 0) {
  58. CHECK(errno == ENOSPC); /* File system full */
  59. errno = 0;
  60. break;
  61. }
  62. remains -= written;
  63. actual += written;
  64. }
  65. CHECK(ftruncate(fd, (actual ? actual - 1 : actual)) != -1);
  66. CHECK(close(fd) != -1);
  67. CHECK(unlink(file_name) != -1);
  68. }
  69. /* Title of this test */
  70. static const char *ftrunc_get_title(void)
  71. {
  72. return "Truncate a large test file";
  73. }
  74. /* Description of this test */
  75. static const char *ftrunc_get_description(void)
  76. {
  77. return
  78. "Create a file named ftrunc_test_file. " \
  79. "Truncate the file to reduce its length by 1. " \
  80. "Then remove the truncated file. "
  81. "The size is given by the -z or --size option, " \
  82. "otherwise it defaults to 1000000.";
  83. }
  84. int main(int argc, char *argv[])
  85. {
  86. int run_test;
  87. /* Set default test file size */
  88. tests_size_parameter = 1000000;
  89. /* Handle common arguments */
  90. run_test = tests_get_args(argc, argv, ftrunc_get_title(),
  91. ftrunc_get_description(), "z");
  92. if (!run_test)
  93. return 1;
  94. /* Change directory to the file system and check it is ok for testing */
  95. tests_check_test_file_system();
  96. /* Do the actual test */
  97. ftrunc();
  98. return 0;
  99. }