~ubuntu-branches/ubuntu/quantal/aria2/quantal

« back to all changes in this revision

Viewing changes to src/File.cc

  • Committer: Package Import Robot
  • Author(s): Kartik Mistry
  • Date: 2011-11-08 20:25:08 UTC
  • mfrom: (2.5.7 sid)
  • Revision ID: package-import@ubuntu.com-20111108202508-scfph8rj6tz0cckk
Tags: 1.13.0-1
* New upstream version:
  + Depends on libgcrypt11 (>= 1.5.0-3) (Closes: #642989)

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
#include <stdlib.h>
38
38
#include <sys/types.h>
39
 
#include <utime.h>
 
39
#ifdef HAVE_UTIME_H
 
40
# include <utime.h>
 
41
#endif // HAVE_UTIME_H
40
42
#include <unistd.h>
41
43
 
42
44
#include <vector>
52
54
 
53
55
namespace aria2 {
54
56
 
55
 
#ifdef __MINGW32__
56
 
# define WIN32_LEAN_AND_MEAN
57
 
# include <windows.h>
58
 
#endif // __MINGW32__
59
 
 
60
57
File::File(const std::string& name) : name_(name) {}
61
58
 
62
59
File::File(const File& c) : name_(c.name_) {}
72
69
}
73
70
 
74
71
int File::fillStat(a2_struct_stat& fstat) {
75
 
  return a2stat(name_.c_str(), &fstat);
 
72
  return a2stat(utf8ToWChar(name_).c_str(), &fstat);
76
73
}
77
74
 
78
75
bool File::exists() {
98
95
 
99
96
bool File::remove() {
100
97
  if(isFile()) {
101
 
    return unlink(name_.c_str()) == 0;
 
98
    return a2unlink(utf8ToWChar(name_).c_str()) == 0;
102
99
  } else if(isDir()) {
103
 
    return rmdir(name_.c_str()) == 0;
 
100
    return a2rmdir(utf8ToWChar(name_).c_str()) == 0;
104
101
  } else {
105
102
    return false;
106
103
  }
153
150
      A2_LOG_DEBUG(fmt("%s exists and is a directory.", dir.c_str()));
154
151
      continue;
155
152
    }
156
 
    if(a2mkdir(dir.c_str(), DIR_OPEN_MODE) == -1) {
 
153
    if(a2mkdir(utf8ToWChar(dir).c_str(), DIR_OPEN_MODE) == -1) {
157
154
      A2_LOG_DEBUG(fmt("Failed to create %s", dir.c_str()));
158
155
      return false;
159
156
    }
172
169
 
173
170
std::string File::getBasename() const
174
171
{
175
 
  std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
 
172
  std::string::size_type lastSlashIndex =
 
173
    name_.find_last_of(getPathSeparators());
176
174
  if(lastSlashIndex == std::string::npos) {
177
175
    return name_;
178
176
  } else {
182
180
 
183
181
std::string File::getDirname() const
184
182
{
185
 
  std::string::size_type lastSlashIndex = name_.find_last_of(A2STR::SLASH_C);
 
183
  std::string::size_type lastSlashIndex =
 
184
    name_.find_last_of(getPathSeparators());
186
185
  if(lastSlashIndex == std::string::npos) {
187
186
    if(name_.empty()) {
188
187
      return A2STR::NIL;
205
204
{
206
205
#ifdef __MINGW32__
207
206
  /* MinGW's rename() doesn't delete an existing destination */
208
 
  if (_access(dest.c_str(), 0) == 0) {
209
 
    if (_unlink(dest.c_str()) != 0) {
 
207
  if (_waccess(utf8ToWChar(dest).c_str(), 0) == 0) {
 
208
    if (a2unlink(utf8ToWChar(dest).c_str()) != 0) {
210
209
      return false;
211
210
    }
212
211
  }
213
212
#endif // __MINGW32__
214
 
  if(rename(name_.c_str(), dest.c_str()) == 0) {
 
213
  if(a2rename(utf8ToWChar(name_).c_str(), utf8ToWChar(dest).c_str()) == 0) {
215
214
    name_ = dest;
216
215
    return true;
217
216
  } else {
221
220
 
222
221
bool File::utime(const Time& actime, const Time& modtime) const
223
222
{
224
 
  struct utimbuf ub;
 
223
#if defined HAVE_UTIMES && !(defined __MINGW32__)
 
224
  struct timeval times[2] = {
 
225
    { actime.getTime(), 0 },
 
226
    { modtime.getTime(), 0 }
 
227
  };
 
228
  return utimes(name_.c_str(), times) == 0;
 
229
#else // !HAVE_UTIMES
 
230
  a2utimbuf ub;
225
231
  ub.actime = actime.getTime();
226
232
  ub.modtime = modtime.getTime();
227
 
  return ::utime(name_.c_str(), &ub) == 0;
 
233
  return a2utime(utf8ToWChar(name_).c_str(), &ub) == 0;
 
234
#endif // !HAVE_UTIMES
228
235
}
229
236
 
230
237
Time File::getModifiedTime()
238
245
 
239
246
std::string File::getCurrentDir()
240
247
{
 
248
#ifdef __MINGW32__
 
249
  const size_t buflen = 2048;
 
250
  wchar_t buf[buflen];
 
251
  if(_wgetcwd(buf, buflen)) {
 
252
    return wCharToUtf8(buf);
 
253
  } else {
 
254
    return A2STR::DOT_C;
 
255
  }
 
256
#else // !__MINGW32__
241
257
  const size_t buflen = 2048;
242
258
  char buf[buflen];
243
259
  if(getcwd(buf, buflen)) {
245
261
  } else {
246
262
    return A2STR::DOT_C;
247
263
  }
 
264
#endif // !__MINGW32__
 
265
}
 
266
 
 
267
const std::string& File::getPathSeparators()
 
268
{
 
269
#ifdef __MINGW32__
 
270
  static std::string s = "/\\";
 
271
#else // !__MINGW32__
 
272
  static std::string s = "/";
 
273
#endif // !__MINGW32__
 
274
  return s;
248
275
}
249
276
 
250
277
} // namespace aria2