blob: 2db7baf2f49fd3b9505791931122d2defa220276 [file] [log] [blame]
robbiewf0888c02002-12-04 19:42:27 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
robbiewf0888c02002-12-04 19:42:27 +000018 */
19
20/* 10/31/2002 Port to LTP robbiew@us.ibm.com */
21/* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
22
Wanlong Gao354ebb42012-12-07 10:10:04 +080023 /* inode1.c */
robbiewf0888c02002-12-04 19:42:27 +000024/*======================================================================
25 =================== TESTPLAN SEGMENT ===================
26CALLS: mkdir, stat, open
27
28 run using TERM mode
29
30>KEYS: < file system management I/O
31>WHAT: < Do the system's file system management and I/O functions work
32 < correctly?
33>HOW: < Construct a directory tree, create files in it, and verify
34 < that this was done as expected.
35>BUGS: <
36======================================================================*/
37/* modified by dale 25-Jul-84 */
38
39/************************************************/
subrata_modak4bb656a2009-02-26 12:02:09 +000040#define PATH_STRING_LENGTH 100
robbiewf0888c02002-12-04 19:42:27 +000041#define NAME_LENGTH 8
42#define MAX_PATH_STRING_LENGTH (PATH_STRING_LENGTH - NAME_LENGTH)
43#define MAX_DEPTH 3
44#define MAX_BREADTH 3
45#define FILE_LENGTH 100
46#define DIRECTORY_MODE 00777
47#define FILE_MODE 00777
48
49/* #define PRINT define to get list while running */
50
51#define TRUE 1
52#define FALSE 0
53#define READ 0
54#define WRITE 1
55
56#include <stdio.h>
robbiewa70576c2003-03-04 18:33:41 +000057#include <errno.h>
robbiewf0888c02002-12-04 19:42:27 +000058#include <sys/types.h>
59#include <sys/stat.h>
60#include <fcntl.h>
61#include <signal.h>
62#include <errno.h>
63
64/** LTP Port **/
65#include "test.h"
66#include "usctest.h"
67
68void blexit(void);
69void blenter(void);
70void setup(void);
71void fail_exit(void);
72void anyfail(void);
73void ok_exit(void);
74
75#define FAILED 0
76#define PASSED 1
77
78int local_flag = PASSED;
79int block_number;
robbiewf0888c02002-12-04 19:42:27 +000080FILE *temp;
81
Wanlong Gao354ebb42012-12-07 10:10:04 +080082char *TCID = "inode01"; /* Test program identifier. */
83int TST_TOTAL = 2; /* Total number of test cases. */
robbiewf0888c02002-12-04 19:42:27 +000084/**************/
85
86#ifdef LINUX
87#include <string.h>
88#endif
89
90char name[NAME_LENGTH + 1];
91char path_string[PATH_STRING_LENGTH + 1];
92char read_string[PATH_STRING_LENGTH + 1];
93char write_string[PATH_STRING_LENGTH + 1];
94char rm_string[200];
95
Wanlong Gao354ebb42012-12-07 10:10:04 +080096FILE *list_stream = NULL;
97int file_id;
98int list_id;
robbiewf0888c02002-12-04 19:42:27 +000099
100int increment_name(), get_next_name(), mode(), escrivez();
101
102int main()
robbiewf0888c02002-12-04 19:42:27 +0000103{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 char root[16]; //as pids can get much longer
robbiewf0888c02002-12-04 19:42:27 +0000105 int gen_ret_val, ch_ret_val, level;
106 int ret_val;
107 int generate(), check();
subrata_modak7523e3b2008-10-17 10:03:54 +0000108 char path_list_string[PATH_STRING_LENGTH + 1];
robbiewf0888c02002-12-04 19:42:27 +0000109 int status;
110 int len;
111 int term();
Jan Stancekcb7c6912013-03-20 10:44:33 +0100112 int snp_ret;
robbiewf0888c02002-12-04 19:42:27 +0000113
114 strcpy(path_string, "inode");
115 sprintf(root, "A%d", getpid());
116 strcat(path_string, root);
117
118 strcpy(rm_string, "rm -rf ");
119 strcat(rm_string, path_string);
120
121 setup();
122
123 if (signal(SIGTERM, (void (*)())term) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800124 fprintf(temp, "\tSIGTERM signal set failed!, errno=%d\n",
125 errno);
robbiewf0888c02002-12-04 19:42:27 +0000126 fail_exit();
127 }
128
129 blenter();
subrata_modakbdbaec52009-02-26 12:14:51 +0000130
robbiewf0888c02002-12-04 19:42:27 +0000131 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 /* */
robbiewf0888c02002-12-04 19:42:27 +0000133 /* make the root directory for */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800134 /* the tree */
135 /* */
robbiewf0888c02002-12-04 19:42:27 +0000136 /********************************/
137
138 ret_val = mkdir(path_string, DIRECTORY_MODE);
139
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800140 if (ret_val == -1) {
robbiewf0888c02002-12-04 19:42:27 +0000141 perror("mkdir error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800142 fprintf(temp, "\tcreating directory '%s'\n", path_string);
143 fprintf(temp, "\t\n%s Impossible to create directory %s\n",
144 root, path_string);
robbiewf0888c02002-12-04 19:42:27 +0000145 fail_exit();
146 }
robbiewf0888c02002-12-04 19:42:27 +0000147#ifdef PRINT
148 printf("\n%s\n", path_string);
149#endif
150
151 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800152 /* */
153 /* create the "path_list" file, in */
robbiewf0888c02002-12-04 19:42:27 +0000154 /* which the list of generated paths */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800155 /* will be stored so that they later */
156 /* may be checked */
157 /* */
robbiewf0888c02002-12-04 19:42:27 +0000158 /****************************************/
159
Jan Stancekcb7c6912013-03-20 10:44:33 +0100160 snp_ret = snprintf(path_list_string, sizeof(path_list_string),
161 "%s/path_list", path_string);
162 if (snp_ret < 0 || snp_ret >= sizeof(path_list_string)) {
163 tst_resm(TBROK, "snprintf(path_list_string,..) returned %d",
164 snp_ret);
165 fail_exit();
166 }
robbiewf0888c02002-12-04 19:42:27 +0000167 list_id = creat(path_list_string, FILE_MODE);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800168 if (list_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800169 fprintf(temp,
170 "\t\n%s The path_list file cannot be created, errno=%d \n",
171 root, errno);
robbiewf0888c02002-12-04 19:42:27 +0000172 fail_exit();
173 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000174
robbiewf0888c02002-12-04 19:42:27 +0000175 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800176 /* */
177 /* and store its name in path_list */
178 /* */
robbiewf0888c02002-12-04 19:42:27 +0000179 /****************************************/
180
181 strcpy(write_string, path_string);
182 len = strlen(write_string);
183 write_string[len++] = 'D';
184 write_string[len] = '\0';
185 escrivez(write_string);
186
187 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800188 /* */
189 /* generate the directory-file tree */
190 /* */
robbiewf0888c02002-12-04 19:42:27 +0000191 /****************************************/
192
193 level = 0;
194
195#ifdef PRINT
196 printf("\n\t%s\n\n", "GENERATING:");
197#endif
198
199 gen_ret_val = generate(path_string, level);
200
Wanlong Gao354ebb42012-12-07 10:10:04 +0800201 if (gen_ret_val) {
202 fprintf(temp,
203 "Failure occured in generate routine, return value %d\n",
204 gen_ret_val);
robbiewf0888c02002-12-04 19:42:27 +0000205 local_flag = FAILED;
206 }
207
208 blexit();
209 blenter();
210
211 close(list_id);
212 list_id = open(path_list_string, READ);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800213 if (list_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 fprintf(temp,
215 "\t\n%s The path_list file cannot be opened for reading, errno=%d\n",
216 root, errno);
robbiewf0888c02002-12-04 19:42:27 +0000217 fail_exit();
218 }
219 list_stream = fdopen(list_id, "r");
220
221 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 /* */
223 /* check the directory-file tree */
224 /* for correctness */
225 /* */
robbiewf0888c02002-12-04 19:42:27 +0000226 /****************************************/
227
228#ifdef PRINT
229 printf("\n\t%s\n\n", "CHECKING:");
230#endif
231
232 ch_ret_val = check();
233
Wanlong Gao354ebb42012-12-07 10:10:04 +0800234 if (ch_ret_val) {
235 fprintf(temp,
236 "Failure occured in check routine, return value %d\n",
237 ch_ret_val);
robbiewf0888c02002-12-04 19:42:27 +0000238 local_flag = FAILED;
239 }
240
Wanlong Gao354ebb42012-12-07 10:10:04 +0800241 status = fclose(list_stream);
242 if (status != 0) {
243 fprintf(temp,
244 "Failed to close list_stream: ret=%d errno=%d (%s)\n",
245 status, errno, strerror(errno));
246 local_flag = FAILED;
247 }
robbiew5c367e42005-12-23 18:16:06 +0000248
robbiewf0888c02002-12-04 19:42:27 +0000249 blexit();
250
robbiewf0888c02002-12-04 19:42:27 +0000251 /*
252 * Now fork and exec a system call to remove the directory.
253 */
254
255#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800256 fprintf(temp, "\nClean up:\trm string = %s\n", rm_string);
robbiewf0888c02002-12-04 19:42:27 +0000257#endif
258 fflush(stdout);
259 fflush(temp);
260
261 status = system(rm_string);
262
263 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800264 fprintf(temp, "Caution-``%s'' may have failed\n", rm_string);
robbiewf0888c02002-12-04 19:42:27 +0000265 fprintf(temp, "rm command exit status = %d\n", status);
266 }
267
robbiewf0888c02002-12-04 19:42:27 +0000268 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800269 /* */
270 /* .....and exit main */
271 /* */
robbiewf0888c02002-12-04 19:42:27 +0000272 /****************************************/
273
274 anyfail();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800275 /***** NOT REACHED ******/
Garrett Cooper2c282152010-12-16 00:55:50 -0800276 tst_exit();
robbiewf0888c02002-12-04 19:42:27 +0000277}
278
279int generate(string, level)
280
281/****************************************/
282/* */
283/* generate recursively a tree of */
284/* directories and files: within */
285/* created directory, an alternating */
286/* series of files and directories */
287/* are constructed---until tree */
288/* breadth and depth limits are */
289/* reached or an error occurs */
290/* */
291/****************************************/
robbiewf0888c02002-12-04 19:42:27 +0000292 /***************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800293 /* */
294char string[]; /* the directory path */
robbiewf0888c02002-12-04 19:42:27 +0000295 /* string below which a */
296 /* tree is generated */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800297 /* */
robbiewf0888c02002-12-04 19:42:27 +0000298 /***************************/
299
300 /***************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800301 /* */
302int level; /* the tree depth variable */
303 /* */
robbiewf0888c02002-12-04 19:42:27 +0000304 /***************************/
305{
306 int switch_flag;
307 int ret_val = 0;
308 int new_ret_val, len, ret_len;
309 char new_string[PATH_STRING_LENGTH + 1];
310 int new_level;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800311 int i, j; /* iteration counters */
Jan Stancekcb7c6912013-03-20 10:44:33 +0100312 int snp_ret;
robbiewf0888c02002-12-04 19:42:27 +0000313
314 switch_flag = level & TRUE;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800315 if (strlen(string) >= MAX_PATH_STRING_LENGTH) {
robbiewf0888c02002-12-04 19:42:27 +0000316
317 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800318 /* */
319 /* Maximum path name length */
320 /* reached */
321 /* */
robbiewf0888c02002-12-04 19:42:27 +0000322 /********************************/
323
Wanlong Gao354ebb42012-12-07 10:10:04 +0800324 fprintf(temp, "\tMaximum path_name length reached.\n");
325 return (-1);
326 } else if (level < MAX_DEPTH) {
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800327 for (i = 0; i <= MAX_BREADTH; i++) {
robbiewf0888c02002-12-04 19:42:27 +0000328 get_next_name();
Jan Stancekcb7c6912013-03-20 10:44:33 +0100329 snp_ret = snprintf(new_string, sizeof(new_string),
330 "%s/%s", string, name);
331 if (snp_ret < 0 || snp_ret >= sizeof(new_string)) {
332 tst_resm(TBROK, "snprintf(new_string,..) "
333 "returned %d", snp_ret);
334 fail_exit();
335 }
robbiewf0888c02002-12-04 19:42:27 +0000336
337 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800338 /* */
339 /* switch between creating files */
340 /* and making directories */
341 /* */
robbiewf0888c02002-12-04 19:42:27 +0000342 /****************************************/
343
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800344 if (switch_flag) {
robbiewf0888c02002-12-04 19:42:27 +0000345 switch_flag = FALSE;
346
347 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800348 /* */
349 /* create a new file */
350 /* */
robbiewf0888c02002-12-04 19:42:27 +0000351 /****************************************/
352
353 file_id = creat(new_string, FILE_MODE);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800354 if (file_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800355 fprintf(temp,
356 "\tImpossible to create file %s, errno=%d\n",
Garrett Cooper2c282152010-12-16 00:55:50 -0800357 new_string, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800358 return (-2);
robbiewf0888c02002-12-04 19:42:27 +0000359 }
robbiewf0888c02002-12-04 19:42:27 +0000360#ifdef PRINT
361 printf("%d %s F\n", level, new_string);
362#endif
363
robbiewf0888c02002-12-04 19:42:27 +0000364 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800365 /* */
366 /* write to it */
367 /* */
robbiewf0888c02002-12-04 19:42:27 +0000368 /****************************************/
369
370 len = strlen(new_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800371 for (j = 1; j <= FILE_LENGTH; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800372 ret_len =
373 write(file_id, new_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800374 if (ret_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800375 fprintf(temp,
376 "\tUnsuccessful write to file %s, expected return of %d, got %d, errno=%d\n",
377 new_string, len,
378 ret_len, errno);
379 return (-3);
Garrett Cooper2c282152010-12-16 00:55:50 -0800380 }
robbiewf0888c02002-12-04 19:42:27 +0000381 }
382 close(file_id);
383
384 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800385 /* */
386 /* and store its name in path_list */
387 /* */
robbiewf0888c02002-12-04 19:42:27 +0000388 /****************************************/
389
390 strcpy(write_string, new_string);
391 len = strlen(write_string);
392 write_string[len++] = 'F';
393 write_string[len] = '\0';
subrata_modakbdbaec52009-02-26 12:14:51 +0000394 escrivez(write_string);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800395 } else {
robbiewf0888c02002-12-04 19:42:27 +0000396 switch_flag = TRUE;
397
398 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800399 /* */
400 /* or make a directory */
401 /* */
robbiewf0888c02002-12-04 19:42:27 +0000402 /****************************************/
403
robbiewf0888c02002-12-04 19:42:27 +0000404 ret_val = mkdir(new_string, DIRECTORY_MODE);
405
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800406 if (ret_val != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800407 fprintf(temp,
408 "\tImpossible to create directory %s, errno=%d\n",
409 new_string, errno);
410 return (-5);
robbiewf0888c02002-12-04 19:42:27 +0000411 }
robbiewf0888c02002-12-04 19:42:27 +0000412#ifdef PRINT
413 printf("%d %s D\n", level, new_string);
414#endif
415
416 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800417 /* */
418 /* store its name in path_list */
419 /* */
robbiewf0888c02002-12-04 19:42:27 +0000420 /****************************************/
421
422 strcpy(write_string, new_string);
423 len = strlen(write_string);
424 write_string[len++] = 'D';
425 write_string[len] = '\0';
subrata_modakbdbaec52009-02-26 12:14:51 +0000426 escrivez(write_string);
robbiewf0888c02002-12-04 19:42:27 +0000427
428 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800429 /* */
430 /* and generate a new level */
431 /* */
robbiewf0888c02002-12-04 19:42:27 +0000432 /****************************************/
433
434 new_level = level + 1;
435 new_ret_val = generate(new_string, new_level);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800436 if (new_ret_val < ret_val)
subrata_modakbdbaec52009-02-26 12:14:51 +0000437 ret_val = new_ret_val;
robbiewf0888c02002-12-04 19:42:27 +0000438 }
439 }
440
441 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800442 /* */
443 /* Maximum breadth reached */
444 /* */
robbiewf0888c02002-12-04 19:42:27 +0000445 /********************************/
446
Wanlong Gao354ebb42012-12-07 10:10:04 +0800447 return (ret_val);
448 } else
449 /********************************/
450 /* */
451 /* Maximum depth reached */
452 /* */
453 /********************************/
subrata_modak43337a32009-02-26 11:43:51 +0000454 return 0;
subrata_modak4bb656a2009-02-26 12:02:09 +0000455}
Wanlong Gao354ebb42012-12-07 10:10:04 +0800456
robbiewf0888c02002-12-04 19:42:27 +0000457int check()
458
459/****************************************/
460/* */
461/* check for file and directory */
462/* correctness by reading records */
463/* from the path_list and attempting */
464/* to determine if the corresponding */
465/* files or directories are as */
466/* created */
467/* */
468/****************************************/
469{
470 int len, path_mode, val, ret_len, j;
471
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800472 for (;;) {
robbiewf0888c02002-12-04 19:42:27 +0000473
474 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800475 /* */
476 /* read a path string from path_list */
477 /* */
robbiewf0888c02002-12-04 19:42:27 +0000478 /****************************************/
479
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800480 if (fscanf(list_stream, "%s", path_string) == EOF) {
robbiewf0888c02002-12-04 19:42:27 +0000481
482#ifdef PRINT
483 printf("\nEnd of path_list file reached \n");
484#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800485
subrata_modak43337a32009-02-26 11:43:51 +0000486 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000487 }
robbiewf0888c02002-12-04 19:42:27 +0000488#ifdef PRINT
489 printf("%s\n", path_string);
490#endif
491
492 len = strlen(path_string);
493 len--;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800494 if (path_string[len] == 'F') {
robbiewf0888c02002-12-04 19:42:27 +0000495
Wanlong Gao354ebb42012-12-07 10:10:04 +0800496 /********************************/
497 /* */
498 /* this should be a file */
499 /* */
robbiewf0888c02002-12-04 19:42:27 +0000500 /********************************/
501
502 path_string[len] = '\0';
503 file_id = open(path_string, READ);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800504 if (file_id <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800505 fprintf(temp,
506 "\tImpossible to open file %s, errno=%d\n",
robbiewf0888c02002-12-04 19:42:27 +0000507 path_string, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800508 return (-1);
robbiewf0888c02002-12-04 19:42:27 +0000509 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800510
robbiewf0888c02002-12-04 19:42:27 +0000511 else {
512 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800513 /* */
514 /* check its contents */
515 /* */
robbiewf0888c02002-12-04 19:42:27 +0000516 /********************************/
Garrett Cooper2c282152010-12-16 00:55:50 -0800517
robbiewf0888c02002-12-04 19:42:27 +0000518 len = strlen(path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800519 for (j = 1; j <= FILE_LENGTH; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800520 ret_len =
521 read(file_id, read_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800522 if (len != ret_len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800523 fprintf(temp,
524 "\tFile read error for file %s, expected return of %d, got %d, errno=%d\n",
525 path_string, len,
526 ret_len, errno);
527 return (-3);
robbiewf0888c02002-12-04 19:42:27 +0000528 }
529 read_string[len] = '\0';
530 val = strcmp(read_string, path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800531 if (val != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800532 fprintf(temp,
533 "\tContents of file %s are different than expected: %s\n",
534 path_string,
535 read_string);
536 return (-4);
robbiewf0888c02002-12-04 19:42:27 +0000537 }
538 }
539 close(file_id);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800540 } /* else for */
541 if (ret_len <= 0) {
542 fprintf(temp, "\tImpossible to read file %s\n",
543 path_string);
544 return (-2);
545 }
546 } else {
robbiewf0888c02002-12-04 19:42:27 +0000547
Wanlong Gao354ebb42012-12-07 10:10:04 +0800548 /********************************/
549 /* */
550 /* otherwise.......... */
551 /* it should be a directory */
552 /* */
robbiewf0888c02002-12-04 19:42:27 +0000553 /********************************/
554
555 path_string[len] = '\0';
556 path_mode = mode(path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800557 if (path_mode == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800558 fprintf(temp,
559 "\tPreviously created directory path %s was not open\n",
560 path_string);
561 return (-4);
robbiewf0888c02002-12-04 19:42:27 +0000562 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800563 if ((040000 & path_mode) != 040000) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800564 fprintf(temp,
565 "\tPath %s was not recognized to be a directory\n",
566 path_string);
567 fprintf(temp, "\tIts mode is %o\n", path_mode);
568 return (-5);
robbiewf0888c02002-12-04 19:42:27 +0000569 }
570 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800571 } /* while */
robbiewf0888c02002-12-04 19:42:27 +0000572}
573
Wanlong Gao354ebb42012-12-07 10:10:04 +0800574int get_next_name()
robbiewf0888c02002-12-04 19:42:27 +0000575
576/****************************************/
577/* */
578/* get the next---in a dictionary */
579/* sense---file or directory name */
580/* */
581/****************************************/
robbiewf0888c02002-12-04 19:42:27 +0000582{
583 static int k;
584 int i;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800585 int last_position;
robbiewf0888c02002-12-04 19:42:27 +0000586
587 last_position = NAME_LENGTH - 1;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800588 if (k == 0) {
robbiewf0888c02002-12-04 19:42:27 +0000589
590 /************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800591 /* */
592 /* initialize name */
593 /* */
robbiewf0888c02002-12-04 19:42:27 +0000594 /************************/
595
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800596 for (i = 0; i < NAME_LENGTH; i++)
robbiewf0888c02002-12-04 19:42:27 +0000597 name[i] = 'a';
598 name[NAME_LENGTH] = '\0';
599 k++;
600 }
601 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800602 /* */
603 else
604 increment_name(last_position); /* i.e., beginning at the last */
605 /* position */
606 /* */
robbiewf0888c02002-12-04 19:42:27 +0000607 /********************************/
subrata_modak43337a32009-02-26 11:43:51 +0000608 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000609}
610
611int increment_name(position)
612
613/****************************************/
614/* */
615/* recursively revise the letters in */
616/* a name to get the lexiographically */
617/* next name */
618/* */
619/****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800620 int position;
robbiewf0888c02002-12-04 19:42:27 +0000621{
622 int next_position;
623
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800624 if (name[position] == 'z')
625 if (position == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800626 fprintf(temp,
627 "\tERROR: There are no more available names\n");
robbiewf0888c02002-12-04 19:42:27 +0000628 fail_exit();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800629 } else {
630 name[position] = 'a'; /**********************/
631 next_position = --position; /* */
632 increment_name(next_position); /* increment the */
633 /* previous letter */
634 /* */
subrata_modakbdbaec52009-02-26 12:14:51 +0000635 /**********************/
robbiewf0888c02002-12-04 19:42:27 +0000636 }
637 /*********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800638 /* */
639 else
640 name[position]++; /* otherwise, increment this one */
641 return 0; /* */
robbiewf0888c02002-12-04 19:42:27 +0000642 /*********************************/
643}
644
645int mode(path_string)
646
647/****************************************/
648/* */
649/* determine and return the mode of */
650/* the file named by path_string */
651/* */
652/****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800653 char path_string[];
robbiewf0888c02002-12-04 19:42:27 +0000654{
655 struct stat buf;
656 int ret_val, mod;
657
658 ret_val = stat(path_string, &buf);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800659 if (ret_val == -1)
660 return (-1);
robbiewf0888c02002-12-04 19:42:27 +0000661 else {
662 mod = buf.st_mode;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800663 return (mod);
robbiewf0888c02002-12-04 19:42:27 +0000664 }
665}
666
667int escrivez(string)
668
669char string[];
670{
671 char write_string[PATH_STRING_LENGTH + 1];
672 int len, ret_len;
673
674 strcpy(write_string, string);
675 len = strlen(write_string);
676 write_string[len] = '\n';
Wanlong Gao354ebb42012-12-07 10:10:04 +0800677 len++;
robbiewf0888c02002-12-04 19:42:27 +0000678 ret_len = write(list_id, write_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800679 if (len != ret_len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800680 fprintf(temp,
681 "\tA string of deviant length %d written to path_list, errno=%d\n",
682 ret_len, errno);
robbiewf0888c02002-12-04 19:42:27 +0000683 fail_exit();
684 }
subrata_modak43337a32009-02-26 11:43:51 +0000685 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000686}
687
688int term()
689{
690 int status;
691
692 fprintf(temp, "\tterm - got SIGTERM, cleaning up.\n");
693
694 if (list_stream != NULL)
695 fclose(list_stream);
696 close(list_id);
697 close(file_id);
698
699 status = system(rm_string);
700 if (status) {
701 fprintf(temp, "Caution - ``%s'' may have failed.\n", rm_string);
702 fprintf(temp, "rm command exit status = %d\n", status);
703 }
704
705 ok_exit();
706 /***NOT REACHED***/
subrata_modak43337a32009-02-26 11:43:51 +0000707 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000708
709}
710
711/** LTP Port **/
712/*
713 * setup
714 *
715 * Do set up - here its a dummy function
716 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800717void setup()
robbiewf0888c02002-12-04 19:42:27 +0000718{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800719 tst_tmpdir();
720 temp = stderr;
robbiewf0888c02002-12-04 19:42:27 +0000721}
722
723/*
724 * Function: blexit()
725 *
726 * Description: This function will exit a block, a block may be a lo
727gical unit
728 * of a test. It will report the status if the test ie
729fail or
730 * pass.
731 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800732void blexit()
robbiewf0888c02002-12-04 19:42:27 +0000733{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800734 (local_flag == PASSED) ? tst_resm(TPASS, "Test block %d", block_number)
735 : tst_resm(TFAIL, "Test block %d", block_number);
736 block_number++;
737 return;
robbiewf0888c02002-12-04 19:42:27 +0000738}
739
740/*
741 * Function: blenter()
742 *
743 * Description: Print message on entering a new block
744 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800745void blenter()
robbiewf0888c02002-12-04 19:42:27 +0000746{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800747 local_flag = PASSED;
748 return;
robbiewf0888c02002-12-04 19:42:27 +0000749}
750
751/*
752 * fail_exit()
753 *
754 * Exit on failure
755 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800756void fail_exit()
robbiewf0888c02002-12-04 19:42:27 +0000757{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100758 tst_brkm(TFAIL, tst_rmdir, "Test failed");
robbiewf0888c02002-12-04 19:42:27 +0000759}
760
761/*
762 *
763 * Function: anyfail()
764 *
765 * Description: Exit a test.
766 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800767void anyfail()
robbiewf0888c02002-12-04 19:42:27 +0000768{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800769 (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
770 : tst_resm(TPASS, "Test passed");
771 tst_rmdir();
772 tst_exit();
robbiewf0888c02002-12-04 19:42:27 +0000773}
774
775/*
776 * ok_exit
777 *
778 * Calling block passed the test
779 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800780void ok_exit()
robbiewf0888c02002-12-04 19:42:27 +0000781{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800782 local_flag = PASSED;
783 return;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700784}