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

refactor: use type enum in file stats for asar archive #39969

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 18 additions & 26 deletions lib/asar/fs-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Buffer } from 'buffer';
import { constants } from 'fs';
import * as path from 'path';
import * as util from 'util';
import type * as Crypto from 'crypto';
Expand Down Expand Up @@ -67,18 +68,22 @@ const gid = process.getgid?.() ?? 0;

const fakeTime = new Date();

const asarStatsToFsStats = function (stats: NodeJS.AsarFileStat) {
const { Stats, constants } = require('fs');
enum AsarFileType {
kFile = (constants as any).UV_DIRENT_FILE,
kDirectory = (constants as any).UV_DIRENT_DIR,
kLink = (constants as any).UV_DIRENT_LINK,
}

let mode = constants.S_IROTH ^ constants.S_IRGRP ^ constants.S_IRUSR ^ constants.S_IWUSR;
const fileTypeToMode = new Map<AsarFileType, number>([
[AsarFileType.kFile, constants.S_IFREG],
[AsarFileType.kDirectory, constants.S_IFDIR],
[AsarFileType.kLink, constants.S_IFLNK]
]);

if (stats.isFile) {
mode ^= constants.S_IFREG;
} else if (stats.isDirectory) {
mode ^= constants.S_IFDIR;
} else if (stats.isLink) {
mode ^= constants.S_IFLNK;
}
const asarStatsToFsStats = function (stats: NodeJS.AsarFileStat) {
const { Stats } = require('fs');

const mode = constants.S_IROTH | constants.S_IRGRP | constants.S_IRUSR | constants.S_IWUSR | fileTypeToMode.get(stats.type)!;

return new Stats(
1, // dev
Expand Down Expand Up @@ -241,7 +246,6 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
const logASARAccess = (asarPath: string, filePath: string, offset: number) => {
if (!process.env.ELECTRON_LOG_ASAR_READS) return;
if (!logFDs.has(asarPath)) {
const path = require('path');
const logFilename = `${path.basename(asarPath, '.asar')}-access-log.txt`;
const logPath = path.join(require('os').tmpdir(), logFilename);
logFDs.set(asarPath, fs.openSync(logPath, 'a'));
Expand Down Expand Up @@ -657,13 +661,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
nextTick(callback!, [error]);
return;
}
if (stats.isFile) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_FILE));
} else if (stats.isDirectory) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_DIR));
} else if (stats.isLink) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_LINK));
}
dirents.push(new fs.Dirent(file, stats.type));
}
nextTick(callback!, [null, dirents]);
return;
Expand Down Expand Up @@ -700,13 +698,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
if (!stats) {
throw createError(AsarError.NOT_FOUND, { asarPath, filePath: childPath });
}
if (stats.isFile) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_FILE));
} else if (stats.isDirectory) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_DIR));
} else if (stats.isLink) {
dirents.push(new fs.Dirent(file, fs.constants.UV_DIRENT_LINK));
}
dirents.push(new fs.Dirent(file, stats.type));
}
return dirents;
}
Expand Down Expand Up @@ -757,7 +749,7 @@ export const wrapFsWithAsar = (fs: Record<string, any>) => {
const stats = archive.stat(filePath);
if (!stats) return -34;

return (stats.isDirectory) ? 1 : 0;
return (stats.type === AsarFileType.kDirectory) ? 1 : 0;
};

// Calling mkdir for directory inside asar archive should throw ENOTDIR
Expand Down
4 changes: 1 addition & 3 deletions shell/common/api/electron_api_asar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ class Archive : public node::ObjectWrap {
gin_helper::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("size", stats.size);
dict.Set("offset", stats.offset);
dict.Set("isFile", stats.is_file);
dict.Set("isDirectory", stats.is_directory);
dict.Set("isLink", stats.is_link);
dict.Set("type", static_cast<int>(stats.type));
args.GetReturnValue().Set(dict.GetHandle());
}

Expand Down
6 changes: 2 additions & 4 deletions shell/common/asar/archive.cc
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,12 @@ bool Archive::Stat(const base::FilePath& path, Stats* stats) const {
return false;

if (node->Find("link")) {
stats->is_file = false;
stats->is_link = true;
stats->type = FileType::kLink;
return true;
}

if (node->Find("files")) {
stats->is_file = false;
stats->is_directory = true;
stats->type = FileType::kDirectory;
return true;
}

Expand Down
13 changes: 9 additions & 4 deletions shell/common/asar/archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <unordered_map>
#include <vector>

#include <uv.h>

#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/synchronization/lock.h"
Expand Down Expand Up @@ -49,11 +51,14 @@ class Archive {
absl::optional<IntegrityPayload> integrity;
};

enum class FileType {
kFile = UV_DIRENT_FILE,
kDirectory = UV_DIRENT_DIR,
kLink = UV_DIRENT_LINK,
};

struct Stats : public FileInfo {
Stats() : is_file(true), is_directory(false), is_link(false) {}
bool is_file;
bool is_directory;
bool is_link;
FileType type = FileType::kFile;
};

explicit Archive(const base::FilePath& path);
Expand Down
4 changes: 1 addition & 3 deletions typings/internal-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ declare namespace NodeJS {
type AsarFileStat = {
size: number;
offset: number;
isFile: boolean;
isDirectory: boolean;
isLink: boolean;
type: number;
}

interface AsarArchive {
Expand Down