summaryrefslogtreecommitdiffstats
path: root/base/file_util_posix.cc
blob: 4f77e94be84ac7ba2e6b3fc1a1a22c8fd5e41c1f (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//    * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//    * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//    * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "base/file_util.h"

#include <sys/stat.h>
#include <sys/syslimits.h>
#include <fcntl.h>
#include <libgen.h>
#include <time.h>

#include <fstream>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/string_util.h"

namespace file_util {

std::wstring GetDirectoryFromPath(const std::wstring& path) {
  char full_path[PATH_MAX];
  base::strlcpy(full_path, WideToUTF8(path).c_str(), arraysize(full_path));
  return UTF8ToWide(dirname(full_path));
}
  
bool AbsolutePath(std::wstring* path) {
  return ResolveShortcut(path);
}

bool Delete(const std::wstring& path, bool recursive) {
  std::string utf8_path = WideToUTF8(path);
  struct stat64 file_info;
  if (stat64(utf8_path.c_str(), &file_info) != 0);
    return false;
  if (!S_ISDIR(file_info.st_mode))
    return (unlink(utf8_path.c_str()) == 0);
  if (!recursive)
    return (rmdir(utf8_path.c_str()) == 0);

  // TODO(erikkay): delete directories
  DCHECK(recursive);
  return false;
}

bool Move(const std::wstring& from_path, const std::wstring& to_path) {
  return (rename(WideToUTF8(from_path).c_str(),
                 WideToUTF8(to_path).c_str()) == 0);
}

bool CopyTree(const std::wstring& from_path, const std::wstring& to_path) {
  // TODO(erikkay): implement
  return false;
}

bool PathExists(const std::wstring& path) {
  struct stat64 file_info;
  return (stat64(WideToUTF8(path).c_str(), &file_info) == 0);
}

// TODO(erikkay): implement
#if 0
bool GetFileCreationLocalTimeFromHandle(int fd,
                                        LPSYSTEMTIME creation_time) {
  if (!file_handle)
    return false;
  
  FILETIME utc_filetime;
  if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL))
    return false;
  
  FILETIME local_filetime;
  if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime))
    return false;
  
  return !!FileTimeToSystemTime(&local_filetime, creation_time);
}

