File: directory.cpp

package info (click to toggle)
zipios%2B%2B 0.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 952 kB
  • sloc: cpp: 4,150; ansic: 148; makefile: 132; sh: 1
file content (395 lines) | stat: -rw-r--r-- 12,410 bytes parent folder | download | duplicates (9)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/** \file
    This file and directory.h are borrowed from the dir_it library
    available at http://www.boost.org. dir_it is a directory iterator.
*/

// -*-C++-*- directory.cc
// <!!----------------------------------------------------------------------> 
// <!! Copyright (C) 1998 Dietmar Kuehl, Claas Solutions GmbH > 
// <!!> 
// <!! Permission to use, copy, modify, distribute and sell this > 
// <!! software for any purpose is hereby granted without fee, provided > 
// <!! that the above copyright notice appears in all copies and that > 
// <!! both that copyright notice and this permission notice appear in > 
// <!! supporting documentation. Dietmar Kuehl and Claas Solutions make no > 
// <!! representations about the suitability of this software for any > 
// <!! purpose. It is provided "as is" without express or implied warranty. > 
// <!!----------------------------------------------------------------------> 

// Author: Dietmar Kuehl dietmar.kuehl@claas-solutions.de 
// Title:  Implementation of the directory iterator
// Version: $Name:  $ $Id: directory.cpp,v 1.3 2003/11/08 18:20:51 thomas Exp $

// -------------------------------------------------------------------------- 

#include "directory.h"

#if defined(unix) || defined(__unix) || defined(__unix__)
#  define BOOST_UNIX 1
#elif defined(_WINDOWS)
#  define BOOST_WINNT 1
#endif 

// -------------------------------------------------------------------------- 
// The POSIX version uses the functions opendir(), readdir(), and closdir()
// to find directory entries. In addition, stat() is used to find out
// about specific file attributes.

#if defined(BOOST_UNIX)

#ifndef __USE_BSD
#define __USE_BSD
#endif 
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>

struct boost::filesystem::dir_it::representation
{
	representation():
		m_handle(0),
		m_refcount(1),
		m_stat_p(false)
	{
	}

	representation(std::string const &dirname):
		m_handle(opendir(dirname.c_str())),
		m_refcount(1),
		m_directory(dirname),
		m_stat_p(false)
	{
	        if( m_directory.size() == 0 )
	                m_directory = "./" ;
		if (m_directory[m_directory.size() - 1] != '/')
			m_directory += '/';
		operator++ ();
	}

	~representation() { if ( m_handle )  closedir(m_handle); }

	representation *reference()
	{
		++m_refcount;
		return this;
	}
	representation *release() { return --m_refcount? 0: this; }

	representation &operator++()
	{
		if (m_handle)
			{
				m_stat_p = false;
				dirent *rc = readdir(m_handle);
				if (rc != 0)
					m_current = rc->d_name;
				else
					{
						m_current = "";
						closedir(m_handle);
						m_handle = 0;
					}
			}

		return *this;
	}

	bool operator== (representation const &rep) const
	{
		return (m_handle == 0) == (rep.m_handle == 0);
	}

	std::string const &operator* () { return m_current; }

	struct stat &get_stat()
	{
		if (!m_stat_p)
			stat( (m_directory + m_current).c_str(), &m_stat);
		return m_stat;
	}

	void set_mode(mode_t m, bool nv)
	{
		if (((get_stat().st_mode & m) == 0) == nv)
			chmod((m_directory + m_current).c_str(), get_stat().st_mode ^ m);
	}
	void change_owner(uid_t uid) { chown((m_directory + m_current).c_str(), uid, get_stat().st_gid); }
	void change_group(gid_t gid) { chown((m_directory + m_current).c_str(), get_stat().st_uid, gid); }

private:
	DIR         *m_handle;
	int         m_refcount;
	std::string m_directory;
	std::string m_current;
	struct stat m_stat;
	bool        m_stat_p;
};

namespace boost
{
	namespace filesystem
	{

		template <> bool get<is_link>(dir_it const &it) { return S_ISLNK(it.rep->get_stat().st_mode); }
		template <> bool get<is_regular>(dir_it const &it) { return S_ISREG(it.rep->get_stat().st_mode); }
		template <> bool get<is_directory>(dir_it const &it) { return S_ISDIR(it.rep->get_stat().st_mode); }
		template <> bool get<is_char_device>(dir_it const &it) { return S_ISCHR(it.rep->get_stat().st_mode); }
		template <> bool get<is_block_device>(dir_it const &it) { return S_ISBLK(it.rep->get_stat().st_mode); }
		template <> bool get<is_fifo>(dir_it const &it) { return S_ISFIFO(it.rep->get_stat().st_mode); }
		template <> bool get<is_socket>(dir_it const &it) { return S_ISSOCK(it.rep->get_stat().st_mode); }

