Skip to content

Commit 367264e

Browse files
committed
Disk: only detect folders that specified by --disk-folders
Can be used to improve performance by ignoring network disks
1 parent 36a0c27 commit 367264e

File tree

6 files changed

+62
-65
lines changed

6 files changed

+62
-65
lines changed

src/detection/disk/disk.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,29 @@
11
#include "disk.h"
22

3-
const char* ffDetectDisksImpl(FFlist* disks);
3+
bool ffDiskMatchMountpoint(FFDiskOptions* options, const char* mountpoint)
4+
{
5+
#ifdef _WIN32
6+
const char separator = ';';
7+
#else
8+
const char separator = ':';
9+
#endif
10+
11+
uint32_t mountpointLength = (uint32_t) strlen(mountpoint);
12+
13+
uint32_t startIndex = 0;
14+
while(startIndex < options->folders.length)
15+
{
16+
uint32_t colonIndex = ffStrbufNextIndexC(&options->folders, startIndex, separator);
17+
18+
uint32_t folderLength = colonIndex - startIndex;
19+
if (folderLength == mountpointLength && memcmp(options->folders.chars + startIndex, mountpoint, mountpointLength) == 0)
20+
return true;
21+
22+
startIndex = colonIndex + 1;
23+
}
24+
25+
return false;
26+
}
427

528
static int compareDisks(const FFDisk* disk1, const FFDisk* disk2)
629
{
@@ -9,7 +32,7 @@ static int compareDisks(const FFDisk* disk1, const FFDisk* disk2)
932

1033
const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks)
1134
{
12-
const char* error = ffDetectDisksImpl(disks);
35+
const char* error = ffDetectDisksImpl(options, disks);
1336

1437
if (error) return error;
1538
if (disks->length == 0) return "No disks found";

src/detection/disk/disk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ typedef struct FFDisk
2626
* If error is not set, disks contains at least one disk.
2727
*/
2828
const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks /* list of FFDisk */);
29+
30+
const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks);
31+
bool ffDiskMatchMountpoint(FFDiskOptions* options, const char* mountpoint);

src/detection/disk/disk_bsd.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void detectFsInfo(struct statfs* fs, FFDisk* disk)
9090
}
9191
#endif
9292

