Skip to content

Commit

Permalink
Call C++11 get_time and put_time (s3fs-fuse#2375)
Browse files Browse the repository at this point in the history
This removes workarounds and fixed-length buffers.
  • Loading branch information
gaul authored Nov 20, 2023
1 parent 43f81b7 commit 10a72bf
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 43 deletions.
10 changes: 6 additions & 4 deletions src/metaheader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/

#include <ctime>
#include <iomanip>
#include <sstream>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
Expand Down Expand Up @@ -254,8 +256,8 @@ time_t cvtIAMExpireStringToTime(const char* s)
if(!s){
return 0L;
}
memset(&tm, 0, sizeof(struct tm));
strptime(s, "%Y-%m-%dT%H:%M:%S", &tm);
std::istringstream ss(s);
ss >> std::get_time(&tm, "%Y-%m-%dT%H:%M:%S");
return timegm(&tm); // GMT
}

Expand All @@ -265,8 +267,8 @@ time_t get_lastmodified(const char* s)
if(!s){
return -1;
}
memset(&tm, 0, sizeof(struct tm));
strptime(s, "%a, %d %b %Y %H:%M:%S %Z", &tm);
std::istringstream ss(s);
ss >> std::get_time(&tm, "%a, %d %b %Y %H:%M:%S %Z");
return timegm(&tm); // GMT
}

Expand Down
5 changes: 2 additions & 3 deletions src/s3fs_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@ std::string S3fsLog::GetCurrentTime()
struct timeval now;
struct timespec tsnow;
struct tm res;
char tmp[32];
if(-1 == clock_gettime(S3FS_CLOCK_MONOTONIC, &tsnow)){
now.tv_sec = tsnow.tv_sec;
now.tv_usec = (tsnow.tv_nsec / 1000);
}else{
gettimeofday(&now, nullptr);
}
strftime(tmp, sizeof(tmp), "%Y-%m-%dT%H:%M:%S", gmtime_r(&now.tv_sec, &res));
current_time << tmp << "." << std::setfill('0') << std::setw(3) << (now.tv_usec / 1000) << "Z ";
current_time << std::put_time(gmtime_r(&now.tv_sec, &res), "%Y-%m-%dT%H:%M:%S")
<< "." << std::setfill('0') << std::setw(3) << (now.tv_usec / 1000) << "Z ";
}
return current_time.str();
}
Expand Down
42 changes: 12 additions & 30 deletions src/string_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <cerrno>
#include <climits>
#include <iomanip>

#include <sstream>

#include "s3fs_logger.h"
Expand All @@ -47,24 +46,6 @@ std::string str(const struct timespec value)
return s.str();
}

#ifdef __MSYS__
/*
* Polyfill for strptime function
*
* This source code is from https://gist.github.com/jeremyfromearth/5694aa3a66714254752179ecf3c95582 .
*/
char* strptime(const char* s, const char* f, struct tm* tm)
{
std::istringstream input(s);
input.imbue(std::locale(setlocale(LC_ALL, nullptr)));
input >> std::get_time(tm, f);
if (input.fail()) {
return nullptr;
}
return (char*)(s + input.tellg());
}
#endif

bool s3fs_strtoofft(off_t* value, const char* str, int base)
{
if(value == nullptr || str == nullptr){
Expand Down Expand Up @@ -259,11 +240,11 @@ bool get_keyword_value(const std::string& target, const char* keyword, std::stri
//
std::string get_date_rfc850()
{
char buf[100];
std::ostringstream ss;
time_t t = time(nullptr);
struct tm res;
strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S GMT", gmtime_r(&t, &res));
return buf;
ss << std::put_time(gmtime_r(&t, &res), "%a, %d %b %Y %H:%M:%S GMT");
return ss.str();
}

void get_date_sigv3(std::string& date, std::string& date8601)
Expand All @@ -275,18 +256,18 @@ void get_date_sigv3(std::string& date, std::string& date8601)

std::string get_date_string(time_t tm)
{
char buf[100];
std::ostringstream ss;
struct tm res;
strftime(buf, sizeof(buf), "%Y%m%d", gmtime_r(&tm, &res));
return buf;
ss << std::put_time(gmtime_r(&tm, &res), "%Y%m%d");
return ss.str();
}

std::string get_date_iso8601(time_t tm)
{
char buf[100];
std::ostringstream s;
struct tm res;
strftime(buf, sizeof(buf), "%Y%m%dT%H%M%SZ", gmtime_r(&tm, &res));
return buf;
s << std::put_time(gmtime_r(&tm, &res), "%Y%m%dT%H%M%SZ");
return s.str();
}

bool get_unixtime_from_iso8601(const char* pdate, time_t& unixtime)
Expand All @@ -296,8 +277,9 @@ bool get_unixtime_from_iso8601(const char* pdate, time_t& unixtime)
}

struct tm tm;
const char* prest = strptime(pdate, "%Y-%m-%dT%T", &tm);
if(prest == pdate){
std::istringstream ss(pdate);
ss >> std::get_time(&tm, "%Y-%m-%dT%T");
if(ss.fail()){
// wrong format
return false;
}
Expand Down
6 changes: 0 additions & 6 deletions src/string_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ static inline const char* SAFESTRPTR(const char *strptr) { return strptr ? strpt
// TODO: rename to to_string?
std::string str(const struct timespec value);

#ifdef __MSYS__
//
// Polyfill for strptime function.
//
char* strptime(const char* s, const char* f, struct tm* tm);
#endif
//
// Convert string to off_t. Returns false on bad input.
// Replacement for C++11 std::stoll.
Expand Down

0 comments on commit 10a72bf

Please sign in to comment.