bool GetFileCreationLocalTime(const std::string& filename,
                              LPSYSTEMTIME creation_time) {
  ScopedHandle file_handle(
      CreateFile(filename.c_str(), GENERIC_READ, 
                 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
                 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
  return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time);
}
#endif

bool ResolveShortcut(std::wstring* path) {
  char full_path[PATH_MAX];
  if (!realpath(WideToUTF8(*path).c_str(), full_path))
    return false;
  *path = UTF8ToWide(full_path);
  return true;
}

bool CreateShortcutLink(const char *source, const char *destination,
                        const char *working_dir, const char *arguments, 
                        const char *description, const char *icon,
                        int icon_index) {
  // TODO(erikkay): implement
  return false;
}

bool CreateTemporaryFileName(std::wstring* temp_file) {
  std::wstring tmpdir;
  if (!GetTempDir(&tmpdir))
    return false;
  tmpdir.append(L"/com.google.chrome.XXXXXX");
  // this should be OK since mktemp just replaces characters in place
  char* buffer = const_cast<char*>(WideToUTF8(tmpdir).c_str());
  *temp_file = UTF8ToWide(mktemp(buffer));
  return true;
}

bool CreateNewTempDirectory(const std::wstring& prefix,
                            std::wstring* new_temp_path) {
  std::wstring tmpdir;
  if (!GetTempDir(&tmpdir))
    return false;
  tmpdir.append(L"/com.google.chrome.XXXXXX");
  // this should be OK since mkdtemp just replaces characters in place
  char* buffer = const_cast<char*>(WideToUTF8(tmpdir).c_str());
  char* dtemp = mkdtemp(buffer);
  if (!dtemp)
    return false;
  *new_temp_path = UTF8ToWide(dtemp);
  return true;
}

bool CreateDirectory(const std::wstring& full_path) {
  return (mkdir(WideToUTF8(full_path).c_str(), 0777) == 0);
}

bool GetFileSize(const std::wstring& file_path, int64* file_size) {
  struct stat64 file_info;
  if (stat64(WideToUTF8(file_path).c_str(), &file_info) != 0)
    return false;
  *file_size = file_info.st_size;
  return true;
}

int ReadFile(const std::wstring& filename, char* data, int size) {
  int fd = open(WideToUTF8(filename).c_str(), O_RDONLY);
  if (fd < 0)
    return -1;
  
  int ret_value = read(fd, data, size);
  close(fd);
  return ret_value;
}

int WriteFile(const std::wstring& filename, const char* data, int size) {
  int fd = open(WideToUTF8(filename).c_str(), O_WRONLY | O_CREAT | O_TRUNC, 
                0666);
  if (fd < 0)
    return -1;
  
  int ret_value = write(fd, data, size);
  close(fd);
  return ret_value;
}

// Gets the current working directory for the process.
bool GetCurrentDirectory(std::wstring* dir) {
  char system_buffer[PATH_MAX] = "";
  getcwd(system_buffer, sizeof(system_buffer));
  *dir = UTF8ToWide(system_buffer);
  return true;
}

// Sets the current working directory for the process.
bool SetCurrentDirectory(const std::wstring& current_directory) {
  int ret = chdir(WideToUTF8(current_directory).c_str());
  return (ret == 0);
}
  
// TODO(erikkay): implement
#if 0
FileEnumerator::FileEnumerator(const std::string& root_path,
                               bool recursive,
                               FileEnumerator::FILE_TYPE file_type)
    : recursive_(recursive),
      file_type_(file_type),
      is_in_find_op_(false),
      find_handle_(INVALID_HANDLE_VALUE) {
  pending_paths_.push(root_path);
}

FileEnumerator::FileEnumerator(const std::string& root_path,
                               bool recursive,
                               FileEnumerator::FILE_TYPE file_type,
                               const std::string& pattern)
    : recursive_(recursive),
      file_type_(file_type),
      is_in_find_op_(false),
      pattern_(pattern),
      find_handle_(INVALID_HANDLE_VALUE) {
  pending_paths_.push(root_path);
}

FileEnumerator::~FileEnumerator() {
  if (find_handle_ != INVALID_HANDLE_VALUE)
    FindClose(find_handle_);
}

std::wstring FileEnumerator::Next() {
  if (!is_in_find_op_) {
    if (pending_paths_.empty())
      return std::wstring();
    
    // The last find FindFirstFile operation is done, prepare a new one.
    // root_path_ must have the trailing directory character.
    root_path_ = pending_paths_.top();
    file_util::AppendToPath(&root_path_, std::wstring());
    pending_paths_.pop();
    
    // Start a new find operation.
    std::wstring src(root_path_);
    
    if (pattern_.empty())
      file_util::AppendToPath(&src, "*");  // No pattern = match everything.
    else
      file_util::AppendToPath(&src, pattern_);
    
    find_handle_ = FindFirstFile(src.c_str(), &find_data_);
    is_in_find_op_ = true;
    
  } else {
    // Search for the next file/directory.
    if (!FindNextFile(find_handle_, &find_data_)) {
      FindClose(find_handle_);
      find_handle_ = INVALID_HANDLE_VALUE;
    }
  }
  
  if (INVALID_HANDLE_VALUE == find_handle_) {
    is_in_find_op_ = false;
    
    // This is reached when we have finished a directory and are advancing to
    // the next one in the queue. We applied the pattern (if any) to the files
    // in the root search directory, but for those directories which were
    // matched, we want to enumerate all files inside them. This will happen
    // when the handle is empty.
    pattern_.clear();
    
    return Next();
  }
  
  std::wstring cur_file(find_data_.cFileName);
  // Skip over . and ..
  if (L"." == cur_file || L".." == cur_file)
    return Next();
  
  // Construct the absolute filename.
  cur_file.insert(0, root_path_);
  
  if (recursive_ && find_data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
    // If |cur_file| is a directory, and we are doing recursive searching, add
    // it to pending_paths_ so we scan it after we finish scanning this
    // directory.
    pending_paths_.push(cur_file);
    return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next();
  }
  return (file_type_ & FileEnumerator::FILES) ? cur_file : Next();
}
#endif
  
  
} // namespace file_util