This file is indexed.

/usr/share/gocode/src/github.com/shirou/gopsutil/disk/disk_darwin.h is in golang-github-shirou-gopsutil-dev 2.17.08-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/storage/IOBlockStorageDriver.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/IOBSD.h>

// The iterator of all things disk. Allocated by StartIOCounterFetch, released
// by EndIOCounterFetch.
static io_iterator_t diskIter;

// Begins fetching IO counters.
//
// Returns 1 if the fetch started successfully, false otherwise.
//
// If the fetch was started successfully, you must call EndIOCounterFetch once
// done to release resources.
int StartIOCounterFetch()
{
    if (IOServiceGetMatchingServices(kIOMasterPortDefault,
                                     IOServiceMatching(kIOMediaClass),
                                     &diskIter) != kIOReturnSuccess) {
        return 0;
    }

    return 1;
}

// Releases resources from fetching IO counters.
void EndIOCounterFetch()
{
    IOObjectRelease(diskIter);
}

// The current disk entry of interest. Allocated by FetchNextDisk(), released by
// ReadDiskInfo().
static io_registry_entry_t diskEntry;

// The parent of diskEntry. Same lifetimes.
static io_registry_entry_t parentEntry;

// Fetches the next disk. Note that a disk entry is allocated, and will be held
// until it is processed and freed by ReadDiskInfo.
int FetchNextDisk()
{
    while ((diskEntry = IOIteratorNext(diskIter)) != 0) {
        // We are iterating IOMedia. We need to get the parent too (IOBSD).
        if (IORegistryEntryGetParentEntry(diskEntry, kIOServicePlane, &parentEntry) != kIOReturnSuccess) {
            // something is wrong...
            IOObjectRelease(diskEntry);
            continue;
        }

        if (!IOObjectConformsTo(parentEntry, "IOBlockStorageDriver")) {
            // no use to us, try the next disk
            IOObjectRelease(diskEntry);
            IOObjectRelease(parentEntry);
            continue;
        }

        // Got a disk OK.
        return 1;
    }

    // No more disks.
    return 0;
}

// Reads the current disk (from iteration) info into DiskInfo struct.
// Once done, all resources from the current iteration of reading are freed,
// ready for FetchNextDisk() to be called again.
int ReadDiskInfo(DiskInfo *info)
{
    // Parent props. Allocated by us.
    CFDictionaryRef parentProps = NULL;

    // Disk props. Allocated by us.
    CFDictionaryRef diskProps = NULL;

    // Disk stats, fetched by us, but not allocated by us.
    CFDictionaryRef stats = NULL;

    if (IORegistryEntryCreateCFProperties(diskEntry, (CFMutableDictionaryRef *)&parentProps,
                                          kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
    {
        // can't get parent props, give up
        CFRelease(parentProps);
        IOObjectRelease(diskEntry);
        IOObjectRelease(parentEntry);
        return -1;
    }

    if (IORegistryEntryCreateCFProperties(parentEntry, (CFMutableDictionaryRef *)&diskProps,
                                          kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess)
    {
        // can't get disk props, give up
        CFRelease(parentProps);
        CFRelease(diskProps);
        IOObjectRelease(diskEntry);
        IOObjectRelease(parentEntry);
        return -1;
    }

    // Start fetching
    CFStringRef cfDiskName = (CFStringRef)CFDictionaryGetValue(parentProps, CFSTR(kIOBSDNameKey));
    CFStringGetCString(cfDiskName, info->DiskName, MAX_DISK_NAME, CFStringGetSystemEncoding());
    stats = (CFDictionaryRef)CFDictionaryGetValue( diskProps, CFSTR(kIOBlockStorageDriverStatisticsKey));

    if (stats == NULL) {
        // stat fetch failed...
        CFRelease(parentProps);
        CFRelease(diskProps);
        IOObjectRelease(parentEntry);
        IOObjectRelease(diskEntry);
        return -1;
    }

    CFNumberRef cfnum;

    if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsReadsKey)))) {
        CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->Reads);
    } else {
        info->Reads = 0;
    }

    if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsWritesKey)))) {
        CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->Writes);
    } else {
        info->Writes = 0;
    }

    if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey)))) {
        CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->ReadBytes);
    } else {
        info->ReadBytes = 0;
    }

    if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey)))) {
        CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->WriteBytes);
    } else {
        info->WriteBytes = 0;
    }

    if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsTotalReadTimeKey)))) {
        CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->ReadTime);
    } else {
        info->ReadTime = 0;
    }
    if ((cfnum = (CFNumberRef)CFDictionaryGetValue(stats, CFSTR(kIOBlockStorageDriverStatisticsTotalWriteTimeKey)))) {
        CFNumberGetValue(cfnum, kCFNumberSInt64Type, &info->WriteTime);
    } else {
        info->WriteTime = 0;
    }

    // note: read/write time are in ns, but we want ms.
    info->ReadTime = info->ReadTime / 1000 / 1000;
    info->WriteTime = info->WriteTime / 1000 / 1000;

    CFRelease(parentProps);
    CFRelease(diskProps);
    IOObjectRelease(parentEntry);
    IOObjectRelease(diskEntry);
    return 0;
}