Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Exception Handling to File Name for Telemetry Caching #1267

Merged
merged 9 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 29 additions & 15 deletions Library/FileSystemHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as fs from "fs";
import path = require("path");
import { promisify } from "util";
import Logging = require("./Logging");

export const statAsync = promisify(fs.stat);
export const lstatAsync = promisify(fs.lstat);
Expand Down Expand Up @@ -39,15 +40,19 @@ export const confirmDirExists = async (directory: string): Promise<void> => {
* Computes the size (in bytes) of all files in a directory at the root level. Asynchronously.
*/
export const getShallowDirectorySize = async (directory: string): Promise<number> => {
// Get the directory listing
const files = await readdirAsync(directory);
let totalSize = 0;
// Query all file sizes
for (const file of files) {
const fileStats = await statAsync(path.join(directory, file));
if (fileStats.isFile()) {
totalSize += fileStats.size;
try {
// Get the directory listing
const files = await readdirAsync(directory);
// Query all file sizes
for (const file of files) {
const fileStats = await statAsync(path.join(directory, file));
if (fileStats.isFile()) {
totalSize += fileStats.size;
}
}
} catch {
Logging.warn(`Failed to get directory size for ${directory}`);
}
return totalSize;
JacksonWeber marked this conversation as resolved.
Show resolved Hide resolved
};
Expand All @@ -56,21 +61,30 @@ export const getShallowDirectorySize = async (directory: string): Promise<number
* Computes the size (in bytes) of all files in a directory at the root level. Synchronously.
*/
export const getShallowDirectorySizeSync = (directory: string): number => {
let files = fs.readdirSync(directory);
let totalSize = 0;
for (let i = 0; i < files.length; i++) {
totalSize += fs.statSync(path.join(directory, files[i])).size;
try {
let files = fs.readdirSync(directory);
for (let i = 0; i < files.length; i++) {
totalSize += fs.statSync(path.join(directory, files[i])).size;
}
} catch {
Logging.warn(`Failed to get directory size synchronously for ${directory}`)
}
return totalSize;
JacksonWeber marked this conversation as resolved.
Show resolved Hide resolved
return totalSize
}

/**
* Computes the size (in bytes) of a file asynchronously.
* Computes the size (in bytes) of a file asynchronously. Returns -1 if the file does not exist.
*/
export const getShallowFileSize = async (filePath: string): Promise<number> => {
const fileStats = await statAsync(filePath);
if (fileStats.isFile()) {
return fileStats.size;
try {
const fileStats = await statAsync(filePath);
if (fileStats.isFile()) {
return fileStats.size;
}
} catch {
Logging.warn(`Failed to get file size for ${filePath}`);
return -1;
}
JacksonWeber marked this conversation as resolved.
Show resolved Hide resolved
}

4 changes: 2 additions & 2 deletions Library/Sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class Sender {
try {
//create file - file name for now is the timestamp, a better approach would be a UUID but that
//would require an external dependency
var fileName = new Date().getTime() + ".ai.json";
var fileName = `${new Date().getTime()}.ai.json`;
var fileFullPath = path.join(this._tempDir, fileName);

// Mode 600 is w/r for creator and no read access for others (only applies on *nix)
Expand Down Expand Up @@ -465,7 +465,7 @@ class Sender {

//create file - file name for now is the timestamp, a better approach would be a UUID but that
//would require an external dependency
var fileName = new Date().getTime() + ".ai.json";
var fileName = `${new Date().getTime()}.ai.json`;
var fileFullPath = path.join(this._tempDir, fileName);

// Mode 600 is w/r for creator and no access for anyone else (only applies on *nix)
Expand Down
1 change: 0 additions & 1 deletion Tests/EndToEnd.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { JsonConfig } from "../Library/JsonConfig";
import { FileAccessControl } from "../Library/FileAccessControl";
import FileSystemHelper = require("../Library/FileSystemHelper");
import AutoCollectHttpRequests = require("../AutoCollection/HttpRequests");

/**
* A fake response class that passes by default
*/
Expand Down
Loading
Loading