[BugFix] Multiboot Database duplicate files

When the sd card is mounted into the root namespace the database
picks up files through both paths

previously we hid the mounted drive but this causes issues with users
databases when the drive letter changes

Adds a way to keep track of volumes mounted in the root namespace

Hides the enumerated volume in root

Database:
we can just parse the root directory ('/') and get to any mounted
volume but we can also enumerate a volume in the root directory
when this occurs it leads to multiple entries since the files can
be reached through multiple paths ex, /Foo could also be /SD1/Foo
Instead we will attempt to rewrite the root with any non-hidden volumes
failing that just leave the paths alone

Change-Id: I7bdba8cfaf63902d2a3852d28484bcf8ca317ebd
This commit is contained in:
William Wilgus 2024-03-21 18:27:23 -04:00
parent ae01ea7fd9
commit fdc3668a6a
11 changed files with 198 additions and 51 deletions

View file

@ -344,3 +344,9 @@ struct dirinfo dir_get_info(DIR *dirp, struct dirent *entry)
file_error:
return (struct dirinfo){ .attribute = 0 };
}
const char* root_realpath(void)
{
/* Native only, for APP and SIM see respective filesystem-.c files */
return root_get_realpath(); /* rb_namespace.c */
}

View file

@ -23,6 +23,7 @@
#include "fileobj_mgr.h"
#include "rb_namespace.h"
#include "file_internal.h"
#include <stdio.h> /*snprintf*/
/* Define LOGF_ENABLE to enable logf output in this file */
//#define LOGF_ENABLE
@ -82,39 +83,48 @@ static void unmount_item(int item)
set_root_item_state(item, 0);
}
static char *root_realpath_internal(void)
{
static char root_realpath[ROOT_MAX_REALPATH];
return root_realpath;
}
const char* root_get_realpath(void)
{
return root_realpath_internal();
}
/* mount the directory that enumerates into the root namespace */
int root_mount_path(const char *path, unsigned int flags)
{
const char *folder = NULL; /* is a folder enumerated in the root? */
#ifdef HAVE_MULTIVOLUME
int volume = path_strip_volume(path, NULL, false);
int volume = path_strip_volume(path, &folder, false);
if (volume == ROOT_VOLUME)
return -EINVAL;
if (!CHECK_VOL(volume))
return -ENOENT;
char volname[VOL_MAX_LEN+2];
make_volume_root(volume, volname);
#else
const char volname = PATH_ROOTSTR;
if (!path_is_absolute(path))
{
logf("Path not absolute %s", path);
return -ENOENT;
}
path_dirname(path, &folder);
#endif /* HAVE_MULTIVOLUME */
bool contents = flags & NSITEM_CONTENTS;
int item = contents ? ROOT_CONTENTS_INDEX : IF_MV_VOL(volume);
int item = IF_MV_VOL(volume);
unsigned int state = get_root_item_state(item);
logf("%s: item:%d, st:%u, %s", __func__, item, state, path);
if (state)
return -EBUSY;
if (contents)
if (contents && state) /* volume must be mounted to enumerate into the root namespace */
{
if (get_root_item_state(ROOT_CONTENTS_INDEX))
return -EBUSY; /* error something is already enumerated */
/* cache information about the target */
struct filestr_base stream;
struct path_component_info compinfo;
int e = errno;
int rc = open_stream_internal(path, FF_DIR | FF_PROBE | FF_INFO |
FF_DEVPATH, &stream, &compinfo);
@ -124,17 +134,41 @@ int root_mount_path(const char *path, unsigned int flags)
errno = e;
return rc;
}
if (!fileobj_mount(&compinfo.info, FO_DIRECTORY, &root_bindp))
return -EBUSY;
}
int root_state = NSITEM_MOUNTED | (flags & (NSITEM_HIDDEN|NSITEM_CONTENTS));
set_root_item_state(ROOT_CONTENTS_INDEX, root_state);
flags |= state; /* preserve the state of the mounted volume */
if (!folder)
{
folder = "";
}
else
{
/*if a folder has been enumerated don't mark the whole volume */
if (folder[0] != '\0' && folder[1] != '\0')
flags &= ~NSITEM_CONTENTS;
}
snprintf(root_realpath_internal(), ROOT_MAX_REALPATH,"%s%s", volname, folder);
}
else if (state) /* error volume already mounted */
return -EBUSY;
state = NSITEM_MOUNTED | (flags & (NSITEM_HIDDEN|NSITEM_CONTENTS));
set_root_item_state(item, state);
return 0;
}
/* check if volume in path is mounted in the root namespace */
bool ns_volume_is_visible(IF_MV_NONVOID(int volume))
{
int item = IF_MV_VOL(volume);
if ((item == ROOT_VOLUME) || !CHECK_VOL(item))
return false;
unsigned int state = get_root_item_state(item);
return state && (((state & NSITEM_HIDDEN) == 0) || (state & NSITEM_CONTENTS));
}
/* inform root that an entire volume is being unmounted */
void root_unmount_volume(IF_MV_NONVOID(int volume))
{
@ -154,7 +188,10 @@ void root_unmount_volume(IF_MV_NONVOID(int volume))
uint32_t state = get_root_item_state(ROOT_CONTENTS_INDEX);
if (state && (volume < 0 || BASEINFO_VOL(&root_bindp->info) == volume))
#endif
{
unmount_item(ROOT_CONTENTS_INDEX);
root_realpath_internal()[0] = '\0';
}
}
/* parse the root part of a path */
@ -268,8 +305,13 @@ int root_readdir_dirent(struct filestr_base *stream,
state = get_root_item_state(item);
if ((state & (NSITEM_MOUNTED|NSITEM_HIDDEN)) == NSITEM_MOUNTED)
{
logf("Found mounted item: %d %s", item, entry->d_name);
break;
#if 1 /* hide the volume enumerated into the root namespace */
if (item == ROOT_CONTENTS_INDEX || (state & NSITEM_CONTENTS) == 0)
{
logf("Found mounted item: %d %s", item, entry->d_name);
break;
}
#endif
}
item++;

View file

@ -63,6 +63,9 @@
#ifndef dir_exists
#define dir_exists FS_PREFIX(dir_exists)
#endif
#ifndef root_realpath
#define root_realpath FS_PREFIX(root_realpath)
#endif
#endif /* !DIRFUNCTIONS_DEFINED */
#ifndef DIRENT_DEFINED
@ -83,6 +86,9 @@ struct dirinfo
#ifndef DIRFUNCTIONS_DECLARED
/* TIP: set errno to zero before calling to see if anything failed */
struct dirinfo dir_get_info(DIR *dirp, struct DIRENT *entry);
const char* root_realpath(void);
#endif /* !DIRFUNCTIONS_DECLARED */
#endif /* _DIR_H_ */

View file

@ -139,8 +139,10 @@ static inline void fileop_onsync_internal(struct filestr_base *stream)
static inline void volume_onmount_internal(IF_MV_NONVOID(int volume))
{
#if defined(HAVE_MULTIBOOT) && !defined(SIMULATOR) && !defined(BOOTLOADER)
#if (defined(HAVE_MULTIVOLUME) || (defined(HAVE_MULTIBOOT) && !defined(BOOTLOADER)))
char path[VOL_MAX_LEN+2];
#endif
#if defined(HAVE_MULTIBOOT) && !defined(SIMULATOR) && !defined(BOOTLOADER)
char rtpath[MAX_PATH / 2];
make_volume_root(volume, path);
@ -183,7 +185,6 @@ standard_redirect:
root_mount_path(RB_ROOT_CONTENTS_DIR, NSITEM_CONTENTS);
}
#elif defined(HAVE_MULTIVOLUME)
char path[VOL_MAX_LEN+2];
make_volume_root(volume, path);
root_mount_path(path, RB_ROOT_VOL_HIDDEN(volume) ? NSITEM_HIDDEN : 0);
if (volume == path_strip_volume(RB_ROOT_CONTENTS_DIR, NULL, false))

View file

@ -37,6 +37,8 @@ struct ns_scan_info
};
/* root functions */
#define ROOT_MAX_REALPATH 80
const char* root_get_realpath(void);
int root_mount_path(const char *path, unsigned int flags);
void root_unmount_volume(IF_MV_NONVOID(int volume));
int root_readdir_dirent(struct filestr_base *stream,
@ -49,6 +51,7 @@ int ns_open_root(IF_MV(int volume,) unsigned int *callflagsp,
struct file_base_info *infop, uint16_t *attrp);
int ns_open_stream(const char *path, unsigned int callflags,
struct filestr_base *stream, struct ns_scan_info *scanp);
bool ns_volume_is_visible(IF_MV_NONVOID(int volume));
/* closes the namespace stream */
static inline int ns_close_stream(struct filestr_base *stream)

View file

@ -600,3 +600,8 @@ int os_volume_path(IF_MV(int volume, ) char *buffer, size_t bufsize)
return 0;
}
const char* app_root_realpath(void)
{
return PATH_ROOTSTR;
}