Adding zstd_decompress to the filestore

This commit is contained in:
Дмитрий Ценеков 2025-11-15 19:56:24 +03:00 committed by Fabian
parent c380cabc4c
commit 56b8c85e3f
3 changed files with 19 additions and 9 deletions

View file

@ -1068,7 +1068,7 @@ FS.prototype.get_buffer = async function(idx)
else if(inode.status === STATUS_ON_STORAGE) else if(inode.status === STATUS_ON_STORAGE)
{ {
dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum"); dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum");
return await this.storage.read(inode.sha256sum, 0, inode.size); return await this.storage.read(inode.sha256sum, 0, inode.size, inode.size);
} }
else else
{ {
@ -1095,7 +1095,7 @@ FS.prototype.get_data = async function(idx, offset, count)
else if(inode.status === STATUS_ON_STORAGE) else if(inode.status === STATUS_ON_STORAGE)
{ {
dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum"); dbg_assert(inode.sha256sum, "Filesystem get_data: found inode on server without sha256sum");
return await this.storage.read(inode.sha256sum, offset, count); return await this.storage.read(inode.sha256sum, offset, count, inode.size);
} }
else else
{ {

View file

@ -9,9 +9,10 @@ export function FileStorageInterface() {}
* @param {string} sha256sum * @param {string} sha256sum
* @param {number} offset * @param {number} offset
* @param {number} count * @param {number} count
* @param {number} file_size
* @return {!Promise<Uint8Array>} null if file does not exist. * @return {!Promise<Uint8Array>} null if file does not exist.
*/ */
FileStorageInterface.prototype.read = function(sha256sum, offset, count) {}; FileStorageInterface.prototype.read = function(sha256sum, offset, count, file_size) {};
/** /**
* Add a read-only file to the filestorage. * Add a read-only file to the filestorage.
@ -83,8 +84,9 @@ MemoryFileStorage.prototype.uncache = function(sha256sum)
* @implements {FileStorageInterface} * @implements {FileStorageInterface}
* @param {FileStorageInterface} file_storage * @param {FileStorageInterface} file_storage
* @param {string} baseurl * @param {string} baseurl
* @param {function(number,Uint8Array):ArrayBuffer} zstd_decompress
*/ */
export function ServerFileStorageWrapper(file_storage, baseurl) export function ServerFileStorageWrapper(file_storage, baseurl, zstd_decompress)
{ {
dbg_assert(baseurl, "ServerMemoryFileStorage: baseurl should not be empty"); dbg_assert(baseurl, "ServerMemoryFileStorage: baseurl should not be empty");
@ -95,19 +97,26 @@ export function ServerFileStorageWrapper(file_storage, baseurl)
this.storage = file_storage; this.storage = file_storage;
this.baseurl = baseurl; this.baseurl = baseurl;
this.zstd_decompress = zstd_decompress;
} }
/** /**
* @param {string} sha256sum * @param {string} sha256sum
* @param {number} file_size
* @return {!Promise<Uint8Array>} * @return {!Promise<Uint8Array>}
*/ */
ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum) ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum, file_size)
{ {
return new Promise((resolve, reject) => return new Promise((resolve, reject) =>
{ {
load_file(this.baseurl + sha256sum, { done: async buffer => load_file(this.baseurl + sha256sum, { done: async buffer =>
{ {
const data = new Uint8Array(buffer); let data = new Uint8Array(buffer);
if (sha256sum.endsWith('.zst')) {
data = new Uint8Array(
this.zstd_decompress(file_size, data)
);
}
await this.cache(sha256sum, data); await this.cache(sha256sum, data);
resolve(data); resolve(data);
}}); }});
@ -118,14 +127,15 @@ ServerFileStorageWrapper.prototype.load_from_server = function(sha256sum)
* @param {string} sha256sum * @param {string} sha256sum
* @param {number} offset * @param {number} offset
* @param {number} count * @param {number} count
* @param {number} file_size
* @return {!Promise<Uint8Array>} * @return {!Promise<Uint8Array>}
*/ */
ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count) ServerFileStorageWrapper.prototype.read = async function(sha256sum, offset, count, file_size)
{ {
const data = await this.storage.read(sha256sum, offset, count); const data = await this.storage.read(sha256sum, offset, count);
if(!data) if(!data)
{ {
const full_file = await this.load_from_server(sha256sum); const full_file = await this.load_from_server(sha256sum, file_size);
return full_file.subarray(offset, offset + count); return full_file.subarray(offset, offset + count);
} }
return data; return data;

View file

@ -436,7 +436,7 @@ V86.prototype.continue_init = async function(emulator, options)
if(base_url) if(base_url)
{ {
file_storage = new ServerFileStorageWrapper(file_storage, base_url); file_storage = new ServerFileStorageWrapper(file_storage, base_url, this.zstd_decompress);
} }
settings.fs9p = this.fs9p = new FS(file_storage); settings.fs9p = this.fs9p = new FS(file_storage);