dirent-util.c revision a09561746f15b84da9471b5c4be74e53d19e4f3f
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering/***
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering This file is part of systemd.
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering Copyright 2010-2012 Lennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering systemd is free software; you can redistribute it and/or modify it
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering under the terms of the GNU Lesser General Public License as published by
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering (at your option) any later version.
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering systemd is distributed in the hope that it will be useful, but
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering Lesser General Public License for more details.
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering You should have received a copy of the GNU Lesser General Public License
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering***/
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include <fcntl.h>
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include <sys/stat.h>
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include <sys/types.h>
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include <unistd.h>
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include "dirent-util.h"
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering#include "string-util.h"
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poetteringint dirent_ensure_type(DIR *d, struct dirent *de) {
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering struct stat st;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering assert(d);
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering assert(de);
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering if (de->d_type != DT_UNKNOWN)
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return 0;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return -errno;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering de->d_type =
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISREG(st.st_mode) ? DT_REG :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISDIR(st.st_mode) ? DT_DIR :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISLNK(st.st_mode) ? DT_LNK :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISFIFO(st.st_mode) ? DT_FIFO :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISSOCK(st.st_mode) ? DT_SOCK :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISCHR(st.st_mode) ? DT_CHR :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering S_ISBLK(st.st_mode) ? DT_BLK :
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering DT_UNKNOWN;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return 0;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering}
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poetteringbool dirent_is_file(const struct dirent *de) {
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering assert(de);
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering if (hidden_file(de->d_name))
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return false;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering if (de->d_type != DT_REG &&
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering de->d_type != DT_LNK &&
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering de->d_type != DT_UNKNOWN)
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return false;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return true;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering}
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poetteringbool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) {
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering assert(de);
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering if (de->d_type != DT_REG &&
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering de->d_type != DT_LNK &&
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering de->d_type != DT_UNKNOWN)
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return false;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering if (hidden_file_allow_backup(de->d_name))
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return false;
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering return endswith(de->d_name, suffix);
a09561746f15b84da9471b5c4be74e53d19e4f3fLennart Poettering}