blob: 86c3b955cdd936667f4734349a9b119065b3ca82 [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"
robbiewf0888c02002-12-04 19:42:27 +000066
67void blexit(void);
68void blenter(void);
69void setup(void);
70void fail_exit(void);
71void anyfail(void);
72void ok_exit(void);
73
74#define FAILED 0
75#define PASSED 1
76
77int local_flag = PASSED;
78int block_number;
robbiewf0888c02002-12-04 19:42:27 +000079FILE *temp;
80
Wanlong Gao354ebb42012-12-07 10:10:04 +080081char *TCID = "inode01"; /* Test program identifier. */
82int TST_TOTAL = 2; /* Total number of test cases. */
robbiewf0888c02002-12-04 19:42:27 +000083/**************/
84
85#ifdef LINUX
86#include <string.h>
87#endif
88
89char name[NAME_LENGTH + 1];
90char path_string[PATH_STRING_LENGTH + 1];
91char read_string[PATH_STRING_LENGTH + 1];
92char write_string[PATH_STRING_LENGTH + 1];
93char rm_string[200];
94
Wanlong Gao354ebb42012-12-07 10:10:04 +080095FILE *list_stream = NULL;
96int file_id;
97int list_id;
robbiewf0888c02002-12-04 19:42:27 +000098
99int increment_name(), get_next_name(), mode(), escrivez();
100
101int main()
robbiewf0888c02002-12-04 19:42:27 +0000102{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800103 char root[16]; //as pids can get much longer
robbiewf0888c02002-12-04 19:42:27 +0000104 int gen_ret_val, ch_ret_val, level;
105 int ret_val;
106 int generate(), check();
subrata_modak7523e3b2008-10-17 10:03:54 +0000107 char path_list_string[PATH_STRING_LENGTH + 1];
robbiewf0888c02002-12-04 19:42:27 +0000108 int status;
109 int len;
110 int term();
Jan Stancekcb7c6912013-03-20 10:44:33 +0100111 int snp_ret;
robbiewf0888c02002-12-04 19:42:27 +0000112
113 strcpy(path_string, "inode");
114 sprintf(root, "A%d", getpid());
115 strcat(path_string, root);
116
117 strcpy(rm_string, "rm -rf ");
118 strcat(rm_string, path_string);
119
120 setup();
121
122 if (signal(SIGTERM, (void (*)())term) == SIG_ERR) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800123 fprintf(temp, "\tSIGTERM signal set failed!, errno=%d\n",
124 errno);
robbiewf0888c02002-12-04 19:42:27 +0000125 fail_exit();
126 }
127
128 blenter();
subrata_modakbdbaec52009-02-26 12:14:51 +0000129
robbiewf0888c02002-12-04 19:42:27 +0000130 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 /* */
robbiewf0888c02002-12-04 19:42:27 +0000132 /* make the root directory for */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 /* the tree */
134 /* */
robbiewf0888c02002-12-04 19:42:27 +0000135 /********************************/
136
137 ret_val = mkdir(path_string, DIRECTORY_MODE);
138
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800139 if (ret_val == -1) {
robbiewf0888c02002-12-04 19:42:27 +0000140 perror("mkdir error");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 fprintf(temp, "\tcreating directory '%s'\n", path_string);
142 fprintf(temp, "\t\n%s Impossible to create directory %s\n",
143 root, path_string);
robbiewf0888c02002-12-04 19:42:27 +0000144 fail_exit();
145 }
robbiewf0888c02002-12-04 19:42:27 +0000146#ifdef PRINT
147 printf("\n%s\n", path_string);
148#endif
149
150 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800151 /* */
152 /* create the "path_list" file, in */
robbiewf0888c02002-12-04 19:42:27 +0000153 /* which the list of generated paths */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 /* will be stored so that they later */
155 /* may be checked */
156 /* */
robbiewf0888c02002-12-04 19:42:27 +0000157 /****************************************/
158
Jan Stancekcb7c6912013-03-20 10:44:33 +0100159 snp_ret = snprintf(path_list_string, sizeof(path_list_string),
160 "%s/path_list", path_string);
161 if (snp_ret < 0 || snp_ret >= sizeof(path_list_string)) {
162 tst_resm(TBROK, "snprintf(path_list_string,..) returned %d",
163 snp_ret);
164 fail_exit();
165 }
robbiewf0888c02002-12-04 19:42:27 +0000166 list_id = creat(path_list_string, FILE_MODE);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800167 if (list_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800168 fprintf(temp,
169 "\t\n%s The path_list file cannot be created, errno=%d \n",
170 root, errno);
robbiewf0888c02002-12-04 19:42:27 +0000171 fail_exit();
172 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000173
robbiewf0888c02002-12-04 19:42:27 +0000174 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 /* */
176 /* and store its name in path_list */
177 /* */
robbiewf0888c02002-12-04 19:42:27 +0000178 /****************************************/
179
180 strcpy(write_string, path_string);
181 len = strlen(write_string);
182 write_string[len++] = 'D';
183 write_string[len] = '\0';
184 escrivez(write_string);
185
186 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800187 /* */
188 /* generate the directory-file tree */
189 /* */
robbiewf0888c02002-12-04 19:42:27 +0000190 /****************************************/
191
192 level = 0;
193
194#ifdef PRINT
195 printf("\n\t%s\n\n", "GENERATING:");
196#endif
197
198 gen_ret_val = generate(path_string, level);
199
Wanlong Gao354ebb42012-12-07 10:10:04 +0800200 if (gen_ret_val) {
201 fprintf(temp,
202 "Failure occured in generate routine, return value %d\n",
203 gen_ret_val);
robbiewf0888c02002-12-04 19:42:27 +0000204 local_flag = FAILED;
205 }
206
207 blexit();
208 blenter();
209
210 close(list_id);
211 list_id = open(path_list_string, READ);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800212 if (list_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800213 fprintf(temp,
214 "\t\n%s The path_list file cannot be opened for reading, errno=%d\n",
215 root, errno);
robbiewf0888c02002-12-04 19:42:27 +0000216 fail_exit();
217 }
218 list_stream = fdopen(list_id, "r");
219
220 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800221 /* */
222 /* check the directory-file tree */
223 /* for correctness */
224 /* */
robbiewf0888c02002-12-04 19:42:27 +0000225 /****************************************/
226
227#ifdef PRINT
228 printf("\n\t%s\n\n", "CHECKING:");
229#endif
230
231 ch_ret_val = check();
232
Wanlong Gao354ebb42012-12-07 10:10:04 +0800233 if (ch_ret_val) {
234 fprintf(temp,
235 "Failure occured in check routine, return value %d\n",
236 ch_ret_val);
robbiewf0888c02002-12-04 19:42:27 +0000237 local_flag = FAILED;
238 }
239
Wanlong Gao354ebb42012-12-07 10:10:04 +0800240 status = fclose(list_stream);
241 if (status != 0) {
242 fprintf(temp,
243 "Failed to close list_stream: ret=%d errno=%d (%s)\n",
244 status, errno, strerror(errno));
245 local_flag = FAILED;
246 }
robbiew5c367e42005-12-23 18:16:06 +0000247
robbiewf0888c02002-12-04 19:42:27 +0000248 blexit();
249
robbiewf0888c02002-12-04 19:42:27 +0000250 /*
251 * Now fork and exec a system call to remove the directory.
252 */
253
254#ifdef DEBUG
Wanlong Gao354ebb42012-12-07 10:10:04 +0800255 fprintf(temp, "\nClean up:\trm string = %s\n", rm_string);
robbiewf0888c02002-12-04 19:42:27 +0000256#endif
257 fflush(stdout);
258 fflush(temp);
259
260 status = system(rm_string);
261
262 if (status) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800263 fprintf(temp, "Caution-``%s'' may have failed\n", rm_string);
robbiewf0888c02002-12-04 19:42:27 +0000264 fprintf(temp, "rm command exit status = %d\n", status);
265 }
266
robbiewf0888c02002-12-04 19:42:27 +0000267 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800268 /* */
269 /* .....and exit main */
270 /* */
robbiewf0888c02002-12-04 19:42:27 +0000271 /****************************************/
272
273 anyfail();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800274 /***** NOT REACHED ******/
Garrett Cooper2c282152010-12-16 00:55:50 -0800275 tst_exit();
robbiewf0888c02002-12-04 19:42:27 +0000276}
277
278int generate(string, level)
279
280/****************************************/
281/* */
282/* generate recursively a tree of */
283/* directories and files: within */
284/* created directory, an alternating */
285/* series of files and directories */
286/* are constructed---until tree */
287/* breadth and depth limits are */
288/* reached or an error occurs */
289/* */
290/****************************************/
robbiewf0888c02002-12-04 19:42:27 +0000291 /***************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800292 /* */
293char string[]; /* the directory path */
robbiewf0888c02002-12-04 19:42:27 +0000294 /* string below which a */
295 /* tree is generated */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800296 /* */
robbiewf0888c02002-12-04 19:42:27 +0000297 /***************************/
298
299 /***************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800300 /* */
301int level; /* the tree depth variable */
302 /* */
robbiewf0888c02002-12-04 19:42:27 +0000303 /***************************/
304{
305 int switch_flag;
306 int ret_val = 0;
307 int new_ret_val, len, ret_len;
308 char new_string[PATH_STRING_LENGTH + 1];
309 int new_level;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800310 int i, j; /* iteration counters */
Jan Stancekcb7c6912013-03-20 10:44:33 +0100311 int snp_ret;
robbiewf0888c02002-12-04 19:42:27 +0000312
313 switch_flag = level & TRUE;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800314 if (strlen(string) >= MAX_PATH_STRING_LENGTH) {
robbiewf0888c02002-12-04 19:42:27 +0000315
316 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800317 /* */
318 /* Maximum path name length */
319 /* reached */
320 /* */
robbiewf0888c02002-12-04 19:42:27 +0000321 /********************************/
322
Wanlong Gao354ebb42012-12-07 10:10:04 +0800323 fprintf(temp, "\tMaximum path_name length reached.\n");
324 return (-1);
325 } else if (level < MAX_DEPTH) {
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800326 for (i = 0; i <= MAX_BREADTH; i++) {
robbiewf0888c02002-12-04 19:42:27 +0000327 get_next_name();
Jan Stancekcb7c6912013-03-20 10:44:33 +0100328 snp_ret = snprintf(new_string, sizeof(new_string),
329 "%s/%s", string, name);
330 if (snp_ret < 0 || snp_ret >= sizeof(new_string)) {
331 tst_resm(TBROK, "snprintf(new_string,..) "
332 "returned %d", snp_ret);
333 fail_exit();
334 }
robbiewf0888c02002-12-04 19:42:27 +0000335
336 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800337 /* */
338 /* switch between creating files */
339 /* and making directories */
340 /* */
robbiewf0888c02002-12-04 19:42:27 +0000341 /****************************************/
342
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800343 if (switch_flag) {
robbiewf0888c02002-12-04 19:42:27 +0000344 switch_flag = FALSE;
345
346 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800347 /* */
348 /* create a new file */
349 /* */
robbiewf0888c02002-12-04 19:42:27 +0000350 /****************************************/
351
352 file_id = creat(new_string, FILE_MODE);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800353 if (file_id == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800354 fprintf(temp,
355 "\tImpossible to create file %s, errno=%d\n",
Garrett Cooper2c282152010-12-16 00:55:50 -0800356 new_string, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800357 return (-2);
robbiewf0888c02002-12-04 19:42:27 +0000358 }
robbiewf0888c02002-12-04 19:42:27 +0000359#ifdef PRINT
360 printf("%d %s F\n", level, new_string);
361#endif
362
robbiewf0888c02002-12-04 19:42:27 +0000363 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800364 /* */
365 /* write to it */
366 /* */
robbiewf0888c02002-12-04 19:42:27 +0000367 /****************************************/
368
369 len = strlen(new_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800370 for (j = 1; j <= FILE_LENGTH; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800371 ret_len =
372 write(file_id, new_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800373 if (ret_len != len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800374 fprintf(temp,
375 "\tUnsuccessful write to file %s, expected return of %d, got %d, errno=%d\n",
376 new_string, len,
377 ret_len, errno);
378 return (-3);
Garrett Cooper2c282152010-12-16 00:55:50 -0800379 }
robbiewf0888c02002-12-04 19:42:27 +0000380 }
381 close(file_id);
382
383 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800384 /* */
385 /* and store its name in path_list */
386 /* */
robbiewf0888c02002-12-04 19:42:27 +0000387 /****************************************/
388
389 strcpy(write_string, new_string);
390 len = strlen(write_string);
391 write_string[len++] = 'F';
392 write_string[len] = '\0';
subrata_modakbdbaec52009-02-26 12:14:51 +0000393 escrivez(write_string);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800394 } else {
robbiewf0888c02002-12-04 19:42:27 +0000395 switch_flag = TRUE;
396
397 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800398 /* */
399 /* or make a directory */
400 /* */
robbiewf0888c02002-12-04 19:42:27 +0000401 /****************************************/
402
robbiewf0888c02002-12-04 19:42:27 +0000403 ret_val = mkdir(new_string, DIRECTORY_MODE);
404
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800405 if (ret_val != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800406 fprintf(temp,
407 "\tImpossible to create directory %s, errno=%d\n",
408 new_string, errno);
409 return (-5);
robbiewf0888c02002-12-04 19:42:27 +0000410 }
robbiewf0888c02002-12-04 19:42:27 +0000411#ifdef PRINT
412 printf("%d %s D\n", level, new_string);
413#endif
414
415 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800416 /* */
417 /* store its name in path_list */
418 /* */
robbiewf0888c02002-12-04 19:42:27 +0000419 /****************************************/
420
421 strcpy(write_string, new_string);
422 len = strlen(write_string);
423 write_string[len++] = 'D';
424 write_string[len] = '\0';
subrata_modakbdbaec52009-02-26 12:14:51 +0000425 escrivez(write_string);
robbiewf0888c02002-12-04 19:42:27 +0000426
427 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800428 /* */
429 /* and generate a new level */
430 /* */
robbiewf0888c02002-12-04 19:42:27 +0000431 /****************************************/
432
433 new_level = level + 1;
434 new_ret_val = generate(new_string, new_level);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800435 if (new_ret_val < ret_val)
subrata_modakbdbaec52009-02-26 12:14:51 +0000436 ret_val = new_ret_val;
robbiewf0888c02002-12-04 19:42:27 +0000437 }
438 }
439
440 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800441 /* */
442 /* Maximum breadth reached */
443 /* */
robbiewf0888c02002-12-04 19:42:27 +0000444 /********************************/
445
Wanlong Gao354ebb42012-12-07 10:10:04 +0800446 return (ret_val);
447 } else
448 /********************************/
449 /* */
450 /* Maximum depth reached */
451 /* */
452 /********************************/
subrata_modak43337a32009-02-26 11:43:51 +0000453 return 0;
subrata_modak4bb656a2009-02-26 12:02:09 +0000454}
Wanlong Gao354ebb42012-12-07 10:10:04 +0800455
robbiewf0888c02002-12-04 19:42:27 +0000456int check()
457
458/****************************************/
459/* */
460/* check for file and directory */
461/* correctness by reading records */
462/* from the path_list and attempting */
463/* to determine if the corresponding */
464/* files or directories are as */
465/* created */
466/* */
467/****************************************/
468{
469 int len, path_mode, val, ret_len, j;
470
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800471 for (;;) {
robbiewf0888c02002-12-04 19:42:27 +0000472
473 /****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800474 /* */
475 /* read a path string from path_list */
476 /* */
robbiewf0888c02002-12-04 19:42:27 +0000477 /****************************************/
478
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800479 if (fscanf(list_stream, "%s", path_string) == EOF) {
robbiewf0888c02002-12-04 19:42:27 +0000480
481#ifdef PRINT
482 printf("\nEnd of path_list file reached \n");
483#endif
Garrett Cooper2c282152010-12-16 00:55:50 -0800484
subrata_modak43337a32009-02-26 11:43:51 +0000485 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000486 }
robbiewf0888c02002-12-04 19:42:27 +0000487#ifdef PRINT
488 printf("%s\n", path_string);
489#endif
490
491 len = strlen(path_string);
492 len--;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800493 if (path_string[len] == 'F') {
robbiewf0888c02002-12-04 19:42:27 +0000494
Wanlong Gao354ebb42012-12-07 10:10:04 +0800495 /********************************/
496 /* */
497 /* this should be a file */
498 /* */
robbiewf0888c02002-12-04 19:42:27 +0000499 /********************************/
500
501 path_string[len] = '\0';
502 file_id = open(path_string, READ);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800503 if (file_id <= 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800504 fprintf(temp,
505 "\tImpossible to open file %s, errno=%d\n",
robbiewf0888c02002-12-04 19:42:27 +0000506 path_string, errno);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800507 return (-1);
robbiewf0888c02002-12-04 19:42:27 +0000508 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800509
robbiewf0888c02002-12-04 19:42:27 +0000510 else {
511 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800512 /* */
513 /* check its contents */
514 /* */
robbiewf0888c02002-12-04 19:42:27 +0000515 /********************************/
Garrett Cooper2c282152010-12-16 00:55:50 -0800516
robbiewf0888c02002-12-04 19:42:27 +0000517 len = strlen(path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800518 for (j = 1; j <= FILE_LENGTH; j++) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800519 ret_len =
520 read(file_id, read_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800521 if (len != ret_len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800522 fprintf(temp,
523 "\tFile read error for file %s, expected return of %d, got %d, errno=%d\n",
524 path_string, len,
525 ret_len, errno);
526 return (-3);
robbiewf0888c02002-12-04 19:42:27 +0000527 }
528 read_string[len] = '\0';
529 val = strcmp(read_string, path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800530 if (val != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800531 fprintf(temp,
532 "\tContents of file %s are different than expected: %s\n",
533 path_string,
534 read_string);
535 return (-4);
robbiewf0888c02002-12-04 19:42:27 +0000536 }
537 }
538 close(file_id);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800539 } /* else for */
540 if (ret_len <= 0) {
541 fprintf(temp, "\tImpossible to read file %s\n",
542 path_string);
543 return (-2);
544 }
545 } else {
robbiewf0888c02002-12-04 19:42:27 +0000546
Wanlong Gao354ebb42012-12-07 10:10:04 +0800547 /********************************/
548 /* */
549 /* otherwise.......... */
550 /* it should be a directory */
551 /* */
robbiewf0888c02002-12-04 19:42:27 +0000552 /********************************/
553
554 path_string[len] = '\0';
555 path_mode = mode(path_string);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800556 if (path_mode == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800557 fprintf(temp,
558 "\tPreviously created directory path %s was not open\n",
559 path_string);
560 return (-4);
robbiewf0888c02002-12-04 19:42:27 +0000561 }
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800562 if ((040000 & path_mode) != 040000) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800563 fprintf(temp,
564 "\tPath %s was not recognized to be a directory\n",
565 path_string);
566 fprintf(temp, "\tIts mode is %o\n", path_mode);
567 return (-5);
robbiewf0888c02002-12-04 19:42:27 +0000568 }
569 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800570 } /* while */
robbiewf0888c02002-12-04 19:42:27 +0000571}
572
Wanlong Gao354ebb42012-12-07 10:10:04 +0800573int get_next_name()
robbiewf0888c02002-12-04 19:42:27 +0000574
575/****************************************/
576/* */
577/* get the next---in a dictionary */
578/* sense---file or directory name */
579/* */
580/****************************************/
robbiewf0888c02002-12-04 19:42:27 +0000581{
582 static int k;
583 int i;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800584 int last_position;
robbiewf0888c02002-12-04 19:42:27 +0000585
586 last_position = NAME_LENGTH - 1;
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800587 if (k == 0) {
robbiewf0888c02002-12-04 19:42:27 +0000588
589 /************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800590 /* */
591 /* initialize name */
592 /* */
robbiewf0888c02002-12-04 19:42:27 +0000593 /************************/
594
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800595 for (i = 0; i < NAME_LENGTH; i++)
robbiewf0888c02002-12-04 19:42:27 +0000596 name[i] = 'a';
597 name[NAME_LENGTH] = '\0';
598 k++;
599 }
600 /********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800601 /* */
602 else
603 increment_name(last_position); /* i.e., beginning at the last */
604 /* position */
605 /* */
robbiewf0888c02002-12-04 19:42:27 +0000606 /********************************/
subrata_modak43337a32009-02-26 11:43:51 +0000607 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000608}
609
610int increment_name(position)
611
612/****************************************/
613/* */
614/* recursively revise the letters in */
615/* a name to get the lexiographically */
616/* next name */
617/* */
618/****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800619 int position;
robbiewf0888c02002-12-04 19:42:27 +0000620{
621 int next_position;
622
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800623 if (name[position] == 'z')
624 if (position == 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800625 fprintf(temp,
626 "\tERROR: There are no more available names\n");
robbiewf0888c02002-12-04 19:42:27 +0000627 fail_exit();
Wanlong Gao354ebb42012-12-07 10:10:04 +0800628 } else {
629 name[position] = 'a'; /**********************/
630 next_position = --position; /* */
631 increment_name(next_position); /* increment the */
632 /* previous letter */
633 /* */
subrata_modakbdbaec52009-02-26 12:14:51 +0000634 /**********************/
robbiewf0888c02002-12-04 19:42:27 +0000635 }
636 /*********************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800637 /* */
638 else
639 name[position]++; /* otherwise, increment this one */
640 return 0; /* */
robbiewf0888c02002-12-04 19:42:27 +0000641 /*********************************/
642}
643
644int mode(path_string)
645
646/****************************************/
647/* */
648/* determine and return the mode of */
649/* the file named by path_string */
650/* */
651/****************************************/
Wanlong Gao354ebb42012-12-07 10:10:04 +0800652 char path_string[];
robbiewf0888c02002-12-04 19:42:27 +0000653{
654 struct stat buf;
655 int ret_val, mod;
656
657 ret_val = stat(path_string, &buf);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800658 if (ret_val == -1)
659 return (-1);
robbiewf0888c02002-12-04 19:42:27 +0000660 else {
661 mod = buf.st_mode;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800662 return (mod);
robbiewf0888c02002-12-04 19:42:27 +0000663 }
664}
665
666int escrivez(string)
667
668char string[];
669{
670 char write_string[PATH_STRING_LENGTH + 1];
671 int len, ret_len;
672
673 strcpy(write_string, string);
674 len = strlen(write_string);
675 write_string[len] = '\n';
Wanlong Gao354ebb42012-12-07 10:10:04 +0800676 len++;
robbiewf0888c02002-12-04 19:42:27 +0000677 ret_len = write(list_id, write_string, len);
Garrett Cooperdf3eb162010-11-28 22:44:32 -0800678 if (len != ret_len) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800679 fprintf(temp,
680 "\tA string of deviant length %d written to path_list, errno=%d\n",
681 ret_len, errno);
robbiewf0888c02002-12-04 19:42:27 +0000682 fail_exit();
683 }
subrata_modak43337a32009-02-26 11:43:51 +0000684 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000685}
686
687int term()
688{
689 int status;
690
691 fprintf(temp, "\tterm - got SIGTERM, cleaning up.\n");
692
693 if (list_stream != NULL)
694 fclose(list_stream);
695 close(list_id);
696 close(file_id);
697
698 status = system(rm_string);
699 if (status) {
700 fprintf(temp, "Caution - ``%s'' may have failed.\n", rm_string);
701 fprintf(temp, "rm command exit status = %d\n", status);
702 }
703
704 ok_exit();
705 /***NOT REACHED***/
subrata_modak43337a32009-02-26 11:43:51 +0000706 return 0;
robbiewf0888c02002-12-04 19:42:27 +0000707
708}
709
710/** LTP Port **/
711/*
712 * setup
713 *
714 * Do set up - here its a dummy function
715 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800716void setup()
robbiewf0888c02002-12-04 19:42:27 +0000717{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800718 tst_tmpdir();
719 temp = stderr;
robbiewf0888c02002-12-04 19:42:27 +0000720}
721
722/*
723 * Function: blexit()
724 *
725 * Description: This function will exit a block, a block may be a lo
726gical unit
727 * of a test. It will report the status if the test ie
728fail or
729 * pass.
730 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800731void blexit()
robbiewf0888c02002-12-04 19:42:27 +0000732{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800733 (local_flag == PASSED) ? tst_resm(TPASS, "Test block %d", block_number)
734 : tst_resm(TFAIL, "Test block %d", block_number);
735 block_number++;
736 return;
robbiewf0888c02002-12-04 19:42:27 +0000737}
738
739/*
740 * Function: blenter()
741 *
742 * Description: Print message on entering a new block
743 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800744void blenter()
robbiewf0888c02002-12-04 19:42:27 +0000745{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800746 local_flag = PASSED;
747 return;
robbiewf0888c02002-12-04 19:42:27 +0000748}
749
750/*
751 * fail_exit()
752 *
753 * Exit on failure
754 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800755void fail_exit()
robbiewf0888c02002-12-04 19:42:27 +0000756{
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100757 tst_brkm(TFAIL, tst_rmdir, "Test failed");
robbiewf0888c02002-12-04 19:42:27 +0000758}
759
760/*
761 *
762 * Function: anyfail()
763 *
764 * Description: Exit a test.
765 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800766void anyfail()
robbiewf0888c02002-12-04 19:42:27 +0000767{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800768 (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed")
769 : tst_resm(TPASS, "Test passed");
770 tst_rmdir();
771 tst_exit();
robbiewf0888c02002-12-04 19:42:27 +0000772}
773
774/*
775 * ok_exit
776 *
777 * Calling block passed the test
778 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800779void ok_exit()
robbiewf0888c02002-12-04 19:42:27 +0000780{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800781 local_flag = PASSED;
782 return;
Chris Dearmanec6edca2012-10-17 19:54:01 -0700783}