summaryrefslogtreecommitdiff
path: root/src/modulemanager.cpp
diff options
context:
space:
mode:
authorAdam <Adam@anope.org>2011-08-11 23:10:08 -0400
committerAdam <Adam@anope.org>2011-08-11 23:10:08 -0400
commitd44a1d08673c6f78df162a76616152fc2ea7cbd5 (patch)
treedb1819fa65c3f84ec5e630441052f73127e194b6 /src/modulemanager.cpp
parentc2780e1de47b4aead8ca5e2c7a7675ea987a9bb4 (diff)
Fixed Windows runtime problems
Diffstat (limited to 'src/modulemanager.cpp')
-rw-r--r--src/modulemanager.cpp46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp
index 82f7cec24..b2907e467 100644
--- a/src/modulemanager.cpp
+++ b/src/modulemanager.cpp
@@ -84,10 +84,6 @@ void ModuleManager::LoadModuleList(std::list<Anope::string> &ModuleList)
static ModuleReturn moduleCopyFile(const Anope::string &name, Anope::string &output)
{
Anope::string input = services_dir + "/modules/" + name + ".so";
-
- int source = open(input.c_str(), O_RDONLY);
- if (source == -1)
- return MOD_ERR_NOEXIST;
struct stat s;
if (stat(input.c_str(), &s) == -1)
@@ -95,40 +91,46 @@ static ModuleReturn moduleCopyFile(const Anope::string &name, Anope::string &out
else if (!S_ISREG(s.st_mode))
return MOD_ERR_NOEXIST;
+ std::ifstream source(input.c_str(), std::ios_base::in | std::ios_base::binary);
+ if (!source.is_open())
+ return MOD_ERR_NOEXIST;
+
char *tmp_output = strdup(output.c_str());
- int target = mkstemp(tmp_output);
- if (target == -1)
+ int target_fd = mkstemp(tmp_output);
+ if (target_fd == -1 || close(target_fd) == -1)
{
free(tmp_output);
- close(source);
+ source.close();
return MOD_ERR_FILE_IO;
}
output = tmp_output;
free(tmp_output);
Log(LOG_DEBUG) << "Runtime module location: " << output;
+
+ std::ofstream target(output.c_str(), std::ios_base::in | std::ios_base::binary);
+ if (!target.is_open())
+ {
+ source.close();
+ return MOD_ERR_FILE_IO;
+ }
+ int want = s.st_size;
char *buffer = new char[s.st_size];
- bool err = false;
- for (;;)
+ while (want > 0 && !source.fail() && !target.fail())
{
- int read_len = read(source, buffer, s.st_size);
- if (read_len <= 0)
- break;
- int writ_len = write(target, buffer, read_len);
- if (writ_len < 0)
- {
- err = true;
- break;
- }
+ source.read(buffer, want);
+ int read_len = source.gcount();
+
+ target.write(buffer, read_len);
+ want -= read_len;
}
delete [] buffer;
- close(source);
- if (close(target) == -1 || err == true)
- return MOD_ERR_FILE_IO;
+ source.close();
+ target.close();
- return MOD_ERR_OK;
+ return !source.fail() && !target.fail() ? MOD_ERR_OK : MOD_ERR_FILE_IO;
}
/* This code was found online at http://www.linuxjournal.com/article/3687#comment-26593