]>
Commit | Line | Data |
---|---|---|
1 | #include <darwintest.h> | |
2 | ||
3 | #include "xnu_quick_test_helpers.h" | |
4 | ||
5 | #include <fcntl.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | void create_target_directory( const char * the_targetp ) | |
9 | { | |
10 | int err; | |
11 | const char * my_targetp; | |
12 | ||
13 | my_targetp = getenv("TMPDIR"); | |
14 | if ( my_targetp == NULL ) | |
15 | my_targetp = "/tmp"; | |
16 | ||
17 | T_ASSERT_LT( strlen( the_targetp ), (unsigned long)( PATH_MAX - 1 ), | |
18 | "check target path too long - \"%s\"", the_targetp ); | |
19 | ||
20 | for ( ;; ) { | |
21 | int my_rand; | |
22 | char my_name[64]; | |
23 | ||
24 | my_rand = rand( ); | |
25 | sprintf( &my_name[0], "xnu_quick_test-%d", my_rand ); | |
26 | T_ASSERT_LT( strlen( &my_name[0] ) + strlen( the_targetp ) + 2, (unsigned long)PATH_MAX, | |
27 | "check target path plus our test directory name is too long: " | |
28 | "target path - \"%s\" test directory name - \"%s\"", | |
29 | the_targetp, &my_name[0] ); | |
30 | ||
31 | /* append generated directory name onto our path */ | |
32 | g_target_path[0] = 0x00; | |
33 | strcat( &g_target_path[0], the_targetp ); | |
34 | if ( g_target_path[ (strlen(the_targetp) - 1) ] != '/' ) { | |
35 | strcat( &g_target_path[0], "/" ); | |
36 | } | |
37 | strcat( &g_target_path[0], &my_name[0] ); | |
38 | ||
39 | /* try to create the test directory */ | |
40 | err = mkdir( &g_target_path[0], (S_IRWXU | S_IRWXG | S_IROTH) ); | |
41 | if ( err == 0 ) { | |
42 | break; | |
43 | } | |
44 | err = errno; | |
45 | if ( EEXIST != err ) { | |
46 | T_ASSERT_FAIL( "test directory creation failed - \"%s\" \n" | |
47 | "mkdir call failed with error %d - \"%s\"", | |
48 | &g_target_path[0], errno, strerror( err) ); | |
49 | } | |
50 | } | |
51 | ||
52 | } /* create_target_directory */ | |
53 | ||
54 | /* | |
55 | * create_random_name - creates a file with a random / unique name in the given directory. | |
56 | * when do_open is true we create a file else we generaate a name that does not exist in the | |
57 | * given directory (we do not create anything when do_open is 0). | |
58 | * WARNING - caller provides enough space in path buffer for longest possible name. | |
59 | * WARNING - assumes caller has appended a trailing '/' on the path passed to us. | |
60 | * RAND_MAX is currently 2147483647 (ten characters plus one for a slash) | |
61 | */ | |
62 | int create_random_name( char *the_pathp, int do_open ) { | |
63 | int i, my_err; | |
64 | int my_fd = -1; | |
65 | ||
66 | for ( i = 0; i < 1; i++ ) { | |
67 | int my_rand; | |
68 | char *myp; | |
69 | char my_name[32]; | |
70 | ||
71 | my_rand = rand( ); | |
72 | sprintf( &my_name[0], "%d", my_rand ); | |
73 | T_ASSERT_LT_ULONG((strlen( &my_name[0] ) + strlen( the_pathp ) + 2), (unsigned long)PATH_MAX, | |
74 | "check if path to test file is less than PATH_MAX"); | |
75 | ||
76 | // append generated file name onto our path | |
77 | myp = strrchr( the_pathp, '/' ); | |
78 | *(myp + 1) = 0x00; | |
79 | strcat( the_pathp, &my_name[0] ); | |
80 | if ( do_open ) { | |
81 | /* create a file with this name */ | |
82 | my_fd = open( the_pathp, (O_RDWR | O_CREAT | O_EXCL), | |
83 | (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) ); | |
84 | T_EXPECT_TRUE((my_fd != -1 || errno == EEXIST), "open file with name %s", the_pathp); | |
85 | ||
86 | if( errno == EEXIST ) | |
87 | continue; | |
88 | } | |
89 | else { | |
90 | /* make sure the name is unique */ | |
91 | struct stat my_sb; | |
92 | my_err = stat( the_pathp, &my_sb ); | |
93 | T_EXPECT_TRUE((my_err == 0 || errno == ENOENT), "make sure the name is unique"); | |
94 | ||
95 | if(errno == ENOENT) break; | |
96 | /* name already exists, try another */ | |
97 | i--; | |
98 | continue; | |
99 | } | |
100 | } | |
101 | ||
102 | if ( my_fd != -1 ) | |
103 | close( my_fd ); | |
104 | ||
105 | if(do_open && my_fd == -1) | |
106 | return 1; | |
107 | ||
108 | return 0; | |
109 | } /* create_random_name */ | |
110 | ||
111 | void remove_target_directory() { | |
112 | rmdir(&g_target_path[0]); | |
113 | } | |
114 |