Skip to content
Merged
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
73 changes: 41 additions & 32 deletions harmony/pushy/src/main/ets/DownloadTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ export class DownloadTask {
}

if (!fileIo.accessSync(path)) {
await fileIo.mkdir(path);
try {
await fileIo.mkdir(path);
} catch (error) {
if (!fileIo.accessSync(path)) {
throw error;
}
}
Comment on lines +71 to +77
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Only ignore mkdir failures for existing directories.

Line 74 only checks existence. If a file appears at path, this catch suppresses the original mkdir failure and the caller fails later with a less actionable “not a directory” error. Re-stat the path before treating the exception as a benign race.

Suggested fix
       try {
         await fileIo.mkdir(path);
       } catch (error) {
         if (!fileIo.accessSync(path)) {
           throw error;
         }
+        try {
+          const stat = await fileIo.stat(path);
+          if (!stat.isDirectory()) {
+            throw error;
+          }
+        } catch {
+          throw error;
+        }
       }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@harmony/pushy/src/main/ets/DownloadTask.ts` around lines 71 - 77, The catch
block around fileIo.mkdir in DownloadTask.ts currently calls
fileIo.accessSync(path) without re-statting and may suppress real errors (e.g.,
a regular file at path); change the handler to re-stat the path inside the catch
(using the same fileIo or fs stat API) and only swallow the mkdir error if the
re-stat shows the path exists and is a directory; otherwise rethrow the original
error so callers see the real failure. Ensure you reference the same path
variable and keep fileIo.mkdir and fileIo.accessSync usages consistent.

}
}

Expand Down Expand Up @@ -99,22 +105,14 @@ export class DownloadTask {
}

private async listEntryNames(directory: string): Promise<string[]> {
const entryNames: string[] = [];
const files = await fileIo.listFile(directory);
const validFiles = files.filter(file => file !== '.' && file !== '..');

for (const file of files) {
if (file === '.' || file === '..') {
continue;
}

const filePath = `${directory}/${file}`;
const stat = await fileIo.stat(filePath);
if (!stat.isDirectory()) {
entryNames.push(file);
}
}
const stats = await Promise.all(
validFiles.map(file => fileIo.stat(`${directory}/${file}`)),
);

return entryNames;
return validFiles.filter((_, index) => !stats[index].isDirectory());
}

private async writeFileContent(
Expand Down Expand Up @@ -432,11 +430,10 @@ export class DownloadTask {
await this.recreateDirectory(params.unzipDirectory);

await zlib.decompressFile(params.targetFile, params.unzipDirectory);
const entryNames = await this.listEntryNames(params.unzipDirectory);
const manifestArrays = await this.readManifestArrays(
params.unzipDirectory,
true,
);
const [entryNames, manifestArrays] = await Promise.all([
this.listEntryNames(params.unzipDirectory),
this.readManifestArrays(params.unzipDirectory, true),
]);

NativePatchCore.buildArchivePatchPlan(
ARCHIVE_PATCH_TYPE_FROM_PACKAGE,
Expand Down Expand Up @@ -475,11 +472,10 @@ export class DownloadTask {
await this.recreateDirectory(params.unzipDirectory);

await zlib.decompressFile(params.targetFile, params.unzipDirectory);
const entryNames = await this.listEntryNames(params.unzipDirectory);
const manifestArrays = await this.readManifestArrays(
params.unzipDirectory,
false,
);
const [entryNames, manifestArrays] = await Promise.all([
this.listEntryNames(params.unzipDirectory),
this.readManifestArrays(params.unzipDirectory, false),
]);

const plan = NativePatchCore.buildArchivePatchPlan(
ARCHIVE_PATCH_TYPE_FROM_PPK,
Expand Down Expand Up @@ -524,23 +520,36 @@ export class DownloadTask {
.replace('resources/base/media/', '')
.split('.')[0];
const mediaBuffer = await resourceManager.getMediaByName(mediaName);
const [firstTarget, ...restTargets] = targets;
await this.writeFileContent(firstTarget, mediaBuffer.buffer);
for (const target of restTargets) {
await this.copySandboxFile(firstTarget, target);
const parentDirs = [
...new Set(
targets.map(t => t.substring(0, t.lastIndexOf('/'))).filter(Boolean),
),
];
for (const dir of parentDirs) {
await this.ensureDirectory(dir);
}
await Promise.all(
targets.map(target => this.writeFileContent(target, mediaBuffer.buffer)),
);
continue;
}
const fromContent = await resourceManager.getRawFd(currentFrom);
const [firstTarget, ...restTargets] = targets;
await this.ensureParentDirectory(firstTarget);
const parentDirs = [
...new Set(
targets.map(t => t.substring(0, t.lastIndexOf('/'))).filter(Boolean),
),
];
for (const dir of parentDirs) {
await this.ensureDirectory(dir);
}
if (fileIo.accessSync(firstTarget)) {
await fileIo.unlink(firstTarget);
}
saveFileToSandbox(fromContent, firstTarget);
for (const target of restTargets) {
await this.copySandboxFile(firstTarget, target);
}
await Promise.all(
restTargets.map(target => this.copySandboxFile(firstTarget, target)),
);
}
} catch (error) {
error.message =
Expand Down