From abb56a4a0dbeaedf0e71cf206db8e7e16932ab6b Mon Sep 17 00:00:00 2001 From: David Reiss Date: Thu, 26 Mar 2009 06:23:57 +0000 Subject: [PATCH] THRIFT-255. cpp: Add TSimpleFileTransport, a wrapper around TFDTransport git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@758533 13f79535-47bb-0310-9956-ffa450edef68 --- lib/cpp/Makefile.am | 2 + .../src/transport/TSimpleFileTransport.cpp | 41 +++++++++++++++++++ lib/cpp/src/transport/TSimpleFileTransport.h | 28 +++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 lib/cpp/src/transport/TSimpleFileTransport.cpp create mode 100644 lib/cpp/src/transport/TSimpleFileTransport.h diff --git a/lib/cpp/Makefile.am b/lib/cpp/Makefile.am index 7d3d886c..6b65b928 100644 --- a/lib/cpp/Makefile.am +++ b/lib/cpp/Makefile.am @@ -36,6 +36,7 @@ libthrift_la_SOURCES = src/Thrift.cpp \ src/transport/TTransportException.cpp \ src/transport/TFDTransport.cpp \ src/transport/TFileTransport.cpp \ + src/transport/TSimpleFileTransport.cpp \ src/transport/THttpClient.cpp \ src/transport/TSocket.cpp \ src/transport/TSocketPool.cpp \ @@ -94,6 +95,7 @@ include_transportdir = $(include_thriftdir)/transport include_transport_HEADERS = \ src/transport/TFDTransport.h \ src/transport/TFileTransport.h \ + src/transport/TSimpleFileTransport.h \ src/transport/TServerSocket.h \ src/transport/TServerTransport.h \ src/transport/THttpClient.h \ diff --git a/lib/cpp/src/transport/TSimpleFileTransport.cpp b/lib/cpp/src/transport/TSimpleFileTransport.cpp new file mode 100644 index 00000000..3500d3fe --- /dev/null +++ b/lib/cpp/src/transport/TSimpleFileTransport.cpp @@ -0,0 +1,41 @@ +// Copyright (c) 2006- Facebook +// Distributed under the Thrift Software License +// +// See accompanying file LICENSE or visit the Thrift site at: +// http://developers.facebook.com/thrift/ + +#include "TSimpleFileTransport.h" + +#include +#include +#include + +namespace apache { namespace thrift { namespace transport { + +TSimpleFileTransport:: +TSimpleFileTransport(const std::string& path, bool read, bool write) + : TFDTransport(-1, TFDTransport::CLOSE_ON_DESTROY) { + int flags = 0; + if (read && write) { + flags = O_RDWR; + } else if (read) { + flags = O_RDONLY; + } else if (write) { + flags = O_WRONLY; + } else { + throw TTransportException("Neither READ nor WRITE specified"); + } + if (write) { + flags |= O_CREAT | O_APPEND; + } + int fd = ::open(path.c_str(), + flags, + S_IRUSR | S_IWUSR| S_IRGRP | S_IROTH); + if (fd < 0) { + throw TTransportException("failed to open file for writing: " + path); + } + setFD(fd); + open(); +} + +}}} // apache::thrift::transport diff --git a/lib/cpp/src/transport/TSimpleFileTransport.h b/lib/cpp/src/transport/TSimpleFileTransport.h new file mode 100644 index 00000000..2470fd43 --- /dev/null +++ b/lib/cpp/src/transport/TSimpleFileTransport.h @@ -0,0 +1,28 @@ +// Copyright (c) 2006- Facebook +// Distributed under the Thrift Software License +// +// See accompanying file LICENSE or visit the Thrift site at: +// http://developers.facebook.com/thrift/ + +#ifndef _THRIFT_TRANSPORT_TSIMPLEFILETRANSPORT_H_ +#define _THRIFT_TRANSPORT_TSIMPLEFILETRANSPORT_H_ 1 + +#include "TFDTransport.h" + +namespace apache { namespace thrift { namespace transport { + +/** + * Dead-simple wrapper around a file. + * + * Writeable files are opened with O_CREAT and O_APPEND + */ +class TSimpleFileTransport : public TFDTransport { + public: + TSimpleFileTransport(const std::string& path, + bool read = true, + bool write = false); +}; + +}}} // apache::thrift::transport + +#endif // _THRIFT_TRANSPORT_TSIMPLEFILETRANSPORT_H_ -- 2.17.1