diff options
author | wtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-17 21:18:13 +0000 |
---|---|---|
committer | wtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-17 21:18:13 +0000 |
commit | 028598357fb1cb848a95e83f1614e3d58cb87681 (patch) | |
tree | d0809f4e92efcbde5669bec3be0d4e68dd873185 /net | |
parent | 86fd149aaa8d1e5d0e369dedf9e9ebe15ef137e8 (diff) | |
download | chromium_src-028598357fb1cb848a95e83f1614e3d58cb87681.zip chromium_src-028598357fb1cb848a95e83f1614e3d58cb87681.tar.gz chromium_src-028598357fb1cb848a95e83f1614e3d58cb87681.tar.bz2 |
Use base::Time::Exploded instead of struct tm in
ftp_directory_parser.{h,cc}.
The patch is contributed by Ibrar Ahmed <ibrar.ahmad@gmail.com>.
Original review URL: http://codereview.chromium.org/126096
R=wtc
BUG=http://crbug.com/4965
TEST=none
Review URL: http://codereview.chromium.org/149772
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21003 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/ftp/ftp_directory_parser.cc | 205 | ||||
-rw-r--r-- | net/ftp/ftp_directory_parser.h | 40 | ||||
-rw-r--r-- | net/url_request/url_request_new_ftp_job.cc | 34 |
3 files changed, 120 insertions, 159 deletions
diff --git a/net/ftp/ftp_directory_parser.cc b/net/ftp/ftp_directory_parser.cc index cb89e9f..4c2cd04 100644 --- a/net/ftp/ftp_directory_parser.cc +++ b/net/ftp/ftp_directory_parser.cc @@ -41,14 +41,11 @@ #include "net/ftp/ftp_directory_parser.h" -#include <stdlib.h> -#include <string.h> -#include <ctype.h> - -#include "build/build_config.h" #include "base/basictypes.h" #include "base/string_util.h" +using base::Time; + // #undef anything you don't want to support #define SUPPORT_LSL // /bin/ls -l and dozens of variations therof #define SUPPORT_DLS // /bin/dls format (very, Very, VERY rare) @@ -59,18 +56,6 @@ #define SUPPORT_OS2 // IBM TCP/IP for OS/2 - FTP Server #define SUPPORT_W16 // win16 hosts: SuperTCP or NetManage Chameleon -// Implement the Unix gmtime_r() function for Windows. -#if defined(OS_WIN) -static struct tm *gmtime_r(const time_t *timer, struct tm *result) { - errno_t error = gmtime_s(result, timer); - if (error) { - errno = error; - return NULL; - } - return result; -} -#endif - namespace net { LineType ParseFTPLine(const char *line, @@ -168,10 +153,9 @@ LineType ParseFTPLine(const char *line, while (pos < linelen && isdigit(line[pos])) pos++; if (pos < linelen && line[pos] == ',') { - uint64 seconds = 0; - seconds = StringToInt64(p + 1); - time_t tim = static_cast<time_t>(seconds); - gmtime_r(&tim, &result->fe_time); + uint64 seconds = StringToInt64(p + 1); + Time t = Time::FromTimeT(seconds); + t.LocalExplode(&(result->fe_time)); } } } else if (*p == 's') { @@ -461,7 +445,7 @@ LineType ParseFTPLine(const char *line, tbuf[2] = tolower(p[2]); month_num = 0; for (pos = 0; pos < (12*3); pos += 3) { - if (tbuf[0] == month_names[pos+0] && + if (tbuf[0] == month_names[pos + 0] && tbuf[1] == month_names[pos + 1] && tbuf[2] == month_names[pos + 2]) break; @@ -469,18 +453,17 @@ LineType ParseFTPLine(const char *line, } if (month_num >= 12) month_num = 0; - result->fe_time.tm_mon = month_num; - result->fe_time.tm_mday = StringToInt(tokens[2]); - result->fe_time.tm_year = StringToInt(p + 4); - // NSPR wants year as XXXX + result->fe_time.month = month_num + 1; + result->fe_time.day_of_month = StringToInt(tokens[2]); + result->fe_time.year = StringToInt(p + 4); p = tokens[3] + 2; if (*p == ':') p++; if (p[2] == ':') - result->fe_time.tm_sec = StringToInt(p + 3); - result->fe_time.tm_hour = StringToInt(tokens[3]); - result->fe_time.tm_min = StringToInt(p); + result->fe_time.second = StringToInt(p + 3); + result->fe_time.hour = StringToInt(tokens[3]); + result->fe_time.minute = StringToInt(p); return result->fe_type; } // if (isdigit(*tokens[1])) return FTP_TYPE_JUNK; // junk @@ -587,22 +570,23 @@ LineType ParseFTPLine(const char *line, p = tokens[tokmarker+4]; if (toklen[tokmarker+4] == 10) { // newstyle: YYYY-MM-DD format - result->fe_time.tm_year = StringToInt(p + 0) - 1900; - result->fe_time.tm_mon = StringToInt(p + 5) - 1; - result->fe_time.tm_mday = StringToInt(p + 8); + result->fe_time.year = StringToInt(p + 0); + result->fe_time.month = StringToInt(p + 5); + result->fe_time.day_of_month = StringToInt(p + 8); } else { // oldstyle: [M]M/DD/YY format - pos = toklen[tokmarker + 4]; - result->fe_time.tm_mon = StringToInt(p) - 1; - result->fe_time.tm_mday = StringToInt((p + pos)-5); - result->fe_time.tm_year = StringToInt((p + pos)-2); - if (result->fe_time.tm_year < 70) - result->fe_time.tm_year += 100; + pos = toklen[tokmarker + 4]; + result->fe_time.month = StringToInt(p); + result->fe_time.day_of_month = StringToInt((p + pos)-5); + result->fe_time.year = StringToInt((p + pos)-2); + if (result->fe_time.year < 70) + result->fe_time.year += 100; + result->fe_time.year += 1900; } p = tokens[tokmarker + 5]; pos = toklen[tokmarker + 5]; - result->fe_time.tm_hour = StringToInt(p); - result->fe_time.tm_min = StringToInt((p + pos) - 5); - result->fe_time.tm_sec = StringToInt((p + pos) - 2); + result->fe_time.hour = StringToInt(p); + result->fe_time.minute = StringToInt((p + pos) - 5); + result->fe_time.second = StringToInt((p + pos) - 2); result->fe_cinfs = 1; result->fe_fname = tokens[0]; @@ -705,19 +689,23 @@ LineType ParseFTPLine(const char *line, } } - result->fe_time.tm_mon = StringToInt(tokens[0]+0); - if (result->fe_time.tm_mon != 0) { - result->fe_time.tm_mon--; - result->fe_time.tm_mday = StringToInt(tokens[0]+3); - result->fe_time.tm_year = StringToInt(tokens[0]+6); - if (result->fe_time.tm_year < 80) - result->fe_time.tm_year += 100; + result->fe_time.month = StringToInt(tokens[0] + 0); + if (result->fe_time.month != 0) { + result->fe_time.day_of_month = StringToInt(tokens[0] + 3); + result->fe_time.year = StringToInt(tokens[0] + 6); + // if year has only two digits then assume that + // 00-79 is 2000-2079 + // 80-99 is 1980-1999 + if (result->fe_time.year < 80) + result->fe_time.year += 2000; + else if (result->fe_time.year < 100) + result->fe_time.year += 1900; } - result->fe_time.tm_hour = StringToInt(tokens[1]+0); - result->fe_time.tm_min = StringToInt(tokens[1]+3); - if ((tokens[1][5]) == 'P' && result->fe_time.tm_hour < 12) - result->fe_time.tm_hour += 12; + result->fe_time.hour = StringToInt(tokens[1]+0); + result->fe_time.minute = StringToInt(tokens[1]+3); + if ((tokens[1][5]) == 'P' && result->fe_time.hour < 12) + result->fe_time.hour += 12; // the caller should do this (if dropping "." and ".." is desired) // if (result->fe_type == FTP_TYPE_DIRECTORY && result->fe_fname[0] @@ -805,13 +793,14 @@ LineType ParseFTPLine(const char *line, result->fe_size[pos] = '\0'; } - result->fe_time.tm_mon = StringToInt(&p[35-18]) - 1; - result->fe_time.tm_mday = StringToInt(&p[38-18]); - result->fe_time.tm_year = StringToInt(&p[41-18]); - if (result->fe_time.tm_year < 80) - result->fe_time.tm_year += 100; - result->fe_time.tm_hour = StringToInt(&p[46-18]); - result->fe_time.tm_min = StringToInt(&p[49-18]); + result->fe_time.month = StringToInt(&p[35-18]); + result->fe_time.day_of_month = StringToInt(&p[38 - 18]); + result->fe_time.year = StringToInt(&p[41 - 18]); + if (result->fe_time.year < 80) + result->fe_time.year += 100; + result->fe_time.year += 1900; + result->fe_time.hour = StringToInt(&p[46 - 18]); + result->fe_time.minute = StringToInt(&p[49 - 18]); // The caller should do this (if dropping "." and ".." is desired) // if (result->fe_type == FTP_TYPE_DIRECTORY && @@ -968,33 +957,31 @@ LineType ParseFTPLine(const char *line, result->fe_size[pos] = '\0'; } - result->fe_time.tm_mon = month_num; - result->fe_time.tm_mday = StringToInt(tokens[tokmarker+2]); - if (result->fe_time.tm_mday == 0) - result->fe_time.tm_mday++; + result->fe_time.month = month_num + 1; + result->fe_time.day_of_month = StringToInt(tokens[tokmarker+2]); + if (result->fe_time.day_of_month == 0) + result->fe_time.day_of_month++; p = tokens[tokmarker+3]; pos = (unsigned int)StringToInt(p); if (p[1] == ':') // one digit hour p--; if (p[2] != ':') { // year - result->fe_time.tm_year = pos; + result->fe_time.year = pos; } else { - result->fe_time.tm_hour = pos; - result->fe_time.tm_min = StringToInt(p+3); + result->fe_time.hour = pos; + result->fe_time.minute = StringToInt(p+3); if (p[5] == ':') - result->fe_time.tm_sec = StringToInt(p+6); - - if (!state->now_time) { - time_t now = time(NULL); - state->now_time = now * 1000000; - gmtime_r(&now, &state->now_tm); + result->fe_time.second = StringToInt(p+6); + if (!state->now_tm_valid) { + Time t = Time::Now(); + t.LocalExplode(&(state->now_tm)); + state->now_tm_valid = 1; } - - result->fe_time.tm_year = state->now_tm.tm_year; - if ( (( state->now_tm.tm_mon << 5) + state->now_tm.tm_mday) < - ((result->fe_time.tm_mon << 5) + result->fe_time.tm_mday) ) - result->fe_time.tm_year--; + result->fe_time.year = state->now_tm.year; + if (((state->now_tm.month << 5) + state->now_tm.day_of_month) < + ((result->fe_time.month << 5) + result->fe_time.day_of_month)) + result->fe_time.year--; } // time/year result->fe_fname = tokens[tokmarker+4]; @@ -1130,29 +1117,29 @@ LineType ParseFTPLine(const char *line, tbuf[0] = toupper(p[0]); tbuf[1] = tolower(p[1]); tbuf[2] = tolower(p[2]); - for (pos = 0; pos < (12*3); pos+=3) { - if (tbuf[0] == month_names[pos+0] && - tbuf[1] == month_names[pos+1] && - tbuf[2] == month_names[pos+2]) { - result->fe_time.tm_mon = pos/3; - result->fe_time.tm_mday = StringToInt(tokens[3]); - result->fe_time.tm_year = StringToInt(tokens[4]) - 1900; + for (pos = 0; pos < (12 * 3); pos += 3) { + if (tbuf[0] == month_names[pos + 0] && + tbuf[1] == month_names[pos + 1] && + tbuf[2] == month_names[pos + 2]) { + result->fe_time.month = pos / 3 + 1; + result->fe_time.day_of_month = StringToInt(tokens[3]); + result->fe_time.year = StringToInt(tokens[4]); break; } } pos = 5; // Chameleon toknum of date field } else { - result->fe_time.tm_mon = StringToInt(p+0)-1; - result->fe_time.tm_mday = StringToInt(p+3); - result->fe_time.tm_year = StringToInt(p+6); - if (result->fe_time.tm_year < 80) // SuperTCP - result->fe_time.tm_year += 100; - + result->fe_time.month = StringToInt(p + 0); + result->fe_time.day_of_month = StringToInt(p + 3); + result->fe_time.year = StringToInt(p+6); + if (result->fe_time.year < 80) // SuperTCP + result->fe_time.year += 100; + result->fe_time.year += 1900; pos = 3; // SuperTCP toknum of date field } - result->fe_time.tm_hour = StringToInt(tokens[pos]); - result->fe_time.tm_min = StringToInt(&(tokens[pos][toklen[pos]-2])); + result->fe_time.hour = StringToInt(tokens[pos]); + result->fe_time.minute = StringToInt(&(tokens[pos][toklen[pos]-2])); // the caller should do this (if dropping "." and ".." is desired) // if (result->fe_type == FTP_TYPE_DIRECTORY && @@ -1332,7 +1319,7 @@ LineType ParseFTPLine(const char *line, && isalpha(*p) && isalpha(p[1]) && isalpha(p[2])) { pos = StringToInt(tokens[pos]); if (pos > 0 && pos <= 31) { - result->fe_time.tm_mday = pos; + result->fe_time.day_of_month = pos; month_num = 1; for (pos = 0; pos < (12*3); pos += 3) { if (p[0] == month_names[pos + 0] && @@ -1342,34 +1329,34 @@ LineType ParseFTPLine(const char *line, month_num++; } if (month_num > 12) - result->fe_time.tm_mday = 0; + result->fe_time.day_of_month = 0; else - result->fe_time.tm_mon = month_num - 1; + result->fe_time.month = month_num; } } - if (result->fe_time.tm_mday) { + if (result->fe_time.day_of_month) { tokmarker += 3; // skip mday/mon/yrtime (to find " -> ") p = tokens[tokmarker]; pos = StringToInt(p); if (pos > 24) { - result->fe_time.tm_year = pos - 1900; + result->fe_time.year = pos; } else { if (p[1] == ':') p--; - result->fe_time.tm_hour = pos; - result->fe_time.tm_min = StringToInt(p + 3); - if (!state->now_time) { - time_t now = time(NULL); - state->now_time = now * 1000000; - gmtime_r(&now, &state->now_tm); + result->fe_time.hour = pos; + result->fe_time.minute = StringToInt(p + 3); + if (!state->now_tm_valid) { + Time t = Time::Now(); + t.LocalExplode(&(state->now_tm)); + state->now_tm_valid = 1; } - result->fe_time.tm_year = state->now_tm.tm_year; - if ((( state->now_tm.tm_mon << 4) + - state->now_tm.tm_mday) < - ((result->fe_time.tm_mon << 4) + - result->fe_time.tm_mday)) - result->fe_time.tm_year--; + result->fe_time.year = state->now_tm.year; + if (((state->now_tm.month << 4) + + state->now_tm.day_of_month) < + ((result->fe_time.month << 4) + + result->fe_time.day_of_month)) + result->fe_time.year--; } // got year or time } // got month/mday } // may have year or time diff --git a/net/ftp/ftp_directory_parser.h b/net/ftp/ftp_directory_parser.h index 83b1982..ff7bc40 100644 --- a/net/ftp/ftp_directory_parser.h +++ b/net/ftp/ftp_directory_parser.h @@ -41,21 +41,20 @@ #ifndef NET_FTP_FTP_DIRECTORY_PARSER_H_ #define NET_FTP_FTP_DIRECTORY_PARSER_H_ -#include <time.h> - -#include "base/basictypes.h" +#include "base/time.h" namespace net { struct ListState { - void* magic; // to determine if previously initialized - int64 now_time; // needed for year determination. - struct tm now_tm; // needed for year determination. - int lstyle; // LISTing style. - int parsed_one; // returned anything yet? - char carry_buf[84]; // for VMS multiline. - unsigned int carry_buf_len; // length of name in carry_buf. - unsigned int numlines; // number of lines seen. + void* magic; // to determine if previously + // initialized. + int now_tm_valid; // now_tm contains a valid time? + base::Time::Exploded now_tm; // needed for year determination. + int lstyle; // LISTing style. + int parsed_one; // returned anything yet? + char carry_buf[84]; // for VMS multiline. + unsigned int carry_buf_len; // length of name in carry_buf. + unsigned int numlines; // number of lines seen. }; enum LineType { @@ -67,15 +66,16 @@ enum LineType { }; struct ListResult { - LineType fe_type; - const char* fe_fname; // pointer to filename - unsigned int fe_fnlen; // length of filename - const char* fe_lname; // pointer to symlink name - unsigned int fe_lnlen; // length of symlink name - char fe_size[40]; // size of file in bytes (<= (2^128 - 1)) - int fe_cinfs; // file system is definitely case insensitive - // TODO(ibrar): We should use "base::Time::Exploded" instead of "tm" - struct tm fe_time; // last-modified time + LineType fe_type; + const char* fe_fname; // pointer to filename + unsigned int fe_fnlen; // length of filename + const char* fe_lname; // pointer to symlink name + unsigned int fe_lnlen; // length of symlink name + char fe_size[40]; // size of file in bytes + // (<= (2^128 - 1)) + base::Time::Exploded fe_time; // last-modified time + int fe_cinfs; // file system is definitely + // case insensitive }; // ParseFTPLine() parses line from an FTP LIST command. diff --git a/net/url_request/url_request_new_ftp_job.cc b/net/url_request/url_request_new_ftp_job.cc index e603a38..1bdd6cb 100644 --- a/net/url_request/url_request_new_ftp_job.cc +++ b/net/url_request/url_request_new_ftp_job.cc @@ -77,7 +77,7 @@ URLRequestNewFtpJob::~URLRequestNewFtpJob() { // static URLRequestJob* URLRequestNewFtpJob::Factory(URLRequest* request, const std::string& scheme) { - DCHECK(scheme == "ftp"); + DCHECK_EQ(scheme, "ftp"); if (request->url().has_port() && !net::IsPortAllowedByFtp(request->url().IntPort())) @@ -200,47 +200,21 @@ int URLRequestNewFtpJob::ProcessFtpDir(net::IOBuffer *buf, while (getline(iss, line)) { struct net::ListState state; struct net::ListResult result; - base::Time::Exploded et; std::replace(line.begin(), line.end(), '\r', '\0'); net::LineType line_type = ParseFTPLine(line.c_str(), &state, &result); switch (line_type) { case net::FTP_TYPE_DIRECTORY: - // TODO(ibrar): There is some problem in ParseFTPLine function or - // in conversion between tm and base::Time::Exploded. - // It returns wrong date/time (Differnce is 1 day and 17 Hours). - memset(&et, 0, sizeof(base::Time::Exploded)); - et.second = result.fe_time.tm_sec; - et.minute = result.fe_time.tm_min; - et.hour = result.fe_time.tm_hour; - et.day_of_month = result.fe_time.tm_mday; - et.month = result.fe_time.tm_mon + 1; - et.year = result.fe_time.tm_year + 1900; - et.day_of_week = result.fe_time.tm_wday; - file_entry.append(net::GetDirectoryListingEntry( RawByteSequenceToFilename(result.fe_fname, encoding_), - result.fe_fname, true, 0, base::Time::FromLocalExploded(et))); + result.fe_fname, true, 0, + base::Time::FromLocalExploded(result.fe_time))); break; case net::FTP_TYPE_FILE: - // TODO(ibrar): There should be a way to create a Time object based - // on "tm" structure. This will remove bunch of line of code to convert - // tm to Time object. - memset(&et, 0, sizeof(base::Time::Exploded)); - et.second = result.fe_time.tm_sec; - et.minute = result.fe_time.tm_min; - et.hour = result.fe_time.tm_hour; - et.day_of_month = result.fe_time.tm_mday; - et.month = result.fe_time.tm_mon + 1; - et.year = result.fe_time.tm_year + 1900; - et.day_of_week = result.fe_time.tm_wday; - // TODO(ibrar): There is some problem in ParseFTPLine function or - // in conversion between tm and base::Time::Exploded. - // It returns wrong date/time (Differnce is 1 day and 17 Hours). if (StringToInt64(result.fe_size, &file_size)) file_entry.append(net::GetDirectoryListingEntry( RawByteSequenceToFilename(result.fe_fname, encoding_), result.fe_fname, false, file_size, - base::Time::FromLocalExploded(et))); + base::Time::FromLocalExploded(result.fe_time))); break; case net::FTP_TYPE_SYMLINK: case net::FTP_TYPE_JUNK: |