Skip to content

Commit 645bf5c

Browse files
committed
Disk: moves disk hiding by folder and FS type to detection code
Don't probe if hided Fixes #2043
1 parent 7638e0e commit 645bf5c

File tree

8 files changed

+85
-45
lines changed

8 files changed

+85
-45
lines changed

src/detection/disk/disk.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks)
1010
const char* error = ffDetectDisksImpl(options, disks);
1111

1212
if (error) return error;
13-
if (disks->length == 0) return "No disks found";
13+
if (disks->length == 0) return NULL;
1414

1515
//We need to sort the disks, so that we can detect, which disk a path resides on
1616
// For example for /boot/efi/bootmgr we need to check /boot/efi before /boot
@@ -31,3 +31,27 @@ const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks)
3131

3232
return NULL;
3333
}
34+
35+
#ifndef _WIN32
36+
#include <fnmatch.h>
37+
38+
bool ffDiskMatchesFolderPatterns(FFstrbuf* folders, const char* path, char separator)
39+
{
40+
uint32_t startIndex = 0;
41+
while(startIndex < folders->length)
42+
{
43+
uint32_t sepIndex = ffStrbufNextIndexC(folders, startIndex, separator);
44+
45+
char savedSep = folders->chars[sepIndex]; // Can be '\0' if at end
46+
folders->chars[sepIndex] = '\0';
47+
48+
bool matched = fnmatch(&folders->chars[startIndex], path, 0) == 0;
49+
folders->chars[sepIndex] = savedSep;
50+
51+
if (matched) return true;
52+
53+
startIndex = sepIndex + 1;
54+
}
55+
return false;
56+
}
57+
#endif

src/detection/disk/disk.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,7 @@ typedef struct FFDisk
3535
const char* ffDetectDisks(FFDiskOptions* options, FFlist* disks /* list of FFDisk */);
3636

3737
const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks);
38+
39+
#ifndef _WIN32
40+
bool ffDiskMatchesFolderPatterns(FFstrbuf* folders, const char* path, char separator);
41+
#endif

src/detection/disk/disk_bsd.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
151151
else if(!ffStrEquals(fs->f_mntonname, "/") && !ffStrStartsWith(fs->f_mntfromname, "/dev/") && !ffStrEquals(fs->f_fstypename, "zfs") && !ffStrEquals(fs->f_fstypename, "fusefs.sshfs"))
152152
continue;
153153

154+
if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, fs->f_mntonname, FF_DISK_FOLDER_SEPARATOR))
155+
continue;
156+
157+
if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, fs->f_fstypename, ':'))
158+
continue;
159+
154160
#ifdef __FreeBSD__
155161
// f_bavail and f_ffree are signed on FreeBSD...
156162
if(fs->f_bavail < 0) fs->f_bavail = 0;

src/detection/disk/disk_haiku.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
2727
continue;
2828
}
2929

30+
if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, path.Path(), FF_DISK_FOLDER_SEPARATOR))
31+
continue;
32+
33+
if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, fs.fsh_name, ':'))
34+
continue;
35+
3036
FFDisk* disk = (FFDisk*) ffListAdd(disks);
3137

3238
disk->bytesTotal = (uint64_t)fs.total_blocks * (uint64_t) fs.block_size;

src/detection/disk/disk_linux.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
297297
else if(!isPhysicalDevice(device))
298298
continue;
299299

300+
if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, device->mnt_dir, FF_DISK_FOLDER_SEPARATOR))
301+
continue;
302+
303+
if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, device->mnt_type, ':'))
304+
continue;
305+
300306
//We have a valid device, add it to the list
301307
FFDisk* disk = ffListAdd(disks);
302308
disk->type = FF_DISK_VOLUME_TYPE_NONE;

src/detection/disk/disk_sunos.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
119119
else if(!isPhysicalDevice(&device))
120120
continue;
121121

122+
if (options->hideFolders.length && ffDiskMatchesFolderPatterns(&options->hideFolders, device.mnt_mountp, FF_DISK_FOLDER_SEPARATOR))
123+
continue;
124+
125+
if (options->hideFS.length && ffStrbufSeparatedContainS(&options->hideFS, device.mnt_fstype, ':'))
126+
continue;
127+
122128
//We have a valid device, add it to the list
123129
FFDisk* disk = ffListAdd(disks);
124130
disk->type = FF_DISK_VOLUME_TYPE_NONE;

src/detection/disk/disk_windows.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,29 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
5353
else if(driveType == DRIVE_NO_ROOT_DIR)
5454
continue;
5555

