summaryrefslogtreecommitdiffabout
path: root/lib/sitecing_util.cc
blob: 5466b281ce24a2ef3d73b0688b1fa49164c11378 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#ifdef USE_PCH
 #include "pch.h"
#else
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <sys/ipc.h>
 #include <sys/sem.h>
 #include <errno.h>
 #include <iostream>
 #include <fstream>
 #include <cassert>
 #include "sitecing/sitecing_util.h"
#endif

namespace sitecing {

    /*
     * XXX: all of these utilities could be sheerly optimized.
     */

    string normalize_path(const string& path,int opts) {
	const char *s = path.c_str();
	string rv;
	string::size_type notslash = 0;
	if( (*s)=='.' && s[1]=='/' )
	    s+=2;
	if(opts&strip_leading_slash)
	    for(;(*s) && (*s)=='/';s++);
	for(;*s;s++) {
	    if( (*s)=='/' ) {
		if(s[1]=='/')
		    continue;
		if(s[1]=='.' && s[2]=='/') {
		    s+=2;
		    continue;
		}
	    }
	    if(opts&restrict_dotdot) {
		if(
			( rv.empty() && s[0]=='.' && s[1]=='.' && s[2]=='/' )	// "^../"
			|| ( s[0]=='/' && s[1]=='.' && s[2]=='.' && (s[3]==0 || s[3]=='/') ) // "/..(/|$)"
		  )
		    throw utility_restricted_sequence(CODEPOINT,"restricted updir sequence encountered");
	    }
	    rv += *s;
	    if( (*s) != '/' )
		notslash=rv.length();
	}
	if(!(opts&strip_trailing_slash))
	    notslash++;
	if(notslash<rv.length())
	    rv.erase(notslash); // XXX: check the logic of stripping/not strippling trailing slash
	return rv;
    }

    string strip_prefix(const string& str,const string& prefix) {
	if(str.compare(0,prefix.length(),prefix))
	    throw utility_no_prefix(CODEPOINT,"no such prefix");
	return str.substr(prefix.length());
    }

    string strip_suffix(const string& str,const string& suffix) {
	if(str.compare(str.length()-suffix.length(),suffix.length(),suffix))
	    throw utility_no_suffix(CODEPOINT,"no such suffix");
	return str.substr(0,str.length()-suffix.length());
    }

    string dir_name(const string& filename) {
	string::size_type sl = filename.find_last_of('/');
	if(sl==string::npos)
	    return ""; // no slashes -- no dir.
	string::size_type nosl = filename.find_last_not_of('/',sl);
	if(nosl==string::npos)
	    return ""; // only slashes -- no dir. XXX: only slashes after the last slash... does it mean no dir?
	return filename.substr(0,nosl+1);
    }

    void make_path(const string& path,mode_t mode) {
	struct stat st;
	for(string::size_type sl=0;sl!=string::npos;sl=path.find('/',sl+1)) {
	    if(!sl)
		continue;
	    string p = path.substr(0,sl);
	    if(stat(p.c_str(),&st) || !S_ISDIR(st.st_mode)) {
		if(mkdir(p.c_str(),mode))
		    throw konforka::exception(CODEPOINT,"failed to mkdir()");
	    }
	}
	if(stat(path.c_str(),&st) || !S_ISDIR(st.st_mode)) {
	    if(mkdir(path.c_str(),mode))
		throw konforka::exception(CODEPOINT,"failed to mkdir()");
	}
    }

    void file_lock::lock(const string& f) {
	unlock();
	fd = open(f.c_str(),O_CREAT|O_RDWR,S_IRUSR|S_IWUSR);
	if(fd<0)
	    throw konforka::exception(CODEPOINT,"failed to open/create lockfile");
	try {
	    lock();
	}catch(konforka::exception& ke) {
	    ke.see(CODEPOINT);
	    close(fd); fd=-1;
	    throw;
	}catch(...) {
	    close(fd); fd=-1;
	    throw;
	}
    }
    void file_lock::lock() {
	assert(fd>=0);
	struct flock fl;
	fl.l_type = F_WRLCK;
	fl.l_whence=SEEK_SET;
	fl.l_start=fl.l_len=0;
	for(int tries=3;tries;tries--) {
	    if(!fcntl(fd,F_SETLK,&fl))
		return;
	    sleep(8);
	}
	throw konforka::exception(CODEPOINT,"failed to obtain file lock");
    }
    void file_lock::unlock() {
	if(fd<0)
	    return;
	struct flock fl;
	fl.l_type = F_UNLCK;
	fl.l_whence=SEEK_SET;
	fl.l_start=fl.l_len=0;
	int rv = fcntl(fd,F_SETLK,&fl);
	close(fd);
	fd=-1;
	if(rv)
	    throw konforka::exception(CODEPOINT,"failed to release file lock");
    }

