summaryrefslogtreecommitdiffstats
path: root/net/ftp/ftp_directory_listing_parser_netware.cc
blob: 1e8c80e357f82bd1b4b1fbef3618ea759d6cea14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/ftp/ftp_directory_listing_parser_netware.h"

#include <vector>

#include "base/string_number_conversions.h"
#include "base/string_split.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "net/ftp/ftp_directory_listing_parser.h"
#include "net/ftp/ftp_util.h"

namespace {

bool LooksLikeNetwarePermissionsListing(const string16& text) {
  if (text.length() != 10)
    return false;

  if (text[0] != '[' || text[9] != ']')
    return false;
  return (text[1] == 'R' || text[1] == '-') &&
         (text[2] == 'W' || text[2] == '-') &&
         (text[3] == 'C' || text[3] == '-') &&
         (text[4] == 'E' || text[4] == '-') &&
         (text[5] == 'A' || text[5] == '-') &&
         (text[6] == 'F' || text[6] == '-') &&
         (text[7] == 'M' || text[7] == '-') &&
         (text[8] == 'S' || text[8] == '-');
}

}  // namespace

namespace net {

bool ParseFtpDirectoryListingNetware(
    const std::vector<string16>& lines,
    const base::Time& current_time,
    std::vector<FtpDirectoryListingEntry>* entries) {
  if (!lines.empty() && !StartsWith(lines[0], ASCIIToUTF16("total "), true))
    return false;

  for (size_t i = 1U; i < lines.size(); i++) {
    if (lines[i].empty())
      continue;

    std::vector<string16> columns;
    base::SplitString(CollapseWhitespace(lines[i], false), ' ', &columns);

    if (columns.size() != 8)
      return false;

    FtpDirectoryListingEntry entry;

    if (columns[0].length() != 1)
      return false;
    if (columns[0][0] == 'd') {
      entry.type = FtpDirectoryListingEntry::DIRECTORY;
    } else if (columns[0][0] == '-') {
      entry.type = FtpDirectoryListingEntry::FILE;
    } else {
      return false;
    }

    // Note: on older Netware systems the permissions listing is in the same
    // column as the entry type (just there is no space between them). We do not
    // support the older format here for simplicity.
    if (!LooksLikeNetwarePermissionsListing(columns[1]))
      return false;

    if (!base::StringToInt64(columns[3], &entry.size))
      return false;
    if (entry.size < 0)
      return false;
    if (entry.type != FtpDirectoryListingEntry::FILE)
      entry.size = -1;

    // Netware uses the same date listing format as Unix "ls -l".
    if (!FtpUtil::LsDateListingToTime(columns[4], columns[5], columns[6],
                                      current_time, &entry.last_modified)) {
      return false;
    }

    entry.name = columns[7];

    entries->push_back(entry);
  }

  return true;
}

}  // namespace net