		template <> bool get<user_read>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IRUSR; }
		template <> void set<user_read>(dir_it const &it, bool nv) { it.rep->set_mode(S_IRUSR, nv); }
		template <> bool get<user_write>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IWUSR; }
		template <> void set<user_write>(dir_it const &it, bool nv) { it.rep->set_mode(S_IWUSR, nv); }
		template <> bool get<user_execute>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IXUSR; }
		template <> void set<user_execute>(dir_it const &it, bool nv) { it.rep->set_mode(S_IXUSR, nv); }
		template <> bool get<set_uid>(dir_it const &it) { return it.rep->get_stat().st_mode & S_ISUID; }
		template <> void set<set_uid>(dir_it const &it, bool nv) { it.rep->set_mode(S_ISUID, nv); }

		template <> bool get<group_read>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IRGRP; }
		template <> void set<group_read>(dir_it const &it, bool nv) { it.rep->set_mode(S_IRGRP, nv); }
		template <> bool get<group_write>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IWGRP; }
		template <> void set<group_write>(dir_it const &it, bool nv) { it.rep->set_mode(S_IWGRP, nv); }
		template <> bool get<group_execute>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IXGRP; }
		template <> void set<group_execute>(dir_it const &it, bool nv) { it.rep->set_mode(S_IXGRP, nv); }
		template <> bool get<set_gid>(dir_it const &it) { return it.rep->get_stat().st_mode & S_ISGID; }
		template <> void set<set_gid>(dir_it const &it, bool nv) { it.rep->set_mode(S_ISGID, nv); }

		template <> bool get<other_read>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IROTH; }
		template <> void set<other_read>(dir_it const &it, bool nv) { it.rep->set_mode(S_IROTH, nv); }
		template <> bool get<other_write>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IWOTH; }
		template <> void set<other_write>(dir_it const &it, bool nv) { it.rep->set_mode(S_IWOTH, nv); }
		template <> bool get<other_execute>(dir_it const &it) { return it.rep->get_stat().st_mode & S_IXOTH; }
		template <> void set<other_execute>(dir_it const &it, bool nv) { it.rep->set_mode(S_IXOTH, nv); }
		template <> bool get<sticky>(dir_it const &it) { return it.rep->get_stat().st_mode & S_ISVTX; }
		template <> void set<sticky>(dir_it const &it, bool nv) { it.rep->set_mode(S_ISVTX, nv); }

		template <> nlink_t get<links>(dir_it const &it) { return it.rep->get_stat().st_nlink; }
		template <> size_t get<size>(dir_it const &it) { return it.rep->get_stat().st_size; }
		template <> unsigned long get<blocks>(dir_it const &it) { return it.rep->get_stat().st_blocks; }
		template <> unsigned long get<blksize>(dir_it const &it) { return it.rep->get_stat().st_blksize; }

		template <> mtime::value_type get<mtime>(dir_it const &it) { return &it.rep->get_stat().st_mtime; }
		template <> atime::value_type get<atime>(dir_it const &it) { return &it.rep->get_stat().st_atime; }
		template <> ctime::value_type get<ctime>(dir_it const &it) { return &it.rep->get_stat().st_ctime; }

		template <> uid_t get<uid>(dir_it const &it) { return it.rep->get_stat().st_uid; }
		template <> void set<uid>(dir_it const &it, uid_t uid) { it.rep->change_owner(uid); }
		template <> std::string get<uname>(dir_it const &it)
			{
				struct passwd *pw = getpwuid(it.rep->get_stat().st_uid);
				if (pw == 0)
					throw unknown_uid(it.rep->get_stat().st_uid);
				return pw->pw_name;
			}
		template <> void set<uname>(dir_it const &it, std::string name)
			{
				struct passwd *pw = getpwnam(name.c_str());
				if (pw != 0)
					it.rep->change_owner(pw->pw_uid);
				else
					throw unknown_uname(name);
			}

		template <> gid_t get<gid>(dir_it const &it) { return it.rep->get_stat().st_gid; }
		template <> void set<gid>(dir_it const &it, gid_t gid) { it.rep->change_group(gid); }
		template <> std::string get<gname>(dir_it const &it)
			{
				struct group *grp = getgrgid(it.rep->get_stat().st_gid);
				if (grp == 0)
					throw unknown_gid(it.rep->get_stat().st_gid);
				return grp->gr_name;
			}
		template <> void set<gname>(dir_it const &it, std::string name)
			{
				struct group *grp = getgrnam(name.c_str());
				if (grp != 0)
					it.rep->change_group(grp->gr_gid);
				else
					throw unknown_gname(name);
			}


		template <> bool get<is_hidden>(dir_it const &it) { return (*it)[0] == '.'; }
	}
}

