[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[myserver-commit] [2813] refactoring copying files in server
From: |
Daniele Perrone |
Subject: |
[myserver-commit] [2813] refactoring copying files in server |
Date: |
Sun, 14 Sep 2008 18:19:20 +0000 |
Revision: 2813
http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2813
Author: dperrone
Date: 2008-09-14 18:19:19 +0000 (Sun, 14 Sep 2008)
Log Message:
-----------
refactoring copying files in server
Modified Paths:
--------------
trunk/myserver/include/base/file/files_utility.h
trunk/myserver/src/base/file/files_utility.cpp
trunk/myserver/src/server/server.cpp
Modified: trunk/myserver/include/base/file/files_utility.h
===================================================================
--- trunk/myserver/include/base/file/files_utility.h 2008-09-14 15:55:15 UTC
(rev 2812)
+++ trunk/myserver/include/base/file/files_utility.h 2008-09-14 18:19:19 UTC
(rev 2813)
@@ -25,11 +25,11 @@
using namespace std;
-class FilesUtility
+class FilesUtility
{
FileHandle handle;
string filename;
-
+
private:
/*! Don't allow instances for this class. */
FilesUtility();
@@ -73,11 +73,12 @@
static int deleteFile(string const &file)
{return deleteFile(file.c_str());}
- static int renameFile(const char*, const char*);
+ static int renameFile(const char*, const char*);
static int renameFile(string const &before, string const &after)
{return renameFile(before.c_str(), after.c_str());}
static int copyFile(const char*, const char*, int overwrite);
+ static int copyFile(File, File);
static int copyFile(string const &src, string const &dest, int
overwrite)
{return copyFile(src.c_str(), dest.c_str(), overwrite);}
Modified: trunk/myserver/src/base/file/files_utility.cpp
===================================================================
--- trunk/myserver/src/base/file/files_utility.cpp 2008-09-14 15:55:15 UTC
(rev 2812)
+++ trunk/myserver/src/base/file/files_utility.cpp 2008-09-14 18:19:19 UTC
(rev 2813)
@@ -81,7 +81,7 @@
lpath++;
continue;
}
-
+
if(*lpath=='.')
{
lpath++;
@@ -170,7 +170,7 @@
destFile = open(dest, O_WRONLY);
else
destFile = open(dest, O_WRONLY | O_CREAT);
-
+
if(destFile == -1)
{
close(srcFile);
@@ -193,7 +193,7 @@
close(srcFile);
close(destFile);
return -1;
- }
+ }
}while(dim);
@@ -204,7 +204,34 @@
#endif
}
+/*!
+ *Copy the file from [SRC] to [DEST]. Returns 0 on success.
+ *\param src The source File.
+ *\param dest The destination File.
+ *\param overwrite Overwrite the dest file if already exists?
+ */
+int FilesUtility::copyFile(File src, File dest)
+{
+ char buffer[512];
+ u_long nbr, nbw;
+ int ret;
+
+ for (;;) {
+ ret = src.readFromFile(buffer, 512, &nbr);
+ if (ret)
+ return -1;
+
+ if (!nbr)
+ break;
+
+ ret = dest.writeToFile(buffer, nbr, &nbw);
+ if (ret)
+ return -1;
+ }
+ return 0;
+}
+
/*!
*Delete an existing file passing the path.
*Return a non-null value on errors.
@@ -264,7 +291,7 @@
return (S_ISLNK(F_Stats.st_mode))? 1 : 0;
#endif
-
+
}
/*!
@@ -278,9 +305,9 @@
OFSTRUCT of;
int ret = OpenFile(filename, &of, OF_EXIST);
return (ret != HFILE_ERROR)?1:0;*/
- HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
- NULL, OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
+ HANDLE hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ,
+ NULL, OPEN_EXISTING,
+ FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL);
int nRet = hFile != INVALID_HANDLE_VALUE ? 1 : 0;
CloseHandle(hFile);
@@ -373,7 +400,7 @@
{
#ifdef NOT_WIN
return ::chown(filename, uid, gid) ? 1 : 0;
-#endif
+#endif
return 0;
}
@@ -575,7 +602,7 @@
{
ext.assign("");
}
-
+
}
/*!
@@ -595,7 +622,7 @@
#endif
#ifdef NOT_WIN
strncpy(out, filePath, buffersize);
- return 0;
+ return 0;
#endif
}
Modified: trunk/myserver/src/server/server.cpp
===================================================================
--- trunk/myserver/src/server/server.cpp 2008-09-14 15:55:15 UTC (rev
2812)
+++ trunk/myserver/src/server/server.cpp 2008-09-14 18:19:19 UTC (rev
2813)
@@ -35,7 +35,7 @@
#include <include/base/ssl/ssl.h>
#include <include/base/socket/ssl_socket.h>
-extern "C"
+extern "C"
{
#ifdef WIN32
//#include <Ws2tcpip.h>
@@ -75,7 +75,7 @@
*/
Server* Server::instance = 0;
-Server::Server() : connectionsScheduler(this),
+Server::Server() : connectionsScheduler(this),
listenThreads(&connectionsScheduler, this)
{
toReboot = 0;
@@ -112,7 +112,7 @@
setcwdBuffer();
XmlParser::startXML();
myserver_safetime_init();
-
+
/* Startup the socket library. */
logWriteln(languageParser.getValue("MSG_ISOCK") );
@@ -259,7 +259,7 @@
/* Load the home directories configuration. */
homeDir.load();
-
+
loadPlugins();
/* Load the virtual hosts configuration from the xml file. */
@@ -304,7 +304,7 @@
if(logManager.getType() == LogManager::TYPE_CONSOLE)
logWriteln(languageParser.getValue("MSG_BREAK"));
-
+
serverReady = 1;
/* Finally we can give control to the main loop. */
@@ -350,7 +350,7 @@
new FtpProtocol(),
new ControlProtocol(),
0};
-
+
for (int j = 0; protocolsSet[j]; j++)
{
char protocolName[32];
@@ -359,13 +359,13 @@
protocol->registerName(protocolName, 32);
getProtocolsManager()->addProtocol(protocolName, protocol);
}
-
+
getPluginsManager()->addNamespace(&executors);
getPluginsManager()->addNamespace(&protocols);
getPluginsManager()->addNamespace(&filters);
getPluginsManager()->addNamespace(&genericPluginsManager);
-
+
getPluginsManager()->preLoad(this, &languageParser, *externalPath);
getPluginsManager()->load(this, &languageParser, *externalPath);
getPluginsManager()->postLoad(this, &languageParser);
@@ -383,13 +383,13 @@
u_long configsCheck = 0;
u_long purgeThreadsCounter = 0;
- mainConfTime =
+ mainConfTime =
FilesUtility::getLastModTime(mainConfigurationFile->c_str());
- hostsConfTime =
+ hostsConfTime =
FilesUtility::getLastModTime(vhostConfigurationFile->c_str());
- mimeConfTime =
+ mimeConfTime =
FilesUtility::getLastModTime(mimeConfigurationFile->c_str());
-
+
/*
*Keep thread alive.
*When the endServer flag is set to True exit
@@ -427,7 +427,7 @@
{
string msg("main-conf-changed");
notifyMulticast(msg, 0);
-
+
reboot();
/* Store new mtime values. */
mainConfTime = mainConfTimeNow;
@@ -437,7 +437,7 @@
{
string msg("mime-conf-changed");
notifyMulticast(msg, 0);
-
+
if(logManager.getType() == LogManager::TYPE_CONSOLE)
{
char beep[]={static_cast<char>(0x7), '\0'};
@@ -447,9 +447,9 @@
logWriteln("Reloading MIMEtypes.xml");
getMimeManager()->loadXML(getMIMEConfFile());
-
+
logWriteln("Reloaded");
-
+
mimeConfTime = mimeConfNow;
}
else if(hostsConfTimeNow != hostsConfTime)
@@ -464,31 +464,31 @@
char beep[]={static_cast<char>(0x7), '\0'};
logManager.write(beep);
}
-
+
logWriteln("Rebooting...");
-
+
connectionsScheduler.release();
Socket::stopBlockingOperations(true);
-
+
listenThreads.beginFastReboot();
listenThreads.terminate();
-
+
connectionsScheduler.terminateConnections();
clearAllConnections();
-
+
Socket::stopBlockingOperations(false);
connectionsScheduler.restart();
listenThreads.initialize(&languageParser);
-
+
vhostList = new VhostManager(&listenThreads);
-
+
if(vhostList == 0)
continue;
-
+
delete oldvhost;
/* Load the virtual hosts configuration from the xml file. */
@@ -501,7 +501,7 @@
{
listenThreads.commitFastReboot();
}
-
+
hostsConfTime = hostsConfTimeNow;
logWriteln("Reloaded");
}
@@ -918,7 +918,7 @@
{
languageFile->assign("languages/english.xml");
}
-
+
if(languageParser.open(languageFile->c_str()))
{
string err;
@@ -1059,7 +1059,7 @@
{
int serversProcessesInitialPort = atoi(data);
getProcessServerManager()->setInitialPort(serversProcessesInitialPort);
- }
+ }
{
xmlNodePtr node =
@@ -1146,7 +1146,7 @@
/*
*We can use MAX_IP_STRING_LEN only because we use NI_NUMERICHOST
*in getnameinfo call; Otherwise we should have used NI_MAXHOST.
- *ip is the string containing the address of the remote host connecting
+ *ip is the string containing the address of the remote host connecting
*to the server.
*localIp is the local address used by the connection.
*port is the remote port used by the client to open the connection.
@@ -1163,21 +1163,21 @@
return 0;
/*
- *Do not accept this connection if a MAX_CONNECTIONS_TO_ACCEPT limit is
+ *Do not accept this connection if a MAX_CONNECTIONS_TO_ACCEPT limit is
*defined.
*/
- if(maxConnectionsToAccept &&
+ if(maxConnectionsToAccept &&
((u_long)connectionsScheduler.getConnectionsNumber() >=
maxConnectionsToAccept))
return 0;
#if ( HAVE_IPV6 )
if ( asockIn->ss_family == AF_INET )
- ret = getnameinfo(reinterpret_cast<const sockaddr *>(asockIn),
+ ret = getnameinfo(reinterpret_cast<const sockaddr *>(asockIn),
sizeof(sockaddr_in),
ip, MAX_IP_STRING_LEN, NULL, 0, NI_NUMERICHOST);
else
- ret = getnameinfo(reinterpret_cast<const sockaddr *>(asockIn),
- sizeof(sockaddr_in6), ip, MAX_IP_STRING_LEN,
+ ret = getnameinfo(reinterpret_cast<const sockaddr *>(asockIn),
+ sizeof(sockaddr_in6), ip, MAX_IP_STRING_LEN,
NULL, 0, NI_NUMERICHOST);
if(ret)
return 0;
@@ -1189,19 +1189,19 @@
s.getsockname((MYSERVER_SOCKADDR*)&localSockIn, &dim);
if ( asockIn->ss_family == AF_INET )
- ret = getnameinfo(reinterpret_cast<const sockaddr *>(&localSockIn),
- sizeof(sockaddr_in), localIp, MAX_IP_STRING_LEN,
+ ret = getnameinfo(reinterpret_cast<const sockaddr *>(&localSockIn),
+ sizeof(sockaddr_in), localIp, MAX_IP_STRING_LEN,
NULL, 0, NI_NUMERICHOST);
else// AF_INET6
- ret = getnameinfo(reinterpret_cast<const sockaddr *>(&localSockIn),
- sizeof(sockaddr_in6), localIp, MAX_IP_STRING_LEN,
+ ret = getnameinfo(reinterpret_cast<const sockaddr *>(&localSockIn),
+ sizeof(sockaddr_in6), localIp, MAX_IP_STRING_LEN,
NULL, 0, NI_NUMERICHOST);
if(ret)
return 0;
#else// !HAVE_IPV6
dim = sizeof(localSockIn);
s.getsockname((MYSERVER_SOCKADDR*)&localSockIn, &dim);
- strncpy(ip, inet_ntoa(((sockaddr_in *)asockIn)->sin_addr),
+ strncpy(ip, inet_ntoa(((sockaddr_in *)asockIn)->sin_addr),
MAX_IP_STRING_LEN);
strncpy(localIp, inet_ntoa(((sockaddr_in *)&localSockIn)->sin_addr),
MAX_IP_STRING_LEN);
@@ -1252,7 +1252,7 @@
ConnectionPtr Server::addConnectionToList(Socket* s,
MYSERVER_SOCKADDRIN* /*asockIn*/,
char *ipAddr, char *localIpAddr,
- u_short port, u_short localPort,
+ u_short port, u_short localPort,
int /*id*/)
{
int doSSLhandshake = 0;
@@ -1284,22 +1284,22 @@
}
protocol =
Server::getInstance()->getProtocol(newConnection->host->getProtocolName());
-
+
if(protocol)
opts = protocol->getProtocolOptions();
-
+
if(opts & PROTOCOL_USES_SSL)
doSSLhandshake = 1;
-
+
if(opts & PROTOCOL_FAST_CHECK)
doFastCheck = 1;
-
+
{
string msg("new-connection");
-
+
handlers = getHandlers(msg);
-
+
if(handlers)
{
for(size_t i = 0; i < handlers->size(); i++)
@@ -1349,7 +1349,7 @@
*is bigger than it say to the protocol that will parse the connection
*to remove it from the active connections list.
*/
- if(maxConnections &&
+ if(maxConnections &&
((u_long)connectionsScheduler.getConnectionsNumber() > maxConnections))
newConnection->setToRemove(CONNECTION_REMOVE_OVERLOAD);
@@ -1474,7 +1474,7 @@
u_long ret;
threadsMutex->lock();
ret = threads.size();
- threadsMutex->unlock();
+ threadsMutex->unlock();
return ret;
}
@@ -1582,7 +1582,7 @@
File inputF;
File outputF;
ret = inputF.openFile("myserver.xml.default",
- File::MYSERVER_OPEN_READ |
+ File::MYSERVER_OPEN_READ |
File::MYSERVER_OPEN_IFEXISTS);
if(ret)
{
@@ -1601,19 +1601,8 @@
return -1;
}
- for(;;)
- {
- ret = inputF.readFromFile(buffer, 512, &nbr );
- if(ret)
- return -1;
+ FilesUtility::copyFile(inputF,outputF);
- if(!nbr)
- break;
-
- ret = outputF.writeToFile(buffer, nbr, &nbw);
- if(ret)
- return -1;
- }
inputF.closeFile();
outputF.closeFile();
}
@@ -1624,7 +1613,7 @@
if(mimeConfigurationFile)
delete mimeConfigurationFile;
-
+
mimeConfigurationFile = new string();
#ifndef WIN32
@@ -1709,7 +1698,7 @@
File inputF;
File outputF;
mimeConfigurationFile->assign("MIMEtypes.xml");
- ret = inputF.openFile("MIMEtypes.xml.default",
+ ret = inputF.openFile("MIMEtypes.xml.default",
File::MYSERVER_OPEN_READ |
File::MYSERVER_OPEN_IFEXISTS);
if(ret)
@@ -1738,8 +1727,8 @@
inputF.closeFile();
outputF.closeFile();
}
-
+
/*
*If the virtualhosts.xml file doesn't exist copy it
*from the default one.
@@ -1749,7 +1738,7 @@
File inputF;
File outputF;
vhostConfigurationFile->assign("virtualhosts.xml");
- ret = inputF.openFile("virtualhosts.xml.default",
+ ret = inputF.openFile("virtualhosts.xml.default",
File::MYSERVER_OPEN_READ |
File::MYSERVER_OPEN_IFEXISTS );
if(ret)
@@ -1760,7 +1749,7 @@
return -1;
}
ret = outputF.openFile("virtualhosts.xml",
- File::MYSERVER_OPEN_WRITE |
+ File::MYSERVER_OPEN_WRITE |
File::MYSERVER_OPEN_ALWAYS);
if(ret)
return -1;
@@ -1775,7 +1764,7 @@
if(ret)
return -1;
}
-
+
inputF.closeFile();
outputF.closeFile();
}
@@ -1833,7 +1822,7 @@
if(Process::setAdditionalGroups(0, 0))
{
- out << languageParser.getValue("ERR_ERROR")
+ out << languageParser.getValue("ERR_ERROR")
<< ": setAdditionalGroups";
logPreparePrintError();
logWriteln(out.str().c_str());
@@ -2067,7 +2056,7 @@
vector<Multicast<string, void*, int>*>* handlers;
purgeThreadsThreshold = 1;
-
+
if(isRebooting())
return -1;
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [myserver-commit] [2813] refactoring copying files in server,
Daniele Perrone <=