23 #include "../../../core/info.hpp"
24 #include "../../../core/exception.hpp"
25 #include "../../../core/trace.hpp"
29 using namespace libjmmcg;
40 const TCHAR SetFileSecurity_name[]=_T(
"SetFileSecurityW");
47 Directory::Directory(
const tstring &unc_to_dir,
const TCHAR *
const machine,
const TCHAR *
const user,
const DWORD access)
48 : LoadLibraryWrapper(security_lib_name),
49 pSetFileSecurity(
reinterpret_cast<SetFileSecurityType>(::GetProcAddress(Handle(),SetFileSecurity_name))),
50 unc(unc_to_dir+dir_separator_s) {
51 JMMCG_TRACE(_T(
"Directory::Directory(...): Dir name: ")<<unc);
52 unsigned long err=sd.Allow(machine,user,access);
54 info::function info(__LINE__,
__PRETTY_FUNCTION__,
typeid(*
this),info::function::argument(
_T(
"const tstring &"),unc_to_dir));
55 info.add_arg(
_T(
"const tstring &"),machine);
56 info.add_arg(
_T(
"const tstring &"),user);
57 info.add_arg(
_T(
"const DWORD"),access);
58 throw exception_type(
_T(
"Failed to add access allowed ACE to security descriptor for user."),info,__REV_INFO__);
60 err=sd.Allow(machine,
_T(
"SYSTEM"),GENERIC_ALL);
62 info::function info(__LINE__,
__PRETTY_FUNCTION__,
typeid(*
this),info::function::argument(
_T(
"const tstring &"),unc_to_dir));
63 info.add_arg(
_T(
"const tstring &"),machine);
64 info.add_arg(
_T(
"const tstring &"),user);
65 info.add_arg(
_T(
"const DWORD"),access);
66 throw exception_type(
_T(
"Failed to add access allowed ACE to security descriptor for user 'SYSTEM' on the specified machine with an access mask of 'GENERIC_ALL'."),info,__REV_INFO__);
68 SECURITY_ATTRIBUTES sa={
69 sizeof(SECURITY_ATTRIBUTES),
70 const_cast<SECURITY_DESCRIPTOR *>(&sd.SD()),
73 if (!::CreateDirectory(unc.c_str(),&sa)) {
74 info::function info(__LINE__,
__PRETTY_FUNCTION__,
typeid(*
this),info::function::argument(
_T(
"const tstring &"),unc_to_dir));
75 info.add_arg(
_T(
"const tstring &"),machine);
76 info.add_arg(
_T(
"const tstring &"),user);
77 info.add_arg(
_T(
"const DWORD"),access);
78 throw exception_type(
_T(
"Failed to create the specified directory on the specified machine using the specified user name with the specified access mask."),info,__REV_INFO__);
82 Directory::~Directory(
void) {
84 bool bWorking = finder.FindFile((unc+wildcard).c_str());
87 bWorking=finder.FindNextFile();
88 if (!finder.IsDirectory()) {
89 const tstring path(unc+finder.GetFileName().GetBuffer(0));
90 if (pSetFileSecurity) {
92 (*pSetFileSecurity)(path.c_str(),DACL_SECURITY_INFORMATION,
const_cast<SECURITY_DESCRIPTOR *>(&sd.SD()));
94 JMMCG_TRACE(_T(
"Directory::~Directory(): Deleting: ")<<path);
95 if (!DeleteFile(path.c_str())) {
96 JMMCG_TRACE(_T(
"Directory::~Directory(): Failed to delete: ")<<path);
101 bool err=::RemoveDirectory(unc.c_str());
103 std::stringstream ss;
104 ss<<
"Failed to delete directory '"<<unc<<
"'. "<<(emptied ?
"For some unknown reason" :
"Because it wasn't empty")<<
".";
105 throw exception_type(ss.str(),info::function(__LINE__,
__PRETTY_FUNCTION__,
typeid(*
this)),__REV_INFO__);
107 JMMCG_TRACE(_T(
"Directory::~Directory(): Deleted (and all contents) directory: ")<<unc);