/src/connectathon/basic/test7.c

https://github.com/jvrao/nfs-ganesha · C · 306 lines · 261 code · 27 blank · 18 comment · 47 complexity · 2e28745be9fa44766d30350568ade0b7 MD5 · raw file

  1. /*
  2. * @(#)test7.c 1.7 99/08/29 Connectathon Testsuite
  3. * 1.4 Lachman ONC Test Suite source
  4. *
  5. * Test rename, link
  6. *
  7. * Uses the following important system calls against the server:
  8. *
  9. * chdir()
  10. * creat()
  11. * stat()
  12. * rename()
  13. * link()
  14. * unlink()
  15. */
  16. #if defined (DOS) || defined (WIN32)
  17. /* If Dos, Windows or Win32 */
  18. #define DOSorWIN32
  19. #endif
  20. #ifndef DOSorWIN32
  21. #include <sys/param.h>
  22. #include <unistd.h>
  23. #endif
  24. #include <sys/stat.h>
  25. #include <errno.h>
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #ifdef DOSorWIN32
  29. #include <time.h>
  30. #else
  31. #include <sys/time.h>
  32. #endif
  33. #include <sys/types.h>
  34. #include "tests.h"
  35. #include "Connectathon_config_parsing.h"
  36. static int Tflag = 0; /* print timing */
  37. static int Fflag = 0; /* test function only; set count to 1, negate -t */
  38. static int Nflag = 0; /* Suppress directory operations */
  39. static void usage()
  40. {
  41. fprintf(stdout, "usage: %s [-htfn] <config_file>\n", Myname);
  42. fprintf(stdout, " Flags: h Help - print this usage info\n");
  43. fprintf(stdout, " t Print execution time statistics\n");
  44. fprintf(stdout, " f Test function only (negate -t)\n");
  45. fprintf(stdout, " n Suppress test directory create operations\n");
  46. }
  47. int main(int argc, char *argv[])
  48. {
  49. int files; /* number of files in each dir */
  50. int fi;
  51. int count; /* times to do each file */
  52. int ct;
  53. int totfiles = 0;
  54. int totdirs = 0;
  55. char *fname;
  56. char *dname;
  57. char *nname;
  58. struct timeval time;
  59. char str[MAXPATHLEN];
  60. char new[MAXPATHLEN];
  61. struct stat statb;
  62. char *opts;
  63. int oerrno;
  64. struct testparam *param;
  65. struct btest *b;
  66. char *config_file;
  67. char *test_dir;
  68. char *log_file;
  69. FILE *log;
  70. umask(0);
  71. setbuf(stdout, NULL);
  72. Myname = *argv++;
  73. argc--;
  74. while(argc && **argv == '-')
  75. {
  76. for(opts = &argv[0][1]; *opts; opts++)
  77. {
  78. switch (*opts)
  79. {
  80. case 'h': /* help */
  81. usage();
  82. exit(1);
  83. break;
  84. case 't': /* time */
  85. Tflag++;
  86. break;
  87. case 'f': /* funtionality */
  88. Fflag++;
  89. break;
  90. case 'n': /* No Test Directory create */
  91. Nflag++;
  92. break;
  93. default:
  94. error("unknown option '%c'", *opts);
  95. usage();
  96. exit(1);
  97. }
  98. }
  99. argc--;
  100. argv++;
  101. }
  102. if(argc)
  103. {
  104. config_file = *argv;
  105. argc--;
  106. argv++;
  107. }
  108. else
  109. {
  110. fprintf(stderr, "Missing config_file");
  111. exit(1);
  112. }
  113. if(argc != 0)
  114. {
  115. fprintf(stderr, "too many parameters");
  116. usage();
  117. exit(1);
  118. }
  119. param = readin_config(config_file);
  120. if(param == NULL)
  121. {
  122. fprintf(stderr, "Nothing built\n");
  123. exit(1);
  124. }
  125. b = get_btest_args(param, SEVEN);
  126. if(b == NULL)
  127. {
  128. fprintf(stderr, "Missing basic test number 7 in the config file '%s'\n",
  129. config_file);
  130. free_testparam(param);
  131. exit(1);
  132. }
  133. if(b->files == -1)
  134. {
  135. fprintf(stderr,
  136. "Missing 'files' parameter in the config file '%s' for the basic test number 7\n",
  137. config_file);
  138. free_testparam(param);
  139. exit(1);
  140. }
  141. if(b->count == -1)
  142. {
  143. fprintf(stderr,
  144. "Missing 'count' parameter in the config file '%s' for the basic test number 7\n",
  145. config_file);
  146. free_testparam(param);
  147. exit(1);
  148. }
  149. count = b->count;
  150. files = b->files;
  151. fname = b->fname;
  152. dname = b->dname;
  153. nname = b->nname;
  154. test_dir = get_test_directory(param);
  155. log_file = get_log_file(param);
  156. free_testparam(param);
  157. if(!Fflag)
  158. {
  159. Tflag = 0;
  160. count = 1;
  161. }
  162. fprintf(stdout, "%s: link and rename\n", Myname);
  163. if(!Nflag)
  164. testdir(test_dir);
  165. else
  166. mtestdir(test_dir);
  167. dirtree(1, files, 0, fname, dname, &totfiles, &totdirs);
  168. starttime();
  169. for(ct = 0; ct < count; ct++)
  170. {
  171. for(fi = 0; fi < files; fi++)
  172. {
  173. sprintf(str, "%s%d", fname, fi);
  174. sprintf(new, "%s%d", nname, fi);
  175. if(rename(str, new) < 0)
  176. {
  177. error("can't rename %s to %s", str, new);
  178. exit(1);
  179. }
  180. if(stat(str, &statb) == 0)
  181. {
  182. error("%s exists after rename to %s", str, new);
  183. exit(1);
  184. }
  185. if(stat(new, &statb) < 0)
  186. {
  187. error("can't stat %s after rename from %s", new, str);
  188. exit(1);
  189. }
  190. if(statb.st_nlink != 1)
  191. {
  192. error("%s has %d links after rename (expect 1)", new, statb.st_nlink);
  193. exit(1);
  194. }
  195. #ifndef DOSorWIN32
  196. if(link(new, str) < 0)
  197. {
  198. oerrno = errno;
  199. error("can't link %s to %s", new, str);
  200. errno = oerrno;
  201. if(errno == EOPNOTSUPP)
  202. complete();
  203. exit(1);
  204. }
  205. if(stat(new, &statb) < 0)
  206. {
  207. error("can't stat %s after link", new);
  208. exit(1);
  209. }
  210. if(statb.st_nlink != 2)
  211. {
  212. error("%s has %d links after link (expect 2)", new, statb.st_nlink);
  213. exit(1);
  214. }
  215. if(stat(str, &statb) < 0)
  216. {
  217. error("can't stat %s after link", str);
  218. exit(1);
  219. }
  220. if(statb.st_nlink != 2)
  221. {
  222. error("%s has %d links after link (expect 2)", str, statb.st_nlink);
  223. exit(1);
  224. }
  225. if(unlink(new) < 0)
  226. {
  227. error("can't unlink %s", new);
  228. exit(1);
  229. }
  230. if(stat(str, &statb) < 0)
  231. {
  232. error("can't stat %s after unlink %s", str, new);
  233. exit(1);
  234. }
  235. if(statb.st_nlink != 1)
  236. {
  237. error("%s has %d links after unlink (expect 1)", str, statb.st_nlink);
  238. exit(1);
  239. }
  240. #else /* DOSorWIN32 */
  241. /* just rename back to orig name */
  242. if(rename(new, str) < 0)
  243. {
  244. error("can't rename %s to %s", new, str);
  245. exit(1);
  246. }
  247. if(stat(str, &statb) < 0)
  248. {
  249. error("can't find %s after rename", str);
  250. exit(1);
  251. }
  252. if(stat(new, &statb) == 0)
  253. {
  254. error("still found %s after rename", new);
  255. exit(1);
  256. }
  257. #endif /* DOSorWIN32 */
  258. }
  259. }
  260. endtime(&time);
  261. fprintf(stdout, "\t%d renames and links on %d files", files * count * 2, files);
  262. if(Tflag)
  263. {
  264. fprintf(stdout, " in %ld.%02ld seconds",
  265. (long)time.tv_sec, (long)time.tv_usec / 10000);
  266. }
  267. fprintf(stdout, "\n");
  268. /* Cleanup files left around */
  269. rmdirtree(1, files, 0, fname, dname, &totfiles, &totdirs, 1);
  270. if((log = fopen(log_file, "a")) == NULL)
  271. {
  272. printf("Enable to open the file '%s'\n", log_file);
  273. complete();
  274. }
  275. fprintf(log, "b7\t%d\t%d\t%ld.%02ld\n", files * count * 2, files, (long)time.tv_sec,
  276. (long)time.tv_usec / 10000);
  277. fclose(log);
  278. complete();
  279. }