|
a/src/utils/pathut.h |
|
b/src/utils/pathut.h |
|
... |
|
... |
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
#ifndef _PATHUT_H_INCLUDED_
|
17 |
#ifndef _PATHUT_H_INCLUDED_
|
18 |
#define _PATHUT_H_INCLUDED_
|
18 |
#define _PATHUT_H_INCLUDED_
|
19 |
#include "autoconfig.h"
|
|
|
20 |
|
19 |
|
21 |
#include <string>
|
20 |
#include <string>
|
22 |
#include <vector>
|
21 |
#include <vector>
|
23 |
#include <set>
|
22 |
#include <set>
|
24 |
|
23 |
|
25 |
#include MEMORY_INCLUDE
|
24 |
// Must be called in main thread before starting other threads
|
|
|
25 |
extern void pathut_init_mt();
|
26 |
|
26 |
|
27 |
/// Add a / at the end if none there yet.
|
27 |
/// Add a / at the end if none there yet.
|
28 |
extern void path_catslash(std::string &s);
|
28 |
extern void path_catslash(std::string& s);
|
29 |
/// Concatenate 2 paths
|
29 |
/// Concatenate 2 paths
|
30 |
extern std::string path_cat(const std::string &s1, const std::string &s2);
|
30 |
extern std::string path_cat(const std::string& s1, const std::string& s2);
|
31 |
/// Get the simple file name (get rid of any directory path prefix
|
31 |
/// Get the simple file name (get rid of any directory path prefix
|
32 |
extern std::string path_getsimple(const std::string &s);
|
32 |
extern std::string path_getsimple(const std::string& s);
|
33 |
/// Simple file name + optional suffix stripping
|
33 |
/// Simple file name + optional suffix stripping
|
34 |
extern std::string path_basename(const std::string &s,
|
34 |
extern std::string path_basename(const std::string& s,
|
35 |
const std::string &suff = std::string());
|
35 |
const std::string& suff = std::string());
|
36 |
/// Component after last '.'
|
36 |
/// Component after last '.'
|
37 |
extern std::string path_suffix(const std::string &s);
|
37 |
extern std::string path_suffix(const std::string& s);
|
38 |
/// Get the father directory
|
38 |
/// Get the father directory
|
39 |
extern std::string path_getfather(const std::string &s);
|
39 |
extern std::string path_getfather(const std::string& s);
|
40 |
/// Get the current user's home directory
|
40 |
/// Get the current user's home directory
|
41 |
extern std::string path_home();
|
41 |
extern std::string path_home();
|
42 |
/// Expand ~ at the beginning of std::string
|
42 |
/// Expand ~ at the beginning of std::string
|
43 |
extern std::string path_tildexpand(const std::string &s);
|
43 |
extern std::string path_tildexpand(const std::string& s);
|
44 |
/// Use getcwd() to make absolute path if needed. Beware: ***this can fail***
|
44 |
/// Use getcwd() to make absolute path if needed. Beware: ***this can fail***
|
45 |
/// we return an empty path in this case.
|
45 |
/// we return an empty path in this case.
|
46 |
extern std::string path_absolute(const std::string &s);
|
46 |
extern std::string path_absolute(const std::string& s);
|
47 |
/// Clean up path by removing duplicated / and resolving ../ + make it absolute
|
47 |
/// Clean up path by removing duplicated / and resolving ../ + make it absolute
|
48 |
extern std::string path_canon(const std::string &s, const std::string *cwd=0);
|
48 |
extern std::string path_canon(const std::string& s, const std::string *cwd = 0);
|
49 |
/// Use glob(3) to return the file names matching pattern inside dir
|
49 |
/// Use glob(3) to return the file names matching pattern inside dir
|
50 |
extern std::vector<std::string> path_dirglob(const std::string &dir,
|
50 |
extern std::vector<std::string> path_dirglob(const std::string& dir,
|
51 |
const std::string pattern);
|
51 |
const std::string pattern);
|
52 |
/// Encode according to rfc 1738
|
52 |
/// Encode according to rfc 1738
|
53 |
extern std::string url_encode(const std::string& url,
|
53 |
extern std::string url_encode(const std::string& url,
|
54 |
std::string::size_type offs = 0);
|
54 |
std::string::size_type offs = 0);
|
55 |
/// Transcode to utf-8 if possible or url encoding, for display.
|
|
|
56 |
extern bool printableUrl(const std::string &fcharset,
|
|
|
57 |
const std::string &in, std::string &out);
|
|
|
58 |
//// Convert to file path if url is like file://. This modifies the
|
55 |
//// Convert to file path if url is like file://. This modifies the
|
59 |
//// input (and returns a copy for convenience)
|
56 |
//// input (and returns a copy for convenience)
|
60 |
extern std::string fileurltolocalpath(std::string url);
|
57 |
extern std::string fileurltolocalpath(std::string url);
|
61 |
/// Test for file:/// url
|
58 |
/// Test for file:/// url
|
62 |
extern bool urlisfileurl(const std::string& url);
|
59 |
extern bool urlisfileurl(const std::string& url);
|
63 |
///
|
60 |
///
|
64 |
extern std::string url_parentfolder(const std::string& url);
|
61 |
extern std::string url_parentfolder(const std::string& url);
|
65 |
|
62 |
|
66 |
/// Return the host+path part of an url. This is not a general
|
63 |
/// Return the host+path part of an url. This is not a general
|
67 |
/// routine, it does the right thing only in the recoll context
|
64 |
/// routine, it does the right thing only in the recoll context
|
68 |
extern std::string url_gpath(const std::string& url);
|
65 |
extern std::string url_gpath(const std::string& url);
|
69 |
|
|
|
70 |
/// Same but, in the case of a Windows local path, also turn "c:/" into
|
|
|
71 |
/// "/c/" This should be used only for splitting the path in rcldb, it
|
|
|
72 |
/// would better be local in there, but I prefer to keep all the
|
|
|
73 |
/// system-specific path stuff in pathut
|
|
|
74 |
extern std::string url_gpathS(const std::string& url);
|
|
|
75 |
|
66 |
|
76 |
/// Stat parameter and check if it's a directory
|
67 |
/// Stat parameter and check if it's a directory
|
77 |
extern bool path_isdir(const std::string& path);
|
68 |
extern bool path_isdir(const std::string& path);
|
78 |
|
69 |
|
79 |
/// Retrieve file size
|
70 |
/// Retrieve file size
|
|
... |
|
... |
90 |
extern int path_fileprops(const std::string path, struct stat *stp,
|
81 |
extern int path_fileprops(const std::string path, struct stat *stp,
|
91 |
bool follow = true);
|
82 |
bool follow = true);
|
92 |
|
83 |
|
93 |
/// Check that path is traversable and last element exists
|
84 |
/// Check that path is traversable and last element exists
|
94 |
/// Returns true if last elt could be checked to exist. False may mean that
|
85 |
/// Returns true if last elt could be checked to exist. False may mean that
|
95 |
/// the file/dir does not exist or that an error occurred.
|
86 |
/// the file/dir does not exist or that an error occurred.
|
96 |
extern bool path_exists(const std::string& path);
|
87 |
extern bool path_exists(const std::string& path);
|
97 |
|
88 |
|
98 |
/// Return separator for PATH environment variable
|
89 |
/// Return separator for PATH environment variable
|
99 |
extern std::string path_PATHsep();
|
90 |
extern std::string path_PATHsep();
|
100 |
|
91 |
|
101 |
/// Dump directory
|
92 |
/// Dump directory
|
102 |
extern bool readdir(const std::string& dir, std::string& reason,
|
93 |
extern bool readdir(const std::string& dir, std::string& reason,
|
103 |
std::set<std::string>& entries);
|
94 |
std::set<std::string>& entries);
|
104 |
|
95 |
|
105 |
/** A small wrapper around statfs et al, to return percentage of disk
|
96 |
/** A small wrapper around statfs et al, to return percentage of disk
|
106 |
occupation */
|
97 |
occupation
|
107 |
bool fsocc(const std::string &path, int *pc, // Percent occupied
|
98 |
@param[output] pc percent occupied
|
108 |
long long *avmbs = 0 // Mbs available to non-superuser. Mb=1024*1024
|
99 |
@param[output] avmbs Mbs available to non-superuser. Mb=1024*1024
|
109 |
);
|
100 |
*/
|
110 |
|
101 |
bool fsocc(const std::string& path, int *pc, long long *avmbs = 0);
|
111 |
/// Retrieve the temp dir location: $RECOLL_TMPDIR else $TMPDIR else /tmp
|
|
|
112 |
extern const std::string& tmplocation();
|
|
|
113 |
|
|
|
114 |
/// Create temporary directory (inside the temp location)
|
|
|
115 |
extern bool maketmpdir(std::string& tdir, std::string& reason);
|
|
|
116 |
|
102 |
|
117 |
/// mkdir -p
|
103 |
/// mkdir -p
|
118 |
extern bool makepath(const std::string& path);
|
104 |
extern bool makepath(const std::string& path);
|
119 |
|
105 |
|
120 |
/// Sub-directory for default recoll config (e.g: .recoll)
|
|
|
121 |
extern std::string path_defaultrecollconfsubdir();
|
|
|
122 |
/// Where we create the user data subdirs
|
106 |
/// Where we create the user data subdirs
|
123 |
extern std::string path_homedata();
|
107 |
extern std::string path_homedata();
|
124 |
/// e.g. /usr/share/recoll. Depends on OS and config
|
|
|
125 |
extern const std::string& path_sharedatadir();
|
|
|
126 |
/// Test if path is absolute
|
108 |
/// Test if path is absolute
|
127 |
extern bool path_isabsolute(const std::string& s);
|
109 |
extern bool path_isabsolute(const std::string& s);
|
128 |
|
110 |
|
129 |
/// Test if path is root (x:/). root is defined by root/.. == root
|
111 |
/// Test if path is root (x:/). root is defined by root/.. == root
|
130 |
extern bool path_isroot(const std::string& p);
|
112 |
extern bool path_isroot(const std::string& p);
|
|
... |
|
... |
135 |
#ifdef _WIN32
|
117 |
#ifdef _WIN32
|
136 |
/// Convert \ separators to /
|
118 |
/// Convert \ separators to /
|
137 |
void path_slashize(std::string& s);
|
119 |
void path_slashize(std::string& s);
|
138 |
#endif
|
120 |
#endif
|
139 |
|
121 |
|
140 |
/// Temporary file class
|
|
|
141 |
class TempFileInternal {
|
|
|
142 |
public:
|
|
|
143 |
TempFileInternal(const std::string& suffix);
|
|
|
144 |
~TempFileInternal();
|
|
|
145 |
const char *filename()
|
|
|
146 |
{
|
|
|
147 |
return m_filename.c_str();
|
|
|
148 |
}
|
|
|
149 |
const std::string &getreason()
|
|
|
150 |
{
|
|
|
151 |
return m_reason;
|
|
|
152 |
}
|
|
|
153 |
void setnoremove(bool onoff)
|
|
|
154 |
{
|
|
|
155 |
m_noremove = onoff;
|
|
|
156 |
}
|
|
|
157 |
bool ok()
|
|
|
158 |
{
|
|
|
159 |
return !m_filename.empty();
|
|
|
160 |
}
|
|
|
161 |
private:
|
|
|
162 |
std::string m_filename;
|
|
|
163 |
std::string m_reason;
|
|
|
164 |
bool m_noremove;
|
|
|
165 |
};
|
|
|
166 |
|
|
|
167 |
typedef STD_SHARED_PTR<TempFileInternal> TempFile;
|
|
|
168 |
|
|
|
169 |
/// Temporary directory class. Recursively deleted by destructor.
|
|
|
170 |
class TempDir {
|
|
|
171 |
public:
|
|
|
172 |
TempDir();
|
|
|
173 |
~TempDir();
|
|
|
174 |
const char *dirname() {return m_dirname.c_str();}
|
|
|
175 |
const std::string &getreason() {return m_reason;}
|
|
|
176 |
bool ok() {return !m_dirname.empty();}
|
|
|
177 |
/// Recursively delete contents but not self.
|
|
|
178 |
bool wipe();
|
|
|
179 |
private:
|
|
|
180 |
std::string m_dirname;
|
|
|
181 |
std::string m_reason;
|
|
|
182 |
TempDir(const TempDir &) {}
|
|
|
183 |
TempDir& operator=(const TempDir &) {return *this;};
|
|
|
184 |
};
|
|
|
185 |
|
|
|
186 |
/// Lock/pid file class. This is quite close to the pidfile_xxx
|
122 |
/// Lock/pid file class. This is quite close to the pidfile_xxx
|
187 |
/// utilities in FreeBSD with a bit more encapsulation. I'd have used
|
123 |
/// utilities in FreeBSD with a bit more encapsulation. I'd have used
|
188 |
/// the freebsd code if it was available elsewhere
|
124 |
/// the freebsd code if it was available elsewhere
|
189 |
class Pidfile {
|
125 |
class Pidfile {
|
190 |
public:
|
126 |
public:
|
191 |
Pidfile(const std::string& path) : m_path(path), m_fd(-1) {}
|
127 |
Pidfile(const std::string& path) : m_path(path), m_fd(-1) {}
|
192 |
~Pidfile();
|
128 |
~Pidfile();
|
193 |
/// Open/create the pid file.
|
129 |
/// Open/create the pid file.
|
194 |
/// @return 0 if ok, > 0 for pid of existing process, -1 for other error.
|
130 |
/// @return 0 if ok, > 0 for pid of existing process, -1 for other error.
|
195 |
pid_t open();
|
131 |
pid_t open();
|
196 |
/// Write pid into the pid file
|
132 |
/// Write pid into the pid file
|
|
... |
|
... |
198 |
int write_pid();
|
134 |
int write_pid();
|
199 |
/// Close the pid file (unlocks)
|
135 |
/// Close the pid file (unlocks)
|
200 |
int close();
|
136 |
int close();
|
201 |
/// Delete the pid file
|
137 |
/// Delete the pid file
|
202 |
int remove();
|
138 |
int remove();
|
203 |
const std::string& getreason() {return m_reason;}
|
139 |
const std::string& getreason() {
|
|
|
140 |
return m_reason;
|
|
|
141 |
}
|
204 |
private:
|
142 |
private:
|
205 |
std::string m_path;
|
143 |
std::string m_path;
|
206 |
int m_fd;
|
144 |
int m_fd;
|
207 |
std::string m_reason;
|
145 |
std::string m_reason;
|
208 |
pid_t read_pid();
|
146 |
pid_t read_pid();
|
209 |
int flopen();
|
147 |
int flopen();
|
210 |
};
|
148 |
};
|
211 |
|
149 |
|
212 |
|
|
|
213 |
|
|
|
214 |
// Freedesktop thumbnail standard path routine
|
|
|
215 |
// On return, path will have the appropriate value in all cases,
|
|
|
216 |
// returns true if the file already exists
|
|
|
217 |
extern bool thumbPathForUrl(const std::string& url, int size, std::string& path);
|
|
|
218 |
|
|
|
219 |
// Must be called in main thread before starting other threads
|
|
|
220 |
extern void pathut_init_mt();
|
|
|
221 |
|
|
|
222 |
#endif /* _PATHUT_H_INCLUDED_ */
|
150 |
#endif /* _PATHUT_H_INCLUDED_ */
|