aboutsummaryrefslogtreecommitdiffstats
path: root/src/platforms/posix/posixFile.cpp
blob: b371fe73b7d9449debf9193a57928e7a9dd34219 (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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//
// VMime library (http://www.vmime.org)
// Copyright (C) 2002-2008 Vincent Richard <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library.  Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//

#include "vmime/platforms/posix/posixFile.hpp"

#include <unistd.h>
#include <fcntl.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <dirent.h>

#include <stdio.h>
#include <string.h>

#include "vmime/exception.hpp"


#if VMIME_HAVE_FILESYSTEM_FEATURES


namespace vmime {
namespace platforms {
namespace posix {


//
// posixFileIterator
//

posixFileIterator::posixFileIterator(const vmime::utility::file::path& path, const vmime::string& nativePath)
	: m_path(path), m_nativePath(nativePath), m_dir(NULL), m_dirEntry(NULL)
{
	if ((m_dir = ::opendir(m_nativePath.c_str())) == NULL)
		posixFileSystemFactory::reportError(path, errno);

	getNextElement();
}


posixFileIterator::~posixFileIterator()
{
	if (m_dir != NULL)
		::closedir(m_dir);
}


bool posixFileIterator::hasMoreElements() const
{
	return (m_dirEntry != NULL);
}


ref <vmime::utility::file> posixFileIterator::nextElement()
{
	ref <posixFile> file = vmime::create <posixFile>
		(m_path / vmime::utility::file::path::component(m_dirEntry->d_name));

	getNextElement();

	return (file);
}


void posixFileIterator::getNextElement()
{
	while ((m_dirEntry = ::readdir(m_dir)) != NULL)
	{
		const char* name = m_dirEntry->d_name;
		const int len = ::strlen(name);

		if (!(len == 1 && name[0] == '.') &&
		    !(len == 2 && name[0] == '.' && name[1] == '.'))
		{
			break;
		}
	}
}



//
// posixFileWriterOutputStream
//

posixFileWriterOutputStream::posixFileWriterOutputStream(const vmime::utility::file::path& path, const int fd)
	: m_path(path), m_fd(fd)
{
}


posixFileWriterOutputStream::~posixFileWriterOutputStream()
{
	::close(m_fd);
}


void posixFileWriterOutputStream::write(const value_type* const data, const size_type count)
{
	if (::write(m_fd, data, count) == -1)
		posixFileSystemFactory::reportError(m_path, errno);
}


void posixFileWriterOutputStream::flush()
{
	::fsync(m_fd);
}



//
// posixFileReaderInputStream
//

posixFileReaderInputStream::posixFileReaderInputStream(const vmime::utility::file::path& path, const int fd)
	: m_path(path), m_fd(fd), m_eof(false)
{
}


posixFileReaderInputStream::~posixFileReaderInputStream()
{
	::close(m_fd);
}


bool posixFileReaderInputStream::eof() const
{
	return (m_eof);
}


void posixFileReaderInputStream::reset()
{
	::lseek(m_fd, 0, SEEK_SET);
}


vmime::utility::stream::size_type posixFileReaderInputStream::read
	(value_type* const data, const size_type count)
{
	ssize_t c = 0;

	if ((c = ::read(m_fd, data, count)) == -1)
		posixFileSystemFactory::reportError(m_path, errno);

	if (c == 0)
		m_eof = true;

	return static_cast <size_type>(c);
}


vmime::utility::stream::size_type posixFileReaderInputStream::skip(const size_type count)
{
	const off_t curPos = ::lseek(m_fd, 0, SEEK_CUR);
	const off_t newPos = ::lseek(m_fd, count, SEEK_CUR);

	return static_cast <size_type>(newPos - curPos);
}



//
// posixFileWriter
//

posixFileWriter::posixFileWriter(const vmime::utility::file::path& path, const vmime::string& nativePath)
	: m_path(path), m_nativePath(nativePath)
{
}


ref <vmime::utility::outputStream> posixFileWriter::getOutputStream()
{
	int fd = 0;

	if ((fd = ::open(m_nativePath.c_str(), O_WRONLY, 0660)) == -1)
		posixFileSystemFactory::reportError(m_path, errno);

	return vmime::create <posixFileWriterOutputStream>(m_path, fd);
}



//
// posixFileReader
//

posixFileReader::posixFileReader(const vmime::utility::file::path& path, const vmime::string& nativePath)
	: m_path(path), m_nativePath(nativePath)
{
}


ref <vmime::utility::inputStream> posixFileReader::getInputStream()
{
	int fd = 0;

	if ((fd = ::open(m_nativePath.c_str(), O_RDONLY, 0640)) == -1)
		posixFileSystemFactory::reportError(m_path, errno);

	return vmime::create <posixFileReaderInputStream>(m_path, fd);
}



//
// posixFile
//

posixFile::posixFile(const vmime::utility::file::path& path)
	: m_path(path), m_nativePath(posixFileSystemFactory::pathToStringImpl(path))
{
}


void posixFile::createFile()
{
	int fd = 0;

	if ((fd = ::open(m_nativePath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660)) == -1)
		posixFileSystemFactory::reportError(m_path, errno);

	::close(fd);
}


void posixFile::createDirectory(const bool createAll)
{
	createDirectoryImpl(m_path, m_path, createAll);
}


bool posixFile::isFile() const
{
	struct stat buf;
	return (::stat(m_nativePath.c_str(), &buf) == 0 && S_ISREG(buf.st_mode));
}


bool posixFile::isDirectory() const
{
	struct stat buf;
	return (::stat(m_nativePath.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode));
}


bool posixFile::canRead() const
{
	struct stat buf;
	return (::stat(m_nativePath.c_str(), &buf) == 0 &&
			S_ISREG(buf.st_mode) &&
			::access(m_nativePath.c_str(), R_OK | F_OK) == 0);
}


bool posixFile::canWrite() const
{
	struct stat buf;
	return (::stat(m_nativePath.c_str(), &buf) == 0 &&
			S_ISREG(buf.st_mode) &&
			::access(m_nativePath.c_str(), W_OK | F_OK) == 0);
}


posixFile::length_type posixFile::getLength()
{
	struct stat buf;

	if (::stat(m_nativePath.c_str(), &buf) != 0)
		posixFileSystemFactory::reportError(m_path, errno);

	return static_cast <length_type>(buf.st_size);
}


const posixFile::path& posixFile::getFullPath() const
{
	return (m_path);
}


bool posixFile::exists() const
{
	struct stat buf;
	return (::stat(m_nativePath.c_str(), &buf) == 0);
}


ref <vmime::utility::file> posixFile::getParent() const
{
	if (m_path.isEmpty())
		return NULL;
	else
		return vmime::create <posixFile>(m_path.getParent());
}


void posixFile::rename(const path& newName)
{
	const vmime::string newNativePath = posixFileSystemFactory::pathToStringImpl(newName);

	if (::rename(m_nativePath.c_str(), newNativePath.c_str()) != 0)
		posixFileSystemFactory::reportError(m_path, errno);

	m_path = newName;
	m_nativePath = newNativePath;
}


void posixFile::remove()
{
	struct stat buf;

	if (::stat(m_nativePath.c_str(), &buf) != 0)
		posixFileSystemFactory::reportError(m_path, errno);

	if (S_ISDIR(buf.st_mode))
	{
		if (::rmdir(m_nativePath.c_str()) != 0)
			posixFileSystemFactory::reportError(m_path, errno);
	}
	else if (S_ISREG(buf.st_mode))
	{
		if (::unlink(m_nativePath.c_str()) != 0)
			posixFileSystemFactory::reportError(m_path, errno);
	}
}


ref <vmime::utility::fileWriter> posixFile::getFileWriter()
{
	return vmime::create <posixFileWriter>(m_path, m_nativePath);
}


ref <vmime::utility::fileReader> posixFile::getFileReader()
{
	return vmime::create <posixFileReader>(m_path, m_nativePath);
}


ref <vmime::utility::fileIterator> posixFile::getFiles() const
{
	if (!isDirectory())
		throw vmime::exceptions::not_a_directory(m_path);

	return vmime::create <posixFileIterator>(m_path, m_nativePath);
}


void posixFile::createDirectoryImpl(const vmime::utility::file::path& fullPath,
	const vmime::utility::file::path& path, const bool recursive)
{
	const vmime::string nativePath = posixFileSystemFactory::pathToStringImpl(path);
	struct stat buf;

	if (::stat(nativePath.c_str(), &buf) == 0 && S_ISDIR(buf.st_mode))
		return;

	if (!path.isEmpty() && recursive)
		createDirectoryImpl(fullPath, path.getParent(), true);

	if (::mkdir(nativePath.c_str(), 0750) != 0)
		posixFileSystemFactory::reportError(fullPath, errno);
}



//
// posixFileSystemFactory
//

ref <vmime::utility::file> posixFileSystemFactory::create(const vmime::utility::file::path& path) const
{
	return vmime::create <posixFile>(path);
}


const vmime::utility::file::path posixFileSystemFactory::stringToPath(const vmime::string& str) const
{
	return (stringToPathImpl(str));
}


const vmime::string posixFileSystemFactory::pathToString(const vmime::utility::file::path& path) const
{
	return (pathToStringImpl(path));
}


const vmime::utility::file::path posixFileSystemFactory::stringToPathImpl(const vmime::string& str)
{
	vmime::string::size_type offset = 0;
	vmime::string::size_type prev = 0;

	vmime::utility::file::path path;

	while ((offset = str.find_first_of("/", offset)) != vmime::string::npos)
	{
		if (offset != prev)
			path.appendComponent(vmime::string(str.begin() + prev, str.begin() + offset));

		prev = offset + 1;
		offset++;
	}

	if (prev < str.length())
		path.appendComponent(vmime::string(str.begin() + prev, str.end()));

	return (path);
}


const vmime::string posixFileSystemFactory::pathToStringImpl(const vmime::utility::file::path& path)
{
	vmime::string native = "/";

	for (int i = 0 ; i < path.getSize() ; ++i)
	{
		if (i > 0)
			native += "/";

		native += path[i].getBuffer();
	}

	return (native);
}


bool posixFileSystemFactory::isValidPathComponent(const vmime::utility::file::path::component& comp) const
{
	return (comp.getBuffer().find_first_of("/*") == vmime::string::npos);
}


bool posixFileSystemFactory::isValidPath(const vmime::utility::file::path& path) const
{
	for (int i = 0 ; i < path.getSize() ; ++i)
	{
		if (!isValidPathComponent(path[i]))
			return false;
	}

	return true;
}


void posixFileSystemFactory::reportError(const vmime::utility::path& path, const int err)
{
	vmime::string desc;

	switch (err)
	{
	case EEXIST: desc = "EEXIST: file already exists."; break;
	case EISDIR: desc = "EISDIR: path refers to a directory."; break;
	case EACCES: desc = "EACCES: permission denied"; break;
	case ENAMETOOLONG: desc = "ENAMETOOLONG: path too long."; break;
	case ENOENT: desc = "ENOENT: a directory in the path does not exist."; break;
	case ENOTDIR: desc = "ENOTDIR: path is not a directory."; break;
	case EROFS: desc = "EROFS: read-only filesystem."; break;
	case ELOOP: desc = "ELOOP: too many symbolic links."; break;
	case ENOSPC: desc = "ENOSPC: no space left on device."; break;
	case ENOMEM: desc = "ENOMEM: insufficient kernel memory."; break;
	case EMFILE: desc = "ENFILE: limit on number of files open by the process has been reached."; break;
	case ENFILE: desc = "ENFILE: limit on number of files open on the system has been reached."; break;
#ifndef AIX
	case ENOTEMPTY: desc = "ENOTEMPTY: directory is not empty."; break;
#endif

	default:

		std::ostringstream oss;
		oss << ::strerror(err) << ".";

		desc = oss.str();
		break;
	}

	throw vmime::exceptions::filesystem_exception(desc, path);
}



} // posix
} // platforms
} // vmime


#endif // VMIME_HAVE_FILESYSTEM_FEATURES