Skip to content

Commit

Permalink
improve read tracking -- add excludes
Browse files Browse the repository at this point in the history
  • Loading branch information
williamstein committed Nov 18, 2023
1 parent f2a884c commit 3df24b0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
18 changes: 14 additions & 4 deletions websocketfs/lib/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@ interface Options {
cacheLinkTimeout?: number;

// Read Tracking
// write out filenames of files that were explicitly read to this file
// separated by nulls (and it is null terminated). A file is added no more
// than once. Delete the file to reset things. The leading slash is
// removed from the filenames, so they are relative to the mount point.
// Write out filenames of files that were explicitly read to readTrackingFile
// terminated by NULL, like the --null option to tar expects:
// readTrackingFile
// A file is added no more than once until readTrackingFile is deleted. Delete
// readTrackingFile to reset the processs.
// The leading slash is removed from the filenames, so they are relative to
// the mount point.
readTrackingFile?: string;
// exclude given top level directories from tracking, e.g.,
// ['scratch', 'tmp']
// would exclude scratch and tmp. We also allow ['.*'] to mean "exclude
// all top level hidden directories".
readTrackingExclude?: string[];

// Metadata
// If the metadataFile file path is given, we poll it for modification every few seconds.
Expand Down Expand Up @@ -68,6 +76,7 @@ export default async function mount(
cacheDirTimeout,
cacheLinkTimeout,
readTrackingFile,
readTrackingExclude,
metadataFile,
hidePath,
} = opts;
Expand All @@ -79,6 +88,7 @@ export default async function mount(
cacheDirTimeout,
cacheLinkTimeout,
readTrackingFile,
readTrackingExclude,
metadataFile,
hidePath,
});
Expand Down
34 changes: 31 additions & 3 deletions websocketfs/lib/read-tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,21 @@ const log = debug("websocketfs:read-tracking");
export default class ReadTracking {
private readTrackingFile: string;
private history = new Set<string>();
private excludeHidden: boolean;
private excludePaths: string[];

constructor(readTrackingFile) {
constructor(readTrackingFile: string, readTrackingExclude: string[]) {
this.readTrackingFile = readTrackingFile;
this.excludeHidden = readTrackingExclude.includes(".*");
this.excludePaths = readTrackingExclude
.filter((pattern) => pattern != ".*")
.map((pattern) => {
if (!pattern.endsWith("/")) {
return pattern + "/";
} else {
return pattern;
}
});
this.init();
}

Expand All @@ -30,7 +42,11 @@ export default class ReadTracking {
};

trackRead = async (filename: string) => {
log(`fileWasRead`, { filename });
filename = filename.slice(1);
if (this.isExcluded(filename)) {
return;
}
log(`trackRead`, { filename });
try {
await stat(this.readTrackingFile);
} catch (_) {
Expand All @@ -40,7 +56,19 @@ export default class ReadTracking {
if (this.history.has(filename)) {
return;
}
await appendFile(this.readTrackingFile, `${filename.slice(1)}\0`);
await appendFile(this.readTrackingFile, `${filename}\0`);
this.history.add(filename);
};

private isExcluded = (filename: string) => {
if (this.excludeHidden && filename.startsWith(".")) {
return true;
}
for (const pattern of this.excludePaths) {
if (filename.startsWith(pattern)) {
return true;
}
}
return false;
};
}
7 changes: 6 additions & 1 deletion websocketfs/lib/sftp-fuse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface Options {
cacheDirTimeout?: number;
cacheLinkTimeout?: number;
readTrackingFile?: string;
readTrackingExclude?: string[];
metadataFile?: string;
// reconnect -- defaults to true; if true, automatically reconnects
// to server when connection breaks.
Expand Down Expand Up @@ -76,6 +77,7 @@ export default class SftpFuse {
cacheLinkTimeout,
reconnect = true,
readTrackingFile,
readTrackingExclude = [],
metadataFile,
hidePath,
} = options;
Expand Down Expand Up @@ -111,7 +113,10 @@ export default class SftpFuse {
});
}
if (readTrackingFile) {
this.readTracking = new ReadTracking(readTrackingFile);
this.readTracking = new ReadTracking(
readTrackingFile,
readTrackingExclude,
);
}
if (metadataFile) {
if (this.attrCache != null && this.dirCache != null && cacheTimeout) {
Expand Down
2 changes: 1 addition & 1 deletion websocketfs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "websocketfs",
"version": "0.16.0",
"version": "0.17.0",
"description": "Like sshfs, but over a WebSocket",
"main": "./dist/lib/index.js",
"scripts": {
Expand Down

0 comments on commit 3df24b0

Please sign in to comment.