本文整理汇总了C++中readdir_r函数的典型用法代码示例。如果您正苦于以下问题:C++ readdir_r函数的具体用法?C++ readdir_r怎么用?C++ readdir_r使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了readdir_r函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: psy_enum_devs
static int psy_enum_devs(void)
{
#ifdef WIN32
int fd, i, ret = 0;
char name[MAX_FNAME_SZ];
for (i = 0; i < MAX_PSY_DEVS; i++) {
snprintf(name, MAX_FNAME_SZ, "/dev/power_supply%d", i);
fd = open(name, O_RDWR);
if (fd < 0)
continue;
ret = psy_add_dev(fd);
if (ret) {
close(fd);
break;
}
}
return ret;
#else
DIR *dir;
struct dirent *entry = NULL;
dir = opendir("/sys/class/power_supply");
if (dir == NULL)
return errno;
for (entry = readdir(dir); entry != NULL;) {
if (entry->d_name[0] != '.') {
if (psy_add_dev(entry->d_name)) {
closedir(dir);
return errno;
}
}
if (readdir_r(dir, entry, &entry)) {
closedir(dir);
return errno;
}
}
closedir(dir);
return 0;
#endif
}
示例2: opendir
int VolumeManager::unmountAllAsecsInDir(const char *directory) {
DIR *d = opendir(directory);
int rc = 0;
if (!d) {
SLOGE("Could not open asec dir %s", directory);
return -1;
}
size_t dirent_len = offsetof(struct dirent, d_name) +
fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
struct dirent *dent = (struct dirent *) malloc(dirent_len);
if (dent == NULL) {
SLOGE("Failed to allocate memory for asec dir");
return -1;
}
struct dirent *result;
while (!readdir_r(d, dent, &result) && result != NULL) {
if (dent->d_name[0] == '.')
continue;
if (dent->d_type != DT_REG)
continue;
size_t name_len = strlen(dent->d_name);
if (name_len > 5 && name_len < (ID_BUF_LEN + ASEC_SUFFIX_LEN - 1) &&
!strcmp(&dent->d_name[name_len - 5], ASEC_SUFFIX)) {
char id[ID_BUF_LEN];
strlcpy(id, dent->d_name, name_len - 4);
if (unmountAsec(id, true)) {
/* Register the error, but try to unmount more asecs */
rc = -1;
}
}
}
closedir(d);
free(dent);
return rc;
}
示例3: listsubdir
bool listsubdir(const char *dir, vector<char *> &subdirs)
{
#if defined(WIN32)
defformatstring(pathname)("%s\\*", dir);
WIN32_FIND_DATA FindFileData;
HANDLE Find = FindFirstFile(path(pathname), &FindFileData);
if(Find != INVALID_HANDLE_VALUE)
{
do {
if((FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && FindFileData.cFileName[0] != '.') subdirs.add(newstring(FindFileData.cFileName));
} while(FindNextFile(Find, &FindFileData));
FindClose(Find);
return true;
}
#else
string pathname;
copystring(pathname, dir);
DIR *d = opendir(path(pathname));
if(d)
{
struct dirent *de, b;
while(!readdir_r(d, &b, &de) && de != NULL)
{
#ifdef _DIRENT_HAVE_D_TYPE
if(de->d_type == DT_DIR && de->d_name[0] != '.') subdirs.add(newstring(de->d_name));
else if(de->d_type == DT_UNKNOWN && de->d_name[0] != '.')
#endif
{
struct stat s;
int dl = (int)strlen(pathname);
concatformatstring(pathname, "/%s", de->d_name);
if(!lstat(pathname, &s) && S_ISDIR(s.st_mode) && de->d_name[0] != '.') subdirs.add(newstring(de->d_name));
pathname[dl] = '\0';
}
}
closedir(d);
return true;
}
#endif
else return false;
}
示例4: scandir
void scandir(const std::string& path,
const std::string& extension,
std::vector<std::string>* files) {
files->clear();
std::string search_path = path.empty() ? "." : path;
DIR* dir = opendir(ResolvePath(search_path).c_str());
if (!dir) return;
struct dirent entry;
struct dirent* result;
while (readdir_r(dir, &entry, &result) == 0) {
if (result == NULL) break;
if (mats::ends_with(entry.d_name, extension)) {
files->emplace_back(entry.d_name);
}
}
closedir(dir);
}
示例5: synthesize_all
static void synthesize_all(void)
{
DIR *proc;
struct dirent dirent, *next;
proc = opendir("/proc");
while (!readdir_r(proc, &dirent, &next) && next) {
char *end;
pid_t pid, tgid;
pid = strtol(dirent.d_name, &end, 10);
if (*end) /* only interested in proper numerical dirents */
continue;
tgid = pid_synthesize_comm_event(pid, 1);
pid_synthesize_mmap_samples(pid, tgid);
}
closedir(proc);
}
示例6: local_readdir_r
static int local_readdir_r(FsContext *ctx, V9fsFidOpenState *fs,
struct dirent *entry,
struct dirent **result)
{
int ret;
again:
ret = readdir_r(fs->dir, entry, result);
if (ctx->export_flags & V9FS_SM_MAPPED) {
entry->d_type = DT_UNKNOWN;
}
else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
if (!ret && *result != NULL &&
!strcmp(entry->d_name, VIRTFS_META_DIR)) {
/* skp the meta data directory */
goto again;
}
entry->d_type = DT_UNKNOWN;
}
return ret;
}
示例7: opendir
void CommandListener::AsecCmd::listAsecsInDirectory(SocketClient *cli, const char *directory) {
DIR *d = opendir(directory);
if (!d) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to open asec dir", true);
return;
}
size_t dirent_len = offsetof(struct dirent, d_name) +
fpathconf(dirfd(d), _PC_NAME_MAX) + 1;
struct dirent *dent = (struct dirent *) malloc(dirent_len);
if (dent == NULL) {
cli->sendMsg(ResponseCode::OperationFailed, "Failed to allocate memory", true);
return;
}
struct dirent *result;
while (!readdir_r(d, dent, &result) && result != NULL) {
if (dent->d_name[0] == '.')
continue;
// For whatever reason, the exFAT fuse driver reports DT_UNKNOWN for
// the .asec files, so we'll have to allow that to make external apps
// work properly.
if (dent->d_type != DT_REG && dent->d_type != DT_UNKNOWN)
continue;
size_t name_len = strlen(dent->d_name);
if (name_len > 5 && name_len < 260 &&
!strcmp(&dent->d_name[name_len - 5], ".asec")) {
char id[255];
memset(id, 0, sizeof(id));
strlcpy(id, dent->d_name, name_len - 4);
cli->sendMsg(ResponseCode::AsecListResult, id, false);
}
}
closedir(d);
free(dent);
}
示例8: removeDirectory
void removeDirectory(const Path &path)
{
DIR *d = opendir(path.constData());
size_t path_len = path.size();
char buf[PATH_MAX];
dirent *dbuf = reinterpret_cast<dirent*>(buf);
if (d) {
dirent *p;
while (!readdir_r(d, dbuf, &p) && p) {
char *buf;
size_t len;
/* Skip the names "." and ".." as we don't want to recurse on them. */
if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) {
continue;
}
len = path_len + strlen(p->d_name) + 2;
buf = static_cast<char*>(malloc(len));
if (buf) {
struct stat statbuf;
snprintf(buf, len, "%s/%s", path.constData(), p->d_name);
if (!stat(buf, &statbuf)) {
if (S_ISDIR(statbuf.st_mode)) {
removeDirectory(buf);
} else {
unlink(buf);
}
}
free(buf);
}
}
closedir(d);
}
rmdir(path.constData());
}
示例9: CopyDir
// Create directory and copy contents (does not overwrite existing files)
void CopyDir(const std::string &source_path, const std::string &dest_path)
{
#ifndef _WIN32
if (source_path == dest_path) return;
if (!File::Exists(source_path)) return;
if (!File::Exists(dest_path)) File::CreateFullPath(dest_path);
struct dirent_large {
struct dirent entry;
char padding[FILENAME_MAX+1];
};
struct dirent_large diren;
struct dirent *result = NULL;
DIR *dirp = opendir(source_path.c_str());
if (!dirp) return;
while (!readdir_r(dirp, (dirent*) &diren, &result) && result)
{
const std::string virtualName(result->d_name);
// check for "." and ".."
if (((virtualName[0] == '.') && (virtualName[1] == '\0')) ||
((virtualName[0] == '.') && (virtualName[1] == '.') &&
(virtualName[2] == '\0')))
continue;
std::string source, dest;
source = source_path + virtualName;
dest = dest_path + virtualName;
if (IsDirectory(source))
{
source += '/';
dest += '/';
if (!File::Exists(dest)) File::CreateFullPath(dest);
CopyDir(source, dest);
}
else if (!File::Exists(dest)) File::Copy(source, dest);
}
closedir(dirp);
#endif
}
示例10: listFiles
static void /* List all files in directory 'dirpath' */
listFiles(const char *dirpath)
{
DIR *dirp;
struct dirent entry, *result;
Boolean isCurrent; /* True if 'dirpath' is "." */
isCurrent = strcmp(dirpath, ".") == 0;
dirp = opendir(dirpath);
if (dirp == NULL) {
errMsg("opendir failed on '%s'", dirpath);
return;
}
/* For each entry in this directory, print directory + filename */
for (;;) {
errno = 0; /* To distinguish error from end-of-directory */
if (readdir_r(dirp, &entry, &result) == -1) {
errMsg("readdir_r");
continue;
}
if (result == NULL)
break;
if (strcmp(entry.d_name, ".") == 0 || strcmp(entry.d_name, "..") == 0)
continue; /* Skip . and .. */
if (!isCurrent)
printf("%s/", dirpath);
printf("%s\n", entry.d_name);
}
if (errno != 0)
errExit("readdir");
if (closedir(dirp) == -1)
errMsg("closedir");
}
示例11: perf_event__synthesize_threads
int perf_event__synthesize_threads(perf_event__handler_t process,
struct perf_session *session)
{
DIR *proc;
struct dirent dirent, *next;
union perf_event *comm_event, *mmap_event;
int err = -1;
comm_event = malloc(sizeof(comm_event->comm) + session->id_hdr_size);
if (comm_event == NULL)
goto out;
mmap_event = malloc(sizeof(mmap_event->mmap) + session->id_hdr_size);
if (mmap_event == NULL)
goto out_free_comm;
proc = opendir("/proc");
if (proc == NULL)
goto out_free_mmap;
while (!readdir_r(proc, &dirent, &next) && next) {
char *end;
pid_t pid = strtol(dirent.d_name, &end, 10);
if (*end) /* only interested in proper numerical dirents */
continue;
__event__synthesize_thread(comm_event, mmap_event, pid,
process, session);
}
closedir(proc);
err = 0;
out_free_mmap:
free(mmap_event);
out_free_comm:
free(comm_event);
out:
return err;
}
示例12: arc_dir
value arc_dir(arc *c, value dirname)
{
char *utf_filename;
DIR *dirp;
int en;
value dirlist;
struct dirent *entry, *result;
int delen;
TYPECHECK(dirname, T_STRING);
utf_filename = alloca(FIX2INT(arc_strutflen(c, dirname)) + 1);
arc_str2cstr(c, dirname, utf_filename);
dirp = opendir(utf_filename);
if (dirp == NULL) {
en = errno;
arc_err_cstrfmt(c, "dir: cannot open directory \"%s\", (%s; errno=%d)", utf_filename, strerror(en), en);
return(CNIL);
}
dirlist = CNIL;
delen = offsetof(struct dirent, d_name)
+ pathconf(utf_filename, _PC_NAME_MAX) + 1;
entry = (struct dirent *)alloca(delen);
for (;;) {
if (readdir_r(dirp, entry, &result) != 0) {
/* error */
en = errno;
arc_err_cstrfmt(c, "dir: error reading directory \"%s\", (%s; errno=%d)", utf_filename, strerror(en), en);
return(CNIL);
}
/* end of list */
if (result == NULL)
break;
/* ignore the . and .. directories */
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
dirlist = cons(c, arc_mkstringc(c, entry->d_name), dirlist);
}
closedir(dirp);
return(dirlist);
}
示例13: opendir
ssize_t native_dir::read_all_files(std::vector<std::string> &files) const
{
DIR *d = opendir(name.c_str());
if (d == NULL) {
fprintf(stderr, "can't open %s: %s\n", name.c_str(),
strerror(errno));
return -1;
}
struct dirent *entryp;
long path_limit = pathconf(name.c_str(), _PC_NAME_MAX);
if (path_limit < 0) {
perror("pathconf");
path_limit = sizeof(entryp->d_name);
}
int len = offsetof(struct dirent, d_name) + path_limit + 1;
entryp = (struct dirent *) malloc(len);
struct dirent *result;
ssize_t num = 0;
while (true) {
int ret = readdir_r(d, entryp, &result);
if (ret != 0) {
fprintf(stderr, "can't read dir %s: %s\n", name.c_str(),
strerror(ret));
num = -1;
break;
}
// End of the directory stream.
if (result == NULL)
break;
assert(result == entryp);
std::string file = entryp->d_name;
if (file != "." && file != "..")
files.push_back(file);
num++;
}
free(entryp);
closedir(d);
return num;
}
示例14: ReadDirR
int32_t ReadDirR(DIR* dir, void* buffer, int32_t bufferSize, DirectoryEntry* outputEntry)
{
assert(buffer != nullptr);
assert(dir != nullptr);
assert(outputEntry != nullptr);
if (bufferSize < sizeof(dirent))
{
assert(!"Buffer size too small; use GetDirentSize to get required buffer size");
return ERANGE;
}
// On successful cal to readdir_r, result and &entry should point to the same
// data; a NULL temp pointer but return of 0 means that we reached the end of the
// directory stream; finally, a NULL temp pointer with a positive return value
// means an error occurred.
dirent* result = nullptr;
dirent* entry = (dirent*)buffer;
int ret = readdir_r(dir, entry, &result);
if (ret == 0)
{
if (result != nullptr)
{
assert(result == entry);
ConvertDirent(*entry, outputEntry);
}
else
{
ret = -1; // errno values are positive so signal the end-of-stream with a non-error value
*outputEntry = { };
}
}
else
{
*outputEntry = { };
}
return ret;
}
示例15: ls
inline Try<std::list<std::string>> ls(const std::string& directory)
{
DIR* dir = opendir(directory.c_str());
if (dir == nullptr) {
// Preserve `opendir` error.
return ErrnoError("Failed to opendir '" + directory + "'");
}
dirent* temp = (dirent*) malloc(os::dirent_size(dir));
if (temp == nullptr) {
// Preserve `malloc` error.
ErrnoError error("Failed to allocate directory entries");
closedir(dir);
return error;
}
std::list<std::string> result;
struct dirent* entry;
int error;
while ((error = readdir_r(dir, temp, &entry)) == 0 && entry != nullptr) {
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
result.push_back(entry->d_name);
}
free(temp);
closedir(dir);
if (error != 0) {
// Preserve `readdir_r` error.
return ErrnoError("Failed to read directories");
}
return result;
}