aboutsummaryrefslogtreecommitdiff
path: root/lfn.h
diff options
context:
space:
mode:
authorJavier <dev.git@javispedro.com>2026-11-22 19:02:35 +0100
committerJavier <dev.git@javispedro.com>2026-11-22 19:02:35 +0100
commitbc437f9709a7a48865bae2a4aa40536bfc80ebbc (patch)
tree94f152361f4ab8e4964419a350100c71e737c9ad /lfn.h
parent186879bf4f702beaa687037197aec39715454ede (diff)
downloadvbados-bc437f9709a7a48865bae2a4aa40536bfc80ebbc.tar.gz
vbados-bc437f9709a7a48865bae2a4aa40536bfc80ebbc.zip
vbsf: when resolving lfn, list only files starting with the same prefix as the short filenameHEADmaster
Diffstat (limited to 'lfn.h')
-rw-r--r--lfn.h82
1 files changed, 39 insertions, 43 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);
}
}