1 /* 2 * Copyright (c) 2003, 2004 Todd C. Miller <Todd.Miller (at) courtesan.com> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 * 16 * Sponsored in part by the Defense Advanced Research Projects 17 * Agency (DARPA) and Air Force Research Laboratory, Air Force 18 * Materiel Command, USAF, under agreement number F39502-99-1-0512. 19 */ 20 21 #include <errno.h> 22 #include <fts.h> 23 #include <ftw.h> 24 #include <sys/stat.h> 25 #include <sys/types.h> 26 #include <unistd.h> 27 28 static int do_nftw(const char *path, 29 int (*ftw_fn)(const char*, const struct stat*, int), 30 int (*nftw_fn)(const char*, const struct stat*, int, FTW*), 31 int nfds, 32 int nftw_flags) { 33 // TODO: nfds is currently unused. 34 if (nfds < 1) { 35 errno = EINVAL; 36 return -1; 37 } 38 39 // Translate to fts_open options. 40 int fts_options = FTS_LOGICAL | FTS_COMFOLLOW | FTS_NOCHDIR; 41 if (nftw_fn) { 42 fts_options = FTS_COMFOLLOW | ((nftw_flags & FTW_PHYS) ? FTS_PHYSICAL : FTS_LOGICAL); 43 if (!(nftw_flags & FTW_CHDIR)) fts_options |= FTS_NOCHDIR; 44 if (nftw_flags & FTW_MOUNT) fts_options |= FTS_XDEV; 45 } 46 bool postorder = (nftw_flags & FTW_DEPTH) != 0; 47 48 // Call fts_open. 49 char* const paths[2] = { const_cast<char*>(path), nullptr }; 50 FTS* fts = fts_open(paths, fts_options, nullptr); 51 if (fts == nullptr) { 52 return -1; 53 } 54 55 // Translate fts_read results into ftw/nftw callbacks. 56 int error = 0; 57 FTSENT* cur; 58 while (error == 0 && (cur = fts_read(fts)) != nullptr) { 59 int fn_flag; 60 switch (cur->fts_info) { 61 case FTS_D: 62 // In the postorder case, we'll translate FTS_DP to FTW_DP later. 63 // In the can't-access case, we'll translate FTS_DNR to FTW_DNR later. 64 if (postorder || access(cur->fts_path, R_OK) == -1) continue; 65 fn_flag = FTW_D; 66 break; 67 case FTS_DNR: 68 fn_flag = FTW_DNR; 69 break; 70 case FTS_DP: 71 if (!postorder) continue; 72 fn_flag = FTW_DP; 73 break; 74 case FTS_F: 75 case FTS_DEFAULT: 76 fn_flag = FTW_F; 77 break; 78 case FTS_NS: 79 case FTS_NSOK: 80 fn_flag = FTW_NS; 81 break; 82 case FTS_SL: 83 fn_flag = FTW_SL; 84 break; 85 case FTS_SLNONE: 86 fn_flag = (nftw_fn != nullptr) ? FTW_SLN : FTW_NS; 87 break; 88 case FTS_DC: 89 errno = ELOOP; 90 error = -1; 91 continue; 92 default: 93 error = -1; 94 continue; 95 } 96 97 // Call the appropriate function. 98 if (nftw_fn != nullptr) { 99 FTW ftw; 100 ftw.base = cur->fts_pathlen - cur->fts_namelen; 101 ftw.level = cur->fts_level; 102 error = nftw_fn(cur->fts_path, cur->fts_statp, fn_flag, &ftw); 103 } else { 104 error = ftw_fn(cur->fts_path, cur->fts_statp, fn_flag); 105 } 106 } 107 108 int saved_errno = errno; 109 if (fts_close(fts) != 0 && error == 0) { 110 error = -1; 111 } else { 112 errno = saved_errno; 113 } 114 return error; 115 } 116 117 int ftw(const char* path, int (*ftw_fn)(const char*, const struct stat*, int), int nfds) { 118 return do_nftw(path, ftw_fn, nullptr, nfds, 0); 119 } 120 121 int nftw(const char* path, int (*nftw_fn)(const char*, const struct stat*, int, FTW*), 122 int nfds, int nftw_flags) { 123 return do_nftw(path, nullptr, nftw_fn, nfds, nftw_flags); 124 } 125