From 27a1d9f8c6dc4a5a42aaf38cdb7c240632d67d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Fri, 28 Jun 2024 18:08:16 +0800 Subject: [PATCH] Logo (Image): fix building on Windows --- src/common/io/io.h | 2 ++ src/logo/image/image.c | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/common/io/io.h b/src/common/io/io.h index d188942ca..305cb03e7 100644 --- a/src/common/io/io.h +++ b/src/common/io/io.h @@ -8,11 +8,13 @@ #include #include typedef HANDLE FFNativeFD; + #define FF_INVALID_FD INVALID_HANDLE_VALUE #else #include #include #include typedef int FFNativeFD; + #define FF_INVALID_FD (-1) // procfs's file can be changed between read calls such as /proc/meminfo and /proc/uptime. // one safe way to read correct data is reading the whole file in a single read syscall // 8192 comes from procps-ng: https://gitlab.com/procps-ng/procps/-/blob/master/library/meminfo.c?ref_type=heads#L39 diff --git a/src/logo/image/image.c b/src/logo/image/image.c index bb337c3a3..d6f75c192 100644 --- a/src/logo/image/image.c +++ b/src/logo/image/image.c @@ -670,15 +670,20 @@ FFLogoImageResult ffLogoPrintImageImpl(FFLogoRequestData* requestData, const FFI return printSuccessful ? FF_LOGO_IMAGE_RESULT_SUCCESS : FF_LOGO_IMAGE_RESULT_RUN_ERROR; } -static int getCacheFD(FFLogoRequestData* requestData, const char* fileName) +static FFNativeFD getCacheFD(FFLogoRequestData* requestData, const char* fileName) { uint32_t cacheDirLength = requestData->cacheDir.length; ffStrbufAppendS(&requestData->cacheDir, fileName); + #ifndef _WIN32 int fd = open(requestData->cacheDir.chars, O_RDONLY #ifdef O_CLOEXEC | O_CLOEXEC #endif ); + #else + HANDLE fd = CreateFileA(requestData->cacheDir.chars, GENERIC_READ, + FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); + #endif ffStrbufSubstrBefore(&requestData->cacheDir, cacheDirLength); return fd; } @@ -740,17 +745,17 @@ static bool printCachedPixel(FFLogoRequestData* requestData) return false; } - FF_AUTO_CLOSE_FD int fd = -1; + FF_AUTO_CLOSE_FD FFNativeFD fd = FF_INVALID_FD; if(requestData->type == FF_LOGO_TYPE_IMAGE_KITTY) { fd = getCacheFD(requestData, FF_CACHE_FILE_KITTY_COMPRESSED); - if(fd == -1) + if(fd == FF_INVALID_FD) fd = getCacheFD(requestData, FF_CACHE_FILE_KITTY_UNCOMPRESSED); } else if(requestData->type == FF_LOGO_TYPE_IMAGE_SIXEL) fd = getCacheFD(requestData, FF_CACHE_FILE_SIXEL); - if(fd == -1) + if(fd == FF_INVALID_FD) return false; ffPrintCharTimes('\n', options->paddingTop); @@ -783,7 +788,7 @@ static bool printCachedPixel(FFLogoRequestData* requestData) { char buffer[32768]; ssize_t readBytes; - while((readBytes = ffReadFDData(FFUnixFD2NativeFD(fd), sizeof(buffer), buffer)) > 0) + while((readBytes = ffReadFDData(fd, sizeof(buffer), buffer)) > 0) ffWriteFDData(FFUnixFD2NativeFD(STDOUT_FILENO), (size_t) readBytes, buffer); }