OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_vms.h" | 5 #include "net/ftp/ftp_directory_listing_parser_vms.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/string_split.h" | 10 #include "base/strings/string_split.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "net/ftp/ftp_directory_listing_parser.h" | 14 #include "net/ftp/ftp_directory_listing_parser.h" |
15 #include "net/ftp/ftp_util.h" | 15 #include "net/ftp/ftp_util.h" |
16 | 16 |
17 namespace net { | 17 namespace net { |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Converts the filename component in listing to the filename we can display. | 21 // Converts the filename component in listing to the filename we can display. |
22 // Returns true on success. | 22 // Returns true on success. |
23 bool ParseVmsFilename(const base::string16& raw_filename, | 23 bool ParseVmsFilename(const base::string16& raw_filename, |
24 base::string16* parsed_filename, | 24 base::string16* parsed_filename, |
25 FtpDirectoryListingEntry::Type* type) { | 25 FtpDirectoryListingEntry::Type* type) { |
26 // On VMS, the files and directories are versioned. The version number is | 26 // On VMS, the files and directories are versioned. The version number is |
27 // separated from the file name by a semicolon. Example: ANNOUNCE.TXT;2. | 27 // separated from the file name by a semicolon. Example: ANNOUNCE.TXT;2. |
28 std::vector<base::string16> listing_parts; | 28 std::vector<base::string16> listing_parts = |
29 base::SplitString(raw_filename, ';', &listing_parts); | 29 base::SplitString(raw_filename, base::ASCIIToUTF16(";"), |
| 30 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
30 if (listing_parts.size() != 2) | 31 if (listing_parts.size() != 2) |
31 return false; | 32 return false; |
32 int version_number; | 33 int version_number; |
33 if (!base::StringToInt(listing_parts[1], &version_number)) | 34 if (!base::StringToInt(listing_parts[1], &version_number)) |
34 return false; | 35 return false; |
35 if (version_number < 0) | 36 if (version_number < 0) |
36 return false; | 37 return false; |
37 | 38 |
38 // Even directories have extensions in the listings. Don't display extensions | 39 // Even directories have extensions in the listings. Don't display extensions |
39 // for directories; it's awkward for non-VMS users. Also, VMS is | 40 // for directories; it's awkward for non-VMS users. Also, VMS is |
40 // case-insensitive, but generally uses uppercase characters. This may look | 41 // case-insensitive, but generally uses uppercase characters. This may look |
41 // awkward, so we convert them to lower case. | 42 // awkward, so we convert them to lower case. |
42 std::vector<base::string16> filename_parts; | 43 std::vector<base::string16> filename_parts = |
43 base::SplitString(listing_parts[0], '.', &filename_parts); | 44 base::SplitString(listing_parts[0], base::ASCIIToUTF16("."), |
| 45 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
44 if (filename_parts.size() != 2) | 46 if (filename_parts.size() != 2) |
45 return false; | 47 return false; |
46 if (base::EqualsASCII(filename_parts[1], "DIR")) { | 48 if (base::EqualsASCII(filename_parts[1], "DIR")) { |
47 *parsed_filename = base::StringToLowerASCII(filename_parts[0]); | 49 *parsed_filename = base::StringToLowerASCII(filename_parts[0]); |
48 *type = FtpDirectoryListingEntry::DIRECTORY; | 50 *type = FtpDirectoryListingEntry::DIRECTORY; |
49 } else { | 51 } else { |
50 *parsed_filename = base::StringToLowerASCII(listing_parts[0]); | 52 *parsed_filename = base::StringToLowerASCII(listing_parts[0]); |
51 *type = FtpDirectoryListingEntry::FILE; | 53 *type = FtpDirectoryListingEntry::FILE; |
52 } | 54 } |
53 return true; | 55 return true; |
(...skipping 11 matching lines...) Expand all Loading... |
65 // best information we have. | 67 // best information we have. |
66 const int kBlockSize = 512; | 68 const int kBlockSize = 512; |
67 | 69 |
68 if (base::StringToInt64(input, size)) { | 70 if (base::StringToInt64(input, size)) { |
69 if (*size < 0) | 71 if (*size < 0) |
70 return false; | 72 return false; |
71 *size *= kBlockSize; | 73 *size *= kBlockSize; |
72 return true; | 74 return true; |
73 } | 75 } |
74 | 76 |
75 std::vector<base::string16> parts; | 77 std::vector<base::StringPiece16> parts = |
76 base::SplitString(input, '/', &parts); | 78 base::SplitStringPiece(input, base::ASCIIToUTF16("/"), |
| 79 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
77 if (parts.size() != 2) | 80 if (parts.size() != 2) |
78 return false; | 81 return false; |
79 | 82 |
80 int64 blocks_used, blocks_allocated; | 83 int64 blocks_used, blocks_allocated; |
81 if (!base::StringToInt64(parts[0], &blocks_used)) | 84 if (!base::StringToInt64(parts[0], &blocks_used)) |
82 return false; | 85 return false; |
83 if (!base::StringToInt64(parts[1], &blocks_allocated)) | 86 if (!base::StringToInt64(parts[1], &blocks_allocated)) |
84 return false; | 87 return false; |
85 if (blocks_used > blocks_allocated) | 88 if (blocks_used > blocks_allocated) |
86 return false; | 89 return false; |
(...skipping 21 matching lines...) Expand all Loading... |
108 } | 111 } |
109 | 112 |
110 bool LooksLikeVmsFileProtectionListing(const base::string16& input) { | 113 bool LooksLikeVmsFileProtectionListing(const base::string16& input) { |
111 if (input.length() < 2) | 114 if (input.length() < 2) |
112 return false; | 115 return false; |
113 if (input[0] != '(' || input[input.length() - 1] != ')') | 116 if (input[0] != '(' || input[input.length() - 1] != ')') |
114 return false; | 117 return false; |
115 | 118 |
116 // We expect four parts of the file protection listing: for System, Owner, | 119 // We expect four parts of the file protection listing: for System, Owner, |
117 // Group, and World. | 120 // Group, and World. |
118 std::vector<base::string16> parts; | 121 std::vector<base::string16> parts = base::SplitString( |
119 base::SplitString(input.substr(1, input.length() - 2), ',', &parts); | 122 base::StringPiece16(input).substr(1, input.length() - 2), |
| 123 base::ASCIIToUTF16(","), base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
120 if (parts.size() != 4) | 124 if (parts.size() != 4) |
121 return false; | 125 return false; |
122 | 126 |
123 return LooksLikeVmsFileProtectionListingPart(parts[0]) && | 127 return LooksLikeVmsFileProtectionListingPart(parts[0]) && |
124 LooksLikeVmsFileProtectionListingPart(parts[1]) && | 128 LooksLikeVmsFileProtectionListingPart(parts[1]) && |
125 LooksLikeVmsFileProtectionListingPart(parts[2]) && | 129 LooksLikeVmsFileProtectionListingPart(parts[2]) && |
126 LooksLikeVmsFileProtectionListingPart(parts[3]); | 130 LooksLikeVmsFileProtectionListingPart(parts[3]); |
127 } | 131 } |
128 | 132 |
129 bool LooksLikeVmsUserIdentificationCode(const base::string16& input) { | 133 bool LooksLikeVmsUserIdentificationCode(const base::string16& input) { |
(...skipping 19 matching lines...) Expand all Loading... |
149 return false; | 153 return false; |
150 } | 154 } |
151 | 155 |
152 bool VmsDateListingToTime(const std::vector<base::string16>& columns, | 156 bool VmsDateListingToTime(const std::vector<base::string16>& columns, |
153 base::Time* time) { | 157 base::Time* time) { |
154 DCHECK_EQ(4U, columns.size()); | 158 DCHECK_EQ(4U, columns.size()); |
155 | 159 |
156 base::Time::Exploded time_exploded = { 0 }; | 160 base::Time::Exploded time_exploded = { 0 }; |
157 | 161 |
158 // Date should be in format DD-MMM-YYYY. | 162 // Date should be in format DD-MMM-YYYY. |
159 std::vector<base::string16> date_parts; | 163 std::vector<base::StringPiece16> date_parts = |
160 base::SplitString(columns[2], '-', &date_parts); | 164 base::SplitStringPiece(columns[2], base::ASCIIToUTF16("-"), |
| 165 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
161 if (date_parts.size() != 3) | 166 if (date_parts.size() != 3) |
162 return false; | 167 return false; |
163 if (!base::StringToInt(date_parts[0], &time_exploded.day_of_month)) | 168 if (!base::StringToInt(date_parts[0], &time_exploded.day_of_month)) |
164 return false; | 169 return false; |
165 if (!FtpUtil::AbbreviatedMonthToNumber(date_parts[1], | 170 if (!FtpUtil::AbbreviatedMonthToNumber(date_parts[1].as_string(), |
166 &time_exploded.month)) | 171 &time_exploded.month)) |
167 return false; | 172 return false; |
168 if (!base::StringToInt(date_parts[2], &time_exploded.year)) | 173 if (!base::StringToInt(date_parts[2], &time_exploded.year)) |
169 return false; | 174 return false; |
170 | 175 |
171 // Time can be in format HH:MM, HH:MM:SS, or HH:MM:SS.mm. Try to recognize the | 176 // Time can be in format HH:MM, HH:MM:SS, or HH:MM:SS.mm. Try to recognize the |
172 // last type first. Do not parse the seconds, they will be ignored anyway. | 177 // last type first. Do not parse the seconds, they will be ignored anyway. |
173 base::string16 time_column(columns[3]); | 178 base::string16 time_column(columns[3]); |
174 if (time_column.length() == 11 && time_column[8] == '.') | 179 if (time_column.length() == 11 && time_column[8] == '.') |
175 time_column = time_column.substr(0, 8); | 180 time_column = time_column.substr(0, 8); |
176 if (time_column.length() == 8 && time_column[5] == ':') | 181 if (time_column.length() == 8 && time_column[5] == ':') |
177 time_column = time_column.substr(0, 5); | 182 time_column = time_column.substr(0, 5); |
178 if (time_column.length() != 5) | 183 if (time_column.length() != 5) |
179 return false; | 184 return false; |
180 std::vector<base::string16> time_parts; | 185 std::vector<base::StringPiece16> time_parts = |
181 base::SplitString(time_column, ':', &time_parts); | 186 base::SplitStringPiece(time_column, base::ASCIIToUTF16(":"), |
| 187 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
182 if (time_parts.size() != 2) | 188 if (time_parts.size() != 2) |
183 return false; | 189 return false; |
184 if (!base::StringToInt(time_parts[0], &time_exploded.hour)) | 190 if (!base::StringToInt(time_parts[0], &time_exploded.hour)) |
185 return false; | 191 return false; |
186 if (!base::StringToInt(time_parts[1], &time_exploded.minute)) | 192 if (!base::StringToInt(time_parts[1], &time_exploded.minute)) |
187 return false; | 193 return false; |
188 | 194 |
189 // We don't know the time zone of the server, so just use local time. | 195 // We don't know the time zone of the server, so just use local time. |
190 *time = base::Time::FromLocalExploded(time_exploded); | 196 *time = base::Time::FromLocalExploded(time_exploded); |
191 return true; | 197 return true; |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 entries->push_back(entry); | 292 entries->push_back(entry); |
287 } | 293 } |
288 | 294 |
289 // The only place where we return true is after receiving the "Total" line, | 295 // The only place where we return true is after receiving the "Total" line, |
290 // that should be present in every VMS listing. Alternatively, if the listing | 296 // that should be present in every VMS listing. Alternatively, if the listing |
291 // contains error messages, it's OK not to have the "Total" line. | 297 // contains error messages, it's OK not to have the "Total" line. |
292 return seen_error; | 298 return seen_error; |
293 } | 299 } |
294 | 300 |
295 } // namespace net | 301 } // namespace net |
OLD | NEW |