Loading...
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | /* * Copyright (c) 2017 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this * file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_LICENSE_HEADER_END@ */ #include <strings.h> #include <stddef.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <sys/fsctl.h> #include <sys/vnode.h> #include <sys/errno.h> #include <os/assumes.h> #include <TargetConditionals.h> #include "dirstat.h" #include "dirstat_collection.h" #if !TARGET_OS_SIMULATOR #define HAS_APFS #endif #ifdef HAS_APFS #include <apfs/apfs_fsctl.h> #endif #if DEBUG #define DEBUGPRINT(...) fprintf(stderr, __VA_ARGS__) #else #define DEBUGPRINT(...) do { } while(0) #endif static int fdirstat_fallback(int fd, int flags, struct dirstat *ds); #ifdef HAS_APFS static int fdirstat(int fd, int flags, struct dirstat *ds) { struct apfs_dir_stats_ext dstats = {0}; if (flags & DIRSTAT_FAST_ONLY) { dstats.flags |= APFS_DIR_STATS_FAST_PATH; } int err = ffsctl(fd, APFSIOC_GET_DIR_STATS_EXT, &dstats, 0); if (err == -1) { if (errno == ENOENT) { // <rdar://problem/31696225> errno = ENOTSUP; } return -1; } ds->total_size = dstats.total_size; ds->descendants = dstats.num_children; return 0; } #endif int dirstatat_np(int dfd, const char *path, int flags, struct dirstat *ds_out, size_t ds_size) { #ifdef HAS_APFS // <rdar://problem/32794924> // Until APFS directory sizing is fixed, only the fallback path is // available. flags |= DIRSTAT_FORCE_FALLBACK; // FORCE_FALLBACK trumps FAST_ONLY. Make sure to set errno accordingly in // the case that a confused caller asks for both. if ((flags & (DIRSTAT_FAST_ONLY)) && (flags & DIRSTAT_FORCE_FALLBACK)) { errno = ENOTSUP; return -1; } #endif int fd = openat(dfd, path, O_RDONLY | O_DIRECTORY); DEBUGPRINT("Opened %d:%s as %d\n", dfd, path, fd); if (fd == -1) return -1; struct dirstat ds = {}; int ret = -1; #ifdef HAS_APFS if (!(flags & DIRSTAT_FORCE_FALLBACK)) { ret = fdirstat(fd, flags, &ds); } if (ret == -1 && ((flags & DIRSTAT_FORCE_FALLBACK) || ((errno == ENOTTY) && !(flags & DIRSTAT_FAST_ONLY)))) { ret = fdirstat_fallback(fd, flags, &ds); } #else ret = fdirstat_fallback(fd, flags, &ds); #endif int saved_errno = errno; if (ds_size >= sizeof(ds)) { memcpy(ds_out, &ds, sizeof(ds)); } else { memcpy(ds_out, &ds, ds_size); } close(fd); errno = saved_errno; return ret; } int dirstat_np(const char *path, int flags, struct dirstat *ds, size_t ds_size) { return dirstatat_np(AT_FDCWD, path, flags, ds, ds_size); } #pragma mark Fallback struct dirqueue_entry { STAILQ_ENTRY(dirqueue_entry) entries; char *path; }; static int fdirstat_fallback(int parent_fd, int flags, struct dirstat *ds) { int reterror = 0; /* * This method of gathering disk usage is the fastest by far over other * methods using fts or opendir/readdir + getattrlist or stat to gather * information about filesystem usage. That's because this method avoids * creating vnodes for each item in a directory. We implement a recursive * filesystem search by appending each directory child found to a * processing queue, and then process each child directory in that queue on * a FIFO basis resulting in a breadth-first traversal of the filesystem. * This keeps our actual implementation iterative to avoid deep filesystem * hierarchies overflowing our stack. */ dirstat_fileid_set_t fileid_seen = _dirstat_fileid_set_create(); STAILQ_HEAD(, dirqueue_entry) dirqueue_head = STAILQ_HEAD_INITIALIZER(dirqueue_head); struct attrlist attrlist = { .bitmapcount = ATTR_BIT_MAP_COUNT, .commonattr = ATTR_CMN_RETURNED_ATTRS | ATTR_CMN_ERROR | ATTR_CMN_NAME | ATTR_CMN_OBJTYPE | ATTR_CMN_FILEID, .dirattr = ATTR_DIR_ENTRYCOUNT, .fileattr = ATTR_FILE_LINKCOUNT | ATTR_FILE_ALLOCSIZE | ATTR_FILE_DATAALLOCSIZE, }; typedef struct { /* * fields are in order of possible return in buffer (but note that data * is packed in the actual buffer, and only relevant fields are * returned) */ uint32_t length; attribute_set_t returned; //ATTR_CMN_RETURNED_ATTRS uint32_t error; //ATTR_CMN_ERROR attrreference_t item_name_info; //ATTR_CMN_NAME fsobj_type_t type; //ATTR_CMN_OBJTYPE uint64_t fileid; //ATTR_CMN_FILEID union { struct { u_int32_t entry_count; //ATTR_DIR_ENTRYCOUNT }; struct { u_int32_t link_count; //ATTR_FILE_LINKCOUNT off_t alloc_size; //ATTR_FILE_ALLOCSIZE off_t data_alloc_size; //ATTR_FILE_DATAALLOCSIZE }; }; } max_attr_entry_t; size_t attrbuf_len = (32 * 1024); char *attrbuf = alloca(attrbuf_len); #ifdef HAS_APFS os_assert(!(flags & DIRSTAT_FAST_ONLY)); #endif do { int fd = -1; char *path; if (STAILQ_EMPTY(&dirqueue_head)) { fd = parent_fd; path = NULL; } else { struct dirqueue_entry *dqe = STAILQ_FIRST(&dirqueue_head); STAILQ_REMOVE_HEAD(&dirqueue_head, entries); path = dqe->path; free(dqe); fd = openat(parent_fd, path, O_RDONLY | O_DIRECTORY); if (fd < 0) { DEBUGPRINT( "Unable to open directory %d:%s => %s\n", parent_fd, path, strerror(errno)); free(path); continue; } } while (1) { int ret_entry_count = getattrlistbulk(fd, &attrlist, attrbuf, attrbuf_len, 0); if (-1 == ret_entry_count) { if (fd == parent_fd) { reterror = errno; } DEBUGPRINT( "getattrlistbulk on in %s returned error %s\n", path, strerror(errno)); break; } else if (0 == ret_entry_count) { break; } else { char *cursor = NULL; //pointer into attrbuf char *entry_start = attrbuf; for (int index = 0; index < ret_entry_count; index++) { max_attr_entry_t attrs = {0}; char *name = NULL; cursor = entry_start; memcpy(&attrs.length, cursor, sizeof(attrs.length)); cursor += sizeof(attrs.length); /* set starting point for next entry */ entry_start += attrs.length; memcpy(&attrs.returned, cursor, sizeof(attrs.returned)); cursor += sizeof(attrs.returned); if (attrs.returned.commonattr & ATTR_CMN_ERROR) { memcpy(&attrs.error, cursor, sizeof(attrs.error)); cursor += sizeof(attrs.error); } if (attrs.error) { DEBUGPRINT( "Got error %s while processing in %s\n", strerror(errno), path); continue; } if (attrs.returned.commonattr & ATTR_CMN_NAME) { memcpy(&attrs.item_name_info, cursor, sizeof(attrs.item_name_info)); name = cursor + attrs.item_name_info.attr_dataoffset; if (name + attrs.item_name_info.attr_length > entry_start) { name = NULL; } cursor += sizeof(attrs.item_name_info); } if (attrs.returned.commonattr & ATTR_CMN_OBJTYPE) { memcpy(&attrs.type, cursor, sizeof(attrs.type)); cursor += sizeof(attrs.type); } if (attrs.returned.commonattr & ATTR_CMN_FILEID) { memcpy(&attrs.fileid, cursor, sizeof(attrs.fileid)); cursor += sizeof(attrs.fileid); } if (VDIR == attrs.type) { if (attrs.returned.dirattr & ATTR_DIR_ENTRYCOUNT) { memcpy(&attrs.entry_count, cursor, sizeof(attrs.entry_count)); cursor += sizeof(attrs.entry_count); } else { // Fake it so we go down the right path below attrs.entry_count = -1; } // avoid descending into empty directories if (attrs.entry_count && name) { struct dirqueue_entry *dqe = malloc(sizeof(struct dirqueue_entry)); if (path == NULL) { dqe->path = strdup(name); } else { asprintf(&dqe->path, "%s/%s", path, name); } if (dqe->path != NULL) { STAILQ_INSERT_TAIL(&dirqueue_head, dqe, entries); } else { DEBUGPRINT( "Unable to create dqe\n"); free(dqe); } } else if (attrs.entry_count != 0) { DEBUGPRINT( "Failed to get name for item in %s\n", path); } else if (attrs.entry_count == 0) { // Empty directory, nothing to do } } else { off_t object_size = 0; if (attrs.returned.fileattr & ATTR_FILE_LINKCOUNT) { memcpy(&attrs.link_count, cursor, sizeof(attrs.link_count)); cursor += sizeof(attrs.link_count); } if (attrs.returned.fileattr & ATTR_FILE_ALLOCSIZE) { memcpy(&attrs.alloc_size, cursor, sizeof(attrs.alloc_size)); cursor += sizeof(attrs.alloc_size); object_size = attrs.alloc_size; } if (attrs.returned.fileattr & ATTR_FILE_DATAALLOCSIZE) { memcpy(&attrs.data_alloc_size, cursor, sizeof(attrs.data_alloc_size)); cursor += sizeof(attrs.data_alloc_size); if (0 == object_size) { object_size = attrs.data_alloc_size; } } if (1 == attrs.link_count) { ds->total_size += object_size; } else { bool seen_fileid = _dirstat_fileid_set_add(fileid_seen, attrs.fileid); if (!seen_fileid) { ds->total_size += object_size; } else { DEBUGPRINT( "Skipping hardlinked file at %s/%s\n", path, name); } } } ds->descendants++; } } } if (path) { close(fd); free(path); } } while (!STAILQ_EMPTY(&dirqueue_head)); _dirstat_fileid_set_destroy(fileid_seen); if (reterror) { errno = reterror; return -1; } else { return 0; } } |