Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 9 additions & 38 deletions c++/src/Timezone.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,16 @@
* limitations under the License.
*/

#include "orc/OrcFile.hh"
#include "Timezone.hh"

#include <errno.h>
#include <iostream>
#include <fcntl.h>
#include <map>
#include <sstream>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

namespace orc {

Expand Down Expand Up @@ -698,40 +694,15 @@ namespace orc {
if (itr != timezoneCache.end()) {
return *(itr->second).get();
}
int in = open(filename.c_str(), O_RDONLY);
if (in == -1) {
std::stringstream buffer;
buffer << "failed to open " << filename << " - " << strerror(errno);
throw TimezoneError(buffer.str());
}
struct stat fileInfo;
if (fstat(in, &fileInfo) == -1) {
std::stringstream buffer;
buffer << "failed to stat " << filename << " - " << strerror(errno);
throw TimezoneError(buffer.str());
}
if ((fileInfo.st_mode & S_IFMT) != S_IFREG) {
std::stringstream buffer;
buffer << "non-file in tzfile reader " << filename;
throw TimezoneError(buffer.str());
}
size_t size = static_cast<size_t>(fileInfo.st_size);
std::vector<unsigned char> buffer(size);
size_t posn = 0;
while (posn < size) {
ssize_t ret = read(in, &buffer[posn], size - posn);
if (ret == -1) {
throw TimezoneError(std::string("Failure to read timezone file ") +
filename + " - " + strerror(errno));
}
posn += static_cast<size_t>(ret);
}
if (close(in) == -1) {
std::stringstream err;
err << "failed to close " << filename << " - " << strerror(errno);
throw TimezoneError(err.str());
try {
ORC_UNIQUE_PTR<InputStream> file = readFile(filename);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'auto' is more suitable here

size_t size = static_cast<size_t>(file->getLength());
std::vector<unsigned char> buffer(size);
file->read(&buffer[0], size, 0);
timezoneCache[filename] = std::shared_ptr<Timezone>(new TimezoneImpl(filename, buffer));
} catch(ParseError& err) {
throw TimezoneError(err.what());
}
timezoneCache[filename] = std::shared_ptr<Timezone>(new TimezoneImpl(filename, buffer));
return *timezoneCache[filename].get();
}

Expand Down