Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / ftp / ftp_directory_listing_parser_netware.cc
blobfa04bf214790bb952ed2b4f26b240c18f42e5bd8
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
3 // found in the LICENSE file.
5 #include "net/ftp/ftp_directory_listing_parser_netware.h"
7 #include <vector>
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/string_split.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "net/ftp/ftp_directory_listing_parser.h"
14 #include "net/ftp/ftp_util.h"
16 namespace {
18 bool LooksLikeNetwarePermissionsListing(const base::string16& text) {
19 if (text.length() != 10)
20 return false;
22 if (text[0] != '[' || text[9] != ']')
23 return false;
24 return (text[1] == 'R' || text[1] == '-') &&
25 (text[2] == 'W' || text[2] == '-') &&
26 (text[3] == 'C' || text[3] == '-') &&
27 (text[4] == 'E' || text[4] == '-') &&
28 (text[5] == 'A' || text[5] == '-') &&
29 (text[6] == 'F' || text[6] == '-') &&
30 (text[7] == 'M' || text[7] == '-') &&
31 (text[8] == 'S' || text[8] == '-');
34 } // namespace
36 namespace net {
38 bool ParseFtpDirectoryListingNetware(
39 const std::vector<base::string16>& lines,
40 const base::Time& current_time,
41 std::vector<FtpDirectoryListingEntry>* entries) {
42 if (!lines.empty() &&
43 !base::StartsWith(lines[0], base::ASCIIToUTF16("total "),
44 base::CompareCase::SENSITIVE)) {
45 return false;
48 for (size_t i = 1U; i < lines.size(); i++) {
49 if (lines[i].empty())
50 continue;
52 std::vector<base::string16> columns = base::SplitString(
53 base::CollapseWhitespace(lines[i], false), base::ASCIIToUTF16(" "),
54 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
56 if (columns.size() < 8)
57 return false;
59 FtpDirectoryListingEntry entry;
61 if (columns[0].length() != 1)
62 return false;
63 if (columns[0][0] == 'd') {
64 entry.type = FtpDirectoryListingEntry::DIRECTORY;
65 } else if (columns[0][0] == '-') {
66 entry.type = FtpDirectoryListingEntry::FILE;
67 } else {
68 return false;
71 // Note: on older Netware systems the permissions listing is in the same
72 // column as the entry type (just there is no space between them). We do not
73 // support the older format here for simplicity.
74 if (!LooksLikeNetwarePermissionsListing(columns[1]))
75 return false;
77 if (!base::StringToInt64(columns[3], &entry.size))
78 return false;
79 if (entry.size < 0)
80 return false;
81 if (entry.type != FtpDirectoryListingEntry::FILE)
82 entry.size = -1;
84 // Netware uses the same date listing format as Unix "ls -l".
85 if (!FtpUtil::LsDateListingToTime(columns[4], columns[5], columns[6],
86 current_time, &entry.last_modified)) {
87 return false;
90 entry.name = FtpUtil::GetStringPartAfterColumns(lines[i], 7);
92 entries->push_back(entry);
95 return true;
98 } // namespace net