Skip to content

Commit 8896efe

Browse files
authored
[fix]: fix edge case problem on Windows (if adapter calls 'readDir' on single file) (#2993)
* fix edge case problem on Windows (if adapter calls 'readDir' on single file) * add test and should fail * fix test * and now test should pass
1 parent 2ad6f24 commit 8896efe

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
## __WORK IN PROGRESS__
55
-->
66

7+
## __WORK IN PROGRESS__ - Lucy
8+
* (@foxriver76) fix edge case problem on Windows (if adapter calls `readDir` on single file)
9+
710
## 7.0.6 (2024-12-08) - Lucy
811
* (@foxriver76) fixed UI upgrade if admin is running on privileged port (<1024)
912

packages/controller/test/lib/testFiles.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { TestContext } from '../_Types.js';
2+
import { objectsUtils as utils } from '@iobroker/db-objects-redis';
23

34
export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, context: TestContext): void {
45
const testName = `${context.name} ${context.adapterShortName} files: `;
@@ -223,6 +224,14 @@ export function register(it: Mocha.TestFunction, expect: Chai.ExpectStatic, cont
223224
});
224225
});
225226

227+
it(`${testName}should respond with 'ERROR_NOT_FOUND' if calling readDir on a single file`, async () => {
228+
const objects = context.objects;
229+
const fileName = 'dir/notADir.txt';
230+
231+
await objects.writeFileAsync(testId, fileName, 'dataInFile');
232+
expect(objects.readDirAsync(testId, fileName)).to.be.eventually.rejectedWith(utils.ERRORS.ERROR_NOT_FOUND);
233+
});
234+
226235
it(`${testName}should read file and prevent path traversing`, done => {
227236
const objects = context.objects;
228237
objects.readFile(testId, '../../myFileA/abc1.txt', null, (err, data, _mimeType) => {

packages/db-objects-file/src/lib/objects/objectsInMemFileDB.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ export class ObjectsInMemoryFileDB extends InMemoryFileDB {
627627
}
628628

629629
const location = path.join(this.objectsDir, id, name);
630-
if (fs.existsSync(location)) {
630+
if (fs.existsSync(location) && fs.statSync(location).isDirectory()) {
631631
const dirFiles = fs.readdirSync(location);
632632
for (let i = 0; i < dirFiles.length; i++) {
633633
if (dirFiles[i] === '..' || dirFiles[i] === '.') {

packages/db-objects-jsonl/src/lib/objects/objectsInMemServerRedis.js

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -908,52 +908,53 @@ export class ObjectsInMemoryServer extends ObjectsInMemoryJsonlDB {
908908
return void handler.sendArray(responseId, isScan ? ['0', response] : response);
909909
}
910910
}
911+
911912
if (namespace === this.namespaceFile || namespace === this.namespaceObjects) {
913+
if (isMeta !== undefined) {
914+
// such a request should never happen
915+
return handler.sendArray(responseId, isScan ? ['0', []] : []); // send out file or full db response
916+
}
917+
912918
// Handle request to get meta data keys
913-
if (isMeta === undefined) {
914-
let res;
915-
try {
916-
res = this._readDir(id, name);
917-
if (!res || !res.length) {
918-
res = [
919-
{
920-
file: '_data.json',
921-
stats: {},
922-
isDir: false,
923-
virtualFile: true,
924-
notExists: true,
925-
},
926-
];
927-
}
928-
} catch (e) {
929-
if (!e.message.endsWith(utils.ERRORS.ERROR_NOT_FOUND)) {
930-
return void handler.sendError(responseId, new Error(`ERROR readDir id=${id}: ${e.message}`));
931-
}
932-
res = [];
919+
let res;
920+
try {
921+
res = this._readDir(id, name);
922+
if (!res || !res.length) {
923+
res = [
924+
{
925+
file: '_data.json',
926+
stats: {},
927+
isDir: false,
928+
virtualFile: true,
929+
notExists: true,
930+
},
931+
];
933932
}
934-
let baseName = name || '';
935-
if (baseName.length && !baseName.endsWith('/')) {
936-
baseName += '/';
933+
} catch (e) {
934+
if (!e.message.endsWith(utils.ERRORS.ERROR_NOT_FOUND)) {
935+
return void handler.sendError(responseId, new Error(`ERROR readDir id=${id}: ${e.message}`));
937936
}
938-
res.forEach(arr => {
939-
let entryId = id;
940-
if (arr.isDir) {
941-
if (entryId === '' || entryId === '*') {
942-
entryId = arr.file;
943-
arr.file = '_data.json'; // We return a "virtual file" to mark the directory as existing
944-
} else {
945-
arr.file += '/_data.json'; // We return a "virtual file" to mark the directory as existing
946-
}
947-
}
948-
// We need to simulate the Meta data here, so return both
949-
response.push(this.getFileId(entryId, baseName + arr.file, true));
950-
response.push(this.getFileId(entryId, baseName + arr.file, false));
951-
});
952-
handler.sendArray(responseId, isScan ? ['0', response] : response); // send out file or full db response
953-
} else {
954-
// such a request should never happen
955-
handler.sendArray(responseId, isScan ? ['0', []] : []); // send out file or full db response
937+
res = [];
956938
}
939+
let baseName = name || '';
940+
if (baseName.length && !baseName.endsWith('/')) {
941+
baseName += '/';
942+
}
943+
res.forEach(arr => {
944+
let entryId = id;
945+
if (arr.isDir) {
946+
if (entryId === '' || entryId === '*') {
947+
entryId = arr.file;
948+
arr.file = '_data.json'; // We return a "virtual file" to mark the directory as existing
949+
} else {
950+
arr.file += '/_data.json'; // We return a "virtual file" to mark the directory as existing
951+
}
952+
}
953+
// We need to simulate the Meta data here, so return both
954+
response.push(this.getFileId(entryId, baseName + arr.file, true));
955+
response.push(this.getFileId(entryId, baseName + arr.file, false));
956+
});
957+
handler.sendArray(responseId, isScan ? ['0', response] : response); // send out file or full db response
957958
} else if (namespace === this.namespaceSet) {
958959
handler.sendArray(responseId, isScan ? ['0', []] : []); // send out empty array, we have no sets
959960
} else {

0 commit comments

Comments
 (0)