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_ls.h" | 5 #include "net/ftp/ftp_directory_listing_parser_ls.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" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 // separate this column from the next column (number of links), resulting | 44 // separate this column from the next column (number of links), resulting |
45 // in additional characters at the end. Also, sometimes there is a "+" | 45 // in additional characters at the end. Also, sometimes there is a "+" |
46 // sign at the end indicating the file has ACLs set. | 46 // sign at the end indicating the file has ACLs set. |
47 | 47 |
48 // In fact, we don't even expect three "rwx" triplets of permission | 48 // In fact, we don't even expect three "rwx" triplets of permission |
49 // listing, as some FTP servers like Hylafax only send two. | 49 // listing, as some FTP servers like Hylafax only send two. |
50 return (LooksLikeUnixPermission(text.substr(1, 3)) && | 50 return (LooksLikeUnixPermission(text.substr(1, 3)) && |
51 LooksLikeUnixPermission(text.substr(4, 3))); | 51 LooksLikeUnixPermission(text.substr(4, 3))); |
52 } | 52 } |
53 | 53 |
54 bool LooksLikePermissionDeniedError(const string16& text) { | |
55 // Try to recognize a three-part colon-separated error message: | |
56 // | |
57 // 1. ftpd server name | |
58 // 2. directory name (often just ".") | |
59 // 3. message text (usually "Permission denied") | |
60 std::vector<string16> parts; | |
61 base::SplitString(CollapseWhitespace(text, false), ':', &parts); | |
62 | |
63 if (parts.size() != 3) | |
64 return false; | |
65 | |
66 return parts[2].find(ASCIIToUTF16("Permission denied")) != string16::npos; | |
67 } | |
68 | |
69 // Returns the column index of the end of the date listing and detected | 54 // Returns the column index of the end of the date listing and detected |
70 // last modification time. | 55 // last modification time. |
71 bool DetectColumnOffsetAndModificationTime(const std::vector<string16>& columns, | 56 bool DetectColumnOffsetAndModificationTime(const std::vector<string16>& columns, |
72 const base::Time& current_time, | 57 const base::Time& current_time, |
73 size_t* offset, | 58 size_t* offset, |
74 base::Time* modification_time) { | 59 base::Time* modification_time) { |
75 // The column offset can be arbitrarily large if some fields | 60 // The column offset can be arbitrarily large if some fields |
76 // like owner or group name contain spaces. Try offsets from left to right | 61 // like owner or group name contain spaces. Try offsets from left to right |
77 // and use the first one that matches a date listing. | 62 // and use the first one that matches a date listing. |
78 // | 63 // |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 continue; | 137 continue; |
153 } | 138 } |
154 | 139 |
155 FtpDirectoryListingEntry entry; | 140 FtpDirectoryListingEntry entry; |
156 | 141 |
157 size_t column_offset; | 142 size_t column_offset; |
158 if (!DetectColumnOffsetAndModificationTime(columns, | 143 if (!DetectColumnOffsetAndModificationTime(columns, |
159 current_time, | 144 current_time, |
160 &column_offset, | 145 &column_offset, |
161 &entry.last_modified)) { | 146 &entry.last_modified)) { |
162 // If we can't recognize a normal listing line, maybe it's an error? | 147 // Some servers send a message in on of the first few lines. |
eroman
2012/04/13 18:37:26
nit: "in on of the" --> "in one of the".
Paweł Hajdan Jr.
2012/04/14 10:12:14
Done.
| |
163 // In that case, just ignore the error, but still recognize the data | 148 // All those messages have in common is the string ".:", |
164 // as valid listing. | 149 // where "." means the current directory, and ":" separates it |
165 if (LooksLikePermissionDeniedError(lines[i])) | 150 // from the rest of the message, which may be empty. |
151 if (lines[i].find(ASCIIToUTF16(".:")) != string16::npos) | |
gavinp
2012/04/12 18:03:14
LGTM. Initially I thought this was going to not a
Paweł Hajdan Jr.
2012/04/13 16:02:34
This is parser of "ls -l" format. We have other fo
| |
166 continue; | 152 continue; |
167 | 153 |
168 return false; | 154 return false; |
169 } | 155 } |
170 | 156 |
171 if (!LooksLikeUnixPermissionsListing(columns[0])) | 157 if (!LooksLikeUnixPermissionsListing(columns[0])) |
172 return false; | 158 return false; |
173 if (columns[0][0] == 'l') { | 159 if (columns[0][0] == 'l') { |
174 entry.type = FtpDirectoryListingEntry::SYMLINK; | 160 entry.type = FtpDirectoryListingEntry::SYMLINK; |
175 } else if (columns[0][0] == 'd') { | 161 } else if (columns[0][0] == 'd') { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 entry.name = entry.name.substr(0, pos); | 204 entry.name = entry.name.substr(0, pos); |
219 } | 205 } |
220 | 206 |
221 entries->push_back(entry); | 207 entries->push_back(entry); |
222 } | 208 } |
223 | 209 |
224 return true; | 210 return true; |
225 } | 211 } |
226 | 212 |
227 } // namespace net | 213 } // namespace net |
OLD | NEW |