| 29 | */ |
| 30 | |
| 31 | void |
| 32 | cupsdCleanFiles(const char *path, /* I - Directory to clean */ |
| 33 | const char *pattern) /* I - Filename pattern or NULL */ |
| 34 | { |
| 35 | cups_dir_t *dir; /* Directory */ |
| 36 | cups_dentry_t *dent; /* Directory entry */ |
| 37 | char filename[1024]; /* Filename */ |
| 38 | struct stat fileinfo; /* File link information */ |
| 39 | int status; /* Status from unlink/rmdir */ |
| 40 | |
| 41 | |
| 42 | cupsdLogMessage(CUPSD_LOG_DEBUG, |
| 43 | "cupsdCleanFiles(path=\"%s\", pattern=\"%s\")", path, |
| 44 | pattern ? pattern : "(null)"); |
| 45 | |
| 46 | if ((dir = cupsDirOpen(path)) == NULL) |
| 47 | { |
| 48 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open directory \"%s\" - %s", |
| 49 | path, strerror(errno)); |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | cupsdLogMessage(CUPSD_LOG_INFO, "Cleaning out old files in \"%s\".", path); |
| 54 | |
| 55 | while ((dent = cupsDirRead(dir)) != NULL) |
| 56 | { |
| 57 | if (pattern && fnmatch(pattern, dent->filename, 0)) |
| 58 | continue; |
| 59 | |
| 60 | snprintf(filename, sizeof(filename), "%s/%s", path, dent->filename); |
| 61 | if (lstat(filename, &fileinfo)) |
| 62 | continue; |
| 63 | |
| 64 | if (S_ISDIR(fileinfo.st_mode)) |
| 65 | { |
| 66 | cupsdCleanFiles(filename, pattern); |
| 67 | |
| 68 | status = rmdir(filename); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | status = cupsdUnlinkOrRemoveFile(filename); |
| 73 | } |
| 74 | |
| 75 | if (status) |
| 76 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to remove \"%s\" - %s", filename, |
| 77 | strerror(errno)); |
| 78 | } |
| 79 | |
| 80 | cupsDirClose(dir); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* |
no test coverage detected