OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/ftp/ftp_directory_listing_parser_windows.h" | 5 #include "net/ftp/ftp_directory_listing_parser_os2.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/time.h" | 12 #include "base/time.h" |
13 #include "net/ftp/ftp_directory_listing_parser.h" | 13 #include "net/ftp/ftp_directory_listing_parser.h" |
14 #include "net/ftp/ftp_util.h" | 14 #include "net/ftp/ftp_util.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 bool WindowsDateListingToTime(const std::vector<string16>& columns, | 18 bool WindowsDateListingToTime(const std::vector<string16>& columns, |
eroman
2011/08/10 23:52:16
Delete this.
Paweł Hajdan Jr.
2011/08/11 00:03:19
Good catch, done.
| |
19 base::Time* time) { | 19 base::Time* time) { |
20 DCHECK_LE(3U, columns.size()); | 20 DCHECK_LE(3U, columns.size()); |
21 | 21 |
22 base::Time::Exploded time_exploded = { 0 }; | 22 base::Time::Exploded time_exploded = { 0 }; |
23 | 23 |
24 // Date should be in format MM-DD-YY[YY]. | 24 // Date should be in format MM-DD-YY[YY]. |
25 std::vector<string16> date_parts; | 25 std::vector<string16> date_parts; |
26 base::SplitString(columns[0], '-', &date_parts); | 26 base::SplitString(columns[0], '-', &date_parts); |
27 if (date_parts.size() != 3) | 27 if (date_parts.size() != 3) |
28 return false; | 28 return false; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 | 67 |
68 // We don't know the time zone of the server, so just use local time. | 68 // We don't know the time zone of the server, so just use local time. |
69 *time = base::Time::FromLocalExploded(time_exploded); | 69 *time = base::Time::FromLocalExploded(time_exploded); |
70 return true; | 70 return true; |
71 } | 71 } |
72 | 72 |
73 } // namespace | 73 } // namespace |
74 | 74 |
75 namespace net { | 75 namespace net { |
76 | 76 |
77 bool ParseFtpDirectoryListingWindows( | 77 bool ParseFtpDirectoryListingOS2( |
78 const std::vector<string16>& lines, | 78 const std::vector<string16>& lines, |
79 std::vector<FtpDirectoryListingEntry>* entries) { | 79 std::vector<FtpDirectoryListingEntry>* entries) { |
80 for (size_t i = 0; i < lines.size(); i++) { | 80 for (size_t i = 0; i < lines.size(); i++) { |
81 if (lines[i].empty()) | 81 if (lines[i].empty()) |
82 continue; | 82 continue; |
83 | 83 |
84 std::vector<string16> columns; | 84 std::vector<string16> columns; |
85 base::SplitString(CollapseWhitespace(lines[i], false), ' ', &columns); | 85 base::SplitString(CollapseWhitespace(lines[i], false), ' ', &columns); |
86 | 86 |
87 // Every line of the listing consists of the following: | 87 // Every line of the listing consists of the following: |
88 // | 88 // |
89 // 1. date | 89 // 1. size in bytes (0 for directories) |
90 // 2. time | 90 // 2. type (A for files, DIR for directories) |
91 // 3. size in bytes (or "<DIR>" for directories) | 91 // 3. date |
92 // 4. filename (may be empty or contain spaces) | 92 // 4. time |
93 // 5. filename (may be empty or contain spaces) | |
93 // | 94 // |
94 // For now, make sure we have 1-3, and handle 4 later. | 95 // For now, make sure we have 1-4, and handle 5 later. |
95 if (columns.size() < 3) | 96 if (columns.size() < 4) |
96 return false; | 97 return false; |
97 | 98 |
98 FtpDirectoryListingEntry entry; | 99 FtpDirectoryListingEntry entry; |
99 if (EqualsASCII(columns[2], "<DIR>")) { | 100 if (!base::StringToInt64(columns[0], &entry.size)) |
101 return false; | |
102 if (EqualsASCII(columns[1], "DIR")) { | |
103 if (entry.size != 0) | |
104 return false; | |
100 entry.type = FtpDirectoryListingEntry::DIRECTORY; | 105 entry.type = FtpDirectoryListingEntry::DIRECTORY; |
101 entry.size = -1; | 106 entry.size = -1; |
102 } else { | 107 } else if (EqualsASCII(columns[1], "A")) { |
103 entry.type = FtpDirectoryListingEntry::FILE; | 108 entry.type = FtpDirectoryListingEntry::FILE; |
104 if (!base::StringToInt64(columns[2], &entry.size)) | |
105 return false; | |
106 if (entry.size < 0) | 109 if (entry.size < 0) |
107 return false; | 110 return false; |
111 } else { | |
112 return false; | |
108 } | 113 } |
109 | 114 |
110 if (!WindowsDateListingToTime(columns, &entry.last_modified)) | 115 if (!FtpUtil::WindowsDateListingToTime(columns[2], |
116 columns[3], | |
117 &entry.last_modified)) { | |
111 return false; | 118 return false; |
119 } | |
112 | 120 |
113 entry.name = FtpUtil::GetStringPartAfterColumns(lines[i], 3); | 121 entry.name = FtpUtil::GetStringPartAfterColumns(lines[i], 4); |
114 if (entry.name.empty()) { | 122 if (entry.name.empty()) { |
115 // Some FTP servers send listing entries with empty names. | 123 // Some FTP servers send listing entries with empty names. |
116 // It's not obvious how to display such an entry, so ignore them. | 124 // It's not obvious how to display such an entry, so ignore them. |
117 // We don't want to make the parsing fail at this point though. | 125 // We don't want to make the parsing fail at this point though. |
118 // Other entries can still be useful. | 126 // Other entries can still be useful. |
119 continue; | 127 continue; |
120 } | 128 } |
121 | 129 |
122 entries->push_back(entry); | 130 entries->push_back(entry); |
123 } | 131 } |
124 | 132 |
125 return true; | 133 return true; |
126 } | 134 } |
127 | 135 |
128 } // namespace net | 136 } // namespace net |
OLD | NEW |