OLD | NEW |
1 // Copyright (c) 2010 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_netware.h" | 5 #include "net/ftp/ftp_directory_listing_parser_netware.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/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "net/ftp/ftp_directory_listing_parser.h" |
13 #include "net/ftp/ftp_util.h" | 14 #include "net/ftp/ftp_util.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 bool LooksLikeNetwarePermissionsListing(const string16& text) { | 18 bool LooksLikeNetwarePermissionsListing(const string16& text) { |
18 if (text.length() != 10) | 19 if (text.length() != 10) |
19 return false; | 20 return false; |
20 | 21 |
21 if (text[0] != '[' || text[9] != ']') | 22 if (text[0] != '[' || text[9] != ']') |
22 return false; | 23 return false; |
23 return (text[1] == 'R' || text[1] == '-') && | 24 return (text[1] == 'R' || text[1] == '-') && |
24 (text[2] == 'W' || text[2] == '-') && | 25 (text[2] == 'W' || text[2] == '-') && |
25 (text[3] == 'C' || text[3] == '-') && | 26 (text[3] == 'C' || text[3] == '-') && |
26 (text[4] == 'E' || text[4] == '-') && | 27 (text[4] == 'E' || text[4] == '-') && |
27 (text[5] == 'A' || text[5] == '-') && | 28 (text[5] == 'A' || text[5] == '-') && |
28 (text[6] == 'F' || text[6] == '-') && | 29 (text[6] == 'F' || text[6] == '-') && |
29 (text[7] == 'M' || text[7] == '-') && | 30 (text[7] == 'M' || text[7] == '-') && |
30 (text[8] == 'S' || text[8] == '-'); | 31 (text[8] == 'S' || text[8] == '-'); |
31 } | 32 } |
32 | 33 |
33 } // namespace | 34 } // namespace |
34 | 35 |
35 namespace net { | 36 namespace net { |
36 | 37 |
37 FtpDirectoryListingParserNetware::FtpDirectoryListingParserNetware( | 38 bool ParseFtpDirectoryListingNetware( |
38 const base::Time& current_time) | 39 const std::vector<string16>& lines, |
39 : received_first_line_(false), | 40 const base::Time& current_time, |
40 current_time_(current_time) { | 41 std::vector<FtpDirectoryListingEntry>* entries) { |
41 } | 42 if (!lines.empty() && !StartsWith(lines[0], ASCIIToUTF16("total "), true)) |
| 43 return false; |
42 | 44 |
43 FtpDirectoryListingParserNetware::~FtpDirectoryListingParserNetware() {} | 45 for (size_t i = 1U; i < lines.size(); i++) { |
| 46 if (lines[i].empty()) |
| 47 continue; |
44 | 48 |
45 FtpServerType FtpDirectoryListingParserNetware::GetServerType() const { | 49 std::vector<string16> columns; |
46 return SERVER_NETWARE; | 50 base::SplitString(CollapseWhitespace(lines[i], false), ' ', &columns); |
47 } | |
48 | 51 |
49 bool FtpDirectoryListingParserNetware::ConsumeLine(const string16& line) { | 52 if (columns.size() != 8) |
50 if (!received_first_line_) { | 53 return false; |
51 received_first_line_ = true; | |
52 | 54 |
53 return StartsWith(line, ASCIIToUTF16("total "), true); | 55 FtpDirectoryListingEntry entry; |
| 56 |
| 57 if (columns[0].length() != 1) |
| 58 return false; |
| 59 if (columns[0][0] == 'd') { |
| 60 entry.type = FtpDirectoryListingEntry::DIRECTORY; |
| 61 } else if (columns[0][0] == '-') { |
| 62 entry.type = FtpDirectoryListingEntry::FILE; |
| 63 } else { |
| 64 return false; |
| 65 } |
| 66 |
| 67 // Note: on older Netware systems the permissions listing is in the same |
| 68 // column as the entry type (just there is no space between them). We do not |
| 69 // support the older format here for simplicity. |
| 70 if (!LooksLikeNetwarePermissionsListing(columns[1])) |
| 71 return false; |
| 72 |
| 73 if (!base::StringToInt64(columns[3], &entry.size)) |
| 74 return false; |
| 75 if (entry.size < 0) |
| 76 return false; |
| 77 if (entry.type != FtpDirectoryListingEntry::FILE) |
| 78 entry.size = -1; |
| 79 |
| 80 // Netware uses the same date listing format as Unix "ls -l". |
| 81 if (!FtpUtil::LsDateListingToTime(columns[4], columns[5], columns[6], |
| 82 current_time, &entry.last_modified)) { |
| 83 return false; |
| 84 } |
| 85 |
| 86 entry.name = columns[7]; |
| 87 |
| 88 entries->push_back(entry); |
54 } | 89 } |
55 | 90 |
56 std::vector<string16> columns; | |
57 base::SplitString(CollapseWhitespace(line, false), ' ', &columns); | |
58 | |
59 if (columns.size() != 8) | |
60 return false; | |
61 | |
62 FtpDirectoryListingEntry entry; | |
63 | |
64 if (columns[0].length() != 1) | |
65 return false; | |
66 if (columns[0][0] == 'd') { | |
67 entry.type = FtpDirectoryListingEntry::DIRECTORY; | |
68 } else if (columns[0][0] == '-') { | |
69 entry.type = FtpDirectoryListingEntry::FILE; | |
70 } else { | |
71 return false; | |
72 } | |
73 | |
74 // Note: on older Netware systems the permissions listing is in the same | |
75 // column as the entry type (just there is no space between them). We do not | |
76 // support the older format here for simplicity. | |
77 if (!LooksLikeNetwarePermissionsListing(columns[1])) | |
78 return false; | |
79 | |
80 if (!base::StringToInt64(columns[3], &entry.size)) | |
81 return false; | |
82 if (entry.size < 0) | |
83 return false; | |
84 if (entry.type != FtpDirectoryListingEntry::FILE) | |
85 entry.size = -1; | |
86 | |
87 // Netware uses the same date listing format as Unix "ls -l". | |
88 if (!FtpUtil::LsDateListingToTime(columns[4], columns[5], columns[6], | |
89 current_time_, &entry.last_modified)) { | |
90 return false; | |
91 } | |
92 | |
93 entry.name = columns[7]; | |
94 | |
95 entries_.push(entry); | |
96 return true; | 91 return true; |
97 } | 92 } |
98 | 93 |
99 bool FtpDirectoryListingParserNetware::OnEndOfInput() { | |
100 return true; | |
101 } | |
102 | |
103 bool FtpDirectoryListingParserNetware::EntryAvailable() const { | |
104 return !entries_.empty(); | |
105 } | |
106 | |
107 FtpDirectoryListingEntry FtpDirectoryListingParserNetware::PopEntry() { | |
108 FtpDirectoryListingEntry entry = entries_.front(); | |
109 entries_.pop(); | |
110 return entry; | |
111 } | |
112 | |
113 } // namespace net | 94 } // namespace net |
OLD | NEW |