dll: fuse: getpath

This commit is contained in:
Bill Zissimopoulos
2022-04-12 15:44:54 +01:00
parent 0a91292e05
commit e5879a9cb0
6 changed files with 171 additions and 20 deletions

View File

@ -24,6 +24,7 @@
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fuse.h>
@ -59,6 +60,7 @@
typedef struct
{
const char *rootdir;
size_t rootlen;
} PTFS;
static int ptfs_getattr(const char *path, struct fuse_stat *stbuf)
@ -293,6 +295,43 @@ static int ptfs_utimens(const char *path, const struct fuse_timespec tv[2])
}
#endif
#if defined(_WIN64) || defined(_WIN32)
static int ptfs_getpath(const char *path, char *buf, size_t size,
struct fuse_file_info *fi)
{
if (0 == fi)
{
ptfs_impl_fullpath(path);
if (-1 == getpath(path, buf, size))
return errno;
}
else
{
int fd = fi_fd(fi);
if (-1 == fgetpath(fd, buf, size))
return errno;
}
PTFS *ptfs = fuse_get_context()->private_data;
size = strlen(buf);
if (size > ptfs->rootlen)
{
size -= ptfs->rootlen;
memmove(buf, buf + ptfs->rootlen, size);
buf[size] = '\0';
}
else
{
buf[0] = '/';
buf[1] = '\0';
}
return 0;
}
#endif
#if defined(_WIN64) || defined(_WIN32)
static int ptfs_setcrtime(const char *path, const struct fuse_timespec *tv)
{
@ -344,6 +383,9 @@ static struct fuse_operations ptfs_ops =
#if defined(PTFS_UTIMENS)
.utimens = ptfs_utimens,
#endif
#if defined(_WIN64) || defined(_WIN32)
.getpath = ptfs_getpath,
#endif
#if defined(_WIN64) || defined(_WIN32)
.setcrtime = ptfs_setcrtime,
#endif
@ -413,5 +455,17 @@ int main(int argc, char *argv[])
if (0 == ptfs.rootdir)
usage();
#if defined(_WIN64) || defined(_WIN32)
{
char buf[PATH_MAX];
if (-1 != getpath(ptfs.rootdir, buf, sizeof buf))
{
ptfs.rootlen = strlen(buf);
if (1 == ptfs.rootlen)
ptfs.rootlen = 0;
}
}
#endif
return fuse_main(argc, argv, &ptfs_ops, &ptfs);
}

View File

@ -212,6 +212,28 @@ int open(const char *path, int oflag, ...)
return (int)(intptr_t)h;
}
int fgetpath(int fd, char *buf, size_t size)
{
HANDLE h = (HANDLE)(intptr_t)fd;
union
{
FILE_NAME_INFO V;
UINT8 B[FIELD_OFFSET(FILE_NAME_INFO, FileName) + FSP_FSCTL_TRANSACT_PATH_SIZEMAX];
} NameInfo;
if (!GetFileInformationByHandleEx(h, FileNormalizedNameInfo, &NameInfo, sizeof NameInfo))
return error();
FspPosixEncodeWindowsPath(NameInfo.V.FileName, NameInfo.V.FileNameLength / sizeof(WCHAR));
size = WideCharToMultiByte(CP_UTF8, 0,
NameInfo.V.FileName, NameInfo.V.FileNameLength / sizeof(WCHAR), buf, (int)(size - 1), 0, 0);
if (0 == size)
return error();
buf[size] = '\0';
return 0;
}
int fstat(int fd, struct fuse_stat *stbuf)
{
HANDLE h = (HANDLE)(intptr_t)fd;
@ -303,6 +325,22 @@ int close(int fd)
return 0;
}
int getpath(const char *path, char *buf, size_t size)
{
HANDLE h = CreateFileA(path,
FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
0,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
if (INVALID_HANDLE_VALUE == h)
return error();
int res = fgetpath((int)(intptr_t)h, buf, size);
CloseHandle(h);
return res;
}
int lstat(const char *path, struct fuse_stat *stbuf)
{
HANDLE h = CreateFileA(path,

View File

@ -46,6 +46,7 @@ char *realpath(const char *path, char *resolved);
int statvfs(const char *path, struct fuse_statvfs *stbuf);
int open(const char *path, int oflag, ...);
int fgetpath(int fd, char *buf, size_t size);
int fstat(int fd, struct fuse_stat *stbuf);
int ftruncate(int fd, fuse_off_t size);
int pread(int fd, void *buf, size_t nbyte, fuse_off_t offset);
@ -53,6 +54,7 @@ int pwrite(int fd, const void *buf, size_t nbyte, fuse_off_t offset);
int fsync(int fd);
int close(int fd);
int getpath(const char *path, char *buf, size_t size);
int lstat(const char *path, struct fuse_stat *stbuf);
int chmod(const char *path, fuse_mode_t mode);
int lchown(const char *path, fuse_uid_t uid, fuse_gid_t gid);