|
a/src/utils/pathut.h |
|
b/src/utils/pathut.h |
|
... |
|
... |
18 |
#define _PATHUT_H_INCLUDED_
|
18 |
#define _PATHUT_H_INCLUDED_
|
19 |
#include <unistd.h>
|
19 |
#include <unistd.h>
|
20 |
|
20 |
|
21 |
#include <string>
|
21 |
#include <string>
|
22 |
#include <vector>
|
22 |
#include <vector>
|
|
|
23 |
#include <set>
|
|
|
24 |
|
23 |
#include "refcntr.h"
|
25 |
#include "refcntr.h"
|
24 |
|
26 |
|
25 |
#ifndef NO_NAMESPACES
|
|
|
26 |
using std::string;
|
|
|
27 |
using std::vector;
|
|
|
28 |
#endif
|
|
|
29 |
|
|
|
30 |
/// Add a / at the end if none there yet.
|
27 |
/// Add a / at the end if none there yet.
|
31 |
extern void path_catslash(string &s);
|
28 |
extern void path_catslash(std::string &s);
|
32 |
/// Concatenate 2 paths
|
29 |
/// Concatenate 2 paths
|
33 |
extern string path_cat(const string &s1, const string &s2);
|
30 |
extern std::string path_cat(const std::string &s1, const std::string &s2);
|
34 |
/// 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
|
35 |
extern string path_getsimple(const string &s);
|
32 |
extern std::string path_getsimple(const std::string &s);
|
36 |
/// Simple file name + optional suffix stripping
|
33 |
/// Simple file name + optional suffix stripping
|
37 |
extern string path_basename(const string &s, const string &suff=string());
|
34 |
extern std::string path_basename(const std::string &s,
|
|
|
35 |
const std::string &suff = std::string());
|
|
|
36 |
/// Component after last '.'
|
|
|
37 |
extern std::string path_suffix(const std::string &s);
|
38 |
/// Get the father directory
|
38 |
/// Get the father directory
|
39 |
extern string path_getfather(const 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 string path_home();
|
41 |
extern std::string path_home();
|
42 |
/// Expand ~ at the beginning of string
|
42 |
/// Expand ~ at the beginning of std::string
|
43 |
extern string path_tildexpand(const 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 string path_absolute(const 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 string path_canon(const string &s);
|
48 |
extern std::string path_canon(const std::string &s);
|
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 vector<string> path_dirglob(const string &dir,
|
50 |
extern std::vector<std::string> path_dirglob(const std::string &dir,
|
51 |
const string pattern);
|
51 |
const std::string pattern);
|
52 |
/// Encode according to rfc 1738
|
52 |
/// Encode according to rfc 1738
|
53 |
extern string url_encode(const string& url,
|
53 |
extern std::string url_encode(const std::string& url,
|
54 |
string::size_type offs = 0);
|
54 |
std::string::size_type offs = 0);
|
55 |
/// Transcode to utf-8 if possible or url encoding, for display.
|
55 |
/// Transcode to utf-8 if possible or url encoding, for display.
|
56 |
extern bool printableUrl(const string &fcharset,
|
56 |
extern bool printableUrl(const std::string &fcharset,
|
57 |
const string &in, string &out);
|
57 |
const std::string &in, std::string &out);
|
58 |
//// Convert to file path if url is like file://. This modifies the
|
58 |
//// Convert to file path if url is like file://. This modifies the
|
59 |
//// input (and returns a copy for convenience)
|
59 |
//// input (and returns a copy for convenience)
|
60 |
extern string fileurltolocalpath(string url);
|
60 |
extern std::string fileurltolocalpath(std::string url);
|
61 |
/// Test for file:/// url
|
61 |
/// Test for file:/// url
|
62 |
extern bool urlisfileurl(const string& url);
|
62 |
extern bool urlisfileurl(const std::string& url);
|
63 |
|
63 |
|
64 |
/// Return the host+path part of an url. This is not a general
|
64 |
/// Return the host+path part of an url. This is not a general
|
65 |
/// routine, it does the right thing only in the recoll context
|
65 |
/// routine, it does the right thing only in the recoll context
|
66 |
extern string url_gpath(const string& url);
|
66 |
extern std::string url_gpath(const std::string& url);
|
67 |
|
67 |
|
68 |
/// Stat parameter and check if it's a directory
|
68 |
/// Stat parameter and check if it's a directory
|
69 |
extern bool path_isdir(const string& path);
|
69 |
extern bool path_isdir(const std::string& path);
|
|
|
70 |
|
|
|
71 |
/// Dump directory
|
|
|
72 |
extern bool readdir(const std::string& dir, std::string& reason,
|
|
|
73 |
std::set<std::string>& entries);
|
70 |
|
74 |
|
71 |
/** A small wrapper around statfs et al, to return percentage of disk
|
75 |
/** A small wrapper around statfs et al, to return percentage of disk
|
72 |
occupation */
|
76 |
occupation */
|
73 |
bool fsocc(const string &path, int *pc, // Percent occupied
|
77 |
bool fsocc(const std::string &path, int *pc, // Percent occupied
|
74 |
long *avmbs = 0 // Mbs available to non-superuser
|
78 |
long *avmbs = 0 // Mbs available to non-superuser
|
75 |
);
|
79 |
);
|
76 |
|
80 |
|
77 |
/// Retrieve the temp dir location: $RECOLL_TMPDIR else $TMPDIR else /tmp
|
81 |
/// Retrieve the temp dir location: $RECOLL_TMPDIR else $TMPDIR else /tmp
|
78 |
extern const string& tmplocation();
|
82 |
extern const std::string& tmplocation();
|
79 |
|
83 |
|
80 |
/// Create temporary directory (inside the temp location)
|
84 |
/// Create temporary directory (inside the temp location)
|
81 |
extern bool maketmpdir(string& tdir, string& reason);
|
85 |
extern bool maketmpdir(std::string& tdir, std::string& reason);
|
82 |
|
86 |
|
83 |
/// mkdir -p
|
87 |
/// mkdir -p
|
84 |
extern bool makepath(const string& path);
|
88 |
extern bool makepath(const std::string& path);
|
85 |
|
89 |
|
86 |
/// Temporary file class
|
90 |
/// Temporary file class
|
87 |
class TempFileInternal {
|
91 |
class TempFileInternal {
|
88 |
public:
|
92 |
public:
|
89 |
TempFileInternal(const string& suffix);
|
93 |
TempFileInternal(const std::string& suffix);
|
90 |
~TempFileInternal();
|
94 |
~TempFileInternal();
|
91 |
const char *filename()
|
95 |
const char *filename()
|
92 |
{
|
96 |
{
|
93 |
return m_filename.c_str();
|
97 |
return m_filename.c_str();
|
94 |
}
|
98 |
}
|
95 |
const string &getreason()
|
99 |
const std::string &getreason()
|
96 |
{
|
100 |
{
|
97 |
return m_reason;
|
101 |
return m_reason;
|
98 |
}
|
102 |
}
|
99 |
void setnoremove(bool onoff)
|
103 |
void setnoremove(bool onoff)
|
100 |
{
|
104 |
{
|
|
... |
|
... |
103 |
bool ok()
|
107 |
bool ok()
|
104 |
{
|
108 |
{
|
105 |
return !m_filename.empty();
|
109 |
return !m_filename.empty();
|
106 |
}
|
110 |
}
|
107 |
private:
|
111 |
private:
|
108 |
string m_filename;
|
112 |
std::string m_filename;
|
109 |
string m_reason;
|
113 |
std::string m_reason;
|
110 |
bool m_noremove;
|
114 |
bool m_noremove;
|
111 |
};
|
115 |
};
|
112 |
|
116 |
|
113 |
typedef RefCntr<TempFileInternal> TempFile;
|
117 |
typedef RefCntr<TempFileInternal> TempFile;
|
114 |
|
118 |
|
|
... |
|
... |
116 |
class TempDir {
|
120 |
class TempDir {
|
117 |
public:
|
121 |
public:
|
118 |
TempDir();
|
122 |
TempDir();
|
119 |
~TempDir();
|
123 |
~TempDir();
|
120 |
const char *dirname() {return m_dirname.c_str();}
|
124 |
const char *dirname() {return m_dirname.c_str();}
|
121 |
const string &getreason() {return m_reason;}
|
125 |
const std::string &getreason() {return m_reason;}
|
122 |
bool ok() {return !m_dirname.empty();}
|
126 |
bool ok() {return !m_dirname.empty();}
|
123 |
/// Recursively delete contents but not self.
|
127 |
/// Recursively delete contents but not self.
|
124 |
bool wipe();
|
128 |
bool wipe();
|
125 |
private:
|
129 |
private:
|
126 |
string m_dirname;
|
130 |
std::string m_dirname;
|
127 |
string m_reason;
|
131 |
std::string m_reason;
|
128 |
TempDir(const TempDir &) {}
|
132 |
TempDir(const TempDir &) {}
|
129 |
TempDir& operator=(const TempDir &) {return *this;};
|
133 |
TempDir& operator=(const TempDir &) {return *this;};
|
130 |
};
|
134 |
};
|
131 |
|
135 |
|
132 |
/// Lock/pid file class. This is quite close to the pidfile_xxx
|
136 |
/// Lock/pid file class. This is quite close to the pidfile_xxx
|
133 |
/// utilities in FreeBSD with a bit more encapsulation. I'd have used
|
137 |
/// utilities in FreeBSD with a bit more encapsulation. I'd have used
|
134 |
/// the freebsd code if it was available elsewhere
|
138 |
/// the freebsd code if it was available elsewhere
|
135 |
class Pidfile {
|
139 |
class Pidfile {
|
136 |
public:
|
140 |
public:
|
137 |
Pidfile(const string& path) : m_path(path), m_fd(-1) {}
|
141 |
Pidfile(const std::string& path) : m_path(path), m_fd(-1) {}
|
138 |
~Pidfile();
|
142 |
~Pidfile();
|
139 |
/// Open/create the pid file.
|
143 |
/// Open/create the pid file.
|
140 |
/// @return 0 if ok, > 0 for pid of existing process, -1 for other error.
|
144 |
/// @return 0 if ok, > 0 for pid of existing process, -1 for other error.
|
141 |
pid_t open();
|
145 |
pid_t open();
|
142 |
/// Write pid into the pid file
|
146 |
/// Write pid into the pid file
|
|
... |
|
... |
144 |
int write_pid();
|
148 |
int write_pid();
|
145 |
/// Close the pid file (unlocks)
|
149 |
/// Close the pid file (unlocks)
|
146 |
int close();
|
150 |
int close();
|
147 |
/// Delete the pid file
|
151 |
/// Delete the pid file
|
148 |
int remove();
|
152 |
int remove();
|
149 |
const string& getreason() {return m_reason;}
|
153 |
const std::string& getreason() {return m_reason;}
|
150 |
private:
|
154 |
private:
|
151 |
string m_path;
|
155 |
std::string m_path;
|
152 |
int m_fd;
|
156 |
int m_fd;
|
153 |
string m_reason;
|
157 |
std::string m_reason;
|
154 |
pid_t read_pid();
|
158 |
pid_t read_pid();
|
155 |
int flopen();
|
159 |
int flopen();
|
156 |
};
|
160 |
};
|
157 |
|
161 |
|
158 |
|
162 |
|
159 |
|
163 |
|
160 |
// Freedesktop thumbnail standard path routine
|
164 |
// Freedesktop thumbnail standard path routine
|
161 |
// On return, path will have the appropriate value in all cases,
|
165 |
// On return, path will have the appropriate value in all cases,
|
162 |
// returns true if the file already exists
|
166 |
// returns true if the file already exists
|
163 |
extern bool thumbPathForUrl(const string& url, int size, string& path);
|
167 |
extern bool thumbPathForUrl(const std::string& url, int size, std::string& path);
|
164 |
|
168 |
|
165 |
// Must be called in main thread before starting other threads
|
169 |
// Must be called in main thread before starting other threads
|
166 |
extern void pathut_init_mt();
|
170 |
extern void pathut_init_mt();
|
167 |
|
171 |
|
168 |
#endif /* _PATHUT_H_INCLUDED_ */
|
172 |
#endif /* _PATHUT_H_INCLUDED_ */
|