2010-05-21 03:13:36 +02:00
|
|
|
// Copyright (c) 2010, Amar Takhar <verm@aegisub.org>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
/// @file access.cpp
|
|
|
|
/// @brief Unix access methods.
|
|
|
|
/// @ingroup libaegisub unix
|
|
|
|
|
2012-01-13 15:37:15 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "libaegisub/access.h"
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include "libaegisub/fs.h"
|
|
|
|
|
2010-05-21 03:13:36 +02:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2012-04-03 19:38:45 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
#include <boost/filesystem/path.hpp>
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
namespace agi {
|
|
|
|
namespace acs {
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
void Check(agi::fs::path const& file, acs::Type type) {
|
2010-05-21 03:13:36 +02:00
|
|
|
struct stat file_stat;
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
int file_status = stat(file.c_str(), &file_stat);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
|
|
|
if (file_status != 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case ENOENT:
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::FileNotFound(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
case EACCES:
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::ReadDenied(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
case EIO:
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::FileSystemUnknownError("Fatal I/O error in 'stat' on path: " + file.string());
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case FileRead:
|
|
|
|
case FileWrite:
|
|
|
|
if ((file_stat.st_mode & S_IFREG) == 0)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::NotAFile(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DirRead:
|
|
|
|
case DirWrite:
|
|
|
|
if ((file_stat.st_mode & S_IFDIR) == 0)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::NotADirectory(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-13 15:37:15 +01:00
|
|
|
file_status = access(file.c_str(), R_OK);
|
|
|
|
if (file_status != 0)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::ReadDenied(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
|
2012-01-13 15:37:15 +01:00
|
|
|
if (type == DirWrite || type == FileWrite) {
|
|
|
|
file_status = access(file.c_str(), W_OK);
|
|
|
|
if (file_status != 0)
|
2013-01-04 16:01:50 +01:00
|
|
|
throw fs::WriteDenied(file);
|
2010-05-21 03:13:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace acs
|
|
|
|
} // namespace agi
|