Skip to content

Commit

Permalink
Code changes to support FreeBSD
Browse files Browse the repository at this point in the history
* Code changes to support FreeBSD
  • Loading branch information
abraunegg committed Dec 16, 2024
1 parent 71a71da commit 039abb6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
54 changes: 50 additions & 4 deletions src/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -867,11 +867,9 @@ int main(string[] cliArgs) {
performFileSystemMonitoring = true;

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

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

// Retrieves the maximum number of open files allowed by the system
string getMaxOpenFiles() {
version (Linux) {
try {
// Read max open files from procfs on Linux
return strip(readText("/proc/sys/fs/file-max"));
} catch (Exception e) {
return "Unknown (Error reading /proc/sys/fs/file-max)";
}
} else version (FreeBSD) {
try {
// Read max open files using sysctl on FreeBSD
return strip(executeShell("sysctl -n kern.maxfiles").output);

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

maxfiles is not a recognized word. (unrecognized-spelling)
} catch (Exception e) {
return "Unknown (sysctl error)";
}
} else version (OpenBSD) {
try {
// Read max open files using sysctl on OpenBSD
return strip(executeShell("sysctl -n kern.maxfiles").output);

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

maxfiles is not a recognized word. (unrecognized-spelling)
} catch (Exception e) {
return "Unknown (sysctl error)";
}
} else {
return "Unsupported platform";
}
}

// Retrieves the maximum inotify watches allowed (Linux) or a placeholder for other platforms
string getMaxInotifyWatches() {
version (Linux) {
try {
// Read max inotify watches from procfs on Linux
return strip(readText("/proc/sys/fs/inotify/max_user_watches"));
} catch (Exception e) {
return "Unknown (Error reading /proc/sys/fs/inotify/max_user_watches)";
}
} else version (FreeBSD) {
// FreeBSD uses kqueue instead of inotify, no direct equivalent
return "N/A (uses kqueue)";
} else version (OpenBSD) {
// OpenBSD uses kqueue instead of inotify, no direct equivalent
return "N/A (uses kqueue)";
} else {
return "Unsupported platform";
}
}

// Print error message when --sync or --monitor has not been used and no valid 'no-sync' operation was requested
void printMissingOperationalSwitchesError() {
// notify the user that --sync or --monitor were missing
Expand Down
20 changes: 13 additions & 7 deletions src/monitor.d
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,19 @@ class MonitorBackgroundWorker {
int wd = inotify_add_watch(fd, toStringz(pathname), mask);
if (wd < 0) {
if (errno() == ENOSPC) {
// Get the current value
ulong maxInotifyWatches = to!int(strip(readText("/proc/sys/fs/inotify/max_user_watches")));
addLogEntry("The user limit on the total number of inotify watches has been reached.");
addLogEntry("Your current limit of inotify watches is: " ~ to!string(maxInotifyWatches));
addLogEntry("It is recommended that you change the max number of inotify watches to at least double your existing value.");
addLogEntry("To change the current max number of watches to " ~ to!string((maxInotifyWatches * 2)) ~ " run:");
addLogEntry("EXAMPLE: sudo sysctl fs.inotify.max_user_watches=" ~ to!string((maxInotifyWatches * 2)));
version (Linux) {
// Read max inotify watches from procfs on Linux
ulong maxInotifyWatches = to!int(strip(readText("/proc/sys/fs/inotify/max_user_watches")));
addLogEntry("The user limit on the total number of inotify watches has been reached.");
addLogEntry("Your current limit of inotify watches is: " ~ to!string(maxInotifyWatches));
addLogEntry("It is recommended that you change the max number of inotify watches to at least double your existing value.");
addLogEntry("To change the current max number of watches to " ~ to!string((maxInotifyWatches * 2)) ~ " run:");
addLogEntry("EXAMPLE: sudo sysctl fs.inotify.max_user_watches=" ~ to!string((maxInotifyWatches * 2)));
} else {
// some other platform
addLogEntry("The user limit on the total number of inotify watches has been reached.");
addLogEntry("Please seek support from your distribution on how to increase the max number of inotify watches to at least double your existing value.");
}
}
if (errno() == 13) {
if (verboseLogging) {addLogEntry("WARNING: inotify_add_watch failed - permission denied: " ~ pathname, ["verbose"]);}
Expand Down

1 comment on commit 039abb6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (1)

maxfiles

To accept these unrecognized words as correct, you could run the following commands

... in a clone of the [email protected]:abraunegg/onedrive.git repository
on the support-freebsd branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/main/apply.pl' |
perl - 'https://github.com/abraunegg/onedrive/actions/runs/12359375418/attempts/1'

Please sign in to comment.