fork/gdu
git clone https://git.julia.blue/fork/gdu
af9492f
ignore
1//go:build darwin 2 3package fs 4 5import ( 6"fmt" 7 8"github.com/ebitengine/purego" 9) 10 11// Constants from the macOS SDK, sys/resource.h. Documented in setiopolicy_np(3). 12const ( 13iopolTypeVFSMaterializeDatalessFiles = 3 // IOPOL_TYPE_VFS_MATERIALIZE_DATALESS_FILES 14iopolScopeProcess = 0 // IOPOL_SCOPE_PROCESS 15iopolMaterializeDatalessFilesOff = 1 // IOPOL_MATERIALIZE_DATALESS_FILES_OFF 16) 17 18const libSystem = "/usr/lib/libSystem.B.dylib" 19 20// PreventDatalessMaterialization stops the kernel from materializing dataless 21// files while this process walks the filesystem. 22// 23// On volumes backed by a File Provider extension (iCloud Drive, Google Drive, 24// OneDrive, Dropbox) files can be "dataless": the metadata is local but the 25// contents are not. Traversing such a directory makes the kernel ask the 26// provider to materialize it. That is a synchronous round trip to the provider 27// daemon and often a real download, so scanning is orders of magnitude slower 28// and pulls down data the user never asked for. 29// 30// /usr/bin/du opts out for the same reason, by setting the 31// vfs.nspace.prevent_materialization sysctl (rdar://44903941): 32// https://github.com/apple-oss-distributions/file_cmds/blob/659a8a301e2acf0343f8b8673a154a2ca4d07084/du/du.c#L323 33// 34// We use setiopolicy_np instead. It sets the same per-process kernel state, and 35// it is the API Apple documents for this in TN3150: 36// https://developer.apple.com/documentation/technotes/tn3150-getting-ready-for-data-less-files 37// 38// The scope is IOPOL_SCOPE_PROCESS rather than IOPOL_SCOPE_THREAD because 39// goroutines migrate between OS threads. Thread scope would require 40// LockOSThread on every scanning worker. The call needs no privileges and is 41// inert on volumes with no dataless files, so it is safe to make it always. 42// 43// It goes through libSystem rather than a raw syscall because libSystem is the 44// only ABI Apple guarantees (QA1118). x/sys/unix has no binding for 45// setiopolicy_np, and gdu builds with CGO_ENABLED=0, so purego provides the 46// dlopen/dlsym bridge. 47// 48// Note that with materialization off, reading the contents of a dataless file 49// fails with EDEADLK. Scanning only reads metadata, so it is unaffected. The 50// file viewer (v) and archive browsing do read contents, and show an error 51// dialog for a dataless file. That is intentional: a disk usage tool should not 52// download a file to display it. 53func PreventDatalessMaterialization ()error { 54lib ,err := purego .Dlopen (libSystem ,purego .RTLD_NOW | purego .RTLD_GLOBAL ) 55if err != nil { 56return fmt .Errorf ("dlopen %s: %w" ,libSystem ,err ) 57 } 58 59// Use Dlsym and RegisterFunc, not RegisterLibFunc, as the latter panics if 60// the symbol is missing. 61sym ,err := purego .Dlsym (lib ,"setiopolicy_np" ) 62if err != nil { 63return fmt .Errorf ("dlsym setiopolicy_np: %w" ,err ) 64 } 65 66var setiopolicyNp func (iotype ,scope ,policy int32 )int32 67purego .RegisterFunc (& setiopolicyNp ,sym ) 68 69if rc := setiopolicyNp ( 70iopolTypeVFSMaterializeDatalessFiles , 71iopolScopeProcess , 72iopolMaterializeDatalessFilesOff , 73 );rc != 0 { 74return fmt .Errorf ("setiopolicy_np returned %d" ,rc ) 75 } 76return nil 77}