93-
const char* ffDetectDisksImpl(FFlist* disks)
93+
const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
9494
{
9595
int size = getfsstat(NULL, 0, MNT_WAIT);
9696

@@ -103,7 +103,12 @@ const char* ffDetectDisksImpl(FFlist* disks)
103103

104104
for(struct statfs* fs = buf; fs < buf + size; ++fs)
105105
{
106-
if(!ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs"))
106+
if(__builtin_expect(options->folders.length, 0))
107+
{
108+
if(!ffDiskMatchMountpoint(options, fs->f_mntonname))
109+
continue;
110+
}
111+
else if(!ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs"))
107112
continue;
108113

109114
#ifdef __FreeBSD__

src/detection/disk/disk_linux.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ static void detectStats(FFDisk* disk)
250250
#endif
251251
}
252252

253-
const char* ffDetectDisksImpl(FFlist* disks)
253+
const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
254254
{
255255
FILE* mountsFile = setmntent("/proc/mounts", "r");
256256
if(mountsFile == NULL)
@@ -260,7 +260,12 @@ const char* ffDetectDisksImpl(FFlist* disks)
260260

261261
while((device = getmntent(mountsFile)))
262262
{
263-
if(!isPhysicalDevice(device))
263+
if (__builtin_expect(options->folders.length, 0))
264+
{
265+
if (!ffDiskMatchMountpoint(options, device->mnt_dir))
266+
continue;
267+
}
268+
else if(!isPhysicalDevice(device))
264269
continue;
265270

266271
//We have a valid device, add it to the list

src/detection/disk/disk_windows.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@
66
#include <winioctl.h>
77
#include <assert.h>
88

9-
const char* ffDetectDisksImpl(FFlist* disks)
9+
const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
1010
{
1111
wchar_t buf[MAX_PATH + 1];
1212
uint32_t length = GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf);
1313
if (length == 0 || length >= sizeof(buf) / sizeof(*buf))
1414
return "GetLogicalDriveStringsW(sizeof(buf) / sizeof(*buf), buf) failed";
1515

16+
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate();
17+
1618
for(uint32_t i = 0; i < length; i++)
1719
{
1820
wchar_t* mountpoint = buf + i;
1921

22+
ffStrbufSetWS(&buffer, mountpoint);
23+
i += buffer.length;
24+
2025
UINT driveType = GetDriveTypeW(mountpoint);
21-
if(driveType == DRIVE_NO_ROOT_DIR)
26+
27+
if (__builtin_expect((long) options->folders.length, 0))
2228
{
23-
i += (uint32_t)wcslen(mountpoint);
24-
continue;
29+
if (!ffDiskMatchMountpoint(options, buffer.chars))
30+
continue;
2531
}
32+
else if(driveType == DRIVE_NO_ROOT_DIR)
33+
continue;
2634

2735
FFDisk* disk = ffListAdd(disks);
2836

@@ -77,7 +85,7 @@ const char* ffDetectDisksImpl(FFlist* disks)
7785
else
7886
disk->createTime = 0;
7987

80-
ffStrbufInitWS(&disk->mountpoint, mountpoint);
88+
ffStrbufInitMove(&disk->mountpoint, &buffer);
8189
if (mountpoint[2] == L'\\' && mountpoint[3] == L'\0')
8290
{
8391
wchar_t volumeName[MAX_PATH + 1];
@@ -91,8 +99,6 @@ const char* ffDetectDisksImpl(FFlist* disks)
9199
//Unsupported
92100
disk->filesUsed = 0;
93101
disk->filesTotal = 0;
94-
95-
i += disk->mountpoint.length;
96102
}
97103

98104
return NULL;

src/modules/disk/disk.c

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -131,54 +131,6 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk)
131131
}
132132
}
133133

134-
static void printMountpoint(FFDiskOptions* options, const FFlist* disks, const char* mountpoint)
135-
{
136-
FF_LIST_FOR_EACH(FFDisk, disk, *disks)
137-
{
138-
if(ffStrbufEqualS(&disk->mountpoint, mountpoint))
139-
{
140-
printDisk(options, disk);
141-
return;
142-
}
143-
}
144-
145-
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No disk found for mountpoint: %s", mountpoint);
146-
}
147-
148-
static void printMountpoints(FFDiskOptions* options, const FFlist* disks)
149-
{
150-
#ifdef _WIN32
151-
const char separator = ';';
152-
#else
153-
const char separator = ':';
154-
#endif
155-
156-
FF_STRBUF_AUTO_DESTROY mountpoints = ffStrbufCreateCopy(&options->folders);
157-
ffStrbufTrim(&mountpoints, separator);
158-
159-
uint32_t startIndex = 0;
160-
while(startIndex < mountpoints.length)
161-
{
162-
uint32_t colonIndex = ffStrbufNextIndexC(&mountpoints, startIndex, separator);
163-
mountpoints.chars[colonIndex] = '\0';
164-
165-
printMountpoint(options, disks, mountpoints.chars + startIndex);
166-
167-
startIndex = colonIndex + 1;
168-
}
169-
}
170-
171-
static void printAutodetected(FFDiskOptions* options, const FFlist* disks)
172-
{
173-
FF_LIST_FOR_EACH(FFDisk, disk, *disks)
174-
{
175-
if(disk->type & ~options->showTypes)
176-
continue;
177-
178-
printDisk(options, disk);
179-
}
180-
}
181-
182134
void ffPrintDisk(FFDiskOptions* options)
183135
{
184136
FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk));
@@ -190,10 +142,13 @@ void ffPrintDisk(FFDiskOptions* options)
190142
}
191143
else
192144
{
193-
if(options->folders.length == 0)
194-
printAutodetected(options, &disks);
195-
else
196-
printMountpoints(options, &disks);
145+
FF_LIST_FOR_EACH(FFDisk, disk, disks)
146+
{
147+
if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes))
148+
continue;
149+
150+
printDisk(options, disk);
151+
}
197152
}
198153

199154
FF_LIST_FOR_EACH(FFDisk, disk, disks)

0 commit comments

Comments
 (0)