int rmdirp(char *d, char *d1) { struct stat64 st, cst; int currstat; char *slash; slash = strrchr(d, '/'); currstat = stat64(".", &cst); /* Starts from right most element */ while (d) { /* If it's under current directory */ if (slash == NULL) { /* Stop if it's . or .. */ if (dotdot(d)) { (void) strcpy(d1, d); return (-2); } /* Stop if can not stat it */ } else { /* If there's a slash before it */ /* If extra / in the end */ if (slash != d) { if (++slash == strrchr(d, '\0')) { *(--slash) = '\0'; slash = strrchr(d, '/'); continue; } else { slash--; } } /* Stop if it's . or .. */ if (dotdot(++slash)) { (void) strcpy(d1, d); return (-2); } slash--; /* Stop if can not stat it */ if (stat64(d, &st) < 0) { (void) strcpy(d1, d); return (-1); } if (currstat == 0) { /* Stop if it's current directory */ if ((st.st_ino == cst.st_ino) && (st.st_dev == cst.st_dev)) { (void) strcpy(d1, d); return (-3); } } } /* End of else */ /* Remove it */ if (rmdir(d) != 0) { (void) strcpy(d1, d); return (-1); } /* Have reached left end, break */ if (slash == NULL || slash == d) break; /* Go backward to next directory */ *slash = '\0'; slash = strrchr(d, '/'); } *d1 = '\0'; return (0); }
bool FolderTreeModel::directoryHasChildren(const QString& path) const { QHash<QString, bool>::const_iterator it = m_directoryCache.find(path); if (it != m_directoryCache.end()) { return it.value(); } // Acquire a security token for the path. MDir dir(path); /* * The following code is too expensive, general and SLOW since * QDIR::EntryInfoList returns a full QFileInfolist * * * QDir dir(item->dataPath().toString()); * QFileInfoList all = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); * return (all.count() > 0); * * We can benefit from low-level filesystem APIs, i.e., * Windows API or SystemCalls */ bool has_children = false; #if defined (__WINDOWS__) QString folder = path; folder.replace("/","\\"); //quick subfolder test SHFILEINFOW sfi; SHGetFileInfo((LPCWSTR) folder.constData(), NULL, &sfi, sizeof(sfi), SHGFI_ATTRIBUTES); has_children = (sfi.dwAttributes & SFGAO_HASSUBFOLDER); #else // For OS X and Linux // http://stackoverflow.com/questions/2579948/checking-if-subfolders-exist-linux std::string dot("."), dotdot(".."); QByteArray ba = path.toLocal8Bit(); DIR *directory = opendir(ba); int unknown_count = 0; int total_count = 0; if (directory != NULL) { struct dirent *entry; while (!has_children && ((entry = readdir(directory)) != NULL)) { if (entry->d_name != dot && entry->d_name != dotdot) { total_count++; if (entry->d_type == DT_UNKNOWN) { unknown_count++; } has_children = (entry->d_type == DT_DIR || entry->d_type == DT_LNK); } } closedir(directory); } // If all files are of type DH_UNKNOWN then do a costlier analysis to // determine if the directory has subdirectories. This affects folders on // filesystems that do not fully implement readdir such as JFS. if (directory == NULL || (unknown_count == total_count && total_count > 0)) { QDir dir(path); QFileInfoList all = dir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot); has_children = all.count() > 0; } #endif // Cache and return the result m_directoryCache[path] = has_children; return has_children; }
int main(int argc, char **argv) { int i, startfd = -1, illegal = 0; #ifdef __GLIBC__ putenv("POSIXLY_CORRECT=1"); #endif progname = basename(argv[0]); if (getenv("SYSV3") != NULL) sysv3 = 1; while ((i = getopt(argc, argv, "fiRr")) != EOF) { switch (i) { case 'f': fflag = 1; #ifdef SUS iflag = 0; #endif break; case 'i': #ifdef SUS fflag = 0; #endif iflag = 1; break; case 'R': case 'r': rflag = 1; break; default: illegal = 1; } } if (illegal) usage(); #ifndef SUS if (argv[optind] && argv[optind][0] == '-' && argv[optind][1] == '\0' && (argv[optind-1][0] != '-' || argv[optind-1][1] != '-' || argv[optind-1][2] != '\0')) optind++; #endif if (optind >= argc) usage(); ontty = isatty(0); if (rflag && (startfd = open(".", O_RDONLY)) < 0) { fprintf(stderr, "%s: cannot open current directory\n", progname); exit(1); } while (optind < argc) { if (dotdot(argv[optind]) != NULL) { if (fflag == 0) fprintf(stderr, "%s of %s is not allowed\n", progname, argv[optind]); errcnt |= 1; } else { if (path) free(path); i = strlen(argv[optind]); strcpy(path = smalloc(pathsz = i + 1), argv[optind]); rm(i, argv[optind], startfd, 0, 0); } optind++; } return errcnt; }