storage: make listDir return paths relative to the root

this seems more natural to work with
This commit is contained in:
Girish Ramakrishnan
2025-08-16 07:43:43 +05:30
parent 19682ec21b
commit 832a25601d
5 changed files with 13 additions and 12 deletions
+3 -2
View File
@@ -154,7 +154,8 @@ async function listDir(config, remotePath, batchSize, marker) {
assert.strictEqual(typeof batchSize, 'number');
assert(typeof marker !== 'undefined');
const fullRemotePath = path.join(getRootPath(config), remotePath);
const rootPath = getRootPath(config);
const fullRemotePath = path.join(rootPath, remotePath);
const stack = marker ? marker.stack : [fullRemotePath];
const fileStream = marker ? marker.fileStream : [];
if (!marker) marker = { stack, fileStream };
@@ -170,7 +171,7 @@ async function listDir(config, remotePath, batchSize, marker) {
stack.push(fullEntryPath);
} else if (dirent.isFile()) { // does not include symlink
const stat = await fs.promises.lstat(fullEntryPath);
fileStream.push({ path: path.relative(fullRemotePath, fullEntryPath), size: stat.size });
fileStream.push({ path: path.relative(rootPath, fullEntryPath), size: stat.size });
}
}
+3 -3
View File
@@ -138,7 +138,7 @@ async function listDir(apiConfig, remotePath, batchSize, marker) {
const [files, nextQuery] = result;
if (files.length === 0) return { entries: [], marker: null }; // no more
const entries = files.map(function (f) { return { path: path.relative(fullRemotePath, f.name) }; });
const entries = files.map(function (f) { return { path: path.relative(apiConfig.prefix, f.name) }; });
return { entries, marker: nextQuery || null };
}
@@ -171,8 +171,8 @@ async function copy(apiConfig, fromPath, toPath, progressCallback) {
total += batch.entries.length;
progressCallback({ message: `Copying ${batch.entries.length} files from ${batch.entries[0].path} to ${batch.entries[batch.entries.length-1].path}. total: ${total}` });
await async.eachLimit(batch.entries, concurrency, async (entry) => {
const fullFromPath = path.join(apiConfig.prefix, fromPath, entry.path);
const fullToPath = path.join(apiConfig.prefix, toPath, entry.path);
const fullFromPath = path.join(apiConfig.prefix, entry.path);
const fullToPath = path.join(apiConfig.prefix, toPath, path.relative(fromPath, entry.path));
await copyFile(apiConfig, fullFromPath, fullToPath, progressCallback);
});
if (!batch.marker) break;
+1 -1
View File
@@ -101,7 +101,7 @@ async function listDir(apiConfig, dir, batchSize, marker) {
assert(typeof marker !== 'undefined');
// Result: array of { path, size }
// path is relative to the dir being listed
// path is relative to the prefix/root and not the _dir_ being listed
throw new BoxError(BoxError.NOT_IMPLEMENTED, 'listDir is not implemented');
}
+3 -3
View File
@@ -359,7 +359,7 @@ async function listDir(apiConfig, remotePath, batchSize, marker) {
const [error, listData] = await safe(s3.listObjectsV2(listParams));
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, `Error listing objects in ${fullRemotePath}. ${formatError(error)}`);
if (listData.KeyCount === 0 || listData.Contents.length === 0) return { entries: [], marker: null }; // no more
const entries = listData.Contents.map(function (c) { return { path: path.relative(fullRemotePath, c.Key), size: c.Size }; });
const entries = listData.Contents.map(function (c) { return { path: path.relative(apiConfig.prefix, c.Key), size: c.Size }; });
return { entries, marker: !listData.IsTruncated ? null : listData.NextContinuationToken };
}
@@ -487,8 +487,8 @@ async function copy(apiConfig, fromPath, toPath, progressCallback) {
total += batch.entries.length;
progressCallback({ message: `Copying files from ${total-batch.entries.length}-${total}` });
await async.eachLimit(batch.entries, concurrency, async (entry) => {
const fullFromPath = path.join(apiConfig.prefix, fromPath, entry.path);
const fullToPath = path.join(apiConfig.prefix, toPath, entry.path);
const fullFromPath = path.join(apiConfig.prefix, entry.path);
const fullToPath = path.join(apiConfig.prefix, toPath, path.relative(fromPath, entry.path));
await copyFile(apiConfig, fullFromPath, fullToPath, entry.size, progressCallback);
});
if (!batch.marker) break;