Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
tesh version 2
[simgrid.git] / tools / tesh2 / src / directory.c
1 #include <directory.h>
2 #include <fstreams.h>
3 #include <fstream.h>
4
5 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh);
6
7 directory_t
8 directory_new(const char* name, int load)
9 {
10         directory_t directory;
11         struct stat buffer = {0};
12         
13         if(!name)
14         {
15                 errno = EINVAL;
16                 return NULL;
17         }
18         
19         if(stat(name, &buffer))
20                 return NULL;
21                 
22         if(!S_ISDIR(buffer.st_mode))
23         {
24                 errno = ENOTDIR;
25                 return NULL;
26         }
27         
28         directory = xbt_new0(s_directory_t, 1);
29         
30         if(!strcmp(".",name))
31         {
32                 directory->name = getcwd(NULL, 0);
33         }
34         else if(!strcmp("..",name))
35         {
36                 char* buffer = getcwd(NULL, 0);
37                 chdir(name);
38                 directory->name = getcwd(NULL, 0);
39                 chdir(buffer);
40                 free(buffer);
41         }
42         else
43         {
44                 directory->name = strdup(name); 
45         }
46                 
47         
48         directory->stream = NULL;
49         directory->load = load;
50         
51         return directory;
52 }
53
54 int
55 directory_open(directory_t directory)
56 {
57         if(!directory || directory->stream)
58                 return EINVAL;
59                 
60         if(!(directory->stream = opendir(directory->name)))
61                 return errno;
62         
63         return 0;       
64 }
65
66
67 int
68 directory_close(directory_t directory)
69 {
70         if(!directory)
71                 return EINVAL;
72                 
73         if(!directory->stream)
74                 return EBADF;
75                 
76         if(closedir(directory->stream))
77                 return errno;
78                 
79         directory->stream = NULL;
80         return 0;
81 }
82
83 int
84 directory_load(directory_t directory, fstreams_t fstreams, lstrings_t suffixes)
85 {
86         struct dirent* entry ={0};
87         s_fstream_t sfstream = {0};
88         const char* suffix;
89         int has_valid_suffix;
90         int is_empty = 1;
91         
92         if(!directory || !fstreams)
93                 return EINVAL;
94                 
95         if(!directory->stream)
96                 return EBADF;
97                 
98         sfstream.directory = strdup(directory->name);
99                 
100         while((entry = readdir(directory->stream)))
101         {
102                 has_valid_suffix = 0;
103                 
104                 lstrings_rewind(suffixes);
105                 
106                 while((suffix = lstrings_get(suffixes)))
107                 {
108                         if(!strncmp(suffix, entry->d_name + (strlen(entry->d_name) - strlen(suffix)), strlen(suffix)))
109                         {
110                                 has_valid_suffix = 1;
111                                 break;
112                         }
113                                 
114                         lstrings_move_next(suffixes);   
115                 }
116                 
117                 if(!has_valid_suffix)
118                         continue;
119                         
120                 sfstream.name = strdup(entry->d_name);
121                 
122                 /* check first if the file stream is already in the file streams to run */
123                 if(fstreams_contains(fstreams, &sfstream))
124                 {
125                         WARN1("file %s already registred", entry->d_name);
126                         free(sfstream.name);
127                         continue;
128                 }
129                 
130                 /* add the fstream to the list of file streams to run */
131                 if((errno = fstreams_add(fstreams, fstream_new(directory->name, entry->d_name))))
132                 {
133                         INFO0("fstreams_add() failed");
134                         free(sfstream.directory);
135                         free(sfstream.name);
136                         return errno;
137                 }
138                         
139                 is_empty = 0;
140                 free(sfstream.name);
141         }
142         
143         if(is_empty)
144                 WARN1("no tesh file found in the directory %s", directory->name);       
145                 
146         free(sfstream.directory);
147         
148                         
149         return 0;       
150 }
151
152 int
153 directory_free(void** directoryptr)
154 {
155         directory_t directory;
156         
157         if(!(*directoryptr))
158                 return EINVAL;
159                 
160         directory = *((directory_t*)directoryptr);
161         
162         if(directory->stream)
163                 if(directory_close(directory))
164                         return errno;
165         
166         free(directory->name);
167         
168         free(*directoryptr);
169         *directoryptr  = NULL;
170         
171         return 0;       
172 }
173
174 const char*
175 directory_get_name(directory_t directory)
176 {
177         if(!directory)
178         {
179                 errno = EINVAL;
180                 return NULL;
181         }
182         
183         return (const char*)directory->name;
184 }