PageRenderTime 70ms CodeModel.GetById 42ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/suites/fs/cifs-client/src/c/file_trunc.c

https://bitbucket.org/illumos/illumos-stc
C | 240 lines | 178 code | 37 blank | 25 comment | 23 complexity | 070bb3995cc428464551e23785ebb604 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 2009 Sun Microsystems, Inc. All rights reserved.
  23. * Use is subject to license terms.
  24. */
  25. #pragma ident "%Z%%M% %I% %E% SMI"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <unistd.h>
  29. #include <limits.h>
  30. #include <errno.h>
  31. #include <fcntl.h>
  32. #include <sys/types.h>
  33. #include <sys/fcntl.h>
  34. #include <sys/stat.h>
  35. #include <sys/statvfs.h>
  36. #include <sys/errno.h>
  37. #include <sys/time.h>
  38. #include <sys/ioctl.h>
  39. #include <sys/wait.h>
  40. #include <sys/param.h>
  41. #include <string.h>
  42. #define FSIZE 256*1024*1024
  43. #define BSIZE 512
  44. /* Initialize Globals */
  45. static long fsize = FSIZE;
  46. static size_t bsize = BSIZE;
  47. static int count = 0;
  48. static int rflag = 0;
  49. static int seed = 0;
  50. static int vflag = 0;
  51. static int errflag = 0;
  52. static off_t offset = 0;
  53. static char *filename = NULL;
  54. static void usage(char *execname);
  55. static void parse_options(int argc, char *argv[]);
  56. static void do_write(int fd);
  57. static void do_trunc(int fd);
  58. static void
  59. usage(char *execname)
  60. {
  61. (void) fprintf(stderr,
  62. "usage: %s [-b blocksize] [-c count] [-f filesize]"
  63. " [-o offset] [-s seed] [-r] [-v] filename\n", execname);
  64. (void) exit(1);
  65. }
  66. int
  67. main(int argc, char *argv[])
  68. {
  69. int i = 0;
  70. int fd = -1;
  71. parse_options(argc, argv);
  72. fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);
  73. if (fd < 0) {
  74. perror("open");
  75. exit(3);
  76. }
  77. while (i < count) {
  78. (void) do_write(fd);
  79. (void) do_trunc(fd);
  80. i++;
  81. }
  82. (void) close(fd);
  83. return (0);
  84. }
  85. static void
  86. parse_options(int argc, char *argv[])
  87. {
  88. int c;
  89. extern char *optarg;
  90. extern int optind, optopt;
  91. count = fsize / bsize;
  92. seed = time(NULL);
  93. while ((c = getopt(argc, argv, "b:c:f:o:rs:v")) != -1) {
  94. switch (c) {
  95. case 'b':
  96. bsize = atoi(optarg);
  97. break;
  98. case 'c':
  99. count = atoi(optarg);
  100. break;
  101. case 'f':
  102. fsize = atoi(optarg);
  103. break;
  104. case 'o':
  105. offset = atoi(optarg);
  106. break;
  107. case 'r':
  108. rflag++;
  109. break;
  110. case 's':
  111. seed = atoi(optarg);
  112. break;
  113. case 'v':
  114. vflag++;
  115. break;
  116. case ':':
  117. (void) fprintf(stderr,
  118. "Option -%c requires an operand\n", optopt);
  119. errflag++;
  120. break;
  121. case '?':
  122. (void) fprintf(stderr,
  123. "Unrecognized option: -%c\n", optopt);
  124. errflag++;
  125. break;
  126. }
  127. if (errflag) {
  128. (void) usage(argv[0]);
  129. }
  130. }
  131. if (argc <= optind) {
  132. (void) fprintf(stderr,
  133. "No filename specified\n");
  134. usage(argv[0]);
  135. }
  136. filename = argv[optind];
  137. if (vflag) {
  138. (void) fprintf(stderr, "Seed = %d\n", seed);
  139. }
  140. srandom(seed);
  141. }
  142. static void
  143. do_write(int fd)
  144. {
  145. off_t roffset = 0;
  146. char *buf = NULL;
  147. char *rbuf = NULL;
  148. buf = (char *)calloc(1, bsize);
  149. rbuf = (char *)calloc(1, bsize);
  150. if (buf == NULL || rbuf == NULL) {
  151. perror("malloc");
  152. exit(4);
  153. }
  154. roffset = random() % fsize;
  155. if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
  156. perror("lseek");
  157. exit(5);
  158. }
  159. buf = "SMBFS Test Suite Truncation Test";
  160. if (write(fd, buf, bsize) < bsize) {
  161. perror("write");
  162. exit(6);
  163. }
  164. if (rflag) {
  165. if (lseek64(fd, (offset + roffset), SEEK_SET) < 0) {
  166. perror("lseek");
  167. exit(7);
  168. }
  169. if (read(fd, rbuf, bsize) < bsize) {
  170. perror("read");
  171. exit(8);
  172. }
  173. if (memcmp(buf, rbuf, bsize) != 0) {
  174. perror("memcmp");
  175. exit(9);
  176. }
  177. }
  178. if (vflag) {
  179. (void) fprintf(stderr,
  180. "Wrote to offset %ld\n", (offset + roffset));
  181. if (rflag) {
  182. (void) fprintf(stderr,
  183. "Read back from offset %ld\n", (offset + roffset));
  184. }
  185. }
  186. (void) free(buf);
  187. (void) free(rbuf);
  188. }
  189. static void
  190. do_trunc(int fd)
  191. {
  192. off_t roffset = 0;
  193. roffset = random() % fsize;
  194. if (ftruncate64(fd, (offset + roffset)) < 0) {
  195. perror("truncate");
  196. exit(7);
  197. }
  198. if (vflag) {
  199. (void) fprintf(stderr,
  200. "Truncated at offset %ld\n",
  201. (offset + roffset));
  202. }
  203. }