Replace all uses of std::cout in libaegisub with logging statements and eliminate a lot of nonsense in the windows implementation of acs::Check

Originally committed to SVN as r5500.
This commit is contained in:
Thomas Goyne 2011-07-26 19:51:38 +00:00
parent 64a3cc94ae
commit a33da9cba5
3 changed files with 43 additions and 101 deletions

View file

@ -23,11 +23,10 @@
#include <time.h> #include <time.h>
#endif #endif
#include "libaegisub/cajun/reader.h"
#include "libaegisub/cajun/writer.h" #include "libaegisub/cajun/writer.h"
#include "libaegisub/cajun/elements.h"
#include "libaegisub/access.h" #include "libaegisub/access.h"
#include "libaegisub/json.h"
#include "libaegisub/log.h" #include "libaegisub/log.h"
#include "libaegisub/mru.h" #include "libaegisub/mru.h"
#include "libaegisub/io.h" #include "libaegisub/io.h"
@ -37,26 +36,7 @@ namespace agi {
MRUManager::MRUManager(const std::string &config, const std::string &default_config): config_name(config) { MRUManager::MRUManager(const std::string &config, const std::string &default_config): config_name(config) {
LOG_D("agi/mru") << "Loading MRU List"; LOG_D("agi/mru") << "Loading MRU List";
json::UnknownElement root; json::UnknownElement root = json_util::file(config, default_config);
std::istream *stream;
try {
stream = io::Open(config);
} catch (const acs::AcsNotFound&) {
stream = new std::istringstream(default_config);
}
try {
json::Reader::Read(root, *stream);
} catch (const json::Exception&) {
/// @todo Do something better here, maybe print the exact error
// std::cout << "json::Exception: " << e.what() << std::endl;
delete stream;
stream = new std::istringstream(default_config);
json::Reader::Read(root, *stream);
}
const json::Object& root_new = (json::Object)root; const json::Object& root_new = (json::Object)root;
json::Object::const_iterator index_object(root_new.Begin()), index_objectEnd(root_new.End()); json::Object::const_iterator index_object(root_new.Begin()), index_objectEnd(root_new.End());
@ -68,8 +48,6 @@ MRUManager::MRUManager(const std::string &config, const std::string &default_con
Load(member_name, (json::Array)element); Load(member_name, (json::Array)element);
} }
delete stream;
} }
@ -83,7 +61,6 @@ MRUManager::~MRUManager() {
void MRUManager::Add(const std::string &key, const std::string &entry) { void MRUManager::Add(const std::string &key, const std::string &entry) {
MRUMap::iterator index; MRUMap::iterator index;
if ((index = mru.find(key)) != mru.end()) { if ((index = mru.find(key)) != mru.end()) {
@ -103,7 +80,6 @@ void MRUManager::Add(const std::string &key, const std::string &entry) {
void MRUManager::Remove(const std::string &key, const std::string &entry) { void MRUManager::Remove(const std::string &key, const std::string &entry) {
MRUMap::iterator index; MRUMap::iterator index;
if ((index = mru.find(key)) != mru.end()) { if ((index = mru.find(key)) != mru.end()) {
@ -122,7 +98,6 @@ void MRUManager::Remove(const std::string &key, const std::string &entry) {
const MRUManager::MRUListMap* MRUManager::Get(const std::string &key) { const MRUManager::MRUListMap* MRUManager::Get(const std::string &key) {
MRUMap::iterator index; MRUMap::iterator index;
if ((index = mru.find(key)) != mru.end()) { if ((index = mru.find(key)) != mru.end()) {
@ -134,10 +109,9 @@ const MRUManager::MRUListMap* MRUManager::Get(const std::string &key) {
const std::string MRUManager::GetEntry(const std::string &key, const int entry) { const std::string MRUManager::GetEntry(const std::string &key, const int entry) {
const MRUManager::MRUListMap *map = Get(key); const MRUManager::MRUListMap *map = Get(key);
MRUListMap::const_iterator index = map->begin();; MRUListMap::const_iterator index = map->begin();
if ((unsigned int)entry > map->size()) if ((unsigned int)entry > map->size())
throw MRUErrorIndexOutOfRange("Requested element index is out of range."); throw MRUErrorIndexOutOfRange("Requested element index is out of range.");
@ -149,7 +123,6 @@ const std::string MRUManager::GetEntry(const std::string &key, const int entry)
void MRUManager::Flush() { void MRUManager::Flush() {
json::Object out; json::Object out;
for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) { for (MRUMap::const_iterator i = mru.begin(); i != mru.end(); ++i) {
@ -192,7 +165,6 @@ inline void MRUManager::Prune(MRUListMap& map) {
/// @param key List name. /// @param key List name.
/// @param array json::Array of values. /// @param array json::Array of values.
void MRUManager::Load(const std::string &key, const json::Array& array) { void MRUManager::Load(const std::string &key, const json::Array& array) {
json::Array::const_iterator index(array.Begin()), indexEnd(array.End()); json::Array::const_iterator index(array.Begin()), indexEnd(array.End());
MRUListMap *map = new MRUListMap(); MRUListMap *map = new MRUListMap();
@ -203,10 +175,10 @@ void MRUManager::Load(const std::string &key, const json::Array& array) {
time_t time = (time_t)(json::Number)obj["time"]; time_t time = (time_t)(json::Number)obj["time"];
std::string entry = (json::String)obj["entry"]; std::string entry = (json::String)obj["entry"];
map->insert(std::pair<time_t, std::string>(time, entry)); map->insert(make_pair(time, entry));
} }
mru.insert(std::pair<std::string, MRUListMap*>(key, map)); mru[key] = map;
Prune(*map); Prune(*map);
} }

View file

@ -85,10 +85,10 @@ void Options::LoadConfig(std::istream& stream) {
try { try {
json::Reader::Read(config_root, stream); json::Reader::Read(config_root, stream);
} catch (json::Reader::ParseException& e) { } catch (json::Reader::ParseException& e) {
std::cout << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1 << std::endl << std::endl; LOG_E("option/load") << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1;
} catch (json::Exception& e) { } catch (json::Exception& e) {
/// @todo Do something better here, maybe print the exact error /// @todo Do something better here, maybe print the exact error
std::cout << "json::Exception: " << e.what() << std::endl; LOG_E("option/load") << "json::Exception: " << e.what();
} }
ConfigVisitor config_visitor(values, std::string("")); ConfigVisitor config_visitor(values, std::string(""));
@ -99,13 +99,12 @@ void Options::LoadConfig(std::istream& stream) {
OptionValue* Options::Get(const std::string &name) { OptionValue* Options::Get(const std::string &name) {
OptionValueMap::iterator index; OptionValueMap::iterator index;
if ((index = values.find(name)) != values.end()) if ((index = values.find(name)) != values.end())
return index->second; return index->second;
std::cout << "agi::Options::Get Option not found: (" << name << ")" << std::endl; LOG_E("option/get") << "agi::Options::Get Option not found: (" << name << ")";
throw OptionErrorNotFound("Option value not found"); throw OptionErrorNotFound("Option value not found");
} }

View file

@ -26,6 +26,7 @@
#endif #endif
#include <libaegisub/charset_conv_win.h> #include <libaegisub/charset_conv_win.h>
#include <libaegisub/log.h>
#include <libaegisub/util.h> #include <libaegisub/util.h>
#include <libaegisub/util_win.h> #include <libaegisub/util_win.h>
@ -36,17 +37,14 @@ void CheckFileRead(const std::string &file) {
Check(file, acs::FileRead); Check(file, acs::FileRead);
} }
void CheckFileWrite(const std::string &file) { void CheckFileWrite(const std::string &file) {
Check(file, acs::FileWrite); Check(file, acs::FileWrite);
} }
void CheckDirRead(const std::string &dir) { void CheckDirRead(const std::string &dir) {
Check(dir, acs::DirRead); Check(dir, acs::DirRead);
} }
void CheckDirWrite(const std::string &dir) { void CheckDirWrite(const std::string &dir) {
Check(dir, acs::DirWrite); Check(dir, acs::DirWrite);
} }
@ -60,104 +58,77 @@ is a short (and incomplete) todo
void Check(const std::string &file, acs::Type type) { void Check(const std::string &file, acs::Type type) {
std::wstring wfile = agi::charset::ConvertW(file); std::wstring wfile = agi::charset::ConvertW(file);
SECURITY_DESCRIPTOR* sd; DWORD file_attr = GetFileAttributes(wfile.c_str());
DWORD len = 0;
SECURITY_INFORMATION info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
HANDLE client_token;
PRIVILEGE_SET priv_set;
DWORD priv_set_size = sizeof(PRIVILEGE_SET);
BOOL access_ok;
GENERIC_MAPPING generic_mapping;
DWORD access_check;
DWORD access;
DWORD file_attr;
file_attr = GetFileAttributes(wfile.c_str());
if ((file_attr & INVALID_FILE_ATTRIBUTES) == INVALID_FILE_ATTRIBUTES) { if ((file_attr & INVALID_FILE_ATTRIBUTES) == INVALID_FILE_ATTRIBUTES) {
switch (GetLastError()) { switch (GetLastError()) {
case ERROR_FILE_NOT_FOUND: case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND: case ERROR_PATH_NOT_FOUND:
throw AcsNotFound("File or path not found."); throw AcsNotFound("File or path not found.");
break;
case ERROR_ACCESS_DENIED: case ERROR_ACCESS_DENIED:
throw AcsAccess("Access denied to file or path component"); throw AcsAccess("Access denied to file or path component");
break;
default: default:
throw AcsFatal("Fatal I/O error occurred."); throw AcsFatal("Fatal I/O error occurred.");
break;
} }
} }
switch (type) { switch (type) {
case FileRead: case FileRead:
case FileWrite: { case FileWrite:
if ((file_attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY) if ((file_attr & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
throw AcsNotAFile("Not a file."); throw AcsNotAFile("Not a file");
break; break;
}
case DirRead: case DirRead:
case DirWrite: { case DirWrite:
if ((file_attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY) if ((file_attr & FILE_ATTRIBUTE_DIRECTORY) != FILE_ATTRIBUTE_DIRECTORY)
throw AcsNotADirectory("Not a directory."); throw AcsNotADirectory("Not a directory");
break; break;
}
} }
SECURITY_INFORMATION info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION;
DWORD len = 0;
GetFileSecurity(wfile.c_str(), info, NULL, 0, &len); GetFileSecurity(wfile.c_str(), info, NULL, 0, &len);
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { if (GetLastError() != ERROR_INSUFFICIENT_BUFFER)
std::cout << "GetFileSecurity: fatal: " << util::ErrorString(GetLastError()) << std::endl; LOG_W("acs/check") << "GetFileSecurity: fatal: " << util::ErrorString(GetLastError());
}
sd = (SECURITY_DESCRIPTOR *)malloc(len); std::vector<uint8_t> sd_buff(len);
if (sd == NULL) { SECURITY_DESCRIPTOR *sd = (SECURITY_DESCRIPTOR *)&sd_buff[0];
std::cout << "GetFileSecurity: insufficient memory" << std::endl;
} else { if (!GetFileSecurity(wfile.c_str(), info, sd, len, &len))
if (!GetFileSecurity(wfile.c_str(), info, sd, len, &len)) { LOG_W("acs/check") << "GetFileSecurity failed: " << util::ErrorString(GetLastError());
std::cout << "GetFileSecurity failed: " << util::ErrorString(GetLastError()) << std::endl;
free(sd);
}
}
ImpersonateSelf(SecurityImpersonation); ImpersonateSelf(SecurityImpersonation);
if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &client_token)) { HANDLE client_token;
std::cout << "OpenThreadToken failed: " << util::ErrorString(GetLastError()) << std::endl; if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, TRUE, &client_token))
} LOG_W("acs/check") << "OpenThreadToken failed: " << util::ErrorString(GetLastError());
DWORD access_check;
switch (type) { switch (type) {
case DirRead: case DirRead:
case FileRead: { case FileRead:
access_check = FILE_READ_DATA; access_check = FILE_READ_DATA;
MapGenericMask(&access_check, &generic_mapping);
if(!AccessCheck(sd, client_token, access_check, &generic_mapping, &priv_set, &priv_set_size, &access, &access_ok)) {
std::cout << "AccessCheck failed: " << util::ErrorString(GetLastError()) << std::endl;
}
free(sd);
if (!access)
throw AcsRead("File or directory is not readable.");
break; break;
}
case DirWrite: case DirWrite:
case FileWrite: { case FileWrite:
access_check = FILE_APPEND_DATA | FILE_WRITE_DATA; access_check = FILE_APPEND_DATA | FILE_WRITE_DATA;
MapGenericMask(&access_check, &generic_mapping);
if(!AccessCheck(sd, client_token, access_check, &generic_mapping, &priv_set, &priv_set_size, &access, &access_ok)) {
std::cout << "AccessCheck failed: " << util::ErrorString(GetLastError()) << std::endl;
}
free(sd);
if (!access)
throw AcsWrite("File or directory is not writable.");
break; break;
} default:
default: { LOG_W("acs/check") << "Warning: type not handled";
std::cout << "Warning: type not handled" << std::endl; return;
free(sd);
}
} }
GENERIC_MAPPING generic_mapping;
MapGenericMask(&access_check, &generic_mapping);
PRIVILEGE_SET priv_set;
DWORD priv_set_size = sizeof(PRIVILEGE_SET);
DWORD access;
BOOL access_ok;
if(!AccessCheck(sd, client_token, access_check, &generic_mapping, &priv_set, &priv_set_size, &access, &access_ok))
LOG_W("acs/check") << "AccessCheck failed: " << util::ErrorString(GetLastError());
if (!access)
throw AcsRead("File or directory is not readable");
} }
} // namespace Access } // namespace Access