• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/extensions/extension_creator_filter.h"
6 
7 #include <vector>
8 #include <set>
9 
10 #include "base/files/file_path.h"
11 
12 #if defined(OS_WIN)
13 #include <windows.h>
14 #endif
15 
16 namespace extensions {
17 
ShouldPackageFile(const base::FilePath & file_path)18 bool ExtensionCreatorFilter::ShouldPackageFile(
19     const base::FilePath& file_path) {
20   const base::FilePath& base_name = file_path.BaseName();
21   if (base_name.empty()) {
22     return false;
23   }
24 
25   // The file path that contains one of following special components should be
26   // excluded. See crbug.com/314360 and crbug.com/27840.
27   const base::FilePath::StringType names_to_exclude[] = {
28     FILE_PATH_LITERAL(".DS_Store"),
29     FILE_PATH_LITERAL(".git"),
30     FILE_PATH_LITERAL(".svn"),
31     FILE_PATH_LITERAL("__MACOSX"),
32     FILE_PATH_LITERAL("desktop.ini"),
33     FILE_PATH_LITERAL("Thumbs.db")
34   };
35   std::set<base::FilePath::StringType> names_to_exclude_set(names_to_exclude,
36       names_to_exclude + arraysize(names_to_exclude));
37   std::vector<base::FilePath::StringType> components;
38   file_path.GetComponents(&components);
39   for (size_t i = 0; i < components.size(); i++) {
40     if (names_to_exclude_set.count(components[i]))
41       return false;
42   }
43 
44   base::FilePath::CharType first_character = base_name.value()[0];
45   base::FilePath::CharType last_character =
46       base_name.value()[base_name.value().length() - 1];
47 
48   // dotfile
49   if (first_character == '.') {
50     return false;
51   }
52   // Emacs backup file
53   if (last_character == '~') {
54     return false;
55   }
56   // Emacs auto-save file
57   if (first_character == '#' && last_character == '#') {
58     return false;
59   }
60 
61 #if defined(OS_WIN)
62   // It's correct that we use file_path, not base_name, here, because we
63   // are working with the actual file.
64   DWORD file_attributes = ::GetFileAttributes(file_path.value().c_str());
65   if (file_attributes == INVALID_FILE_ATTRIBUTES) {
66     return false;
67   }
68   if ((file_attributes & FILE_ATTRIBUTE_HIDDEN) != 0) {
69     return false;
70   }
71 #endif
72 
73   return true;
74 }
75 
76 }  // namespace extensions
77