1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
/*
Copyright (C) CFEngine AS
This file is part of CFEngine 3 - written and maintained by CFEngine AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <dir.h>
#include <dir_priv.h>
#include <file_lib.h>
#include <alloc.h>
struct Dir_
{
DIR *dirh;
struct dirent *entrybuf;
};
static size_t GetNameMax(DIR *dirp);
static size_t GetDirentBufferSize(size_t path_len);
Dir *DirOpen(const char *dirname)
{
Dir *ret = xcalloc(1, sizeof(Dir));
int safe_fd;
safe_fd = safe_open(dirname, O_RDONLY);
if (safe_fd < 0)
{
free(ret);
return NULL;
}
ret->dirh = opendir(dirname);
if (ret->dirh == NULL)
{
close(safe_fd);
free(ret);
return NULL;
}
struct stat safe_stat, dir_stat;
bool stat_failed = fstat(safe_fd, &safe_stat) < 0 || fstat(dirfd(ret->dirh), &dir_stat) < 0;
close(safe_fd);
if (stat_failed)
{
closedir(ret->dirh);
free(ret);
return NULL;
}
// Make sure we opened the same file descriptor as safe_open did.
if (safe_stat.st_dev != dir_stat.st_dev || safe_stat.st_ino != dir_stat.st_ino)
{
closedir(ret->dirh);
free(ret);
errno = EACCES;
return NULL;
}
size_t dirent_buf_size = GetDirentBufferSize(GetNameMax(ret->dirh));
ret->entrybuf = xcalloc(1, dirent_buf_size);
return ret;
}
/*
* Returns NULL on EOF or error.
*
* Sets errno to 0 for EOF and non-0 for error.
*/
const struct dirent *DirRead(Dir *dir)
{
errno = 0;
struct dirent *ret;
int err = readdir_r((DIR *) dir->dirh, dir->entrybuf, &ret);
if (err != 0)
{
errno = err;
return NULL;
}
if (ret == NULL)
{
return NULL;
}
return ret;
}
void DirClose(Dir *dir)
{
closedir((DIR *) dir->dirh);
free(dir->entrybuf);
free(dir);
}
/*
* Taken from http://womble.decadent.org.uk/readdir_r-advisory.html
*
* Issued by Ben Hutchings <ben@decadent.org.uk>, 2005-11-02.
*
* Licence
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following condition:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Calculate the required buffer size (in bytes) for directory entries read from
* the given directory handle. Return -1 if this this cannot be done.
*
* This code does not trust values of NAME_MAX that are less than 255, since
* some systems (including at least HP-UX) incorrectly define it to be a smaller
* value.
*
* If you use autoconf, include fpathconf and dirfd in your AC_CHECK_FUNCS list.
* Otherwise use some other method to detect and use them where available.
*/
#if defined(HAVE_FPATHCONF) && defined(_PC_NAME_MAX)
static size_t GetNameMax(DIR *dirp)
{
long name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX);
if (name_max != -1)
{
return name_max;
}
# if defined(NAME_MAX)
return (NAME_MAX > 255) ? NAME_MAX : 255;
# else
return (size_t) (-1);
# endif
}
#else /* FPATHCONF && _PC_NAME_MAX */
# if defined(NAME_MAX)
static size_t GetNameMax(DIR *dirp)
{
return (NAME_MAX > 255) ? NAME_MAX : 255;
}
# else
# error "buffer size for readdir_r cannot be determined"
# endif
#endif /* FPATHCONF && _PC_NAME_MAX */
/*
* Returns size of memory enough to hold path name_len bytes long.
*/
static size_t GetDirentBufferSize(size_t name_len)
{
size_t name_end = (size_t) offsetof(struct dirent, d_name) + name_len + 1;
return (name_end > sizeof(struct dirent) ? name_end : sizeof(struct dirent));
}
struct dirent *AllocateDirentForFilename(const char *filename)
{
struct dirent *entry = xcalloc(1, GetDirentBufferSize(strlen(filename)));
strcpy(entry->d_name, filename);
return entry;
}
|