Skip to content
Snippets Groups Projects
Commit c8ed5ee5 authored by Jan David Mol's avatar Jan David Mol
Browse files

Task #2669: Replaced const char * to std::string for safety.

parent 4eed7f04
No related branches found
No related tags found
No related merge requests found
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#define LOFAR_LCS_STREAM_FILE_STREAM_H #define LOFAR_LCS_STREAM_FILE_STREAM_H
#include <Stream/FileDescriptorBasedStream.h> #include <Stream/FileDescriptorBasedStream.h>
#include <string>
namespace LOFAR { namespace LOFAR {
...@@ -31,9 +32,9 @@ namespace LOFAR { ...@@ -31,9 +32,9 @@ namespace LOFAR {
class FileStream : public FileDescriptorBasedStream class FileStream : public FileDescriptorBasedStream
{ {
public: public:
FileStream(const char *name); // read-only; existing file FileStream(const std::string &name); // read-only; existing file
FileStream(const char *name, int mode); // rd/wr; create file FileStream(const std::string &name, int mode); // rd/wr; create file
FileStream(const char *name, int flags, int mode); // rd/wr; create file, use given flags FileStream(const std::string &name, int flags, int mode); // rd/wr; create file, use given flags
virtual ~FileStream(); virtual ~FileStream();
......
...@@ -47,7 +47,7 @@ class SocketStream : public FileDescriptorBasedStream ...@@ -47,7 +47,7 @@ class SocketStream : public FileDescriptorBasedStream
Client, Server Client, Server
}; };
SocketStream(const char *hostname, uint16 _port, Protocol, Mode, time_t timeout = 0, const char *nfskey = 0); SocketStream(const string &hostname, uint16 _port, Protocol, Mode, time_t timeout = 0, const string &nfskey = 0);
virtual ~SocketStream(); virtual ~SocketStream();
void reaccept(time_t timeout = 0); // only for TCP server socket void reaccept(time_t timeout = 0); // only for TCP server socket
...@@ -57,16 +57,16 @@ class SocketStream : public FileDescriptorBasedStream ...@@ -57,16 +57,16 @@ class SocketStream : public FileDescriptorBasedStream
const Mode mode; const Mode mode;
private: private:
const char *hostname; const string hostname;
uint16 port; uint16 port;
const char *nfskey; const string nfskey;
int listen_sk; int listen_sk;
void accept(time_t timeout); void accept(time_t timeout);
static std::string readkey(const char *nfskey, time_t &timeout); static std::string readkey(const string &nfskey, time_t &timeout);
static void writekey(const char *nfskey, uint16 port); static void writekey(const string &nfskey, uint16 port);
static void deletekey(const char *nfskey); static void deletekey(const string &nfskey);
}; };
} // namespace LOFAR } // namespace LOFAR
......
...@@ -33,23 +33,23 @@ ...@@ -33,23 +33,23 @@
namespace LOFAR { namespace LOFAR {
FileStream::FileStream(const char *name) FileStream::FileStream(const std::string &name)
{ {
if ((fd = open(name, O_RDONLY)) < 0) if ((fd = open(name.c_str(), O_RDONLY)) < 0)
throw SystemCallException(std::string("open ") + name, errno, THROW_ARGS); throw SystemCallException(std::string("open ") + name, errno, THROW_ARGS);
} }
FileStream::FileStream(const char *name, int mode) FileStream::FileStream(const std::string &name, int mode)
{ {
if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, mode)) < 0) if ((fd = open(name.c_str(), O_RDWR | O_CREAT | O_TRUNC, mode)) < 0)
throw SystemCallException(std::string("open ") + name, errno, THROW_ARGS); throw SystemCallException(std::string("open ") + name, errno, THROW_ARGS);
} }
FileStream::FileStream(const char *name, int flags, int mode) FileStream::FileStream(const std::string &name, int flags, int mode)
{ {
if ((fd = open(name, flags, mode)) < 0) if ((fd = open(name.c_str(), flags, mode)) < 0)
throw SystemCallException(std::string("open ") + name, errno, THROW_ARGS); throw SystemCallException(std::string("open ") + name, errno, THROW_ARGS);
} }
......
...@@ -65,7 +65,7 @@ static struct RandomState { ...@@ -65,7 +65,7 @@ static struct RandomState {
} randomState; } randomState;
SocketStream::SocketStream(const char *hostname, uint16 _port, Protocol protocol, Mode mode, time_t timeout, const char *nfskey) SocketStream::SocketStream(const std::string &hostname, uint16 _port, Protocol protocol, Mode mode, time_t timeout, const std::string &nfskey)
: :
protocol(protocol), protocol(protocol),
mode(mode), mode(mode),
...@@ -97,7 +97,7 @@ SocketStream::SocketStream(const char *hostname, uint16 _port, Protocol protocol ...@@ -97,7 +97,7 @@ SocketStream::SocketStream(const char *hostname, uint16 _port, Protocol protocol
int retval; int retval;
struct addrinfo *result; struct addrinfo *result;
if (mode == Client && nfskey) if (mode == Client && nfskey != "")
port = boost::lexical_cast<uint16>(readkey(nfskey, timeout)); port = boost::lexical_cast<uint16>(readkey(nfskey, timeout));
if (mode == Server && autoPort) if (mode == Server && autoPort)
...@@ -105,7 +105,7 @@ SocketStream::SocketStream(const char *hostname, uint16 _port, Protocol protocol ...@@ -105,7 +105,7 @@ SocketStream::SocketStream(const char *hostname, uint16 _port, Protocol protocol
snprintf(portStr, sizeof portStr, "%hu", port); snprintf(portStr, sizeof portStr, "%hu", port);
if ((retval = getaddrinfo(hostname, portStr, &hints, &result)) != 0) if ((retval = getaddrinfo(hostname.c_str(), portStr, &hints, &result)) != 0)
throw SystemCallException("getaddrinfo", retval, THROW_ARGS); throw SystemCallException("getaddrinfo", retval, THROW_ARGS);
// make sure result will be freed // make sure result will be freed
...@@ -210,13 +210,13 @@ void SocketStream::reaccept( time_t timeout ) ...@@ -210,13 +210,13 @@ void SocketStream::reaccept( time_t timeout )
void SocketStream::accept( time_t timeout ) void SocketStream::accept( time_t timeout )
{ {
if (nfskey) if (nfskey != "")
writekey(nfskey, port); writekey(nfskey, port);
// make sure the key will be deleted // make sure the key will be deleted
struct D { struct D {
~D() { ~D() {
if (nfskey) { if (nfskey != "") {
ScopedDelayCancellation dc; // unlink is a cancellation point ScopedDelayCancellation dc; // unlink is a cancellation point
try { try {
...@@ -227,7 +227,7 @@ void SocketStream::accept( time_t timeout ) ...@@ -227,7 +227,7 @@ void SocketStream::accept( time_t timeout )
} }
} }
const char *nfskey; const std::string &nfskey;
} onDestruct = { nfskey }; } onDestruct = { nfskey };
(void)onDestruct; (void)onDestruct;
...@@ -261,7 +261,7 @@ void SocketStream::setReadBufferSize(size_t size) ...@@ -261,7 +261,7 @@ void SocketStream::setReadBufferSize(size_t size)
} }
std::string SocketStream::readkey(const char *nfskey, time_t &timeout) std::string SocketStream::readkey(const std::string &nfskey, time_t &timeout)
{ {
for(;;) { for(;;) {
// sync NFS // sync NFS
...@@ -279,7 +279,7 @@ std::string SocketStream::readkey(const char *nfskey, time_t &timeout) ...@@ -279,7 +279,7 @@ std::string SocketStream::readkey(const char *nfskey, time_t &timeout)
char portStr[16]; char portStr[16];
ssize_t len; ssize_t len;
len = readlink(nfskey, portStr, sizeof portStr - 1); // reserve 1 character to insert \0 below len = readlink(nfskey.c_str(), portStr, sizeof portStr - 1); // reserve 1 character to insert \0 below
if (len >= 0) { if (len >= 0) {
portStr[len] = 0; portStr[len] = 0;
...@@ -300,20 +300,20 @@ std::string SocketStream::readkey(const char *nfskey, time_t &timeout) ...@@ -300,20 +300,20 @@ std::string SocketStream::readkey(const char *nfskey, time_t &timeout)
} }
} }
void SocketStream::writekey(const char *nfskey, uint16 port) void SocketStream::writekey(const std::string &nfskey, uint16 port)
{ {
char portStr[16]; char portStr[16];
snprintf(portStr, sizeof portStr, "%hu", port); snprintf(portStr, sizeof portStr, "%hu", port);
// Symlinks can be atomically created over NFS // Symlinks can be atomically created over NFS
if (symlink(portStr, nfskey) < 0) if (symlink(portStr, nfskey.c_str()) < 0)
throw SystemCallException("symlink", errno, THROW_ARGS); throw SystemCallException("symlink", errno, THROW_ARGS);
} }
void SocketStream::deletekey(const char *nfskey) void SocketStream::deletekey(const std::string &nfskey)
{ {
if (unlink(nfskey) < 0) if (unlink(nfskey.c_str()) < 0)
throw SystemCallException("unlink", errno, THROW_ARGS); throw SystemCallException("unlink", errno, THROW_ARGS);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment