]>
git.ipfire.org Git - thirdparty/cups.git/blob - cups/dir.c
2 * Directory routines for CUPS.
4 * This set of APIs abstracts enumeration of directory entries.
6 * Copyright 2007-2017 by Apple Inc.
7 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
9 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
13 * Include necessary headers...
16 #include "string-private.h"
17 #include "debug-private.h"
22 * Windows implementation...
29 * Types and structures...
32 struct _cups_dir_s
/**** Directory data structure ****/
34 char directory
[1024]; /* Directory filename */
35 HANDLE dir
; /* Directory handle */
36 cups_dentry_t entry
; /* Directory entry */
41 * '_cups_dir_time()' - Convert a FILETIME value to a UNIX time value.
44 time_t /* O - UNIX time */
45 _cups_dir_time(FILETIME ft
) /* I - File time */
47 ULONGLONG val
; /* File time in 0.1 usecs */
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
56 val
= ft
.dwLowDateTime
+ ((ULONGLONG
)ft
.dwHighDateTime
<< 32);
57 return ((time_t)(val
/ 10000000 - 11644732800));
62 * 'cupsDirClose()' - Close a directory.
64 * @since CUPS 1.2/macOS 10.5@
68 cupsDirClose(cups_dir_t
*dp
) /* I - Directory pointer */
71 * Range check input...
78 * Close an open directory handle...
81 if (dp
->dir
!= INVALID_HANDLE_VALUE
)
93 * 'cupsDirOpen()' - Open a directory.
95 * @since CUPS 1.2/macOS 10.5@
98 cups_dir_t
* /* O - Directory pointer or @code NULL@ if the directory could not be opened. */
99 cupsDirOpen(const char *directory
) /* I - Directory name */
101 cups_dir_t
*dp
; /* Directory */
105 * Range check input...
112 * Allocate memory for the directory structure...
115 dp
= (cups_dir_t
*)calloc(1, sizeof(cups_dir_t
));
120 * Copy the directory name for later use...
123 dp
->dir
= INVALID_HANDLE_VALUE
;
125 strlcpy(dp
->directory
, directory
, sizeof(dp
->directory
));
128 * Return the new directory structure...
136 * 'cupsDirRead()' - Read the next directory entry.
138 * @since CUPS 1.2/macOS 10.5@
141 cups_dentry_t
* /* O - Directory entry or @code NULL@ if there are no more */
142 cupsDirRead(cups_dir_t
*dp
) /* I - Directory pointer */
144 WIN32_FIND_DATA entry
; /* Directory entry data */
148 * Range check input...
155 * See if we have already started finding files...
158 if (dp
->dir
== INVALID_HANDLE_VALUE
)
161 * No, find the first file...
164 dp
->dir
= FindFirstFile(dp
->directory
, &entry
);
165 if (dp
->dir
== INVALID_HANDLE_VALUE
)
168 else if (!FindNextFile(dp
->dir
, &entry
))
172 * Copy the name over and convert the file information...
175 strlcpy(dp
->entry
.filename
, entry
.cFileName
, sizeof(dp
->entry
.filename
));
177 if (entry
.dwFileAttributes
& FILE_ATTRIBUTE_DIRECTORY
)
178 dp
->entry
.fileinfo
.st_mode
= 0755 | S_IFDIR
;
180 dp
->entry
.fileinfo
.st_mode
= 0644;
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
);
185 dp
->entry
.fileinfo
.st_size
= entry
.nFileSizeLow
+ ((unsigned long long)entry
.nFileSizeHigh
<< 32);
188 * Return the entry...
191 return (&(dp
->entry
));
196 * 'cupsDirRewind()' - Rewind to the start of the directory.
198 * @since CUPS 1.2/macOS 10.5@
202 cupsDirRewind(cups_dir_t
*dp
) /* I - Directory pointer */
205 * Range check input...
212 * Close an open directory handle...
215 if (dp
->dir
!= INVALID_HANDLE_VALUE
)
218 dp
->dir
= INVALID_HANDLE_VALUE
;
226 * POSIX implementation...
229 # include <sys/types.h>
234 * Types and structures...
237 struct _cups_dir_s
/**** Directory data structure ****/
239 char directory
[1024]; /* Directory filename */
240 DIR *dir
; /* Directory file */
241 cups_dentry_t entry
; /* Directory entry */
246 * 'cupsDirClose()' - Close a directory.
248 * @since CUPS 1.2/macOS 10.5@
252 cupsDirClose(cups_dir_t
*dp
) /* I - Directory pointer */
254 DEBUG_printf(("cupsDirClose(dp=%p)", (void *)dp
));
257 * Range check input...
264 * Close the directory and free memory...
273 * 'cupsDirOpen()' - Open a directory.
275 * @since CUPS 1.2/macOS 10.5@
278 cups_dir_t
* /* O - Directory pointer or @code NULL@ if the directory could not be opened. */
279 cupsDirOpen(const char *directory
) /* I - Directory name */
281 cups_dir_t
*dp
; /* Directory */
284 DEBUG_printf(("cupsDirOpen(directory=\"%s\")", directory
));
287 * Range check input...
294 * Allocate memory for the directory structure...
297 dp
= (cups_dir_t
*)calloc(1, sizeof(cups_dir_t
));
302 * Open the directory...
305 dp
->dir
= opendir(directory
);
313 * Copy the directory name for later use...
316 strlcpy(dp
->directory
, directory
, sizeof(dp
->directory
));
319 * Return the new directory structure...
327 * 'cupsDirRead()' - Read the next directory entry.
329 * @since CUPS 1.2/macOS 10.5@
332 cups_dentry_t
* /* O - Directory entry or @code NULL@ when there are no more */
333 cupsDirRead(cups_dir_t
*dp
) /* I - Directory pointer */
335 struct dirent
*entry
; /* Pointer to entry */
336 char filename
[1024]; /* Full filename */
339 DEBUG_printf(("2cupsDirRead(dp=%p)", (void *)dp
));
342 * Range check input...
349 * Try reading an entry that is not "." or ".."...
355 * Read the next entry...
358 if ((entry
= readdir(dp
->dir
)) == NULL
)
360 DEBUG_puts("3cupsDirRead: readdir() returned a NULL pointer!");
364 DEBUG_printf(("4cupsDirRead: readdir() returned \"%s\"...", entry
->d_name
));
367 * Skip "." and ".."...
370 if (!strcmp(entry
->d_name
, ".") || !strcmp(entry
->d_name
, ".."))
374 * Copy the name over and get the file information...
377 strlcpy(dp
->entry
.filename
, entry
->d_name
, sizeof(dp
->entry
.filename
));
379 snprintf(filename
, sizeof(filename
), "%s/%s", dp
->directory
, entry
->d_name
);
381 if (stat(filename
, &(dp
->entry
.fileinfo
)))
383 DEBUG_printf(("3cupsDirRead: stat() failed for \"%s\" - %s...", filename
,
389 * Return the entry...
392 return (&(dp
->entry
));
398 * 'cupsDirRewind()' - Rewind to the start of the directory.
400 * @since CUPS 1.2/macOS 10.5@
404 cupsDirRewind(cups_dir_t
*dp
) /* I - Directory pointer */
406 DEBUG_printf(("cupsDirRewind(dp=%p)", (void *)dp
));
409 * Range check input...
416 * Rewind the directory...