Skip to content

Commit ec4dd08

Browse files
committed
refactor(plugin): use native recursive Windows directory creation
1 parent f87db38 commit ec4dd08

4 files changed

Lines changed: 13 additions & 16 deletions

File tree

‎plugins/codex-security/native/proof-windows-wide.mts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ function worker(root: string): Record<string, boolean> {
159159
error: 3,
160160
value: [],
161161
});
162+
assert.deepEqual(native.windowsAbsolutePath(Buffer.alloc(0)), emptyAbsolute);
162163
const notDirectory = native.windowsDirectoryEntries(widePath(names[0]!));
163164
assert.notEqual(notDirectory.error, 0);
164165
assert(Number.isInteger(notDirectory.error));
@@ -251,6 +252,9 @@ function worker(root: string): Record<string, boolean> {
251252
files.mkdir(widePath(longDirectory));
252253
files.mkdir(widePath(longDirectory));
253254
assert(files.stat(widePath(longDirectory)).isDirectory());
255+
assert.throws(() => files.mkdir(Buffer.alloc(0)));
256+
assert.throws(() => files.mkdir(widePath(names[0]!)));
257+
assert.throws(() => files.mkdir(widePath(`${names[0]}\\child`)));
254258
const longFile = widePath(win32.join(longDirectory, "file-\udc80"));
255259
files.writeFile(longFile, Buffer.from("long raw path"));
256260
const longLength = files.readInto(longFile, contents);

‎plugins/codex-security/native/src/windows.rs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,10 @@ pub fn windows_environment(name: Buffer) -> napi::Result<Option<Buffer>> {
116116
pub fn windows_absolute_path(path: Buffer) -> napi::Result<BufferResult> {
117117
let path = os_string(path)?;
118118
if path.is_empty() {
119-
// Rust rejects empty paths before Win32; retain the native error contract.
119+
// The public Rust path API rejects empty input before reaching Win32.
120120
let mut value = [0_u16; 256];
121121
let error = unsafe {
122+
SetLastError(0);
122123
GetFullPathNameW([0_u16].as_ptr(), 256, value.as_mut_ptr(), null_mut());
123124
GetLastError()
124125
};
@@ -284,6 +285,11 @@ pub fn create_windows_directory(path: Buffer) -> napi::Result<u32> {
284285
Ok(status(unsafe { CreateDirectoryW(path.as_ptr(), null()) }))
285286
}
286287

288+
#[napi]
289+
pub fn create_windows_directories(path: Buffer) -> napi::Result<u32> {
290+
Ok(io_status(fs::create_dir_all(os_string(path)?)))
291+
}
292+
287293
#[napi]
288294
impl WindowsHandle {
289295
#[napi]

‎plugins/codex-security/native/windows-binding.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export interface WindowsBinding {
4444
flags: number,
4545
): { error: number; handle?: WindowsHandle | null };
4646
createWindowsDirectory(path: Buffer): number;
47+
createWindowsDirectories(path: Buffer): number;
4748
}
4849

4950
export const windowsFlags = {

‎plugins/codex-security/native/windows-files.mts‎

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,21 +143,7 @@ export function windowsFileSystem(native: WindowsBinding) {
143143
}
144144

145145
function mkdir(path: Buffer): void {
146-
const resolved = absolute(path);
147-
const parent = widePath(win32.dirname(pathText(resolved)));
148-
let error = native.createWindowsDirectory(operationPath(resolved));
149-
if (error === 3 && !parent.equals(resolved)) {
150-
mkdir(parent);
151-
error = native.createWindowsDirectory(operationPath(resolved));
152-
}
153-
if (error !== 0) {
154-
try {
155-
if (stat(resolved).isDirectory()) return;
156-
} catch {
157-
// Report the original creation error.
158-
}
159-
check(error, path);
160-
}
146+
check(native.createWindowsDirectories(operationPath(path)), path);
161147
}
162148

163149
function readInto(path: Buffer, buffer: Buffer): number {

0 commit comments

Comments
 (0)