    void pid_file::set(const string& f,bool u) {
	ofstream of(f.c_str(),ios::trunc);
	if(!of)
	    throw konforka::exception(CODEPOINT,"failed to open file for writing pid");
	of << getpid() << endl;
	of.close();
	file_name = f;
	unlink_pid = u;
    }
    void pid_file::unlink() {
	if(!unlink_pid)
	    return;
	::unlink(file_name.c_str());
    }

    void semaphore::init() {
	deinit();
	semid = semget(IPC_PRIVATE,1,IPC_CREAT|0600);
	if(semid<0)
	    throw konforka::exception(CODEPOINT,"failed to semget()");
	if(semctl(semid,0,SETVAL,1))
	    throw konforka::exception(CODEPOINT,"failed to semctl()");
    }
    void semaphore::deinit() {
	if(semid<0)
	    return;
	semctl(semid,0,IPC_RMID,0);
    }
    void semaphore::on() {
	assert(semid>=0);
	struct sembuf sb;
	sb.sem_num=0;
	sb.sem_op=-1;
	sb.sem_flg = SEM_UNDO;
	while(semop(semid,&sb,1)<0) {
	    if(errno!=EINTR)
		throw konforka::exception(CODEPOINT,"failed to semop()");
	}
    }
    void semaphore::off() {
	assert(semid>=0);
	struct sembuf sb;
	sb.sem_num=0;
	sb.sem_op=1;
	sb.sem_flg = SEM_UNDO;
	while(semop(semid,&sb,1)<0) {
	    if(errno!=EINTR)
		throw konforka::exception(CODEPOINT,"failed to semop()");
	}
    }

    void semaphore_lock::lock() {
	assert(sem);
	if(locked)
	    return;
	sem->on();
	locked = true;
    }
    void semaphore_lock::unlock() {
	if(!sem)
	    return;
	if(!locked)
	    return;
	sem->off();
	locked=false;
    }

    string combine_path(const string& origin,const string& relative,int opts) {
	string r = normalize_path(relative,0);
	string rv;
	// XXX: what to do if relative is empty is a question, really.
	if(r.empty()) {
	    return normalize_path( (opts&origin_is_file)?dir_name(origin):origin ,strip_leading_slash|restrict_dotdot|strip_trailing_slash);
	}else{
	    if(r[0]=='/') {
		r.erase(0,1);
	    }else{
		rv = normalize_path((opts&origin_is_file)?dir_name(origin):origin,restrict_dotdot|strip_trailing_slash);
	    }
	}
	string::size_type lsl = rv.rfind('/');
	for(string::size_type sl=r.find('/');sl!=string::npos;sl=r.find('/')) {
	    assert(sl!=0);
	    if(sl==1 && r[0]=='.') {
		// it's a "./"
		r.erase(0,2);
	    }else if(sl==2 && r[0]=='.' && r[1]=='.') {
		// we have a "../"
		if(lsl==string::npos) {
		    if(rv.empty() && (opts&fail_beyond_root))
			throw utility_beyond_root(CODEPOINT,"went beyond root while combining path");
		    rv.clear();
		}else{
		    rv.erase(lsl);
		    lsl = rv.rfind('/');
		}
		r.erase(0,3);
	    }else{
		// we have a "something/"
		lsl = rv.length();
		rv += '/';
		rv += r.substr(0,sl);
		r.erase(0,sl+1);
	    }
	}
	if(r.empty())
	    return rv+'/';
	if(r.length()==2 && r[0]=='.' && r[0]=='.') {
	    if(lsl==string::npos) {
		if(rv.empty() & (opts&fail_beyond_root))
		    throw utility_beyond_root(CODEPOINT,"went beyond root while combining path");
		return "/";
	    }else{
		rv.erase(lsl+1);
		return rv;
	    }
	}
	rv += '/';
	rv += r;
	return rv;
    }

    void auto_chdir::pushdir(const string& td,bool ap) {
	/* TODO: make use of fchdir(2) instead */
	char *tmp = getcwd(0,0);
	assert(tmp);
	saved_pwd = tmp;
	free(tmp);
	autopop=ap;
	if(chdir(td.c_str()))
	    throw konforka::exception(CODEPOINT,"failed to chdir()");
    }
    void auto_chdir::popdir() {
	autopop=false;
	if(chdir(saved_pwd.c_str()))
	    throw konforka::exception(CODEPOINT,"failed to chdir()");
	// XXX: or should it be thrown? after all we call it from destructor...
    }

}