56+
if (options->hideFolders.length && ffStrbufSeparatedContain(&options->hideFolders, &buffer, FF_DISK_FOLDER_SEPARATOR))
57+
continue;
58+
59+
wchar_t diskName[MAX_PATH + 1], diskFileSystem[MAX_PATH + 1];
60+
61+
DWORD diskFlags;
62+
BOOL volumeInfoAvailable = GetVolumeInformationW(mountpoint,
63+
diskName, ARRAY_SIZE(diskName), //Volume name
64+
NULL, //Serial number
65+
NULL, //Max component length
66+
&diskFlags, //File system flags
67+
diskFileSystem, ARRAY_SIZE(diskFileSystem)
68+
);
69+
70+
FF_STRBUF_AUTO_DESTROY diskFileSystemBuf = ffStrbufCreate();
71+
72+
if (volumeInfoAvailable)
73+
{
74+
ffStrbufSetWS(&diskFileSystemBuf, diskFileSystem);
75+
if (options->hideFS.length && ffStrbufSeparatedContain(&options->hideFS, &diskFileSystemBuf, ':'))
76+
continue;
77+
}
78+
5679
FFDisk* disk = ffListAdd(disks);
5780

5881
disk->filesUsed = 0;
@@ -97,20 +120,9 @@ const char* ffDetectDisksImpl(FFDiskOptions* options, FFlist* disks)
97120
(PULARGE_INTEGER)&disk->bytesFree
98121
);
99122

100-
wchar_t diskName[MAX_PATH + 1], diskFileSystem[MAX_PATH + 1];
101-
102-
DWORD diskFlags;
103-
BOOL result = GetVolumeInformationW(mountpoint,
104-
diskName, ARRAY_SIZE(diskName), //Volume name
105-
NULL, //Serial number
106-
NULL, //Max component length
107-
&diskFlags, //File system flags
108-
diskFileSystem, ARRAY_SIZE(diskFileSystem)
109-
);
110-
111-
if(result)
123+
if(volumeInfoAvailable)
112124
{
113-
ffStrbufSetWS(&disk->filesystem, diskFileSystem);
125+
ffStrbufInitMove(&disk->filesystem, &diskFileSystemBuf);
114126
ffStrbufSetWS(&disk->name, diskName);
115127
if(diskFlags & FILE_READ_ONLY_VOLUME)
116128
disk->type |= FF_DISK_VOLUME_TYPE_READONLY_BIT;

src/modules/disk/disk.c

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -189,30 +189,6 @@ static void printDisk(FFDiskOptions* options, const FFDisk* disk, uint32_t index
189189
}
190190
}
191191

192-
static inline bool isMatchFolders(FFstrbuf* folders, const FFstrbuf* path, char separator)
193-
{
194-
#ifndef _WIN32
195-
uint32_t startIndex = 0;
196-
while(startIndex < folders->length)
197-
{
198-
uint32_t colonIndex = ffStrbufNextIndexC(folders, startIndex, separator);
199-
200-
char savedColon = folders->chars[colonIndex]; // Can be '\0' if at end
201-
folders->chars[colonIndex] = '\0';
202-
203-
bool matched = fnmatch(&folders->chars[startIndex], path->chars, 0) == 0;
204-
folders->chars[colonIndex] = savedColon;
205-
206-
if (matched) return true;
207-
208-
startIndex = colonIndex + 1;
209-
}
210-
return false;
211-
#else
212-
return ffStrbufSeparatedContain(folders, path, separator);
213-
#endif
214-
}
215-
216192
bool ffPrintDisk(FFDiskOptions* options)
217193
{
218194
FF_LIST_AUTO_DESTROY disks = ffListCreate(sizeof (FFDisk));
@@ -224,18 +200,18 @@ bool ffPrintDisk(FFDiskOptions* options)
224200
return false;
225201
}
226202

203+
if(disks.length == 0)
204+
{
205+
ffPrintError(FF_DISK_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No disks found");
206+
return false;
207+
}
208+
227209
uint32_t index = 0;
228210
FF_LIST_FOR_EACH(FFDisk, disk, disks)
229211
{
230212
if(__builtin_expect(options->folders.length == 0, 1) && (disk->type & ~options->showTypes))
231213
continue;
232214

233-
if (options->hideFolders.length && isMatchFolders(&options->hideFolders, &disk->mountpoint, FF_DISK_FOLDER_SEPARATOR))
234-
continue;
235-
236-
if (options->hideFS.length && ffStrbufSeparatedContain(&options->hideFS, &disk->filesystem, ':'))
237-
continue;
238-
239215
printDisk(options, disk, ++index);
240216
}
241217

@@ -407,7 +383,7 @@ bool ffGenerateDiskJsonResult(FFDiskOptions* options, yyjson_mut_doc* doc, yyjso
407383

408384
if(error)
409385
{
410-
yyjson_mut_obj_add_str(doc, module, "result", error);
386+
yyjson_mut_obj_add_str(doc, module, "error", error);
411387
return false;
412388
}
413389

0 commit comments

Comments
 (0)