From bc437f9709a7a48865bae2a4aa40536bfc80ebbc Mon Sep 17 00:00:00 2001 From: Javier Date: Sun, 22 Nov 2026 19:02:35 +0100 Subject: vbsf: when resolving lfn, list only files starting with the same prefix as the short filename --- lfn.h | 82 ++++++++++++++++++++++++++++++-------------------------------- sftsr.c | 5 ++-- vboxshfl.h | 24 +++++++++++++++++- 3 files changed, 64 insertions(+), 47 deletions(-) diff --git a/lfn.h b/lfn.h index 94c8c1a..3f25330 100644 --- a/lfn.h +++ b/lfn.h @@ -31,8 +31,9 @@ static inline bool translate_filename_from_host(SHFLSTRING *, bool, bool); static bool matches_8_3_wildcard(const char __far *, const char __far *); static int my_strrchr(const char __far *, char); -/** Private buffer for resolving VirtualBox long filenames. */ -static SHFLSTRING_WITH_BUF(shflstrlfn, SHFL_MAX_LEN); +/** Private buffer that we use for calling VirtualBox to list candidate files, + * when resolving long filenames. */ +static SHFLDIRINFO_WITH_NAME_BUF(lfndirinfo, SHFL_MAX_LEN); static char fcb_name[12]; @@ -251,6 +252,8 @@ static inline bool match_to_8_3_filename(const char __far *dos_filename, const c return false; } +/** Given path and (short) filename, returns the corresponding matching host filename. + * It will do this by listing all possibly matching filenames in the host and comparing hashes. */ static inline char *find_real_name( TSRDATAPTR data, int drive, @@ -263,83 +266,76 @@ static inline char *find_real_name( { SHFLROOT root = data->drives[drive].root; uint8_t hash_chars = data->drives[drive].opt.hash_chars; + SHFLSTRING *shflstr = &lfndirinfo.dirinfo.name; // reuse the lfndirinfo string buffer vboxerr err; dprintf("find_real_name path=%Fs filename=%Fs\n", path, filename); memset(&parms.create, 0, sizeof(SHFLCREATEPARMS)); parms.create.CreateFlags = SHFL_CF_DIRECTORY | SHFL_CF_ACT_OPEN_IF_EXISTS | SHFL_CF_ACT_FAIL_IF_NEW | SHFL_CF_ACCESS_READ; - shflstring_strcpy(&shflstrlfn.shflstr, path); + shflstring_strcpy(shflstr, path); - err = vbox_shfl_open(&data->vb, data->hgcm_client_id, root, &shflstrlfn.shflstr, &parms.create); + err = vbox_shfl_open(&data->vb, data->hgcm_client_id, root, shflstr, &parms.create); if (err) { dputs("open search dir failed"); goto not_found; } - // Check there is room for appending "\*" to the path - if (shflstrlfn.shflstr.u16Size < shflstrlfn.shflstr.u16Length + 2) - { - dputs("path too long"); - vbox_shfl_close(&data->vb, data->hgcm_client_id, root, parms.create.Handle); - goto not_found; - } - - *(uint16_t *)&shflstrlfn.shflstr.ach[shflstrlfn.shflstr.u16Length] = '*\\'; - shflstrlfn.shflstr.u16Length += 2; - shflstrlfn.shflstr.ach[shflstrlfn.shflstr.u16Length] = '\0'; + // Build the search pattern. We use the full path (already in shflstr and '/' terminated) + // + "prefix*" where prefix is the part of the filename before the hash (i.e. xxxx~nnn.txt) + // This prevents at least enumerating the entire list of files each time, + // instead we only enumerate files starting with the prefix. + shflstring_strncat(shflstr, filename, 8 - 1 - hash_chars); + shflstring_append(shflstr, '*'); + dprintf("find_real_name pattern=%s\n", shflstr->ach); - for (;;) - { - unsigned size = sizeof(shfldirinfo), resume = 0, count = 0; + while (1) { + unsigned size = sizeof(lfndirinfo), resume = 0, count = 0; char *d; uint32_t hash; - bool valid; err = vbox_shfl_list(&data->vb, data->hgcm_client_id, root, parms.create.Handle, - SHFL_LIST_RETURN_ONE, &size, &shflstrlfn.shflstr, &shfldirinfo.dirinfo, + SHFL_LIST_RETURN_ONE, &size, shflstr, &lfndirinfo.dirinfo, &resume, &count); - // Reset the size of the buffer - shfldirinfo.dirinfo.name.u16Size = sizeof(shfldirinfo.buf); + // shflstr not needed anymore, and besides it is now used as lfndirinfo + shflstr = NULL; + // Reset the size of the buffer (see find_next_from_vbox) + lfndirinfo.dirinfo.name.u16Size = sizeof(lfndirinfo.buf); - if (err) - { + if (err) { dputs("vbox_shfl_list() failed"); vbox_shfl_close(&data->vb, data->hgcm_client_id, root, parms.create.Handle); break; } - // Calculate hash using host file name - hash = lfn_name_hash(shfldirinfo.dirinfo.name.ach, shfldirinfo.dirinfo.name.u16Length, hash_chars); - - // Copy now, because translate_filename_from_host() converts fName to DOS codepage - // - if (shfldirinfo.dirinfo.name.u16Length > bufsiz) - { + // Store the long file name now, because we are about to mangle it into a short one + if (lfndirinfo.dirinfo.name.u16Length > bufsiz) { vbox_shfl_close(&data->vb, data->hgcm_client_id, root, parms.create.Handle); - return (char *)0; + return NULL; } - d = _fstrcpy_local(dest, &shfldirinfo.dirinfo.name.ach); + d = _fstrcpy_local(dest, &lfndirinfo.dirinfo.name.ach); - translate_filename_from_host(&shfldirinfo.dirinfo.name, false, true); - mangle_to_8_3_filename(hash, hash_chars, fcb_name, &shfldirinfo.dirinfo.name); + // Calculate the hash of this host file name + hash = lfn_name_hash(lfndirinfo.dirinfo.name.ach, lfndirinfo.dirinfo.name.u16Length, hash_chars); - if (match_to_8_3_filename(filename, fcb_name)) - { + // Now convert long file name into a short one + translate_filename_from_host(&lfndirinfo.dirinfo.name, false, true); + mangle_to_8_3_filename(hash, hash_chars, fcb_name, &lfndirinfo.dirinfo.name); + + // And check if it matches with the one we were searching for + if (match_to_8_3_filename(filename, fcb_name)) { vbox_shfl_close(&data->vb, data->hgcm_client_id, root, parms.create.Handle); return d; } } not_found: - if (namelen > bufsiz) - { - return (char *)0; - } - else - { + // Matching host long file name not found: pass the short filename back unchanged + if (namelen > bufsiz) { + return NULL; + } else { return _fstrcpy_local(dest, filename); } } diff --git a/sftsr.c b/sftsr.c index 0e9915f..6db709e 100644 --- a/sftsr.c +++ b/sftsr.c @@ -1040,9 +1040,8 @@ static vboxerr find_next_from_vbox(unsigned openfile, char __far *path) } } else { drive = sdb->drive & DOS_SDB_DRIVE_MASK; - // For find next calls, it's not really important what we pass here, - // as long as it's not empty. - shflstring_strcpy(&shflstr.shflstr, " "); + // For find next calls, it's not really important what we pass here. + shflstring_clear(&shflstr.shflstr); } diff --git a/vboxshfl.h b/vboxshfl.h index 412709a..276add4 100644 --- a/vboxshfl.h +++ b/vboxshfl.h @@ -43,18 +43,21 @@ static inline unsigned shflstring_size_with_buf(const SHFLSTRING *str) static inline unsigned shflstring_size_optional_in(const SHFLSTRING *str) { - if (str->u16Length > 0) { + if (str && str->u16Length > 0) { return sizeof(SHFLSTRING) + str->u16Size; } else { return 0; } } +/** Set str to the empty string. */ static inline void shflstring_clear(SHFLSTRING *str) { str->u16Length = 0; + str->ach[str->u16Length] = '\0'; } +/** Initialize str by copying src. */ static void shflstring_strcpy(SHFLSTRING *str, const char __far *src) { str->u16Length = MIN(_fstrlen(src), str->u16Size - 1); @@ -62,6 +65,7 @@ static void shflstring_strcpy(SHFLSTRING *str, const char __far *src) str->ach[str->u16Length] = '\0'; } +/** Initialize str by copying len first characters of src. */ static void shflstring_strncpy(SHFLSTRING *str, const char __far *src, unsigned len) { str->u16Length = MIN(len, str->u16Size - 1); @@ -69,6 +73,24 @@ static void shflstring_strncpy(SHFLSTRING *str, const char __far *src, unsigned str->ach[str->u16Length] = '\0'; } +/** Append single character to str (if space) */ +static void shflstring_append(SHFLSTRING *str, char c) +{ + if (str->u16Length < str->u16Size - 1) { + str->ach[str->u16Length++] = c; + str->ach[str->u16Length] = '\0'; + } +} + +/** Append len characters of src to str. */ +static void shflstring_strncat(SHFLSTRING *str, const char __far *src, unsigned len) +{ + len = MIN(len, (str->u16Size - 1) - str->u16Length); + _fmemcpy(&str->ach[str->u16Length], src, len); + str->u16Length += len; + str->ach[str->u16Length] = '\0'; +} + static inline void vbox_hgcm_set_parameter_shflroot(VMMDevHGCMCall __far *req, unsigned arg, SHFLROOT root) { vbox_hgcm_set_parameter_uint32(req, arg, root); -- cgit v1.2.3