Skip to content

Commit

Permalink
Fix: only query disk info at startup (#879)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Jan 5, 2024
1 parent 639d30f commit 5f9b96e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/driveSemaphore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class DriveSemaphore {
files: K[],
runnable: (file: K) => (V | Promise<V>),
): Promise<V[]> {
const disks = await FsPoly.disks();
const disks = FsPoly.disksSync();

// Limit the number of ongoing threads to something reasonable
return async.mapLimit(
Expand Down Expand Up @@ -75,10 +75,10 @@ export default class DriveSemaphore {
}
}

const keySemaphore = await this.keySemaphoresMutex.runExclusive(async () => {
const keySemaphore = await this.keySemaphoresMutex.runExclusive(() => {
if (!this.keySemaphores.has(filePathDisk)) {
let { threads } = this;
if (await FsPoly.isSamba(filePathDisk)) {
if (FsPoly.isSamba(filePathDisk)) {
// Forcefully limit the number of files to be processed concurrently from a single
// Samba network share
threads = 1;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fileIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class FileIndexer extends Module {
});

const outputDir = path.resolve(this.options.getOutputDirRoot());
const outputDirDisk = (await FsPoly.disks()).find((mount) => outputDir.startsWith(mount));
const outputDirDisk = FsPoly.disksSync().find((mount) => outputDir.startsWith(mount));

// Sort the file arrays
[...results.entries()]
Expand Down
23 changes: 7 additions & 16 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class FsPoly {
static readonly FILE_READING_CHUNK_SIZE = 1024 * 1024; // 1MiB

// Assume that all drives we're reading from or writing to were already mounted at startup
public static readonly DRIVE_MOUNTS = FsPoly.disksSync();
public static readonly DRIVES = nodeDiskInfo.getDiskInfoSync();

static async canSymlink(tempDir: string): Promise<boolean> {
const source = await this.mktemp(path.join(tempDir, 'source'));
Expand Down Expand Up @@ -56,17 +56,8 @@ export default class FsPoly {
}
}

static async disks(): Promise<string[]> {
const disks = await nodeDiskInfo.getDiskInfo();
return disks
.filter((drive) => drive.available > 0)
.map((drive) => drive.mounted)
// Sort by mount points with the deepest number of subdirectories first
.sort((a, b) => b.split(/[\\/]/).length - a.split(/[\\/]/).length);
}

static disksSync(): string[] {
return nodeDiskInfo.getDiskInfoSync()
return FsPoly.DRIVES
.filter((drive) => drive.available > 0)
.map((drive) => drive.mounted)
// Sort by mount points with the deepest number of subdirectories first
Expand Down Expand Up @@ -97,15 +88,14 @@ export default class FsPoly {
}
}

static async isSamba(filePath: string): Promise<boolean> {
static isSamba(filePath: string): boolean {
const normalizedPath = filePath.replace(/[\\/]/g, path.sep);
if (normalizedPath.startsWith(`${path.sep}${path.sep}`) && normalizedPath !== os.devNull) {
return true;
}

const resolvedPath = path.resolve(normalizedPath);
const drives = await nodeDiskInfo.getDiskInfo();
const filePathDrive = drives
const filePathDrive = this.DRIVES
// Sort by mount points with the deepest number of subdirectories first
.sort((a, b) => b.mounted.split(/[\\/]/).length - a.mounted.split(/[\\/]/).length)
.find((drive) => resolvedPath.startsWith(drive.mounted));
Expand Down Expand Up @@ -245,8 +235,9 @@ export default class FsPoly {
if (path.dirname(oneResolved) === path.dirname(twoResolved)) {
return false;
}
return FsPoly.DRIVE_MOUNTS.find((mount) => oneResolved.startsWith(mount))
!== FsPoly.DRIVE_MOUNTS.find((mount) => twoResolved.startsWith(mount));
const driveMounts = this.disksSync();
return driveMounts.find((mount) => oneResolved.startsWith(mount))
!== driveMounts.find((mount) => twoResolved.startsWith(mount));
}

static async readlink(pathLike: PathLike): Promise<string> {
Expand Down
8 changes: 4 additions & 4 deletions test/polyfill/fsPoly.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ describe('isSamba', () => {
os.devNull,
'test',
path.resolve('test'),
])('should return false: %s', async (filePath) => {
await expect(fsPoly.isSamba(filePath)).resolves.toEqual(false);
])('should return false: %s', (filePath) => {
expect(fsPoly.isSamba(filePath)).toEqual(false);
});

test.each([
'//foo/bar',
'\\\\foo\\bar',
])('should return true: %s', async (filePath) => {
await expect(fsPoly.isSamba(filePath)).resolves.toEqual(true);
])('should return true: %s', (filePath) => {
expect(fsPoly.isSamba(filePath)).toEqual(true);
});
});

Expand Down

0 comments on commit 5f9b96e

Please sign in to comment.