]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 | 2 | * Directory routines for CUPS. |
ef416fc2 | 3 | * |
503b54c9 | 4 | * This set of APIs abstracts enumeration of directory entries. |
ef416fc2 | 5 | * |
7c4b136b | 6 | * Copyright 2007-2017 by Apple Inc. |
503b54c9 | 7 | * Copyright 1997-2005 by Easy Software Products, all rights reserved. |
ef416fc2 | 8 | * |
e3101897 | 9 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 10 | */ |
11 | ||
12 | /* | |
13 | * Include necessary headers... | |
14 | */ | |
15 | ||
71e16022 MS |
16 | #include "string-private.h" |
17 | #include "debug-private.h" | |
ef416fc2 | 18 | #include "dir.h" |
ef416fc2 | 19 | |
20 | ||
21 | /* | |
22 | * Windows implementation... | |
23 | */ | |
24 | ||
25 | #ifdef WIN32 | |
26 | # include <windows.h> | |
27 | ||
28 | /* | |
29 | * Types and structures... | |
30 | */ | |
31 | ||
32 | struct _cups_dir_s /**** Directory data structure ****/ | |
33 | { | |
34 | char directory[1024]; /* Directory filename */ | |
35 | HANDLE dir; /* Directory handle */ | |
36 | cups_dentry_t entry; /* Directory entry */ | |
37 | }; | |
38 | ||
39 | ||
40 | /* | |
41 | * '_cups_dir_time()' - Convert a FILETIME value to a UNIX time value. | |
42 | */ | |
43 | ||
44 | time_t /* O - UNIX time */ | |
45 | _cups_dir_time(FILETIME ft) /* I - File time */ | |
46 | { | |
47 | ULONGLONG val; /* File time in 0.1 usecs */ | |
48 | ||
49 | ||
50 | /* | |
51 | * Convert file time (1/10 microseconds since Jan 1, 1601) to UNIX | |
52 | * time (seconds since Jan 1, 1970). There are 11,644,732,800 seconds | |
53 | * between them... | |
54 | */ | |
55 | ||
536bc2c6 | 56 | val = ft.dwLowDateTime + ((ULONGLONG)ft.dwHighDateTime << 32); |
ef416fc2 | 57 | return ((time_t)(val / 10000000 - 11644732800)); |
58 | } | |
59 | ||
60 | ||
61 | /* | |
62 | * 'cupsDirClose()' - Close a directory. | |
5a738aea | 63 | * |
8072030b | 64 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 65 | */ |
66 | ||
67 | void | |
5a738aea | 68 | cupsDirClose(cups_dir_t *dp) /* I - Directory pointer */ |
ef416fc2 | 69 | { |
70 | /* | |
71 | * Range check input... | |
72 | */ | |
73 | ||
74 | if (!dp) | |
75 | return; | |
76 | ||
77 | /* | |
78 | * Close an open directory handle... | |
79 | */ | |
80 | ||
81 | if (dp->dir != INVALID_HANDLE_VALUE) | |
82 | FindClose(dp->dir); | |
83 | ||
84 | /* | |
85 | * Free memory used... | |
86 | */ | |
87 | ||
88 | free(dp); | |
89 | } | |
90 | ||
91 | ||
92 | /* | |
93 | * 'cupsDirOpen()' - Open a directory. | |
5a738aea | 94 | * |
8072030b | 95 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 96 | */ |
97 | ||
5a738aea | 98 | cups_dir_t * /* O - Directory pointer or @code NULL@ if the directory could not be opened. */ |
ef416fc2 | 99 | cupsDirOpen(const char *directory) /* I - Directory name */ |
100 | { | |
101 | cups_dir_t *dp; /* Directory */ | |
102 | ||
103 | ||
104 | /* | |
105 | * Range check input... | |
106 | */ | |
107 | ||
108 | if (!directory) | |
109 | return (NULL); | |
110 | ||
111 | /* | |
112 | * Allocate memory for the directory structure... | |
113 | */ | |
114 | ||
115 | dp = (cups_dir_t *)calloc(1, sizeof(cups_dir_t)); | |
116 | if (!dp) | |
117 | return (NULL); | |
118 | ||
119 | /* | |
120 | * Copy the directory name for later use... | |
121 | */ | |
122 | ||
123 | dp->dir = INVALID_HANDLE_VALUE; | |
124 | ||
125 | strlcpy(dp->directory, directory, sizeof(dp->directory)); | |
126 | ||
127 | /* | |
128 | * Return the new directory structure... | |
129 | */ | |
130 | ||
131 | return (dp); | |
132 | } | |
133 | ||
134 | ||
135 | /* | |
136 | * 'cupsDirRead()' - Read the next directory entry. | |
5a738aea | 137 | * |
8072030b | 138 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 139 | */ |
140 | ||
5a738aea MS |
141 | cups_dentry_t * /* O - Directory entry or @code NULL@ if there are no more */ |
142 | cupsDirRead(cups_dir_t *dp) /* I - Directory pointer */ | |
ef416fc2 | 143 | { |
144 | WIN32_FIND_DATA entry; /* Directory entry data */ | |
145 | ||
146 | ||
147 | /* | |
148 | * Range check input... | |
149 | */ | |
150 | ||
151 | if (!dp) | |
152 | return (NULL); | |
153 | ||
154 | /* | |
155 | * See if we have already started finding files... | |
156 | */ | |
157 | ||
158 | if (dp->dir == INVALID_HANDLE_VALUE) | |
159 | { | |
160 | /* | |
161 | * No, find the first file... | |
162 | */ | |
163 | ||
164 | dp->dir = FindFirstFile(dp->directory, &entry); | |
165 | if (dp->dir == INVALID_HANDLE_VALUE) | |
166 | return (NULL); | |
167 | } | |
168 | else if (!FindNextFile(dp->dir, &entry)) | |
169 | return (NULL); | |
170 | ||
171 | /* | |
172 | * Copy the name over and convert the file information... | |
173 | */ | |
174 | ||
175 | strlcpy(dp->entry.filename, entry.cFileName, sizeof(dp->entry.filename)); | |
176 | ||
177 | if (entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) | |
178 | dp->entry.fileinfo.st_mode = 0755 | S_IFDIR; | |
179 | else | |
180 | dp->entry.fileinfo.st_mode = 0644; | |
181 | ||
182 | dp->entry.fileinfo.st_atime = _cups_dir_time(entry.ftLastAccessTime); | |
183 | dp->entry.fileinfo.st_ctime = _cups_dir_time(entry.ftCreationTime); | |
184 | dp->entry.fileinfo.st_mtime = _cups_dir_time(entry.ftLastWriteTime); | |
7cf5915e | 185 | dp->entry.fileinfo.st_size = entry.nFileSizeLow + ((unsigned long long)entry.nFileSizeHigh << 32); |
ef416fc2 | 186 | |
187 | /* | |
188 | * Return the entry... | |
189 | */ | |
190 | ||
191 | return (&(dp->entry)); | |
192 | } | |
193 | ||
194 | ||
195 | /* | |
196 | * 'cupsDirRewind()' - Rewind to the start of the directory. | |
5a738aea | 197 | * |
8072030b | 198 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 199 | */ |
200 | ||
201 | void | |
5a738aea | 202 | cupsDirRewind(cups_dir_t *dp) /* I - Directory pointer */ |
ef416fc2 | 203 | { |
204 | /* | |
205 | * Range check input... | |
206 | */ | |
207 | ||
208 | if (!dp) | |
209 | return; | |
210 | ||
211 | /* | |
212 | * Close an open directory handle... | |
213 | */ | |
214 | ||
215 | if (dp->dir != INVALID_HANDLE_VALUE) | |
216 | { | |
217 | FindClose(dp->dir); | |
218 | dp->dir = INVALID_HANDLE_VALUE; | |
219 | } | |
220 | } | |
221 | ||
222 | ||
223 | #else | |
224 | ||
225 | /* | |
226 | * POSIX implementation... | |
227 | */ | |
228 | ||
229 | # include <sys/types.h> | |
230 | # include <dirent.h> | |
231 | ||
232 | ||
233 | /* | |
234 | * Types and structures... | |
235 | */ | |
236 | ||
237 | struct _cups_dir_s /**** Directory data structure ****/ | |
238 | { | |
239 | char directory[1024]; /* Directory filename */ | |
240 | DIR *dir; /* Directory file */ | |
241 | cups_dentry_t entry; /* Directory entry */ | |
242 | }; | |
243 | ||
244 | ||
245 | /* | |
246 | * 'cupsDirClose()' - Close a directory. | |
5a738aea | 247 | * |
8072030b | 248 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 249 | */ |
250 | ||
251 | void | |
5a738aea | 252 | cupsDirClose(cups_dir_t *dp) /* I - Directory pointer */ |
ef416fc2 | 253 | { |
807315e6 | 254 | DEBUG_printf(("cupsDirClose(dp=%p)", (void *)dp)); |
ef416fc2 | 255 | |
256 | /* | |
257 | * Range check input... | |
258 | */ | |
259 | ||
260 | if (!dp) | |
261 | return; | |
262 | ||
263 | /* | |
264 | * Close the directory and free memory... | |
265 | */ | |
266 | ||
267 | closedir(dp->dir); | |
268 | free(dp); | |
269 | } | |
270 | ||
271 | ||
272 | /* | |
273 | * 'cupsDirOpen()' - Open a directory. | |
5a738aea | 274 | * |
8072030b | 275 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 276 | */ |
277 | ||
5a738aea | 278 | cups_dir_t * /* O - Directory pointer or @code NULL@ if the directory could not be opened. */ |
ef416fc2 | 279 | cupsDirOpen(const char *directory) /* I - Directory name */ |
280 | { | |
281 | cups_dir_t *dp; /* Directory */ | |
282 | ||
283 | ||
e07d4801 | 284 | DEBUG_printf(("cupsDirOpen(directory=\"%s\")", directory)); |
ef416fc2 | 285 | |
286 | /* | |
287 | * Range check input... | |
288 | */ | |
289 | ||
290 | if (!directory) | |
291 | return (NULL); | |
292 | ||
293 | /* | |
294 | * Allocate memory for the directory structure... | |
295 | */ | |
296 | ||
297 | dp = (cups_dir_t *)calloc(1, sizeof(cups_dir_t)); | |
298 | if (!dp) | |
299 | return (NULL); | |
300 | ||
301 | /* | |
302 | * Open the directory... | |
303 | */ | |
304 | ||
305 | dp->dir = opendir(directory); | |
306 | if (!dp->dir) | |
307 | { | |
308 | free(dp); | |
309 | return (NULL); | |
310 | } | |
311 | ||
312 | /* | |
313 | * Copy the directory name for later use... | |
314 | */ | |
315 | ||
316 | strlcpy(dp->directory, directory, sizeof(dp->directory)); | |
317 | ||
318 | /* | |
319 | * Return the new directory structure... | |
320 | */ | |
321 | ||
322 | return (dp); | |
323 | } | |
324 | ||
325 | ||
326 | /* | |
327 | * 'cupsDirRead()' - Read the next directory entry. | |
5a738aea | 328 | * |
8072030b | 329 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 330 | */ |
331 | ||
5a738aea MS |
332 | cups_dentry_t * /* O - Directory entry or @code NULL@ when there are no more */ |
333 | cupsDirRead(cups_dir_t *dp) /* I - Directory pointer */ | |
ef416fc2 | 334 | { |
ef416fc2 | 335 | struct dirent *entry; /* Pointer to entry */ |
336 | char filename[1024]; /* Full filename */ | |
337 | ||
338 | ||
807315e6 | 339 | DEBUG_printf(("2cupsDirRead(dp=%p)", (void *)dp)); |
ef416fc2 | 340 | |
341 | /* | |
342 | * Range check input... | |
343 | */ | |
344 | ||
345 | if (!dp) | |
346 | return (NULL); | |
347 | ||
348 | /* | |
349 | * Try reading an entry that is not "." or ".."... | |
350 | */ | |
351 | ||
07725fee | 352 | for (;;) |
ef416fc2 | 353 | { |
2abf387c | 354 | /* |
7c4b136b | 355 | * Read the next entry... |
2abf387c | 356 | */ |
357 | ||
358 | if ((entry = readdir(dp->dir)) == NULL) | |
359 | { | |
e07d4801 | 360 | DEBUG_puts("3cupsDirRead: readdir() returned a NULL pointer!"); |
2abf387c | 361 | return (NULL); |
362 | } | |
363 | ||
e07d4801 | 364 | DEBUG_printf(("4cupsDirRead: readdir() returned \"%s\"...", entry->d_name)); |
2abf387c | 365 | |
2abf387c | 366 | /* |
367 | * Skip "." and ".."... | |
368 | */ | |
369 | ||
07725fee | 370 | if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) |
371 | continue; | |
ef416fc2 | 372 | |
07725fee | 373 | /* |
374 | * Copy the name over and get the file information... | |
375 | */ | |
ef416fc2 | 376 | |
07725fee | 377 | strlcpy(dp->entry.filename, entry->d_name, sizeof(dp->entry.filename)); |
ef416fc2 | 378 | |
07725fee | 379 | snprintf(filename, sizeof(filename), "%s/%s", dp->directory, entry->d_name); |
ef416fc2 | 380 | |
07725fee | 381 | if (stat(filename, &(dp->entry.fileinfo))) |
382 | { | |
e07d4801 | 383 | DEBUG_printf(("3cupsDirRead: stat() failed for \"%s\" - %s...", filename, |
07725fee | 384 | strerror(errno))); |
385 | continue; | |
386 | } | |
387 | ||
388 | /* | |
389 | * Return the entry... | |
390 | */ | |
391 | ||
392 | return (&(dp->entry)); | |
393 | } | |
ef416fc2 | 394 | } |
395 | ||
396 | ||
397 | /* | |
398 | * 'cupsDirRewind()' - Rewind to the start of the directory. | |
5a738aea | 399 | * |
8072030b | 400 | * @since CUPS 1.2/macOS 10.5@ |
ef416fc2 | 401 | */ |
402 | ||
403 | void | |
5a738aea | 404 | cupsDirRewind(cups_dir_t *dp) /* I - Directory pointer */ |
ef416fc2 | 405 | { |
807315e6 | 406 | DEBUG_printf(("cupsDirRewind(dp=%p)", (void *)dp)); |
ef416fc2 | 407 | |
408 | /* | |
409 | * Range check input... | |
410 | */ | |
411 | ||
412 | if (!dp) | |
413 | return; | |
414 | ||
415 | /* | |
416 | * Rewind the directory... | |
417 | */ | |
418 | ||
419 | rewinddir(dp->dir); | |
420 | } | |
ef416fc2 | 421 | #endif /* WIN32 */ |