Skip to content

Commit 039abb6

Browse files
committed
Code changes to support FreeBSD
* Code changes to support FreeBSD
1 parent 71a71da commit 039abb6

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

src/main.d

+50-4
Original file line numberDiff line numberDiff line change
@@ -867,11 +867,9 @@ int main(string[] cliArgs) {
867867
performFileSystemMonitoring = true;
868868

869869
// What are the current values for the platform we are running on
870-
// Max number of open files /proc/sys/fs/file-max
871-
string maxOpenFiles = strip(readText("/proc/sys/fs/file-max"));
870+
string maxOpenFiles = strip(getMaxOpenFiles());
872871
// What is the currently configured maximum inotify watches that can be used
873-
// /proc/sys/fs/inotify/max_user_watches
874-
string maxInotifyWatches = strip(readText("/proc/sys/fs/inotify/max_user_watches"));
872+
string maxInotifyWatches = strip(getMaxInotifyWatches());
875873

876874
// Start the monitor process
877875
addLogEntry("OneDrive synchronisation interval (seconds): " ~ to!string(appConfig.getValueLong("monitor_interval")));
@@ -1238,6 +1236,54 @@ int main(string[] cliArgs) {
12381236
}
12391237
}
12401238

1239+
// Retrieves the maximum number of open files allowed by the system
1240+
string getMaxOpenFiles() {
1241+
version (Linux) {
1242+
try {
1243+
// Read max open files from procfs on Linux
1244+
return strip(readText("/proc/sys/fs/file-max"));
1245+
} catch (Exception e) {
1246+
return "Unknown (Error reading /proc/sys/fs/file-max)";
1247+
}
1248+
} else version (FreeBSD) {
1249+
try {
1250+
// Read max open files using sysctl on FreeBSD
1251+
return strip(executeShell("sysctl -n kern.maxfiles").output);
1252+
} catch (Exception e) {
1253+
return "Unknown (sysctl error)";
1254+
}
1255+
} else version (OpenBSD) {
1256+
try {
1257+
// Read max open files using sysctl on OpenBSD
1258+
return strip(executeShell("sysctl -n kern.maxfiles").output);
1259+
} catch (Exception e) {
1260+
return "Unknown (sysctl error)";
1261+
}
1262+
} else {
1263+
return "Unsupported platform";
1264+
}
1265+
}
1266+
1267+
// Retrieves the maximum inotify watches allowed (Linux) or a placeholder for other platforms
1268+
string getMaxInotifyWatches() {
1269+
version (Linux) {
1270+
try {
1271+
// Read max inotify watches from procfs on Linux
1272+
return strip(readText("/proc/sys/fs/inotify/max_user_watches"));
1273+
} catch (Exception e) {
1274+
return "Unknown (Error reading /proc/sys/fs/inotify/max_user_watches)";
1275+
}
1276+
} else version (FreeBSD) {
1277+
// FreeBSD uses kqueue instead of inotify, no direct equivalent
1278+
return "N/A (uses kqueue)";
1279+
} else version (OpenBSD) {
1280+
// OpenBSD uses kqueue instead of inotify, no direct equivalent
1281+
return "N/A (uses kqueue)";
1282+
} else {
1283+
return "Unsupported platform";
1284+
}
1285+
}
1286+
12411287
// Print error message when --sync or --monitor has not been used and no valid 'no-sync' operation was requested
12421288
void printMissingOperationalSwitchesError() {
12431289
// notify the user that --sync or --monitor were missing

src/monitor.d

+13-7
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,19 @@ class MonitorBackgroundWorker {
5757
int wd = inotify_add_watch(fd, toStringz(pathname), mask);
5858
if (wd < 0) {
5959
if (errno() == ENOSPC) {
60-
// Get the current value
61-
ulong maxInotifyWatches = to!int(strip(readText("/proc/sys/fs/inotify/max_user_watches")));
62-
addLogEntry("The user limit on the total number of inotify watches has been reached.");
63-
addLogEntry("Your current limit of inotify watches is: " ~ to!string(maxInotifyWatches));
64-
addLogEntry("It is recommended that you change the max number of inotify watches to at least double your existing value.");
65-
addLogEntry("To change the current max number of watches to " ~ to!string((maxInotifyWatches * 2)) ~ " run:");
66-
addLogEntry("EXAMPLE: sudo sysctl fs.inotify.max_user_watches=" ~ to!string((maxInotifyWatches * 2)));
60+
version (Linux) {
61+
// Read max inotify watches from procfs on Linux
62+
ulong maxInotifyWatches = to!int(strip(readText("/proc/sys/fs/inotify/max_user_watches")));
63+
addLogEntry("The user limit on the total number of inotify watches has been reached.");
64+
addLogEntry("Your current limit of inotify watches is: " ~ to!string(maxInotifyWatches));
65+
addLogEntry("It is recommended that you change the max number of inotify watches to at least double your existing value.");
66+
addLogEntry("To change the current max number of watches to " ~ to!string((maxInotifyWatches * 2)) ~ " run:");
67+
addLogEntry("EXAMPLE: sudo sysctl fs.inotify.max_user_watches=" ~ to!string((maxInotifyWatches * 2)));
68+
} else {
69+
// some other platform
70+
addLogEntry("The user limit on the total number of inotify watches has been reached.");
71+
addLogEntry("Please seek support from your distribution on how to increase the max number of inotify watches to at least double your existing value.");
72+
}
6773
}
6874
if (errno() == 13) {
6975
if (verboseLogging) {addLogEntry("WARNING: inotify_add_watch failed - permission denied: " ~ pathname, ["verbose"]);}

0 commit comments

Comments
 (0)