commit c96dbe41b2f53fb5a9ac1e0fc798775c9cd01596 from: Sergey Bronnikov date: Wed Mar 03 09:47:28 2021 UTC Use check_function_exists() to detect functions support Some operating systems missed fallocate(), utimens(), flock(), setxattr(), getxattr(), listxattr() and removexattr() support. Right now source code related to these functions enabled explicitly on OSes where it is supported. It's better to detect functions in runtime using CMake's check_function_exists() and enable appropriate define if function exists in a system. Seems ioctl() supported everywhere, so condition compilation has been removed. commit - 51509974650a6c946194909defb2ce20944a7d79 commit + c96dbe41b2f53fb5a9ac1e0fc798775c9cd01596 blob - 4ff61ed78acad0a8b635930425a54000e6c24f0e blob + b394952185f2ee07c189fd42fb9d353bafbd7d43 --- CMakeLists.txt +++ CMakeLists.txt @@ -20,10 +20,22 @@ add_executable(${PROJECT_NAME} ${UNRELIABLEFS_SRC}) target_include_directories(${PROJECT_NAME} PRIVATE ${FUSE_INCLUDE_DIRS}) target_link_libraries(${PROJECT_NAME} ${FUSE_LIBRARIES}) +check_function_exists(fallocate HAVE_FALLOCATE) +check_function_exists(fallocate HAVE_FLOCK) check_function_exists(utimensat HAVE_UTIMENSAT) +check_function_exists(setxattr HAVE_XATTR) +if (${HAVE_FALLOCATE}) + target_compile_definitions(${PROJECT_NAME} PUBLIC HAVE_FALLOCATE) +endif () +if (${HAVE_FLOCK}) + target_compile_definitions(${PROJECT_NAME} PUBLIC HAVE_FLOCK) +endif () if (${HAVE_UTIMENSAT}) target_compile_definitions(${PROJECT_NAME} PUBLIC HAVE_UTIMENSAT) endif () +if (${HAVE_XATTR}) + target_compile_definitions(${PROJECT_NAME} PUBLIC HAVE_XATTR) +endif () enable_testing() add_test(NAME pytest COMMAND pytest -c ${PROJECT_SOURCE_DIR}/tests/pytest.ini ${PROJECT_SOURCE_DIR}/tests/ blob - e9ac458231bf34ae4c4ecfbc35e39c6f6409c2c2 blob + 669ce4c215fb555f05bb65fc8530cc8ba85be8d8 --- unreliablefs.c +++ unreliablefs.c @@ -48,14 +48,16 @@ static struct fuse_operations unreliable_ops = { .ftruncate = unreliable_ftruncate, .fgetattr = unreliable_fgetattr, .lock = unreliable_lock, -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) .ioctl = unreliable_ioctl, +#ifdef HAVE_FLOCK .flock = unreliable_flock, +#endif /* HAVE_FLOCK */ +#ifdef HAVE_FALLOCATE .fallocate = unreliable_fallocate, -#endif /* __OpenBSD__ */ +#endif /* HAVE_FALLOCATE */ #ifdef HAVE_UTIMENSAT .utimens = unreliable_utimens, -#endif +#endif /* HAVE_UTIMENSAT */ }; int main(int argc, char *argv[]) blob - f416c6fd23aa5a038a7be649dacca2be8734a0c0 blob + f5c50e4c0504840e9c3bfb0fdd573200b6e7aeaa --- unreliablefs_ops.c +++ unreliablefs_ops.c @@ -9,9 +9,9 @@ #include #include #include -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) +#ifdef HAVE_XATTR #include -#endif /* __OpenBSD__ */ +#endif /* HAVE_XATTR */ #ifdef linux /* For pread()/pwrite()/utimensat() */ @@ -364,7 +364,7 @@ int unreliable_fsync(const char *path, int datasync, s return 0; } -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) +#ifdef HAVE_XATTR int unreliable_setxattr(const char *path, const char *name, const char *value, size_t size, int flags) { @@ -373,16 +373,18 @@ int unreliable_setxattr(const char *path, const char * return ret; } +#ifdef __APPLE__ + ret = setxattr(path, name, value, size, 0, flags); +#else ret = setxattr(path, name, value, size, flags); +#endif /* __APPLE__ */ if (ret == -1) { return -errno; } return 0; } -#endif /* __OpenBSD__ */ -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) int unreliable_getxattr(const char *path, const char *name, char *value, size_t size) { @@ -391,16 +393,18 @@ int unreliable_getxattr(const char *path, const char * return ret; } +#ifdef __APPLE__ + ret = getxattr(path, name, value, size, 0, XATTR_NOFOLLOW); +#else ret = getxattr(path, name, value, size); +#endif /* __APPLE__ */ if (ret == -1) { return -errno; } return 0; } -#endif /* __OpenBSD__ */ -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) int unreliable_listxattr(const char *path, char *list, size_t size) { int ret = error_inject(path, "listxattr"); @@ -408,16 +412,18 @@ int unreliable_listxattr(const char *path, char *list, return ret; } +#ifdef __APPLE__ + ret = listxattr(path, list, size, XATTR_NOFOLLOW); +#else ret = listxattr(path, list, size); +#endif /* __APPLE__ */ if (ret == -1) { return -errno; } return ret; } -#endif /* __OpenBSD__ */ -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) int unreliable_removexattr(const char *path, const char *name) { int ret = error_inject(path, "removexattr"); @@ -425,14 +431,18 @@ int unreliable_removexattr(const char *path, const cha return ret; } +#ifdef __APPLE__ + ret = removexattr(path, name, XATTR_NOFOLLOW); +#else ret = removexattr(path, name); +#endif /* __APPLE__ */ if (ret == -1) { return -errno; } return 0; } -#endif /* __OpenBSD__ */ +#endif /* HAVE_XATTR */ int unreliable_opendir(const char *path, struct fuse_file_info *fi) { @@ -616,7 +626,6 @@ int unreliable_lock(const char *path, struct fuse_file return 0; } -#if !defined(__OpenBSD__) int unreliable_ioctl(const char *path, int cmd, void *arg, struct fuse_file_info *fi, unsigned int flags, void *data) @@ -633,9 +642,8 @@ int unreliable_ioctl(const char *path, int cmd, void * return ret; } -#endif /* __OpenBSD__ */ -#if !defined(__OpenBSD__) +#ifdef HAVE_FLOCK int unreliable_flock(const char *path, struct fuse_file_info *fi, int op) { int ret = error_inject(path, "flock"); @@ -650,9 +658,9 @@ int unreliable_flock(const char *path, struct fuse_fil return 0; } -#endif /* __OpenBSD__ */ +#endif /* HAVE_FLOCK */ -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) +#ifdef HAVE_FALLOCATE int unreliable_fallocate(const char *path, int mode, off_t offset, off_t len, struct fuse_file_info *fi) @@ -690,7 +698,7 @@ int unreliable_fallocate(const char *path, int mode, return 0; } -#endif /* __OpenBSD__ */ +#endif /* HAVE_FALLOCATE */ #ifdef HAVE_UTIMENSAT int unreliable_utimens(const char *path, const struct timespec ts[2]) @@ -708,4 +716,4 @@ int unreliable_utimens(const char *path, const struct return 0; } -#endif +#endif /* HAVE_UTIMENSAT */ blob - 92b635ecae2da610402feb5f1ba53528f8e0c903 blob + a18725fd37d00d30698a131078382b992bdef7f8 --- unreliablefs_ops.h +++ unreliablefs_ops.h @@ -26,12 +26,14 @@ int unreliable_statfs(const char *, struct statvfs *); int unreliable_flush(const char *, struct fuse_file_info *); int unreliable_release(const char *, struct fuse_file_info *); int unreliable_fsync(const char *, int, struct fuse_file_info *); -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) + +#ifdef HAVE_XATTR int unreliable_setxattr(const char *, const char *, const char *, size_t, int); int unreliable_getxattr(const char *, const char *, char *, size_t); int unreliable_listxattr(const char *, char *, size_t); int unreliable_removexattr(const char *, const char *); -#endif /* __OpenBSD__ */ +#endif /* HAVE_XATTR */ + int unreliable_opendir(const char *, struct fuse_file_info *); int unreliable_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi); @@ -48,22 +50,22 @@ int unreliable_ftruncate(const char *, off_t, struct f int unreliable_fgetattr(const char *, struct stat *, struct fuse_file_info *); int unreliable_lock(const char *, struct fuse_file_info *, int cmd, struct flock *); -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) int unreliable_ioctl(const char *, int cmd, void *arg, struct fuse_file_info *, unsigned int flags, void *data); -#endif /* __OpenBSD__ */ int unreliable_write_buf(const char *, struct fuse_bufvec *buf, off_t off, struct fuse_file_info *); int unreliable_read_buf(const char *, struct fuse_bufvec **bufp, size_t size, off_t off, struct fuse_file_info *); -#if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__APPLE__) +#ifdef HAVE_FLOCK int unreliable_flock(const char *, struct fuse_file_info *, int op); +#endif /* HAVE_FLOCK */ +#ifdef HAVE_FALLOCATE int unreliable_fallocate(const char *, int, off_t, off_t, struct fuse_file_info *); -#endif /* __OpenBSD__ */ +#endif /* HAVE_FALLOCATE */ #ifdef HAVE_UTIMENSAT int unreliable_utimens(const char *path, const struct timespec ts[2]); -#endif +#endif /* HAVE_UTIMENSAT */ #endif /* UNRELIABLEFS_OPS_HH */