Skip to content

Commit

Permalink
Adds is_empty to the File System Object (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaiah Becker-Mayer authored Sep 1, 2022
1 parent 524c867 commit b887b09
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 3 additions & 0 deletions web/packages/teleport/src/lib/tdp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ export default class Client extends EventEmitterWebAuthnSender {
lastModified: BigInt(0),
fileType: FileType.File,
size: BigInt(0),
isEmpty: true,
path: path,
},
});
Expand Down Expand Up @@ -306,6 +307,7 @@ export default class Client extends EventEmitterWebAuthnSender {
lastModified: BigInt(0),
fileType: FileType.File,
size: BigInt(0),
isEmpty: true,
path: req.path,
},
});
Expand Down Expand Up @@ -411,6 +413,7 @@ export default class Client extends EventEmitterWebAuthnSender {
lastModified: BigInt(info.lastModified),
fileType: info.kind === 'file' ? FileType.File : FileType.Directory,
size: BigInt(info.size),
isEmpty: info.isEmpty,
path: info.path,
};
}
Expand Down
10 changes: 7 additions & 3 deletions web/packages/teleport/src/lib/tdp/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,12 @@ export type SharedDirectoryListResponse = {
fsoList: FileSystemObject[];
};

// | last_modified uint64 | size uint64 | file_type uint32 | path_length uint32 | path byte[] |
// | last_modified uint64 | size uint64 | file_type uint32 | is_empty bool | path_length uint32 | path byte[] |
export type FileSystemObject = {
lastModified: bigint;
size: bigint;
fileType: FileType;
isEmpty: boolean;
path: string;
};

Expand Down Expand Up @@ -739,11 +740,12 @@ export default class Codec {
return withFsoList.buffer;
}

// | last_modified uint64 | size uint64 | file_type uint32 | path_length uint32 | path byte[] |
// | last_modified uint64 | size uint64 | file_type uint32 | is_empty bool | path_length uint32 | path byte[] |
encodeFileSystemObject(fso: FileSystemObject): Message {
const dataUtf8array = this.encoder.encode(fso.path);

const bufLen = 2 * uint64Length + 2 * uint32Length + dataUtf8array.length;
const bufLen =
byteLength + 2 * uint64Length + 2 * uint32Length + dataUtf8array.length;
const buffer = new ArrayBuffer(bufLen);
const view = new DataView(buffer);
let offset = 0;
Expand All @@ -753,6 +755,8 @@ export default class Codec {
offset += uint64Length;
view.setUint32(offset, fso.fileType);
offset += uint32Length;
view.setUint8(offset, fso.isEmpty ? 1 : 0);
offset += byteLength;
view.setUint32(offset, dataUtf8array.length);
offset += uint32Length;
dataUtf8array.forEach(byte => {
Expand Down
21 changes: 20 additions & 1 deletion web/packages/teleport/src/lib/tdp/sharedDirectoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,35 @@ export class SharedDirectoryManager {

const fileOrDir = await this.walkPath(path);

let isEmpty = true;
if (fileOrDir.kind === 'directory') {
let dir = fileOrDir;
// If dir contains any files or directories, it will
// enter the loop below and we can register it as not
// empty. If it doesn't, it will skip over the loop.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for await (const _ of dir.keys()) {
isEmpty = false;
break;
}

// Magic numbers are the values for directories where the true
// value is unavailable, according to the TDP spec.
return { size: 4096, lastModified: 0, kind: fileOrDir.kind, path };
return {
size: 4096,
lastModified: 0,
kind: fileOrDir.kind,
isEmpty,
path,
};
}

let file = await fileOrDir.getFile();
return {
size: file.size,
lastModified: file.lastModified,
kind: fileOrDir.kind,
isEmpty,
path,
};
}
Expand Down Expand Up @@ -260,5 +278,6 @@ export type FileOrDirInfo = {
size: number; // bytes
lastModified: number; // ms since unix epoch
kind: 'file' | 'directory';
isEmpty: boolean;
path: string;
};

0 comments on commit b887b09

Please sign in to comment.