#elif defined(BOOST_WINNT)

#include "io.h"
#include "direct.h"

struct boost::filesystem::dir_it::representation
{
	representation():
		m_handle(-1),
		m_refcount(1)
	{
	}

	representation(std::string const &dirname):
		m_handle(_findfirst((dirname + "\\*").c_str(), &m_data)),
		m_refcount(1)
	{
	}

	~representation() { if (m_handle != -1) _findclose(m_handle); }

	representation *reference()
	{
		++m_refcount;
		return this;
	}
	representation *release() { return --m_refcount? 0: this; }

	representation &operator++()
	{
		if (m_handle != -1)
			{
				if (_findnext(m_handle, &m_data) == -1)
					{
						_findclose(m_handle);
						m_handle = -1;
					}

			}

		return *this;
	}

	bool operator== (representation const &rep) const
	{
		return (m_handle == -1) == (rep.m_handle == -1);
	}

	std::string operator* () { return m_data.name; }


	struct _finddata_t const &get_data() const
	{
		return m_data;
	}

#if 0
	void set_mode(mode_t m, bool nv)
	{
		if (((get_stat().st_mode & m) == 0) == nv)
			chmod((m_directory + m_current).c_str(), get_stat().st_mode ^ m);
	}
	void change_owner(uid_t uid) { chown((m_directory + m_current).c_str(), uid, get_stat().st_gid); }
	void change_group(gid_t gid) { chown((m_directory + m_current).c_str(), get_stat().st_uid, gid); }
#endif

private:
	struct _finddata_t m_data;
	long               m_handle;
	int                m_refcount;
	std::string        m_directory;
};

namespace boost
{
	namespace filesystem
	{
		get<size>::operator size::value_type() const
		{
			return m_it.rep->get_data().size;
		}
		get<mtime>::operator mtime::value_type() const
		{
			return &m_it.rep->get_data().time_write;
		}
		get<is_directory>::operator is_directory::value_type() const
		{
			return (m_it.rep->get_data().attrib & _A_SUBDIR) != 0;
		}
		get<is_regular>::operator is_regular::value_type() const
		{
			return (m_it.rep->get_data().attrib & _A_SUBDIR) == 0;
		}
		get<is_hidden>::operator is_hidden::value_type() const
		{
			return (m_it.rep->get_data().attrib & _A_HIDDEN) != 0;
		}
		get<user_read>::operator user_read::value_type() const
		{
			return true;
		}
		get<user_write>::operator user_write::value_type() const
		{
			return (m_it.rep->get_data().attrib & _A_RDONLY) == 0;
		}
		get<user_execute>::operator user_execute::value_type() const
		{
			std::string name(*m_it);
			std::string ext(name.substr(name.find_last_of('.')));
			return ext == ".exe" || ext == ".bat";
		}
	}
}
#endif

// -------------------------------------------------------------------------- 

boost::filesystem::dir_it::dir_it():
	rep(new representation())
{
}

boost::filesystem::dir_it::dir_it(std::string const &dirname):
	rep(new representation(dirname))
{
}

boost::filesystem::dir_it::dir_it(boost::filesystem::dir_it const &it):
	rep(it.rep->reference())
{
}

boost::filesystem::dir_it::~dir_it()
{
	delete rep->release();
}

boost::filesystem::dir_it &boost::filesystem::dir_it::operator= (boost::filesystem::dir_it const &it)
{
	it.rep->reference();
	delete rep->release();
	rep = it.rep;
	return *this;
}

// -------------------------------------------------------------------------- 

std::string boost::filesystem::dir_it::operator* () const
{
	return *(*rep);
}

boost::filesystem::dir_it &boost::filesystem::dir_it::operator++ ()
{
	++(*rep);
	return *this;
}

boost::filesystem::dir_it::proxy boost::filesystem::dir_it::operator++ (int)
{
	std::string rc(*(*rep));
	++(*rep);
	return rc;
}

// -------------------------------------------------------------------------- 

bool boost::filesystem::dir_it::operator== (boost::filesystem::dir_it const &it) const
{
	return *rep == *(it.rep);
}

bool boost::filesystem::dir_it::operator!= (boost::filesystem::dir_it const &it) const
{
	return !(*rep == *(it.rep));
}