PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/usr/src/suites/fs/zfs/bin/file_trunc.c

https://bitbucket.org/illumos/illumos-stc
C | 238 lines | 177 code | 36 blank | 25 comment | 23 complexity | 2da83e7134b92cf8565b47dca99e3e0c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <limits.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <sys/types.h>
  32. #include <sys/fcntl.h>
  33. #include <sys/stat.h>
  34. #include <sys/statvfs.h>
  35. #include <sys/errno.h>
  36. #include <sys/time.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/wait.h>
  39. #include <sys/param.h>
  40. #include <string.h>
  41. #define FSIZE 256*1024*1024
  42. #define BSIZE 512
  43. /* Initialize Globals */
  44. static unsigned long fsize = FSIZE;
  45. static size_t bsize = BSIZE;
  46. static int count = 0;
  47. static int rflag = 0;
  48. static int seed = 0;
  49. static int vflag = 0;
  50. static int errflag = 0;
  51. static off_t offset = 0;
  52. static char *filename = NULL;
  53. static void usage(char *execname);
  54. static void parse_options(int argc, char *argv[]);
  55. static void do_write(int fd);
  56. static void do_trunc(int fd);
  57. static void
  58. usage(char *execname)
  59. {
  60. (void) fprintf(stderr,
  61. "usage: %s [-b blocksize] [-c count] [-f filesize]"
  62. " [-o offset] [-s seed] [-r] [-v] filename\n", execname);
  63. (void) exit(1);
  64. }
  65. int
  66. main(int argc, char *argv[])
  67. {
  68. int i = 0;
  69. int fd = -1;
  70. parse_options(argc, argv);
  71. fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
  72. if (fd < 0) {
  73. perror("open");
  74. exit(3);
  75. }
  76. while (i < count) {
  77. (void) do_write(fd);
  78. (void) do_trunc(fd);
  79. i++;
  80. }
  81. (void) close(fd);
  82. return (0);
  83. }
  84. static void
  85. parse_options(int argc, char *argv[])
  86. {
  87. int c;
  88. extern char *optarg;
  89. extern int optind, optopt;
  90. count = fsize / bsize;
  91. seed = time(NULL);
  92. while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) {
  93. switch (c) {
  94. case 'b':
  95. bsize = atoi(optarg);
  96. break;
  97. case 'c':
  98. count = atoi(optarg);
  99. break;
  100. case 'f':
  101. fsize = atol(optarg);
  102. break;
  103. case 'o':
  104. offset = atoi(optarg);
  105. break;
  106. case 'r':
  107. rflag++;
  108. break;
  109. case 's':
  110. seed = atoi(optarg);
  111. break;
  112. case 'v':
  113. vflag++;
  114. break;
  115. case ':':
  116. (void) fprintf(stderr,
  117. "Option -%c requires an operand\n", optopt);
  118. errflag++;
  119. break;
  120. case '?':
  121. (void) fprintf(stderr,
  122. "Unrecognized option: -%c\n", optopt);
  123. errflag++;
  124. break;
  125. }
  126. if (errflag) {
  127. (void) usage(argv[0]);
  128. }
  129. }
  130. if (argc <= optind) {
  131. (void) fprintf(stderr,
  132. "No filename specified\n");
  133. usage(argv[0]);
  134. }
  135. filename = argv[optind];
  136. if (vflag) {
  137. (void) fprintf(stderr, "Seed = %d\n", seed);
  138. }
  139. srandom(seed);
  140. }
  141. static void
  142. do_write(int fd)
  143. {
  144. off_t roffset = 0;
  145. char *buf = NULL;
  146. char *rbuf = NULL;
  147. buf = (char *)calloc(1, bsize);
  148. rbuf = (char *)calloc(1, bsize);
  149. if (buf == NULL || rbuf == NULL) {
  150. perror("malloc");
  151. exit(4);
  152. }
  153. roffset = random() % fsize;
  154. if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
  155. perror("lseek");
  156. exit(5);
  157. }
  158. buf = "ZFS Test Suite Truncation Test";
  159. if (write(fd, buf, bsize) < bsize) {
  160. perror("write");
  161. exit(6);
  162. }
  163. if (rflag) {
  164. if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
  165. perror("lseek");
  166. exit(7);
  167. }
  168. if (read(fd, rbuf, bsize) < bsize) {
  169. perror("read");
  170. exit(8);
  171. }
  172. if (memcmp(buf, rbuf, bsize) != 0) {
  173. perror("memcmp");
  174. exit(9);
  175. }
  176. }
  177. if (vflag) {
  178. (void) fprintf(stderr,
  179. "Wrote to offset %ld\n", (offset + roffset));
  180. if (rflag) {
  181. (void) fprintf(stderr,
  182. "Read back from offset %ld\n", (offset + roffset));
  183. }
  184. }
  185. (void) free(buf);
  186. (void) free(rbuf);
  187. }
  188. static void
  189. do_trunc(int fd)
  190. {
  191. off_t roffset = 0;
  192. roffset = random() % fsize;
  193. if (ftruncate64(fd, (offset + roffset)) < 0) {
  194. perror("truncate");
  195. exit(7);
  196. }
  197. if (vflag) {
  198. (void) fprintf(stderr,
  199. "Truncated at offset %ld\n",
  200. (offset + roffset));
  201. }
  202. }