From 148e32786d7c886e6349ca9d3dd51d191f66a808 Mon Sep 17 00:00:00 2001 From: Levi Neely <141506390+lneely@users.noreply.github.com> Date: Sat, 29 Jun 2024 12:35:11 +0200 Subject: [PATCH] Revert "removed P_OS_POSIX conditional checks" This reverts commit 9ac5c7a1e9952d29268efdf765a6b8060d54e3fc. --- cmd/pcloudcc/pclsync_lib.cpp | 14 ++++++ lib/pclsync/pcompat.c | 92 +++++++++++++++++++++++++++++++++--- lib/pclsync/pcompat.h | 35 +++++++++++--- lib/pclsync/pfs.c | 26 ++++++++++ lib/pclsync/pfs.h | 3 ++ lib/pclsync/pp2p.c | 8 ++++ 6 files changed, 166 insertions(+), 12 deletions(-) diff --git a/cmd/pcloudcc/pclsync_lib.cpp b/cmd/pcloudcc/pclsync_lib.cpp index 5690465..b189d10 100644 --- a/cmd/pcloudcc/pclsync_lib.cpp +++ b/cmd/pcloudcc/pclsync_lib.cpp @@ -152,6 +152,7 @@ clib::pclsync_lib::do_get_pass_from_console(std::string &password) { << std::endl; exit(1); } +#ifdef P_OS_POSIX termios oldt; tcgetattr(STDIN_FILENO, &oldt); termios newt = oldt; @@ -160,6 +161,19 @@ clib::pclsync_lib::do_get_pass_from_console(std::string &password) { std::cout << "Please, enter password" << std::endl; getline(std::cin, password); tcsetattr(STDIN_FILENO, TCSANOW, &oldt); +#else + HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); + DWORD modeoff; + DWORD modeon; + DWORD mode; + GetConsoleMode(hStdin, &mode); + modeoff = mode & ~ENABLE_ECHO_INPUT; + modeon = mode | ENABLE_ECHO_INPUT; + SetConsoleMode(hStdin, modeoff); + std::cout << "Please, enter password" << std::endl; + getline(std::cin, password); + SetConsoleMode(hStdin, modeon); +#endif } void diff --git a/lib/pclsync/pcompat.c b/lib/pclsync/pcompat.c index b53530d..039326d 100644 --- a/lib/pclsync/pcompat.c +++ b/lib/pclsync/pcompat.c @@ -42,7 +42,9 @@ #if defined(P_OS_LINUX) #include -#endif // P_OS_LINUX +#endif + +#if defined(P_OS_POSIX) #include #include @@ -55,6 +57,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +75,8 @@ extern char **environ; #define PSYNC_MAP_ANONYMOUS MAP_ANON #endif +#endif + #define PROXY_NONE 0 #define PROXY_CONNECT 1 @@ -86,10 +91,12 @@ typedef struct { const char *name; } psync_run_data1; +#if defined(P_OS_POSIX) static uid_t psync_uid; static gid_t psync_gid; static gid_t *psync_gids; static int psync_gids_cnt; +#endif static int proxy_type=PROXY_NONE; static int proxy_detected=0; @@ -130,6 +137,7 @@ psync_user_is_admin(){ void psync_compat_init(){ +#if defined(P_OS_POSIX) struct rlimit limit; limit.rlim_cur=limit.rlim_max=2048; if (setrlimit(RLIMIT_NOFILE, &limit)) @@ -154,12 +162,16 @@ psync_compat_init(){ psync_page_size=PAGESIZE; #else psync_page_size=sysconf(_SC_PAGESIZE); +#endif +#else + psync_page_size=-1; #endif debug(D_NOTICE, "detected page size %d", psync_page_size); } int psync_stat_mode_ok(psync_stat_t *buf, unsigned int bits){ +#if defined(P_OS_POSIX) int i; if (psync_uid==0) return 1; @@ -177,6 +189,9 @@ psync_stat_mode_ok(psync_stat_t *buf, unsigned int bits){ return (buf->st_mode&bits)==bits; } return (buf->st_mode&bits)==bits; +#else + return 1; +#endif } char * @@ -410,6 +425,7 @@ psync_millitime(){ return tm.tv_sec*1000+tm.tv_nsec/1000000; } +#if defined(P_OS_POSIX) static void psync_add_file_to_seed(const char *fn, psync_lhash_ctx *hctx, size_t max){ char buff[4096]; @@ -431,6 +447,7 @@ psync_add_file_to_seed(const char *fn, psync_lhash_ctx *hctx, size_t max){ close(fd); } } +#endif #if defined(P_OS_LINUX) static void @@ -477,6 +494,20 @@ psync_get_random_seed_from_db(psync_lhash_ctx *hctx){ psync_get_random_seed_from_query(hctx, res); res=psync_sql_query_rdlock("SELECT * FROM resolver ORDER BY RANDOM() LIMIT 50"); psync_get_random_seed_from_query(hctx, res); +/* res=psync_sql_query_rdlock("SELECT * FROM filerevision ORDER BY RANDOM() LIMIT 50"); + psync_get_random_seed_from_query(hctx, res); + res=psync_sql_query_rdlock("SELECT * FROM file ORDER BY RANDOM() LIMIT 50"); + psync_get_random_seed_from_query(hctx, res); + res=psync_sql_query_rdlock("SELECT * FROM localfile ORDER BY RANDOM() LIMIT 50"); + psync_get_random_seed_from_query(hctx, res); + res=psync_sql_query_rdlock("SELECT * FROM folder ORDER BY RANDOM() LIMIT 25"); + psync_get_random_seed_from_query(hctx, res); + res=psync_sql_query_rdlock("SELECT * FROM localfolder ORDER BY RANDOM() LIMIT 25"); + psync_get_random_seed_from_query(hctx, res); + res=psync_sql_query_rdlock("SELECT * FROM hashchecksum ORDER BY RANDOM() LIMIT 25"); + psync_get_random_seed_from_query(hctx, res); + res=psync_sql_query_rdlock("SELECT * FROM pagecache WHERE type=1 AND rowid>(ABS(RANDOM())%(SELECT MAX(rowid)+1 FROM pagecache)) ORDER BY rowid LIMIT 50"); + psync_get_random_seed_from_query(hctx, res); */ psync_sql_statement("REPLACE INTO setting (id, value) VALUES ('random', RANDOM())"); psync_nanotime(&tm); psync_lhash_update(hctx, &tm, sizeof(&tm)); @@ -539,6 +570,7 @@ psync_get_random_seed(unsigned char *seed, const void *addent, size_t aelen, int int64_t i64; pthread_t threadid; unsigned char lsc[64][PSYNC_LHASH_DIGEST_LEN]; +#if defined(P_OS_POSIX) debug(D_NOTICE, "in"); struct utsname un; struct statvfs stfs; @@ -564,6 +596,7 @@ psync_get_random_seed(unsigned char *seed, const void *addent, size_t aelen, int #else psync_add_file_to_seed("/dev/random", &hctx, PSYNC_HASH_DIGEST_LEN); #endif +#endif #if defined(P_OS_LINUX) psync_get_random_seed_linux(&hctx); #endif @@ -1219,7 +1252,7 @@ psync_socket_pendingdata(psync_socket *sock){ int psync_socket_pendingdata_buf(psync_socket *sock){ int ret; -#if defined(FIONREAD) +#if defined(P_OS_POSIX) && defined(FIONREAD) if (ioctl(sock->sock, FIONREAD, &ret)) return -1; #else @@ -1877,9 +1910,46 @@ empty: return ret; } +#if !defined(P_OS_POSIX) +static int +psync_compat_socketpair(psync_socket_t sockfd[2]){ + psync_socket_t sock; + struct sockaddr_in addr; + socklen_t addrlen; + sock=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (sock==INVALID_SOCKET){ + goto err0; + } + memset(&addr, 0, sizeof(addr)); + addr.sin_family=AF_INET; + addr.sin_addr.s_addr=htonl(INADDR_LOOPBACK); + addrlen=sizeof(addr); + if (bind(sock, (struct sockaddr *)&addr, sizeof(addr))==SOCKET_ERROR || + listen(sock, 1)==SOCKET_ERROR || + getsockname(sock, (struct sockaddr *)&addr, &addrlen)==SOCKET_ERROR || + (sockfd[0]=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET) + goto err1; + if (connect(sockfd[0], (struct sockaddr *)&addr, addrlen)==SOCKET_ERROR || + (sockfd[1]=accept(sock, NULL, NULL))==INVALID_SOCKET) + goto err2; + psync_close_socket(sock); + return 0; +err2: + psync_close_socket(sockfd[0]); +err1: + psync_close_socket(sock); +err0: + return SOCKET_ERROR; +} +#endif + int psync_pipe(psync_socket_t pipefd[2]){ +#if defined(P_OS_POSIX) return pipe(pipefd); +#else + return psync_compat_socketpair(pipefd); +#endif } int @@ -1899,7 +1969,11 @@ psync_pipe_write(psync_socket_t pfd, const void *buff, int num){ int psync_socket_pair(psync_socket_t sfd[2]){ +#if defined(P_OS_POSIX) return socketpair(AF_UNIX, SOCK_STREAM, 0, sfd); +#else + return psync_compat_socketpair(sfd); +#endif } int @@ -2109,7 +2183,7 @@ psync_file_close(psync_file_t fd){ int psync_file_sync(psync_file_t fd){ -#if defined(F_FULLFSYNC) +#if defined(F_FULLFSYNC) && defined(P_OS_POSIX) if (unlikely(fcntl(fd, F_FULLFSYNC))){ while (errno==EINTR){ debug(D_NOTICE, "got EINTR while fsyncing file"); @@ -2128,7 +2202,7 @@ psync_file_sync(psync_file_t fd){ } else return 0; -#else +#elif defined(P_OS_POSIX) #if _POSIX_SYNCHRONIZED_IO>0 if (unlikely(fdatasync(fd))){ #else @@ -2204,6 +2278,7 @@ psync_file_set_creation(psync_file_t fd, time_t ctime){ int psync_set_crtime_mtime(const char *path, time_t crtime, time_t mtime){ +#if defined(P_OS_POSIX) if (mtime){ struct timeval tm[2]; tm[0].tv_sec=mtime; @@ -2219,6 +2294,9 @@ psync_set_crtime_mtime(const char *path, time_t crtime, time_t mtime){ } else return 0; +#else + return -1; +#endif } int @@ -2266,13 +2344,15 @@ psync_file_preread(psync_file_t fd, uint64_t offset, size_t count){ int psync_file_readahead(psync_file_t fd, uint64_t offset, size_t count){ -#if defined(POSIX_FADV_WILLNEED) +#if defined(P_OS_POSIX) && defined(POSIX_FADV_WILLNEED) return posix_fadvise(fd, offset, count, POSIX_FADV_WILLNEED); -#elif defined(F_RDADVISE) +#elif defined(P_OS_POSIX) && defined(F_RDADVISE) struct radvisory ra; ra.ra_offset=offset; ra.ra_count=count; return fcntl(fd, F_RDADVISE, &ra); +#else + return psync_file_preread(fd, offset, count); #endif } diff --git a/lib/pclsync/pcompat.h b/lib/pclsync/pcompat.h index eeb3f3d..3ae9efe 100644 --- a/lib/pclsync/pcompat.h +++ b/lib/pclsync/pcompat.h @@ -37,20 +37,31 @@ // figure out which OS... // XXX: goal is to remove // --- +#if !defined(P_OS_LINUX) || !defined(P_OS_POSIX) +#define P_OS_POSIX #if defined(__linux__) #define P_OS_LINUX #endif +#endif #if defined(P_OS_LINUX) -#define P_OS_ID 7 // linux + +#define P_OS_ID 7 + #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif + #else -#define P_OS_ID 0 // non-linux posix-compliant OS + +#define P_OS_ID 0 + #endif +// "actual" header begins here +// --- + #define _FILE_OFFSET_BITS 64 #include @@ -77,6 +88,12 @@ typedef unsigned long psync_uint_t; #define NTO_STR(s) TO_STR(s) #define TO_STR(s) #s + + + + +#if defined(P_OS_POSIX) // P_OS_POSIX + #include #include #include @@ -101,17 +118,23 @@ typedef unsigned long psync_uint_t; #if defined(st_mtimensec) #define psync_stat_mtime_native(s) ((s)->st_mtime*1000000ULL+(s)->st_mtimensec/1000) #else -#define psync_stat_mtime_native(s) \ - ((s)->st_mtime*1000000ULL+ \ - ((struct timespec *)(&(s)->st_mtime))->tv_nsec/1000) +#define psync_stat_mtime_native(s) \ + ((s)->st_mtime*1000000ULL+\ + ((struct timespec *)(&(s)->st_mtime))->tv_nsec/1000) #endif #define psync_mtime_native_to_mtime(n) ((n)/1000000ULL) + +#else // !P_OS_POSIX + +#define psync_stat_mtime_native(s) ((s)->st_mtime) +#define psync_mtime_native_to_mtime(n) (n) +#endif + #define psync_stat_inode(s) ((s)->st_ino) #define psync_stat_device(s) ((s)->st_dev) #define psync_stat_device_full(s) ((s)->st_dev) - #define psync_deviceid_short(deviceid) (deviceid) typedef struct stat psync_stat_t; diff --git a/lib/pclsync/pfs.c b/lib/pclsync/pfs.c index 751f979..a444fc8 100644 --- a/lib/pclsync/pfs.c +++ b/lib/pclsync/pfs.c @@ -65,7 +65,9 @@ typedef off_t fuse_off_t; #endif +#if defined(P_OS_POSIX) #include +#endif #if defined(P_OS_LINUX) #include @@ -416,8 +418,10 @@ static void psync_row_to_folder_stat(psync_variant_row row, struct FUSE_STAT *st stbuf->st_mode=S_IFDIR | 0755; stbuf->st_nlink=psync_get_number(row[4])+2; stbuf->st_size=FS_BLOCK_SIZE; +#if defined(P_OS_POSIX) stbuf->st_blocks=1; stbuf->st_blksize=FS_BLOCK_SIZE; +#endif stbuf->st_uid=myuid; stbuf->st_gid=mygid; } @@ -438,8 +442,10 @@ static void psync_row_to_file_stat(psync_variant_row row, struct FUSE_STAT *stbu stbuf->st_mode=S_IFREG | 0644; stbuf->st_nlink=1; stbuf->st_size=size; +#if defined(P_OS_POSIX) stbuf->st_blocks=(size+511)/512; stbuf->st_blksize=FS_BLOCK_SIZE; +#endif stbuf->st_uid=myuid; stbuf->st_gid=mygid; } @@ -466,8 +472,10 @@ static void psync_mkdir_to_folder_stat(psync_fstask_mkdir_t *mk, struct FUSE_STA stbuf->st_mode=S_IFDIR | 0755; stbuf->st_nlink=mk->subdircnt+2; stbuf->st_size=FS_BLOCK_SIZE; +#if defined(P_OS_POSIX) stbuf->st_blocks=1; stbuf->st_blksize=FS_BLOCK_SIZE; +#endif stbuf->st_uid=myuid; stbuf->st_gid=mygid; } @@ -498,8 +506,10 @@ static int psync_creat_stat_fake_file(struct FUSE_STAT *stbuf){ stbuf->st_mode=S_IFREG | 0644; stbuf->st_nlink=1; stbuf->st_size=0; +#if defined(P_OS_POSIX) stbuf->st_blocks=0; stbuf->st_blksize=FS_BLOCK_SIZE; +#endif stbuf->st_uid=myuid; stbuf->st_gid=mygid; return 0; @@ -624,8 +634,10 @@ static int psync_creat_local_to_file_stat(psync_fstask_creat_t *cr, struct FUSE_ size=psync_stat_size(&st); stbuf->st_size=size; } +#if defined(P_OS_POSIX) stbuf->st_blocks=(size+511)/512; stbuf->st_blksize=FS_BLOCK_SIZE; +#endif stbuf->st_uid=myuid; stbuf->st_gid=mygid; return 0; @@ -645,8 +657,10 @@ static int psync_creat_static_to_file_stat(psync_fstask_creat_t *cr, struct FUSE stbuf->st_mode=S_IFREG | 0644; stbuf->st_nlink=1; stbuf->st_size=lc->datalen; +#if defined(P_OS_POSIX) stbuf->st_blocks=(lc->datalen+511)/512; stbuf->st_blksize=FS_BLOCK_SIZE; +#endif stbuf->st_uid=myuid; stbuf->st_gid=mygid; return 0; @@ -2171,7 +2185,11 @@ PSYNC_NOINLINE static int psync_fs_do_check_write_space(psync_openfile_t *of, si if (psync_pagecache_free_from_read_cache(size*2) + +#if defined(P_OS_POSIX) #define psync_fs_need_per_folder_refresh() psync_fs_need_per_folder_refresh_f() #define psync_fs_need_per_folder_refresh_const() 1 #else #define psync_fs_need_per_folder_refresh() (psync_invalidate_os_cache_needed() && psync_fs_need_per_folder_refresh_f()) #define psync_fs_need_per_folder_refresh_const() 1 +#endif extern char *psync_fake_prefix; extern size_t psync_fake_prefix_len; diff --git a/lib/pclsync/pp2p.c b/lib/pclsync/pp2p.c index 22be04b..b72942e 100644 --- a/lib/pclsync/pp2p.c +++ b/lib/pclsync/pp2p.c @@ -117,8 +117,12 @@ PSYNC_PURE static const char *p2p_get_address(void *addr){ if (((struct sockaddr_in *)addr)->sin_family==AF_INET) return inet_ntoa(((struct sockaddr_in *)addr)->sin_addr); else{ +#if defined(P_OS_POSIX) static char buff[80]; return inet_ntop(AF_INET6, &((struct sockaddr_in6 *)addr)->sin6_addr, buff, sizeof(buff)); +#else + return "IPv6 address"; /* inet_ntop on Windows is Vista+ */ +#endif } } @@ -126,8 +130,12 @@ PSYNC_PURE static const char *p2p_get_peer_address(){ if (paddr.ss_family==AF_INET) return inet_ntoa(((struct sockaddr_in *)&paddr)->sin_addr); else{ +#if defined(P_OS_POSIX) static char buff[80]; return inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&paddr)->sin6_addr, buff, sizeof(buff)); +#else + return "IPv6 address"; /* inet_ntop on Windows is Vista+ */ +#endif } }