Skip to content

Commit 74c1cc6

Browse files
authored
Merge pull request #708 from wassup05/disks
fix: sidebar disk listing
2 parents af76087 + c0c8255 commit 74c1cc6

File tree

2 files changed

+97
-11
lines changed

2 files changed

+97
-11
lines changed

src/internal/function.go

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"path/filepath"
1111
"regexp"
12+
"runtime"
1213
"slices"
1314
"sort"
1415
"strconv"
@@ -21,18 +22,87 @@ import (
2122
)
2223

2324
// Check if the directory is external disk path
25+
// Todo : This function should be give two directories, and it should return
26+
// if the two share a different disk partition.
27+
// Ideally we shouldn't even try to figure that out in our file operations, and let OS handles it.
28+
// But at least right now its not okay. This returns if `path` is an External disk
29+
// from perspective of `/`, but it should tell from perspective of currently open directory
30+
// The usage of this function in cut/paste is not as expected.
2431
func isExternalDiskPath(path string) bool {
25-
dir := filepath.Dir(path)
32+
// This is very vague. You cannot tell if a path is belonging to an external partition
33+
// if you dont define the source path to compare with
34+
// But making this true will cause slow file operations based on current implementation
35+
if runtime.GOOS == "windows" {
36+
return false
37+
}
38+
39+
// exclude timemachine on MacOS
40+
if strings.HasPrefix(path, "/Volumes/.timemachine") {
41+
return false
42+
}
43+
44+
// to filter out mounted partitions like /, /boot etc
45+
return strings.HasPrefix(path, "/mnt") ||
46+
strings.HasPrefix(path, "/media") ||
47+
strings.HasPrefix(path, "/run/media") ||
48+
strings.HasPrefix(path, "/Volumes")
49+
}
50+
51+
func shouldListDisk(mountPoint string) bool {
52+
if runtime.GOOS == "windows" {
53+
// We need to get C:, D: drive etc in the list
54+
return true
55+
}
2656

27-
// exclude timemachine
28-
if strings.HasPrefix(dir, "/Volumes/.timemachine") {
57+
// Should always list the main disk
58+
if mountPoint == "/" {
59+
return true
60+
}
61+
62+
// Todo : make a configurable field in config.yaml
63+
// excluded_disk_mounts = ["/Volumes/.timemachine"]
64+
// Mountpoints that are in subdirectory of disk_mounts
65+
// but still are to be excluded in disk section of sidebar
66+
if strings.HasPrefix(mountPoint, "/Volumes/.timemachine") {
2967
return false
3068
}
3169

32-
return strings.HasPrefix(dir, "/mnt") ||
33-
strings.HasPrefix(dir, "/media") ||
34-
strings.HasPrefix(dir, "/run/media") ||
35-
strings.HasPrefix(dir, "/Volumes")
70+
// We avoid listing all mounted partitions (Otherwise listed disk could get huge)
71+
// but only a few partitions that usually corresponds to external physical devices
72+
// For example : mounts like /boot, /var/ will get skipped
73+
// This can be inaccurate based on your system setup if you mount any external devices
74+
// on other directories, or if you have some extra mounts on these directories
75+
// Todo : make a configurable field in config.yaml
76+
// disk_mounts = ["/mnt", "/media", "/run/media", "/Volumes"]
77+
// Only block devicies that are mounted on these or any subdirectory of these Mountpoints
78+
// Will be shown in disk sidebar
79+
return strings.HasPrefix(mountPoint, "/mnt") ||
80+
strings.HasPrefix(mountPoint, "/media") ||
81+
strings.HasPrefix(mountPoint, "/run/media") ||
82+
strings.HasPrefix(mountPoint, "/Volumes")
83+
}
84+
85+
func diskName(mountPoint string) string {
86+
// In windows we dont want to use filepath.Base as it returns "\" for when
87+
// mountPoint is any drive root "C:", "D:", etc. Hence causing same name
88+
// for each drive
89+
if runtime.GOOS == "windows" {
90+
return mountPoint
91+
}
92+
93+
// This might cause duplicate names in case you mount two devices in
94+
// /mnt/usb and /mnt/dir2/usb . Full mountpoint is a more accurate way
95+
// but that results in messy UI, hence we do this.
96+
return filepath.Base(mountPoint)
97+
}
98+
99+
func diskLocation(mountPoint string) string {
100+
// In windows if you are in "C:\some\path", "cd C:" will not cd to root of C: drive
101+
// but "cd C:\" will
102+
if runtime.GOOS == "windows" {
103+
return filepath.Join(mountPoint, "\\")
104+
}
105+
return mountPoint
36106
}
37107

38108
func returnFocusType(focusPanel focusPanelType) filePanelFocusType {

src/internal/get_data.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,33 @@ func getPinnedDirectories() []directory {
8282

8383
// Get external media directories
8484
func getExternalMediaFolders() (disks []directory) {
85-
parts, err := disk.Partitions(true)
85+
// only get physical drives
86+
parts, err := disk.Partitions(false)
87+
88+
slog.Debug("disk.Partitions() called", "number of parts", len(parts))
8689

8790
if err != nil {
8891
slog.Error("Error while getting external media: ", "error", err)
8992
return disks
9093
}
94+
9195
for _, disk := range parts {
92-
if isExternalDiskPath(disk.Mountpoint) {
96+
// Not removing this for now, as it is helpful if we need to debug
97+
// any user issues related disk listing in sidebar.
98+
// Todo : We need to evaluate if more debug logs are a performance problem
99+
// even when user had set debug=false in config. We dont write those to log file
100+
// But we do make a functions call, and pass around some strings. So it might/might not be
101+
// a problem. It could be a problem in a hot path though.
102+
slog.Debug("Returned disk by disk.Partition()", "device", disk.Device,
103+
"mountpoint", disk.Mountpoint, "fstype", disk.Fstype)
104+
105+
// shouldListDisk, diskName, and diskLocation, each has runtime.GOOS checks
106+
// We can ideally reduce it to one check only.
107+
if shouldListDisk(disk.Mountpoint) {
108+
93109
disks = append(disks, directory{
94-
name: filepath.Base(disk.Mountpoint),
95-
location: disk.Mountpoint,
110+
name: diskName(disk.Mountpoint),
111+
location: diskLocation(disk.Mountpoint),
96112
})
97113
}
98114
}

0 commit comments

Comments
 (0)