Leosac  0.8.0
Open Source Access Control
unixfs.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
26 #include "unixfs.hpp"
27 
28 extern "C" {
29 #include <dirent.h>
30 #include <unistd.h>
31 }
32 
33 #include <algorithm>
34 #include <cerrno>
35 
36 #include "unixsyscall.hpp"
37 
38 using namespace Leosac::Tools;
39 
40 std::string UnixFs::getCWD()
41 {
42  char *str = getcwd(nullptr, 0);
43  std::string path;
44 
45  if (str)
46  {
47  path = str;
48  free(str);
49  return (path);
50  }
51  else
52  return (".");
53 }
54 
55 UnixFs::FileList UnixFs::listFiles(const std::string &folder,
56  const std::string &extension)
57 {
58  FileList l;
59  DIR *dir;
60  struct dirent *entry;
61  std::string filename;
62  std::string path = folder;
63 
64  if (path.empty())
65  throw(FsException("empty folder"));
66  if (*path.rbegin() != '/')
67  path.append("/");
68  if ((dir = opendir(path.c_str())) == nullptr)
69  throw(FsException(UnixSyscall::getErrorString("opendir", errno)));
70  errno = 0;
71  while ((entry = readdir(dir)) != nullptr)
72  {
73  if (entry->d_type != DT_REG)
74  continue;
75  filename = entry->d_name;
76  if (filename.size() < extension.size())
77  continue;
78  if (std::equal(extension.rbegin(), extension.rend(), filename.rbegin()))
79  l.push_back(path + filename);
80  }
81  if (errno)
82  throw(FsException(UnixSyscall::getErrorString("readdir", errno)));
83  if (closedir(dir) == -1)
84  throw(FsException(UnixSyscall::getErrorString("closedir", errno)));
85  return (l);
86 }
87 
88 std::string UnixFs::stripPath(const std::string &filename)
89 {
90  std::size_t pos;
91 
92  if ((pos = filename.find_last_of('/')) == std::string::npos)
93  return (filename);
94  else
95  return (filename.substr(pos + 1));
96 }
97 
98 std::string UnixFs::readAll(const std::string &path)
99 {
100  std::ifstream file(path);
101 
102  if (!file.good())
103  throw(FsException("could not open " + path + '\''));
104  return (std::string(
105  std::istreambuf_iterator<char>(static_cast<std::istream &>(file)),
106  std::istreambuf_iterator<char>()));
107 }
108 
109 bool UnixFs::fileExists(const std::string &path)
110 {
111  if (access(path.c_str(), F_OK) == -1)
112  {
113  if (errno == ENOENT)
114  return (false);
115  throw(FsException(UnixSyscall::getErrorString("access", errno)));
116  }
117  else
118  return (true);
119 }
unixsyscall.hpp
unix syscall helper functions
Leosac::Tools::UnixFs::listFiles
static FileList listFiles(const std::string &folder, const std::string &extension=std::string())
list all files with the extension ".extension" in folder
Definition: unixfs.cpp:55
Leosac::Tools::UnixFs::FileList
std::list< std::string > FileList
Definition: unixfs.hpp:44
Leosac::Tools::UnixFs::getCWD
static std::string getCWD()
get current working directory without trailing slash
Definition: unixfs.cpp:40
Leosac::Tools::UnixFs::readAll
static std::string readAll(const std::string &path)
read all file contents and put it in a string
Definition: unixfs.cpp:98
Leosac::Tools::UnixSyscall::getErrorString
static std::string getErrorString(const std::string &func, int errNo)
Definition: unixsyscall.cpp:32
Leosac::Tools::UnixFs::stripPath
static std::string stripPath(const std::string &filename)
remove the full path from a filename
Definition: unixfs.cpp:88
unixfs.hpp
unix filesystem helper functions
FsException
Definition: fsexception.hpp:33
Leosac::Tools::UnixFs::fileExists
static bool fileExists(const std::string &path)
Definition: unixfs.cpp:109
Leosac::Tools
Definition: DatabaseLogSink.hpp:27