Skip to content

Commit

Permalink
VFS misc fixes
Browse files Browse the repository at this point in the history
- Correct error for unsupported msync operation.
- VfsAccess now properly checks for symlinks.
- Fix bug in HostfsGetOptimalDirFdName when "dir" is the root directory.
- Fix mmap for Cygwin systems when the file offset is past the end.
  • Loading branch information
trungnt2910 committed Mar 8, 2023
1 parent 6402f85 commit 41ad92f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
30 changes: 23 additions & 7 deletions blink/hostfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,13 @@ static int HostfsGetOptimalDirFdName(struct VfsInfo *dir, const char *name,
if (path[len1 - 1] == '/') {
--len1;
}
if (path[0] == '/') {
--len1;
}
if (len1 + 1 + len2 + 1 >= PATH_MAX) {
ret = enametoolong();
} else {
if (len1 > 0) {
if (len1 >= 0) {
memcpy(hostpath, ((*path) == '/') ? path + 1 : path, len1);
hostpath[len1] = '/';
}
Expand Down Expand Up @@ -304,7 +307,7 @@ int HostfsFinddir(struct VfsInfo *parent, const char *name,
if (fstatat(hostfd, hostname, &st, AT_SYMLINK_NOFOLLOW) == -1) {
VFS_LOGF("HostfsFinddir: fstatat(%d, \"%s\", %p, AT_SYMLINK_NOFOLLOW) "
"failed (%d)",
parentfd, name, &st, errno);
hostfd, hostname, &st, errno);
goto cleananddie;
}
if (HostfsCreateInfo(&outputinfo) == -1) {
Expand Down Expand Up @@ -441,8 +444,8 @@ int HostfsOpen(struct VfsInfo *parent, const char *name, int flags, int mode,
goto cleananddie;
}
outputinfo->filefd = openat(hostfd, hostname, flags, mode);
VFS_LOGF("HostfsOpen: openat(%d, \"%s\", %d, %d) -> %d", parentfd, name,
flags, mode, outputinfo->filefd);
VFS_LOGF("HostfsOpen: openat(%d, \"%s\", %d, %d) -> %d, %s", hostfd, hostname,
flags, mode, outputinfo->filefd, strerror(errno));
if (outputinfo->filefd == -1) {
goto cleananddie;
}
Expand Down Expand Up @@ -1432,16 +1435,29 @@ int HostfsSymlink(const char *target, struct VfsInfo *info, const char *name) {

void *HostfsMmap(struct VfsInfo *info, void *addr, size_t len, int prot,
int flags, off_t offset) {
struct HostfsInfo *hostinfo;
#ifdef __CYGWIN__
struct stat st;
#endif
void *ret;
int fd;
VFS_LOGF("HostfsMmap(%p, %p, %zu, %d, %d, %zd)", info, addr, len, prot, flags,
offset);
if (info == NULL) {
efault();
return MAP_FAILED;
}
hostinfo = (struct HostfsInfo *)info->data;
ret = mmap(addr, len, prot, flags, hostinfo->filefd, offset);
fd = ((struct HostfsInfo *)info->data)->filefd;
#ifdef __CYGWIN__
if (fstat(fd, &st) != -1) {
if (offset >= st.st_size) {
// Cygwin doesn't like mapping files with offset past the end.
fd = -1;
flags |= MAP_ANONYMOUS;
offset = 0;
}
}
#endif
ret = mmap(addr, len, prot, flags, fd, offset);
VFS_LOGF("mmap(%p, %zu, %d, %d, %d, %zd) -> %p", addr, len, prot, flags,
hostinfo->filefd, offset, ret);
return ret;
Expand Down
4 changes: 2 additions & 2 deletions blink/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ int VfsAccess(int dirfd, const char *name, mode_t mode, int flags) {
if (VfsHandleDirfdName(dirfd, name, &dir, &newname) == -1) {
return -1;
}
if (flags & AT_SYMLINK_FOLLOW) {
if (!(flags & AT_SYMLINK_NOFOLLOW)) {
ret = VfsHandleDirfdSymlink(&dir, &newname);
}
unassert(!VfsTraverseMount(&dir, &newname));
Expand Down Expand Up @@ -2519,7 +2519,7 @@ int VfsMsync(void *addr, size_t len, int flags) {
}
if (!map->data->device->ops->msync) {
UNLOCK(&g_vfs.mapslock);
return eopnotsupp();
return enodev();
}
currentaddr = map->addr;
currentlen = map->len;
Expand Down

0 comments on commit 41ad92f

Please sign in to comment.