mirror of https://github.com/kiwix/libkiwix.git
Merge pull request #668 from kiwix/fileExists
Introduce kiwix::fileReadable
This commit is contained in:
commit
e4d99f0374
|
@ -178,16 +178,25 @@ std::string converta2toa3(const std::string& a2code);
|
||||||
*/
|
*/
|
||||||
std::string getFileContent(const std::string& path);
|
std::string getFileContent(const std::string& path);
|
||||||
|
|
||||||
/** checks if file exists.
|
/** 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.
|
* @param path The absolute path provided in string format.
|
||||||
* @return Boolean representing if file exists or not.
|
* @return Boolean representing if file exists or not.
|
||||||
*/
|
*/
|
||||||
bool fileExists(const std::string& path);
|
bool fileExists(const std::string& path);
|
||||||
|
|
||||||
/** provides mimetype from filename.
|
/** 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.
|
* This function provides mimetype from file-name.
|
||||||
*
|
*
|
||||||
|
|
|
@ -70,7 +70,7 @@ Aria2::Aria2():
|
||||||
callCmd.push_back(rpc_secret.c_str());
|
callCmd.push_back(rpc_secret.c_str());
|
||||||
callCmd.push_back(rpc_port.c_str());
|
callCmd.push_back(rpc_port.c_str());
|
||||||
callCmd.push_back(download_dir.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(inputFile.c_str());
|
||||||
}
|
}
|
||||||
callCmd.push_back(session.c_str());
|
callCmd.push_back(session.c_str());
|
||||||
|
|
|
@ -108,7 +108,7 @@ void Book::updateFromXml(const pugi::xml_node& node, const std::string& baseDir)
|
||||||
path = computeAbsolutePath(baseDir, path);
|
path = computeAbsolutePath(baseDir, path);
|
||||||
}
|
}
|
||||||
m_path = path;
|
m_path = path;
|
||||||
m_pathValid = fileExists(path);
|
m_pathValid = fileReadable(path);
|
||||||
m_title = ATTR("title");
|
m_title = ATTR("title");
|
||||||
m_description = ATTR("description");
|
m_description = ATTR("description");
|
||||||
m_language = ATTR("language");
|
m_language = ATTR("language");
|
||||||
|
|
|
@ -305,16 +305,18 @@ std::string kiwix::getFileContent(const std::string& path)
|
||||||
bool kiwix::fileExists(const std::string& path)
|
bool kiwix::fileExists(const std::string& path)
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
return PathFileExistsW(Utf8ToWide(path).c_str());
|
return (_waccess_s(Utf8ToWide(path).c_str(), 0) == 0);
|
||||||
#else
|
#else
|
||||||
bool flag = false;
|
return (access(path.c_str(), F_OK) == 0);
|
||||||
std::fstream fin;
|
#endif
|
||||||
fin.open(path.c_str(), std::ios::in);
|
|
||||||
if (fin.is_open()) {
|
|
||||||
flag = true;
|
|
||||||
}
|
}
|
||||||
fin.close();
|
|
||||||
return flag;
|
bool kiwix::fileReadable(const std::string& path)
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
return (_waccess_s(Utf8ToWide(path).c_str(), 4) == 0);
|
||||||
|
#else
|
||||||
|
return (access(path.c_str(), R_OK) == 0);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,8 @@
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include "../include/tools.h"
|
#include "../include/tools.h"
|
||||||
#include "../src/tools/pathTools.h"
|
#include "../src/tools/pathTools.h"
|
||||||
|
|
||||||
|
@ -190,6 +192,34 @@ TEST(pathTools, appendToDirectory)
|
||||||
P5("a","b","c",".","foo.xml"));
|
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)
|
TEST(pathTools, goUp)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue