From 854058f842b39e1490965c3d64ebe9c5ef6e3320 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Mon, 3 Jan 2022 22:28:16 +0530 Subject: [PATCH 1/3] Introduce kiwix::fileReadable kiwix::fileExists only checks for file existence now kiwix::fileReadable will check if the file is readable (implicitly checking for file existence also) --- include/tools.h | 11 ++++++++++- src/aria2.cpp | 2 +- src/book.cpp | 2 +- src/tools/pathTools.cpp | 20 +++++++++++--------- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/include/tools.h b/include/tools.h index 45f06ca77..b7ffb576d 100644 --- a/include/tools.h +++ b/include/tools.h @@ -180,13 +180,22 @@ std::string getFileContent(const std::string& path); /** checks if file exists. * - * This function returns boolean stating if file exists or not. + * This function returns boolean stating if file exists. * * @param path The absolute path provided in string format. * @return Boolean representing if file exists or not. */ bool fileExists(const std::string& path); +/** checks if file is readable. + * + * This function returns boolean stating if file is readable. + * + * @param path The absolute path provided in string format. + * @return Boolean representing if file is readale or not. + */ +bool fileReadable(const std::string& path); + /** provides mimetype from filename. * * This function provides mimetype from file-name. diff --git a/src/aria2.cpp b/src/aria2.cpp index 1ee015ff4..78f541128 100644 --- a/src/aria2.cpp +++ b/src/aria2.cpp @@ -70,7 +70,7 @@ Aria2::Aria2(): callCmd.push_back(rpc_secret.c_str()); callCmd.push_back(rpc_port.c_str()); callCmd.push_back(download_dir.c_str()); - if (fileExists(session_file)) { + if (fileReadable(session_file)) { callCmd.push_back(inputFile.c_str()); } callCmd.push_back(session.c_str()); diff --git a/src/book.cpp b/src/book.cpp index e04b0192e..17201e33a 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -108,7 +108,7 @@ void Book::updateFromXml(const pugi::xml_node& node, const std::string& baseDir) path = computeAbsolutePath(baseDir, path); } m_path = path; - m_pathValid = fileExists(path); + m_pathValid = fileReadable(path); m_title = ATTR("title"); m_description = ATTR("description"); m_language = ATTR("language"); diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index b4fabf450..7680da3eb 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -305,16 +305,18 @@ std::string kiwix::getFileContent(const std::string& path) bool kiwix::fileExists(const std::string& path) { #ifdef _WIN32 - return PathFileExistsW(Utf8ToWide(path).c_str()); + return (_waccess_s(Utf8ToWide(path).c_str(), 0) == 0); +#else + return (access(path.c_str(), F_OK) == 0); +#endif +} + +bool kiwix::fileReadable(const std::string& path) +{ +#ifdef _WIN32 + return (_waccess_s(Utf8ToWide(path).c_str(), 4) == 0); #else - bool flag = false; - std::fstream fin; - fin.open(path.c_str(), std::ios::in); - if (fin.is_open()) { - flag = true; - } - fin.close(); - return flag; + return (access(path.c_str(), R_OK) == 0); #endif } From 3dbcbe542b2f68d2bc6c2a99a6f4bf188df0e870 Mon Sep 17 00:00:00 2001 From: Nikhil Tanwar <2002nikhiltanwar@gmail.com> Date: Thu, 6 Jan 2022 18:54:58 +0530 Subject: [PATCH 2/3] Add tests for kiwix::fileExists and kiwix::fileReadable --- include/tools.h | 6 +++--- src/tools/pathTools.cpp | 4 ++-- test/pathTools.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/include/tools.h b/include/tools.h index b7ffb576d..75bac9c25 100644 --- a/include/tools.h +++ b/include/tools.h @@ -188,9 +188,9 @@ std::string getFileContent(const std::string& path); bool fileExists(const std::string& path); /** checks if file is readable. - * + * * This function returns boolean stating if file is readable. - * + * * @param path The absolute path provided in string format. * @return Boolean representing if file is readale or not. */ @@ -206,7 +206,7 @@ bool fileReadable(const std::string& path); std::string getMimeTypeForFile(const std::string& filename); /** Provides all available network interfaces - * + * * This function provides the available IPv4 network interfaces */ std::map getNetworkInterfaces(); diff --git a/src/tools/pathTools.cpp b/src/tools/pathTools.cpp index 7680da3eb..31862fef4 100644 --- a/src/tools/pathTools.cpp +++ b/src/tools/pathTools.cpp @@ -306,8 +306,8 @@ bool kiwix::fileExists(const std::string& path) { #ifdef _WIN32 return (_waccess_s(Utf8ToWide(path).c_str(), 0) == 0); -#else - return (access(path.c_str(), F_OK) == 0); +#else + return (access(path.c_str(), F_OK) == 0); #endif } diff --git a/test/pathTools.cpp b/test/pathTools.cpp index 9047e1873..1f8e1e2b6 100644 --- a/test/pathTools.cpp +++ b/test/pathTools.cpp @@ -20,6 +20,8 @@ #include "gtest/gtest.h" #include #include +#include +#include #include "../include/tools.h" #include "../src/tools/pathTools.h" @@ -190,6 +192,34 @@ TEST(pathTools, appendToDirectory) P5("a","b","c",".","foo.xml")); } +TEST(pathTools, fileExists) +{ + ASSERT_TRUE(kiwix::fileExists(P3(".","test","example.zim"))); + ASSERT_FALSE(kiwix::fileExists(P3(".","test","noFile.zim"))); +} + +TEST(pathTools, fileReadable) +{ + ASSERT_TRUE(kiwix::fileReadable(P3(".","test","example.zim"))); + ASSERT_FALSE(kiwix::fileReadable(P3(".","test","noFile.zim"))); +#ifdef _POSIX_SOURCE + std::string path = P3(".","test","example.zim"); + auto myprivs = geteuid(); + if (myprivs != 0) { + int fd = open(path.c_str(), O_RDONLY); + if (fd != -1) { + int wResp = fchmod(fd, ~(S_IRWXU | S_IRWXG | S_IRWXO)); // remove all permissions + if (wResp == 0) { + EXPECT_FALSE(kiwix::fileReadable(P3(".","test","example.zim"))); + } + int resetResp = fchmod(fd, S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP); // reset back permissions to -rw-rw-r-- + if (resetResp == 0) { + EXPECT_TRUE(kiwix::fileReadable(P3(".","test","example.zim"))); + } + } + } +#endif +} TEST(pathTools, goUp) { From aa50845e2273f370519179cf68778084746dbfb7 Mon Sep 17 00:00:00 2001 From: Emmanuel Engelhart Date: Sun, 9 Jan 2022 20:05:30 +0100 Subject: [PATCH 3/3] Fix small typos in comments --- include/tools.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/tools.h b/include/tools.h index 75bac9c25..9ea7f89dc 100644 --- a/include/tools.h +++ b/include/tools.h @@ -178,7 +178,7 @@ std::string converta2toa3(const std::string& a2code); */ std::string getFileContent(const std::string& path); -/** checks if file exists. +/** Checks if file exists. * * This function returns boolean stating if file exists. * @@ -187,7 +187,7 @@ std::string getFileContent(const std::string& path); */ bool fileExists(const std::string& path); -/** checks if file is readable. +/** Checks if file is readable. * * This function returns boolean stating if file is readable. * @@ -196,7 +196,7 @@ bool fileExists(const std::string& path); */ bool fileReadable(const std::string& path); -/** provides mimetype from filename. +/** Provides mimetype from filename. * * This function provides mimetype from file-name. *