本文整理汇总了C++中dirfd函数的典型用法代码示例。如果您正苦于以下问题:C++ dirfd函数的具体用法?C++ dirfd怎么用?C++ dirfd使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dirfd函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C++代码示例。
示例1: remove_directory
int
remove_directory(char * name)
{
DIR * cur_dir = opendir(".");
DIR * dir;
int cur_dir_fd;
int dir_fd;
struct dirent * entry;
struct file_list {
struct file_list * next;
char * file;
};
struct file_list * flst = 0;
if (!cur_dir) {
fprintf(stderr, "Unable to open current directory\n");
return -1;
}
dir = opendir(name);
if (!dir) {
fprintf(stderr, "Unable to open directory to be removed\n");
goto err1;
}
cur_dir_fd = dirfd(cur_dir);
dir_fd = dirfd(dir);
if (cur_dir_fd == -1 || dir_fd == -1) {
fprintf(stderr, "dirfd failed\n");
goto err2;
}
while ((entry = readdir(dir))) {
char * fname = &(entry->d_name[0]);
if (!strcmp(fname, ".")) {
continue;
} else if (!strcmp(fname, "..")) {
continue;
} else {
struct file_list * npos = malloc(sizeof(*npos));
if (!npos) {
fprintf(stderr, "Malloc failed (npos)\n");
break;
}
npos->file = copy_string(fname);
if (!(npos->file)) {
free(npos);
break;
}
npos->next = flst;
flst = npos;
}
}
if (fchdir(dir_fd)) {
perror("Failed to change directory to directory to be removed");
while (flst) {
struct file_list * npos = flst->next;
free(flst->file);
free(flst);
flst = npos;
}
goto err2;
}
while (flst) {
struct file_list * npos = flst->next;
if (unlink(flst->file)) {
perror("Unlink failed");
}
free(flst->file);
free(flst);
flst = npos;
}
if (fchdir(cur_dir_fd)) {
closedir(dir);
closedir(cur_dir);
return -1;
}
err2:
closedir(dir);
err1:
closedir(cur_dir);
{
int res = rmdir(name);
if (res) {
perror("rmdir failed");
}
return res;
}
}
开发者ID:pbroadbery,项目名称:fricas-svn,代码行数:85,代码来源:cfuns-c.c
示例2: DirInit
int DirInit (access_t *p_access, DIR *handle)
{
access_sys_t *p_sys = malloc (sizeof (*p_sys));
if (unlikely(p_sys == NULL))
goto error;
char *uri;
if (!strcmp (p_access->psz_access, "fd"))
{
if (asprintf (&uri, "fd://%s", p_access->psz_location) == -1)
uri = NULL;
}
else
uri = vlc_path2uri (p_access->psz_filepath, "file");
if (unlikely(uri == NULL))
goto error;
/* "Open" the base directory */
directory_t *root = malloc (sizeof (*root));
if (unlikely(root == NULL))
{
free (uri);
goto error;
}
char *psz_sort = var_InheritString (p_access, "directory-sort");
if (!psz_sort)
p_sys->compar = collate;
else if (!strcasecmp (psz_sort, "version"))
p_sys->compar = version;
else if (!strcasecmp (psz_sort, "none"))
p_sys->compar = NULL;
else
p_sys->compar = collate;
free(psz_sort);
root->parent = NULL;
root->handle = handle;
root->uri = uri;
root->filec = vlc_loaddir (handle, &root->filev, visible, p_sys->compar);
if (root->filec < 0)
root->filev = NULL;
root->i = 0;
#ifdef HAVE_OPENAT
struct stat st;
if (fstat (dirfd (handle), &st))
{
free (root);
free (uri);
goto error;
}
root->device = st.st_dev;
root->inode = st.st_ino;
#else
root->path = strdup (p_access->psz_filepath);
#endif
p_access->p_sys = p_sys;
p_sys->current = root;
p_sys->ignored_exts = var_InheritString (p_access, "ignore-filetypes");
p_sys->header = true;
p_sys->i_item_count = 0;
p_sys->xspf_ext = strdup ("");
/* Handle mode */
char *psz = var_InheritString (p_access, "recursive");
if (psz == NULL || !strcasecmp (psz, "none"))
p_sys->mode = MODE_NONE;
else if( !strcasecmp( psz, "collapse" ) )
p_sys->mode = MODE_COLLAPSE;
else
p_sys->mode = MODE_EXPAND;
free( psz );
access_InitFields(p_access);
p_access->pf_read = NULL;
p_access->pf_block = DirBlock;
p_access->pf_seek = NULL;
p_access->pf_control= DirControl;
free (p_access->psz_demux);
p_access->psz_demux = strdup ("xspf-open");
return VLC_SUCCESS;
error:
closedir (handle);
free (p_sys);
return VLC_EGENERIC;
}
开发者ID:DZLiao,项目名称:vlc-2.1.4.32.subproject-2013-update2,代码行数:89,代码来源:directory.c
示例3: main
int
main(int argc, char *argv[])
{
int ch;
struct dirent *dp;
int width;
ssize_t cc;
struct whod *w;
struct whoent *we;
struct myutmp *mp;
cap_rights_t rights;
int f, n, i;
int d_first;
int dfd;
time_t ct;
w = &wd;
(void) setlocale(LC_TIME, "");
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
while ((ch = getopt(argc, argv, "a")) != -1) {
switch ((char)ch) {
case 'a':
aflg = 1;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc != 0)
usage();
if (chdir(_PATH_RWHODIR) < 0)
err(1, "chdir(%s)", _PATH_RWHODIR);
if ((dirp = opendir(".")) == NULL)
err(1, "opendir(%s)", _PATH_RWHODIR);
dfd = dirfd(dirp);
mp = myutmp;
cap_rights_init(&rights, CAP_READ, CAP_LOOKUP);
if (cap_rights_limit(dfd, &rights) < 0 && errno != ENOSYS)
err(1, "cap_rights_limit failed: %s", _PATH_RWHODIR);
/*
* Cache files required for time(3) and localtime(3) before entering
* capability mode.
*/
(void) time(&ct);
(void) localtime(&ct);
if (cap_enter() < 0 && errno != ENOSYS)
err(1, "cap_enter");
(void) time(&now);
cap_rights_init(&rights, CAP_READ);
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_ino == 0 || strncmp(dp->d_name, "whod.", 5) != 0)
continue;
f = openat(dfd, dp->d_name, O_RDONLY);
if (f < 0)
continue;
if (cap_rights_limit(f, &rights) < 0 && errno != ENOSYS)
err(1, "cap_rights_limit failed: %s", dp->d_name);
cc = read(f, (char *)&wd, sizeof(struct whod));
if (cc < WHDRSIZE) {
(void) close(f);
continue;
}
if (down(w, now) != 0) {
(void) close(f);
continue;
}
cc -= WHDRSIZE;
we = w->wd_we;
for (n = cc / sizeof(struct whoent); n > 0; n--) {
if (aflg == 0 && we->we_idle >= 60 * 60) {
we++;
continue;
}
if (nusers >= NUSERS)
errx(1, "too many users");
mp->myutmp = we->we_utmp;
mp->myidle = we->we_idle;
(void) strcpy(mp->myhost, w->wd_hostname);
nusers++;
we++;
mp++;
}
(void) close(f);
}
qsort((char *)myutmp, nusers, sizeof(struct myutmp), utmpcmp);
mp = myutmp;
width = 0;
for (i = 0; i < nusers; i++) {
/* append one for the blank and use 8 for the out_line */
int j;
j = strlen(mp->myhost) + 1 + sizeof(mp->myutmp.out_line);
if (j > width)
width = j;
//.........这里部分代码省略.........
开发者ID:ChaosJohn,项目名称:freebsd,代码行数:101,代码来源:rwho.c
示例4: get_size
int get_size(const char *pkgname, userid_t userid, const char *apkpath,
const char *libdirpath, const char *fwdlock_apkpath, const char *asecpath,
int64_t *_codesize, int64_t *_datasize, int64_t *_cachesize,
int64_t* _asecsize)
{
DIR *d;
int dfd;
struct dirent *de;
struct stat s;
char path[PKG_PATH_MAX];
int64_t codesize = 0;
int64_t datasize = 0;
int64_t cachesize = 0;
int64_t asecsize = 0;
/* count the source apk as code -- but only if it's not
* on the /system partition and its not on the sdcard.
*/
if (validate_system_app_path(apkpath) &&
strncmp(apkpath, android_asec_dir.path, android_asec_dir.len) != 0) {
if (stat(apkpath, &s) == 0) {
codesize += stat_size(&s);
}
}
/* count the forward locked apk as code if it is given
*/
if (fwdlock_apkpath != NULL && fwdlock_apkpath[0] != '!') {
if (stat(fwdlock_apkpath, &s) == 0) {
codesize += stat_size(&s);
}
}
/* count the cached dexfile as code */
if (!create_cache_path(path, apkpath)) {
if (stat(path, &s) == 0) {
codesize += stat_size(&s);
}
}
/* add in size of any libraries */
if (libdirpath != NULL && libdirpath[0] != '!') {
d = opendir(libdirpath);
if (d != NULL) {
dfd = dirfd(d);
codesize += calculate_dir_size(dfd);
closedir(d);
}
}
/* compute asec size if it is given
*/
if (asecpath != NULL && asecpath[0] != '!') {
if (stat(asecpath, &s) == 0) {
asecsize += stat_size(&s);
}
}
if (create_pkg_path(path, pkgname, PKG_DIR_POSTFIX, userid)) {
goto done;
}
d = opendir(path);
if (d == NULL) {
goto done;
}
dfd = dirfd(d);
/* most stuff in the pkgdir is data, except for the "cache"
* directory and below, which is cache, and the "lib" directory
* and below, which is code...
*/
while ((de = readdir(d))) {
const char *name = de->d_name;
if (de->d_type == DT_DIR) {
int subfd;
int64_t statsize = 0;
int64_t dirsize = 0;
/* always skip "." and ".." */
if (name[0] == '.') {
if (name[1] == 0) continue;
if ((name[1] == '.') && (name[2] == 0)) continue;
}
if (fstatat(dfd, name, &s, AT_SYMLINK_NOFOLLOW) == 0) {
statsize = stat_size(&s);
}
subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
if (subfd >= 0) {
dirsize = calculate_dir_size(subfd);
}
if(!strcmp(name,"lib")) {
codesize += dirsize + statsize;
} else if(!strcmp(name,"cache")) {
cachesize += dirsize + statsize;
} else {
datasize += dirsize + statsize;
}
} else if (de->d_type == DT_LNK && !strcmp(name,"lib")) {
// This is the symbolic link to the application's library
// code. We'll count this as code instead of data, since
//.........这里部分代码省略.........
开发者ID:AOSPLUS,项目名称:frameworks_native,代码行数:101,代码来源:commands.c
示例5: is_card_busy
static bool is_card_busy(const char *id) {
char *card_path = NULL, *pcm_path = NULL, *sub_status = NULL;
DIR *card_dir = NULL, *pcm_dir = NULL;
FILE *status_file = NULL;
size_t len;
struct dirent *space = NULL, *de;
bool busy = false;
int r;
pa_assert(id);
/* This simply uses /proc/asound/card.../pcm.../sub.../status to
* check whether there is still a process using this audio device. */
card_path = pa_sprintf_malloc("/proc/asound/card%s", id);
if (!(card_dir = opendir(card_path))) {
pa_log_warn("Failed to open %s: %s", card_path, pa_cstrerror(errno));
goto fail;
}
len = offsetof(struct dirent, d_name) + fpathconf(dirfd(card_dir), _PC_NAME_MAX) + 1;
space = pa_xmalloc(len);
for (;;) {
de = NULL;
if ((r = readdir_r(card_dir, space, &de)) != 0) {
pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r));
goto fail;
}
if (!de)
break;
if (!pa_startswith(de->d_name, "pcm"))
continue;
if (pcm_is_modem(id, de->d_name + 3))
continue;
pa_xfree(pcm_path);
pcm_path = pa_sprintf_malloc("%s/%s", card_path, de->d_name);
if (pcm_dir)
closedir(pcm_dir);
if (!(pcm_dir = opendir(pcm_path))) {
pa_log_warn("Failed to open %s: %s", pcm_path, pa_cstrerror(errno));
continue;
}
for (;;) {
char line[32];
if ((r = readdir_r(pcm_dir, space, &de)) != 0) {
pa_log_warn("readdir_r() failed: %s", pa_cstrerror(r));
goto fail;
}
if (!de)
break;
if (!pa_startswith(de->d_name, "sub"))
continue;
pa_xfree(sub_status);
sub_status = pa_sprintf_malloc("%s/%s/status", pcm_path, de->d_name);
if (status_file)
fclose(status_file);
if (!(status_file = pa_fopen_cloexec(sub_status, "r"))) {
pa_log_warn("Failed to open %s: %s", sub_status, pa_cstrerror(errno));
continue;
}
if (!(fgets(line, sizeof(line)-1, status_file))) {
pa_log_warn("Failed to read from %s: %s", sub_status, pa_cstrerror(errno));
continue;
}
if (!pa_streq(line, "closed\n")) {
busy = true;
break;
}
}
}
fail:
pa_xfree(card_path);
pa_xfree(pcm_path);
pa_xfree(sub_status);
pa_xfree(space);
if (card_dir)
closedir(card_dir);
if (pcm_dir)
//.........这里部分代码省略.........
开发者ID:DryakhlyyZlodey,项目名称:pulseaudio,代码行数:101,代码来源:module-udev-detect.c
示例6: fts_build
//.........这里部分代码省略.........
/* Be quiet about nostat, GCC. */
nostat = 0;
} else if (ISSET(FTS_NOSTAT) && ISSET(FTS_PHYSICAL)) {
nlinks = cur->fts_nlink - (ISSET(FTS_SEEDOT) ? 0 : 2);
nostat = 1;
} else {
nlinks = -1;
nostat = 0;
}
#ifdef notdef
(void)printf("nlinks == %d (cur: %d)\n", nlinks, cur->fts_nlink);
(void)printf("NOSTAT %d PHYSICAL %d SEEDOT %d\n",
ISSET(FTS_NOSTAT), ISSET(FTS_PHYSICAL), ISSET(FTS_SEEDOT));
#endif
/*
* If we're going to need to stat anything or we want to descend
* and stay in the directory, chdir. If this fails we keep going,
* but set a flag so we don't chdir after the post-order visit.
* We won't be able to stat anything, but we can still return the
* names themselves. Note, that since yfts_read won't be able to
* chdir into the directory, it will have to return different path
* names than before, i.e. "a/b" instead of "b". Since the node
* has already been visited in pre-order, have to wait until the
* post-order visit to return the error. There is a special case
* here, if there was nothing to stat then it's not an error to
* not be able to stat. This is all fairly nasty. If a program
* needed sorted entries or stat information, they had better be
* checking FTS_NS on the returned nodes.
*/
cderrno = 0;
if (nlinks || type == BREAD) {
#ifndef _win_
if (fts_safe_changedir(sp, cur, dirfd(dirp), NULL)) {
#else
if (fts_safe_changedir(sp, cur, -1, dirpd)) {
#endif
if (nlinks && type == BREAD)
cur->fts_errno = errno;
cur->fts_flags |= FTS_DONTCHDIR;
descend = 0;
cderrno = errno;
(void)closedir(dirp);
dirp = NULL;
#ifdef _win_
close_dird(dirpd);
dirpd = invalidDirD;
#else
UNUSED(invalidDirD);
#endif
} else
descend = 1;
} else
descend = 0;
/*
* Figure out the max file name length that can be stored in the
* current path -- the inner loop allocates more path as necessary.
* We really wouldn't have to do the maxlen calculations here, we
* could do them in yfts_read before returning the path, but it's a
* lot easier here since the length is part of the dirent structure.
*
* If not changing directories set a pointer so that can just append
* each new name into the path.
*/
开发者ID:noscripter,项目名称:tomita-parser,代码行数:67,代码来源:fts.cpp
示例7: _add_cache_files
static int _add_cache_files(cache_t *cache, cache_dir_t *parentDir, const char *dirName,
DIR* dir, char *pathBase, char *pathPos, size_t pathAvailLen)
{
struct dirent *de;
cache_dir_t* cacheDir = NULL;
int dfd;
CACHE_NOISY(ALOGI("_add_cache_files: parent=%p dirName=%s dir=%p pathBase=%s",
parentDir, dirName, dir, pathBase));
dfd = dirfd(dir);
if (dfd < 0) return 0;
// Sub-directories always get added to the data structure, so if they
// are empty we will know about them to delete them later.
cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
while ((de = readdir(dir))) {
const char *name = de->d_name;
if (de->d_type == DT_DIR) {
int subfd;
DIR *subdir;
/* always skip "." and ".." */
if (name[0] == '.') {
if (name[1] == 0) continue;
if ((name[1] == '.') && (name[2] == 0)) continue;
}
subfd = openat(dfd, name, O_RDONLY | O_DIRECTORY);
if (subfd < 0) {
ALOGE("Couldn't openat %s: %s\n", name, strerror(errno));
continue;
}
subdir = fdopendir(subfd);
if (subdir == NULL) {
ALOGE("Couldn't fdopendir %s: %s\n", name, strerror(errno));
close(subfd);
continue;
}
if (cacheDir == NULL) {
cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
}
if (cacheDir != NULL) {
// Update pathBase for the new path... this may change dirName
// if that is also pointing to the path, but we are done with it
// now.
size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
CACHE_NOISY(ALOGI("Collecting dir %s\n", pathBase));
if (finallen < pathAvailLen) {
_add_cache_files(cache, cacheDir, name, subdir, pathBase,
pathPos+finallen, pathAvailLen-finallen);
} else {
// Whoops, the final path is too long! We'll just delete
// this directory.
ALOGW("Cache dir %s truncated in path %s; deleting dir\n",
name, pathBase);
_delete_dir_contents(subdir, NULL);
if (unlinkat(dfd, name, AT_REMOVEDIR) < 0) {
ALOGE("Couldn't unlinkat %s: %s\n", name, strerror(errno));
}
}
}
closedir(subdir);
} else if (de->d_type == DT_REG) {
// Skip files that start with '.'; they will be deleted if
// their entire directory is deleted. This allows for metadata
// like ".nomedia" to remain in the directory until the entire
// directory is deleted.
if (cacheDir == NULL) {
cacheDir = _add_cache_dir_t(cache, parentDir, dirName);
}
if (name[0] == '.') {
cacheDir->hiddenCount++;
continue;
}
if (cacheDir != NULL) {
// Build final full path for file... this may change dirName
// if that is also pointing to the path, but we are done with it
// now.
size_t finallen = snprintf(pathPos, pathAvailLen, "/%s", name);
CACHE_NOISY(ALOGI("Collecting file %s\n", pathBase));
if (finallen < pathAvailLen) {
struct stat s;
if (stat(pathBase, &s) >= 0) {
_add_cache_file_t(cache, cacheDir, s.st_mtime, name);
} else {
ALOGW("Unable to stat cache file %s; deleting\n", pathBase);
if (unlink(pathBase) < 0) {
ALOGE("Couldn't unlink %s: %s\n", pathBase, strerror(errno));
}
}
} else {
// Whoops, the final path is too long! We'll just delete
// this file.
ALOGW("Cache file %s truncated in path %s; deleting\n",
name, pathBase);
if (unlinkat(dfd, name, 0) < 0) {
//.........这里部分代码省略.........
开发者ID:777rom,项目名称:777_frameworks_base,代码行数:101,代码来源:utils.c
示例8: local_db_populate
static int local_db_populate(alpm_db_t *db)
{
size_t est_count;
int count = 0;
struct stat buf;
struct dirent *ent = NULL;
const char *dbpath;
DIR *dbdir;
if(db->status & DB_STATUS_INVALID) {
RET_ERR(db->handle, ALPM_ERR_DB_INVALID, -1);
}
if(db->status & DB_STATUS_MISSING) {
RET_ERR(db->handle, ALPM_ERR_DB_NOT_FOUND, -1);
}
dbpath = _alpm_db_path(db);
if(dbpath == NULL) {
/* pm_errno set in _alpm_db_path() */
return -1;
}
dbdir = opendir(dbpath);
if(dbdir == NULL) {
RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
}
if(fstat(dirfd(dbdir), &buf) != 0) {
RET_ERR(db->handle, ALPM_ERR_DB_OPEN, -1);
}
db->status |= DB_STATUS_EXISTS;
db->status &= ~DB_STATUS_MISSING;
if(buf.st_nlink >= 2) {
est_count = buf.st_nlink;
} else {
/* Some filesystems don't subscribe to the two-implicit links school of
* thought, e.g. BTRFS, HFS+. See
* http://kerneltrap.org/mailarchive/linux-btrfs/2010/1/23/6723483/thread
*/
est_count = 0;
while(readdir(dbdir) != NULL) {
est_count++;
}
rewinddir(dbdir);
}
if(est_count >= 2) {
/* subtract the '.' and '..' pointers to get # of children */
est_count -= 2;
}
db->pkgcache = _alpm_pkghash_create(est_count);
if(db->pkgcache == NULL){
closedir(dbdir);
RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
}
while((ent = readdir(dbdir)) != NULL) {
const char *name = ent->d_name;
alpm_pkg_t *pkg;
if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {
continue;
}
if(!is_dir(dbpath, ent)) {
continue;
}
pkg = _alpm_pkg_new();
if(pkg == NULL) {
closedir(dbdir);
RET_ERR(db->handle, ALPM_ERR_MEMORY, -1);
}
/* split the db entry name */
if(_alpm_splitname(name, &(pkg->name), &(pkg->version),
&(pkg->name_hash)) != 0) {
_alpm_log(db->handle, ALPM_LOG_ERROR, _("invalid name for database entry '%s'\n"),
name);
_alpm_pkg_free(pkg);
continue;
}
/* duplicated database entries are not allowed */
if(_alpm_pkghash_find(db->pkgcache, pkg->name)) {
_alpm_log(db->handle, ALPM_LOG_ERROR, _("duplicated database entry '%s'\n"), pkg->name);
_alpm_pkg_free(pkg);
continue;
}
pkg->origin = ALPM_PKG_FROM_LOCALDB;
pkg->origin_data.db = db;
pkg->ops = &local_pkg_ops;
pkg->handle = db->handle;
/* explicitly read with only 'BASE' data, accessors will handle the rest */
if(local_db_read(pkg, INFRQ_BASE) == -1) {
_alpm_log(db->handle, ALPM_LOG_ERROR, _("corrupted database entry '%s'\n"), name);
_alpm_pkg_free(pkg);
continue;
}
//.........这里部分代码省略.........
开发者ID:AWhetter,项目名称:pacman,代码行数:101,代码来源:be_local.c
示例9: clear_hugedir
/*
* Clear the hugepage directory of whatever hugepage files
* there are. Checks if the file is locked (i.e.
* if it's in use by another DPDK process).
*/
static int
clear_hugedir(const char * hugedir)
{
DIR *dir;
struct dirent *dirent;
int dir_fd, fd, lck_result;
const char filter[] = "*map_*"; /* matches hugepage files */
/* open directory */
dir = opendir(hugedir);
if (!dir) {
RTE_LOG(INFO, EAL, "Unable to open hugepage directory %s\n",
hugedir);
goto error;
}
dir_fd = dirfd(dir);
dirent = readdir(dir);
if (!dirent) {
RTE_LOG(INFO, EAL, "Unable to read hugepage directory %s\n",
hugedir);
goto error;
}
while(dirent != NULL){
/* skip files that don't match the hugepage pattern */
if (fnmatch(filter, dirent->d_name, 0) > 0) {
dirent = readdir(dir);
continue;
}
/* try and lock the file */
fd = openat(dir_fd, dirent->d_name, O_RDONLY);
/* skip to next file */
if (fd == -1) {
dirent = readdir(dir);
continue;
}
/* non-blocking lock */
lck_result = flock(fd, LOCK_EX | LOCK_NB);
/* if lock succeeds, unlock and remove the file */
if (lck_result != -1) {
flock(fd, LOCK_UN);
unlinkat(dir_fd, dirent->d_name, 0);
}
close (fd);
dirent = readdir(dir);
}
closedir(dir);
return 0;
error:
if (dir)
closedir(dir);
RTE_LOG(INFO, EAL, "Error while clearing hugepage dir: %s\n",
strerror(errno));
return -1;
}
开发者ID:fleitner,项目名称:dpdk,代码行数:69,代码来源:eal_hugepage_info.c
示例10: is_potential_path
//.........这里部分代码省略.........
break;
}
}
}
if( ! has_magic && ! clean_path.empty() )
{
/* Don't test the same path multiple times, which can happen if the path is absolute and the CDPATH contains multiple entries */
std::set<wcstring> checked_paths;
/* Keep a cache of which paths / filesystems are case sensitive */
case_sensitivity_cache_t case_sensitivity_cache;
for (size_t wd_idx = 0; wd_idx < directories.size() && ! result; wd_idx++) {
const wcstring &wd = directories.at(wd_idx);
const wcstring abs_path = apply_working_directory(clean_path, wd);
/* Skip this if it's empty or we've already checked it */
if (abs_path.empty() || checked_paths.count(abs_path))
continue;
checked_paths.insert(abs_path);
/* If we end with a slash, then it must be a directory */
bool must_be_full_dir = abs_path.at(abs_path.size()-1) == L'/';
if (must_be_full_dir)
{
struct stat buf;
if (0 == wstat(abs_path, &buf) && S_ISDIR(buf.st_mode)) {
result = true;
/* Return the path suffix, not the whole absolute path */
if (out_path)
*out_path = clean_path;
}
}
else
{
DIR *dir = NULL;
/* We do not end with a slash; it does not have to be a directory */
const wcstring dir_name = wdirname(abs_path);
const wcstring base_name = wbasename(abs_path);
if (dir_name == L"/" && base_name == L"/")
{
result = true;
if (out_path)
*out_path = clean_path;
}
else if ((dir = wopendir(dir_name))) {
// We opened the dir_name; look for a string where the base name prefixes it
wcstring ent;
// Check if we're case insensitive
bool case_insensitive = fs_is_case_insensitive(dir_name, dirfd(dir), case_sensitivity_cache);
// Don't ask for the is_dir value unless we care, because it can cause extra filesystem acces */
bool is_dir = false;
while (wreaddir_resolving(dir, dir_name, ent, require_dir ? &is_dir : NULL))
{
/* Determine which function to call to check for prefixes */
bool (*prefix_func)(const wcstring &, const wcstring &);
if (case_insensitive) {
prefix_func = string_prefixes_string_case_insensitive;
} else {
prefix_func = string_prefixes_string;
}
if (prefix_func(base_name, ent) && (! require_dir || is_dir))
{
result = true;
if (out_path) {
/* We want to return the path in the same "form" as it was given. Take the given path, get its basename. Append that to the output if the basename actually prefixes the path (which it won't if the given path contains no slashes), and isn't a slash (so we don't duplicate slashes). Then append the directory entry. */
out_path->clear();
const wcstring path_base = wdirname(const_path);
if (prefix_func(path_base, const_path)) {
out_path->append(path_base);
if (! string_suffixes_string(L"/", *out_path))
out_path->push_back(L'/');
}
out_path->append(ent);
/* We actually do want a trailing / for directories, since it makes autosuggestion a bit nicer */
if (is_dir)
out_path->push_back(L'/');
}
break;
}
}
closedir(dir);
}
}
}
}
return result;
}
开发者ID:fimmtiu,项目名称:fish-shell,代码行数:101,代码来源:highlight.cpp
示例11: log_sample
void log_sample(int sample, struct list_sample_data **ptr) {
static int vmstat;
static int schedstat;
char buf[4096];
char key[256];
char val[256];
char rt[256];
char wt[256];
char *m;
int c;
int p;
int mod;
static int e_fd;
ssize_t s;
ssize_t n;
struct dirent *ent;
int fd;
struct list_sample_data *sampledata;
struct ps_sched_struct *ps_prev = NULL;
sampledata = *ptr;
/* all the per-process stuff goes here */
if (!proc) {
/* find all processes */
proc = opendir("/proc");
if (!proc)
return;
procfd = dirfd(proc);
} else {
rewinddir(proc);
}
if (!vmstat) {
/* block stuff */
vmstat = openat(procfd, "vmstat", O_RDONLY);
if (vmstat == -1) {
log_error("Failed to open /proc/vmstat: %m");
exit(EXIT_FAILURE);
}
}
n = pread(vmstat, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
close(vmstat);
return;
}
buf[n] = '\0';
m = buf;
while (m) {
if (sscanf(m, "%s %s", key, val) < 2)
goto vmstat_next;
if (streq(key, "pgpgin"))
sampledata->blockstat.bi = atoi(val);
if (streq(key, "pgpgout")) {
sampledata->blockstat.bo = atoi(val);
break;
}
vmstat_next:
m = bufgetline(m);
if (!m)
break;
}
if (!schedstat) {
/* overall CPU utilization */
schedstat = openat(procfd, "schedstat", O_RDONLY);
if (schedstat == -1) {
log_error("Failed to open /proc/schedstat: %m");
exit(EXIT_FAILURE);
}
}
n = pread(schedstat, buf, sizeof(buf) - 1, 0);
if (n <= 0) {
close(schedstat);
return;
}
buf[n] = '\0';
m = buf;
while (m) {
if (sscanf(m, "%s %*s %*s %*s %*s %*s %*s %s %s", key, rt, wt) < 3)
goto schedstat_next;
if (strstr(key, "cpu")) {
c = atoi((const char*)(key+3));
if (c > MAXCPUS)
/* Oops, we only have room for MAXCPUS data */
break;
sampledata->runtime[c] = atoll(rt);
sampledata->waittime[c] = atoll(wt);
if (c == cpus)
cpus = c + 1;
}
schedstat_next:
m = bufgetline(m);
if (!m)
//.........这里部分代码省略.........
开发者ID:MOBO-OSS,项目名称:systemd-relative,代码行数:101,代码来源:store.c
示例12: __getcwd
//.........这里部分代码省略.........
/* Look at the parent directory. */
#if HAVE_OPENAT_SUPPORT
fd = openat (fd, "..", O_RDONLY);
if (fd < 0)
goto lose;
fd_needs_closing = true;
parent_status = fstat (fd, &st);
#else
dotlist[dotlen++] = '.';
dotlist[dotlen++] = '.';
dotlist[dotlen] = '\0';
parent_status = __lstat (dotlist, &st);
#endif
if (parent_status != 0)
goto lose;
if (dirstream && __closedir (dirstream) != 0)
{
dirstream = NULL;
goto lose;
}
/* Figure out if this directory is a mount point. */
dotdev = st.st_dev;
dotino = st.st_ino;
mount_point = dotdev != thisdev;
/* Search for the last directory. */
#if HAVE_OPENAT_SUPPORT
dirstream = fdopendir (fd);
if (dirstream == NULL)
goto lose;
/* Reset fd. It may have been closed by fdopendir. */
fd = dirfd (dirstream);
fd_needs_closing = false;
#else
dirstream = __opendir (dotlist);
if (dirstream == NULL)
goto lose;
dotlist[dotlen++] = '/';
#endif
for (;;)
{
/* Clear errno to distinguish EOF from error if readdir returns
NULL. */
__set_errno (0);
d = __readdir (dirstream);
/* When we've iterated through all directory entries without finding
one with a matching d_ino, rewind the stream and consider each
name again, but this time, using lstat. This is necessary in a
chroot on at least one system (glibc-2.3.6 + linux 2.6.12), where
.., ../.., ../../.., etc. all had the same device number, yet the
d_ino values for entries in / did not match those obtained
via lstat. */
if (d == NULL && errno == 0 && use_d_ino)
{
use_d_ino = false;
rewinddir (dirstream);
d = __readdir (dirstream);
}
if (d == NULL)
{
if (errno == 0)
/* EOF on dirstream, which can mean e.g., that the current
开发者ID:DanielMSchmidt,项目名称:it-sec,代码行数:67,代码来源:getcwd.c
示例13: change_to_dir
int
change_to_dir(DIR *dir)
{
return fchdir(dirfd(dir));
}
开发者ID:2ion,项目名称:polipo-patched,代码行数:5,代码来源:fts_compat.c
示例14: proc_next_pid
int proc_next_pid(struct proc_processes *ps, pid_t *pid)
{
struct dirent *d;
if (!ps || !pid)
return -EINVAL;
*pid = 0;
errno = 0;
do {
char buf[BUFSIZ], *p;
d = readdir(ps->dir);
if (!d)
return errno ? -1 : 1; /* error or end-of-dir */
if (!isdigit((unsigned char) *d->d_name))
continue;
/* filter out by UID */
if (ps->has_fltr_uid) {
struct stat st;
if (fstat_at(dirfd(ps->dir), "/proc", d->d_name, &st, 0))
continue;
if (ps->fltr_uid != st.st_uid)
continue;
}
/* filter out by NAME */
if (ps->has_fltr_name) {
char procname[256];
FILE *f;
snprintf(buf, sizeof(buf), "%s/stat", d->d_name);
f = fopen_at(dirfd(ps->dir), "/proc", buf,
O_CLOEXEC|O_RDONLY, "r");
if (!f)
continue;
p = fgets(buf, sizeof(buf), f);
fclose(f);
if (sscanf(buf, "%*d (%255[^)])", procname) != 1)
continue;
/* ok, we got the process name. */
if (strcmp(procname, ps->fltr_name) != 0)
continue;
}
p = NULL;
errno = 0;
*pid = (pid_t) strtol(d->d_name, &p, 10);
if (errno || d->d_name == p || (p && *p))
return errno ? -errno : -1;
return 0;
} while (1);
return 0;
}
开发者ID:babuneelam,项目名称:util-linux,代码行数:64,代码来源:procutils.c
示例15: DEBUG
void
PackageSet::init( void )
{
if ( myInit )
return;
myInit = true;
DEBUG( "---------- PackageSet::init --------------" );
for ( auto &i: myPkgSearchPath )
{
// first trim any trailing slashes, win32 opendir doesn't seem to like it
String::strip( i );
File::trimTrailingSeparators( i );
DIR *d = ::opendir( i.c_str() );
if ( d )
{
ON_EXIT{ ::closedir( d ); };
// glibc deprecates readdir_r in 2.24...
#if defined(__GNU_LIBRARY__) && ( __GLIBC__ > 2 || ( __GLIBC__ == 2 && __GLIBC_MINOR__ >= 24 ) )
while ( true )
{
errno = 0;
struct dirent *cur = ::readdir( d );
if ( ! cur )
{
if ( errno != 0 )
{
std::cerr << "WARNING: error reading directory '" << i << "'" << std::endl;
}
break;
}
std::string cname = cur->d_name;
std::string::size_type ePC = cname.rfind( ".pc", std::string::npos, 3 );
if ( ePC != std::string::npos )
{
if ( ePC == ( cname.size() - 3 ) )
{
std::string name = cname.substr( 0, ePC );
// if we found the same name earlier, ignore this one
if ( myPackageConfigs.find( name ) == myPackageConfigs.end() )
{
std::string fullpath = i;
fullpath.push_back( File::pathSeparator() );
fullpath.append( cname );
DEBUG( name << ": " << fullpath );
myPackageConfigs[name] = fullpath;
}
}
}
}
#else
std::unique_ptr<uint8_t[]> rdBuf;
size_t allocSize = 0;
long name_max = fpathconf( dirfd( d ), _PC_NAME_MAX );
if ( name_max == -1 )
{
#if defined(NAME_MAX)
name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
#else
name_max = 255;
#endif
}
allocSize = sizeof(struct dirent) + static_cast<size_t>( name_max ) + 1;
rdBuf.reset( new uint8_t[allocSize] );
struct dirent *curDir = reinterpret_cast<struct dirent *>( rdBuf.get() );
struct dirent *cur = nullptr;
while ( readdir_r( d, curDir, &cur ) == 0 )
{
if ( ! cur )
break;
std::string cname = cur->d_name;
std::string::size_type ePC = cname.rfind( ".pc", std::string::npos, 3 );
if ( ePC != std::string::npos )
{
if ( ePC == ( cname.size() - 3 ) )
{
std::string name = cname.substr( 0, ePC );
// if we found the same name earlier, ignore this one
if ( myPackageConfigs.find( name ) == myPackageConfigs.end() )
{
std::string fullpath = i;
fullpath.push_back( File::pathSeparator() );
fullpath.append( cname );
myPackageConfigs[name] = fullpath;
}
}
}
}
#endif
}
}
}
开发者ID:kdt3rd,项目名称:constructor,代码行数:97,代码来源:PackageSet.cpp
示例16: fpathconf
void
PackageSet::init( void )
{
if ( myInit )
return;
myInit = true;
for ( auto &i: myPkgSearchPath )
{
// first trim any trailing slashes, win32 opendir doesn't seem to like it
String::strip( i );
File::trimTrailingSeparators( i );
DIR *d = ::opendir( i.c_str() );
if ( d )
{
ON_EXIT{ ::closedir( d ); };
std::unique_ptr<uint8_t[]> rdBuf;
size_t allocSize = 0;
long name_max = fpathconf( dirfd( d ), _PC_NAME_MAX );
if ( name_max == -1 )
{
#if defined(NAME_MAX)
name_max = (NAME_MAX > 255) ? NAME_MAX : 255;
#else
name_max = 255;
#endif
}
allocSize = sizeof(struct dirent) + static_cast<size_t>( name_max ) + 1;
rdBuf.reset( new uint8_t[allocSize] );
struct dirent *curDir = reinterpret_cast<struct dirent *>( rdBuf.get() );
struct dirent *cur = nullptr;
while ( readdir_r( d, curDir, &cur ) == 0 )
{
if ( ! cur )
break;
std::string cname = cur->d_name;
std::string::size_type ePC = cname.rfind( ".pc", std::string::npos, 3 );
if ( ePC != std::string::npos )
{
if ( ePC == ( cname.size() - 3 ) )
{
std::string name = cname.substr( 0, ePC );
// if we found the same name earlier, ignore this one
if ( myPackageConfigs.find( name ) == myPackageConfigs.end() )
{
std::string fullpath = i;
fullpath.push_back( File::pathSeparator() );
fullpath.append( cname );
myPackageConfigs[name] = fullpath;
}
}
}
}
}
}
}
开发者ID:iangodin,项目名称:constructor,代码行数:61,代码来源:PackageSet.cpp
|
请发表评论