Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic handling of Windows-style path prefixes #91

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions libc-bottom-half/libpreopen/lib/libpreopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,39 @@

#include "internal.h"

static char* convert_to_posix(char *path) {
size_t path_len = strlen(path);
if (path_len == 0) {
return (NULL);
}

// does it have Windows-style "C:\" or "C:/" prefix?
int offset = 0;
if (path_len >= 3 && path[1] == ':' && (path[2] == '\\' || path[2] == '/')) {
offset = 2;
}

// allocate
size_t po_path_len = path_len - offset + 1;
char *po_path = calloc(sizeof(char), po_path_len);

// convert to POSIX "/"
int i = offset;
char c;
while (i < path_len) {
c = path[i];
if (c == '\\') {
po_path[i - offset] = '/';
} else {
po_path[i - offset] = c;
}
++i;
}

po_path[po_path_len] = '\0';

return po_path;
}

#ifdef __wasilibc_unmodified_upstream
#else
Expand All @@ -58,6 +91,7 @@ struct po_map*
po_add(struct po_map *map, const char *path, int fd)
{
struct po_map_entry *entry;
char *po_path;

po_map_assertvalid(map);

Expand All @@ -75,9 +109,14 @@ po_add(struct po_map *map, const char *path, int fd)
entry = map->entries + map->length;
map->length++;

entry->name = strdup(path);
#ifdef __wasilibc_unmodified_upstream
entry->name = path;
#else
entry->name = convert_to_posix(path);
#endif
entry->fd = fd;


#ifdef WITH_CAPSICUM
#ifdef __wasilibc_unmodified_upstream
if (cap_rights_get(fd, &entry->rights) != 0) {
Expand Down Expand Up @@ -107,10 +146,15 @@ struct po_relpath
po_find(struct po_map* map, const char *path, cap_rights_t *rights)
#else
static struct po_relpath
po_find(struct po_map* map, const char *path,
po_find(struct po_map* map, const char *po_path,
__wasi_rights_t rights_base, __wasi_rights_t rights_inheriting)
#endif
{
#ifdef __wasilibc_unmodified_upstream
#else
char *path = convert_to_posix(po_path);
#endif

const char *relpath ;
struct po_relpath match = { .relative_path = NULL, .dirfd = -1 };
size_t bestlen = 0;
Expand Down Expand Up @@ -185,6 +229,11 @@ po_find(struct po_map* map, const char *path,
match.relative_path = relpath;
match.dirfd = best;

#ifdef __wasilibc_unmodified_upstream
#else
free(path);
#endif

return match;
}

Expand Down