< prev index next >

src/jdk.jpackage/windows/native/libjpackage/FileUtils.cpp

Print this page




  45     tstring buf;
  46     for (char charCode = 0; charCode < 32; ++charCode) {
  47         buf.append(1, charCode);
  48     }
  49     buf += _T("<>:\"|?*/\\");
  50     return buf;
  51 }
  52 
  53 } // namespace
  54 
  55 bool isDirSeparator(const tstring::value_type c) {
  56     return (c == '/' || c == '\\');
  57 }
  58 
  59 bool isFileExists(const tstring &filePath) {
  60     return GetFileAttributes(filePath.c_str()) != INVALID_FILE_ATTRIBUTES;
  61 }
  62 
  63 namespace {
  64 bool isDirectoryAttrs(const DWORD attrs) {
  65     return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;


  66 }
  67 } // namespace
  68 
  69 bool isDirectory(const tstring &filePath) {
  70     return isDirectoryAttrs(GetFileAttributes(filePath.c_str()));
  71 }
  72 
  73 bool isDirectoryNotEmpty(const tstring &dirPath) {
  74     if (!isDirectory(dirPath)) {
  75         return false;
  76     }
  77     return FALSE == PathIsDirectoryEmpty(dirPath.c_str());
  78 }
  79 
  80 tstring dirname(const tstring &path)

  81 {

  82     tstring::size_type pos = path.find_last_of(_T("\\/"));
  83     if (pos != tstring::npos) {
  84         pos = path.find_last_not_of(_T("\\/"), pos); // skip trailing slashes
  85     }
  86     return pos == tstring::npos ? tstring() : path.substr(0, pos + 1);
  87 }
  88 
  89 tstring basename(const tstring &path) {
  90     const tstring::size_type pos = path.find_last_of(_T("\\/"));
  91     if (pos == tstring::npos) {
  92         return path;
  93     }
  94     return path.substr(pos + 1);
  95 }
  96 
  97 tstring suffix(const tstring &path) {
  98     const tstring::size_type pos = path.rfind('.');
  99     if (pos == tstring::npos) {
 100         return tstring();
 101     }


 132     if (path.empty()) {
 133         return path;
 134     }
 135     tstring::const_reverse_iterator it = path.rbegin();
 136     tstring::const_reverse_iterator end = path.rend();
 137 
 138     while (it != end && isDirSeparator(*it)) {
 139         ++it;
 140     }
 141     return path.substr(0, end - it);
 142 }
 143 
 144 tstring normalizePath(tstring v) {
 145     std::replace(v.begin(), v.end(), '/', '\\');
 146     return tstrings::toLower(v);
 147 }
 148 
 149 namespace {
 150 
 151 bool createNewFile(const tstring& path) {
 152     HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);

 153     // if the file exists => h == INVALID_HANDLE_VALUE & GetLastError returns ERROR_FILE_EXISTS



 154     if (h != INVALID_HANDLE_VALUE) {
 155         CloseHandle(h);
 156         LOG_TRACE(tstrings::any() << "Created [" << path << "] file");
 157         return true;
 158     }
 159     return false;
 160 }
 161 
 162 } // namespace
 163 
 164 tstring createTempFile(const tstring &prefix, const tstring &suffix, const tstring &path)

 165 {

 166     const tstring invalidChars = reservedFilenameChars();
 167 
 168     if (prefix.find_first_of(invalidChars) != tstring::npos) {
 169         JP_THROW(tstrings::any() << "Illegal characters in prefix=" << prefix);
 170     }
 171 
 172     if (suffix.find_first_of(invalidChars) != tstring::npos) {
 173         JP_THROW(tstrings::any() << "Illegal characters in suffix=" << suffix);
 174     }
 175 
 176     int rnd = (int)GetTickCount();
 177 
 178     // do no more than 100 attempts
 179     for (int i=0; i<100; i++) {
 180         const tstring filePath = mkpath() << path << (prefix + (tstrings::any() << (rnd + i)).tstr() + suffix);


 181         if (createNewFile(filePath)) {
 182             return filePath;
 183         }
 184     }
 185 
 186     // 100 attempts failed
 187     JP_THROW(tstrings::any() << "createTempFile("  << prefix << ", "
 188                                                     << suffix << ", "
 189                                                     << path << ") failed");
 190 }
 191 
 192 tstring createTempDirectory(const tstring &prefix, const tstring &suffix, const tstring &basedir) {


 193     const tstring filePath = createTempFile(prefix, suffix, basedir);
 194     // delete the file and create directory with the same name
 195     deleteFile(filePath);
 196     createDirectory(filePath);
 197     return filePath;
 198 }
 199 
 200 tstring createUniqueFile(const tstring &prototype) {
 201     if (createNewFile(prototype)) {
 202         return prototype;
 203     }
 204 
 205     return createTempFile(replaceSuffix(basename(prototype)),
 206                                         suffix(prototype), dirname(prototype));
 207 }
 208 
 209 namespace {
 210 
 211 void createDir(const tstring path, LPSECURITY_ATTRIBUTES saAttr, tstring_array* createdDirs=0) {


 212     if (CreateDirectory(path.c_str(), saAttr)) {
 213         LOG_TRACE(tstrings::any() << "Created [" << path << "] directory");
 214         if (createdDirs) {
 215             createdDirs->push_back(removeTrailingSlash(path));
 216         }
 217     } else {
 218         const DWORD createDirectoryErr = GetLastError();
 219         // if saAttr is specified, fail even if the directory exists
 220         if (saAttr != NULL || !isDirectory(path)) {
 221             JP_THROW(SysError(tstrings::any() << "CreateDirectory(" << path << ") failed",

 222                                     CreateDirectory, createDirectoryErr));

 223         }
 224     }
 225 }
 226 
 227 }
 228 
 229 void createDirectory(const tstring &path, tstring_array* createdDirs) {
 230     const tstring dirPath = removeTrailingSlash(path) + _T("\\");
 231 
 232     tstring::size_type pos = dirPath.find_first_of(_T("\\/"));
 233     while (pos != tstring::npos) {
 234         const tstring subdirPath = dirPath.substr(0, pos + 1);
 235         createDir(subdirPath, NULL, createdDirs);
 236         pos = dirPath.find_first_of(_T("\\/"), pos + 1);
 237     }
 238 }
 239 
 240 
 241 void copyFile(const tstring& fromPath, const tstring& toPath, bool failIfExists) {


 242     createDirectory(dirname(toPath));
 243     if (!CopyFile(fromPath.c_str(), toPath.c_str(), (failIfExists ? TRUE : FALSE))) {


 244         JP_THROW(SysError(tstrings::any()
 245                     << "CopyFile(" << fromPath << ", " << toPath << ", "
 246                                     << failIfExists << ") failed", CopyFile));
 247     }
 248     LOG_TRACE(tstrings::any() << "Copied [" << fromPath << "] file to ["
 249                                                             << toPath << "]");
 250 }
 251 
 252 
 253 namespace {
 254 
 255 void moveFileImpl(const tstring& fromPath, const tstring& toPath,
 256                                                                 DWORD flags) {
 257     const bool isDir = isDirectory(fromPath);
 258     if (!MoveFileEx(fromPath.c_str(), toPath.empty() ? NULL : toPath.c_str(),
 259                                                                     flags)) {
 260         JP_THROW(SysError(tstrings::any() << "MoveFileEx(" << fromPath
 261                         << ", " << toPath << ", " << flags << ") failed",

 262                                                                 MoveFileEx));

 263     }
 264 
 265     const bool onReboot = 0 != (flags & MOVEFILE_DELAY_UNTIL_REBOOT);
 266 
 267     const LPCTSTR label = isDir ? _T("folder") : _T("file");
 268 
 269     tstrings::any msg;
 270     if (!toPath.empty()) {
 271         if (onReboot) {
 272             msg << "Move";
 273         } else {
 274             msg << "Moved";
 275         }
 276         msg << " '" << fromPath << "' " << label << " to '" << toPath << "'";
 277     } else {
 278         if (onReboot) {
 279             msg << "Delete";
 280         } else {
 281             msg << "Deleted";
 282         }
 283         msg << " '" << fromPath << "' " << label;
 284     }
 285     if (onReboot) {
 286         msg << " on reboot";
 287     }
 288     LOG_TRACE(msg);
 289 }
 290 
 291 } // namespace
 292 
 293 
 294 void moveFile(const tstring& fromPath, const tstring& toPath, bool failIfExists)

 295 {

 296     createDirectory(dirname(toPath));
 297 
 298     DWORD flags = MOVEFILE_COPY_ALLOWED;
 299     if (!failIfExists) {
 300         flags |= MOVEFILE_REPLACE_EXISTING;
 301     }
 302 
 303     moveFileImpl(fromPath, toPath, flags);
 304 }
 305 
 306 void deleteFile(const tstring &path)
 307 {
 308     if (!deleteFile(path, std::nothrow)) {
 309         JP_THROW(SysError(tstrings::any()
 310                         << "DeleteFile(" << path << ") failed", DeleteFile));
 311     }
 312 }
 313 
 314 namespace {
 315 


 509 
 510 struct FindFileDeleter {
 511     typedef HANDLE pointer;
 512     
 513     void operator()(HANDLE h) {
 514         if (h && h != INVALID_HANDLE_VALUE) {
 515             FindClose(h);
 516         }
 517     }
 518 };
 519 
 520 typedef std::unique_ptr<HANDLE, FindFileDeleter> UniqueFindFileHandle;
 521 
 522 }; // namesace
 523 void iterateDirectory(const tstring &dirPath, DirectoryCallback& callback)
 524 {
 525     const tstring searchString = combinePath(dirPath, _T("*"));
 526     WIN32_FIND_DATA findData;
 527     UniqueFindFileHandle h(FindFirstFile(searchString.c_str(), &findData));
 528     if (h.get() == INVALID_HANDLE_VALUE) {
 529         // GetLastError() == ERROR_FILE_NOT_FOUND is OK - no files in the directory

 530         // ERROR_PATH_NOT_FOUND is returned if the parent directory does not exist



 531         if (GetLastError() != ERROR_FILE_NOT_FOUND) {
 532             JP_THROW(SysError(tstrings::any() << "FindFirstFile(" << dirPath << ") failed", FindFirstFile));


 533         }
 534         return;
 535     }
 536 
 537     do {
 538         const tstring fname(findData.cFileName);
 539         const tstring filePath = combinePath(dirPath, fname);
 540         if (!isDirectoryAttrs(findData.dwFileAttributes)) {
 541             if (!callback.onFile(filePath)) {
 542                 return;
 543             }
 544         } else if (fname != _T(".") && fname != _T("..")) {
 545             if (!callback.onDirectory(filePath)) {
 546                 return;
 547             }
 548         }
 549     } while (FindNextFile(h.get(), &findData));
 550 
 551     // expect GetLastError() == ERROR_NO_MORE_FILES
 552     if (GetLastError() != ERROR_NO_MORE_FILES) {
 553         JP_THROW(SysError(tstrings::any() << "FindNextFile(" << dirPath << ") failed", FindNextFile));


 554     }
 555 }
 556 
 557 
 558 tstring replaceSuffix(const tstring& path, const tstring& newSuffix) {
 559     return (path.substr(0, path.size() - suffix(path).size()) + newSuffix);
 560 }
 561 
 562 
 563 DirectoryIterator& DirectoryIterator::findItems(tstring_array& v) {
 564     if (!isDirectory(root)) {
 565         return *this;
 566     }
 567 
 568     iterateDirectory(root, *this);
 569     v.insert(v.end(), items.begin(), items.end());
 570     items = tstring_array();
 571     return *this;
 572 }
 573 


 639     std::for_each(tmp.begin(), tmp.end(), DeleterFunctor());
 640 }
 641 
 642 Deleter& Deleter::appendFile(const tstring& path) {
 643     paths.push_back(std::make_pair(path, DeleterFunctor::File));
 644     return *this;
 645 }
 646 
 647 Deleter& Deleter::appendEmptyDirectory(const Directory& dir) {
 648      tstring path =  normalizePath(removeTrailingSlash(dir));
 649      const tstring parent = normalizePath(removeTrailingSlash(dir.parent));
 650      while(parent != path) {
 651          appendEmptyDirectory(path);
 652          path = dirname(path);
 653      }
 654 
 655     return *this;
 656 }
 657 
 658 Deleter& Deleter::appendEmptyDirectory(const tstring& path) {
 659     paths.push_back(std::make_pair(path,

 660                                 DeleterFunctor::EmptyDirectory));

 661     return *this;
 662 }
 663 
 664 Deleter& Deleter::appendAllFilesInDirectory(const tstring& path) {
 665     paths.push_back(std::make_pair(path,

 666                                 DeleterFunctor::FilesInDirectory));

 667     return *this;
 668 }
 669 
 670 Deleter& Deleter::appendRecursiveDirectory(const tstring& path) {
 671     paths.push_back(std::make_pair(path,

 672                             DeleterFunctor::RecursiveDirectory));

 673     return *this;
 674 }
 675 
 676 
 677 FileWriter::FileWriter(const tstring& path): dstPath(path) {
 678     tmpFile = FileUtils::createTempFile(_T("jds"), _T(".tmp"),
 679                                                     FileUtils::dirname(path));
 680 
 681     cleaner.appendFile(tmpFile);
 682 
 683     // we want to get exception on error
 684     tmp.exceptions(std::ifstream::failbit | std::ifstream::badbit);
 685     tmp.open(tmpFile, std::ios::binary | std::ios::trunc);
 686 }
 687 
 688 FileWriter& FileWriter::write(const void* buf, size_t bytes) {
 689     tmp.write(static_cast<const char*>(buf), bytes);
 690     return *this;
 691 }
 692 


  45     tstring buf;
  46     for (char charCode = 0; charCode < 32; ++charCode) {
  47         buf.append(1, charCode);
  48     }
  49     buf += _T("<>:\"|?*/\\");
  50     return buf;
  51 }
  52 
  53 } // namespace
  54 
  55 bool isDirSeparator(const tstring::value_type c) {
  56     return (c == '/' || c == '\\');
  57 }
  58 
  59 bool isFileExists(const tstring &filePath) {
  60     return GetFileAttributes(filePath.c_str()) != INVALID_FILE_ATTRIBUTES;
  61 }
  62 
  63 namespace {
  64 bool isDirectoryAttrs(const DWORD attrs) {
  65     return attrs != INVALID_FILE_ATTRIBUTES
  66             && (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
  67 }
  68 } // namespace
  69 
  70 bool isDirectory(const tstring &filePath) {
  71     return isDirectoryAttrs(GetFileAttributes(filePath.c_str()));
  72 }
  73 
  74 bool isDirectoryNotEmpty(const tstring &dirPath) {
  75     if (!isDirectory(dirPath)) {
  76         return false;
  77     }
  78     return FALSE == PathIsDirectoryEmpty(dirPath.c_str());
  79 }
  80 
  81 tstring dirname(const tstring &path) {

  82     tstring::size_type pos = path.find_last_of(_T("\\/"));
  83     if (pos != tstring::npos) {
  84         pos = path.find_last_not_of(_T("\\/"), pos); // skip trailing slashes
  85     }
  86     return pos == tstring::npos ? tstring() : path.substr(0, pos + 1);
  87 }
  88 
  89 tstring basename(const tstring &path) {
  90     const tstring::size_type pos = path.find_last_of(_T("\\/"));
  91     if (pos == tstring::npos) {
  92         return path;
  93     }
  94     return path.substr(pos + 1);
  95 }
  96 
  97 tstring suffix(const tstring &path) {
  98     const tstring::size_type pos = path.rfind('.');
  99     if (pos == tstring::npos) {
 100         return tstring();
 101     }


 132     if (path.empty()) {
 133         return path;
 134     }
 135     tstring::const_reverse_iterator it = path.rbegin();
 136     tstring::const_reverse_iterator end = path.rend();
 137 
 138     while (it != end && isDirSeparator(*it)) {
 139         ++it;
 140     }
 141     return path.substr(0, end - it);
 142 }
 143 
 144 tstring normalizePath(tstring v) {
 145     std::replace(v.begin(), v.end(), '/', '\\');
 146     return tstrings::toLower(v);
 147 }
 148 
 149 namespace {
 150 
 151 bool createNewFile(const tstring& path) {
 152     HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
 153             FILE_ATTRIBUTE_NORMAL, NULL);
 154     // if the file exists => h == INVALID_HANDLE_VALUE & GetLastError
 155     // returns ERROR_FILE_EXISTS
 156     if (h != INVALID_HANDLE_VALUE) {
 157         CloseHandle(h);
 158         LOG_TRACE(tstrings::any() << "Created [" << path << "] file");
 159         return true;
 160     }
 161     return false;
 162 }
 163 
 164 } // namespace
 165 
 166 tstring createTempFile(const tstring &prefix, const tstring &suffix,
 167         const tstring &path) {
 168     const tstring invalidChars = reservedFilenameChars();
 169 
 170     if (prefix.find_first_of(invalidChars) != tstring::npos) {
 171         JP_THROW(tstrings::any() << "Illegal characters in prefix=" << prefix);
 172     }
 173 
 174     if (suffix.find_first_of(invalidChars) != tstring::npos) {
 175         JP_THROW(tstrings::any() << "Illegal characters in suffix=" << suffix);
 176     }
 177 
 178     int rnd = (int)GetTickCount();
 179 
 180     // do no more than 100 attempts
 181     for (int i=0; i<100; i++) {
 182         const tstring filePath = mkpath() << path << (prefix
 183                 + (tstrings::any() << (rnd + i)).tstr() + suffix);
 184         if (createNewFile(filePath)) {
 185             return filePath;
 186         }
 187     }
 188 
 189     // 100 attempts failed
 190     JP_THROW(tstrings::any() << "createTempFile("  << prefix << ", "
 191                                                     << suffix << ", "
 192                                                     << path << ") failed");
 193 }
 194 
 195 tstring createTempDirectory(const tstring &prefix, const tstring &suffix,
 196         const tstring &basedir) {
 197     const tstring filePath = createTempFile(prefix, suffix, basedir);
 198     // delete the file and create directory with the same name
 199     deleteFile(filePath);
 200     createDirectory(filePath);
 201     return filePath;
 202 }
 203 
 204 tstring createUniqueFile(const tstring &prototype) {
 205     if (createNewFile(prototype)) {
 206         return prototype;
 207     }
 208 
 209     return createTempFile(replaceSuffix(basename(prototype)),
 210             suffix(prototype), dirname(prototype));
 211 }
 212 
 213 namespace {
 214 
 215 void createDir(const tstring path, LPSECURITY_ATTRIBUTES saAttr,
 216         tstring_array* createdDirs=0) {
 217     if (CreateDirectory(path.c_str(), saAttr)) {
 218         LOG_TRACE(tstrings::any() << "Created [" << path << "] directory");
 219         if (createdDirs) {
 220             createdDirs->push_back(removeTrailingSlash(path));
 221         }
 222     } else {
 223         const DWORD createDirectoryErr = GetLastError();
 224         // if saAttr is specified, fail even if the directory exists
 225         if (saAttr != NULL || !isDirectory(path)) {
 226             JP_THROW(SysError(tstrings::any() << "CreateDirectory("
 227                 << path << ") failed", CreateDirectory, createDirectoryErr));
 228         }
 229     }
 230 }
 231 
 232 }
 233 
 234 void createDirectory(const tstring &path, tstring_array* createdDirs) {
 235     const tstring dirPath = removeTrailingSlash(path) + _T("\\");
 236 
 237     tstring::size_type pos = dirPath.find_first_of(_T("\\/"));
 238     while (pos != tstring::npos) {
 239         const tstring subdirPath = dirPath.substr(0, pos + 1);
 240         createDir(subdirPath, NULL, createdDirs);
 241         pos = dirPath.find_first_of(_T("\\/"), pos + 1);
 242     }
 243 }
 244 
 245 
 246 void copyFile(const tstring& fromPath, const tstring& toPath,
 247         bool failIfExists) {
 248     createDirectory(dirname(toPath));
 249     if (!CopyFile(fromPath.c_str(), toPath.c_str(),
 250             (failIfExists ? TRUE : FALSE))) {
 251         JP_THROW(SysError(tstrings::any()
 252                 << "CopyFile(" << fromPath << ", " << toPath << ", "
 253                 << failIfExists << ") failed", CopyFile));
 254     }
 255     LOG_TRACE(tstrings::any() << "Copied [" << fromPath << "] file to ["
 256             << toPath << "]");
 257 }
 258 
 259 
 260 namespace {
 261 
 262 void moveFileImpl(const tstring& fromPath, const tstring& toPath,
 263         DWORD flags) {
 264     const bool isDir = isDirectory(fromPath);
 265     if (!MoveFileEx(fromPath.c_str(), toPath.empty() ? NULL : toPath.c_str(),
 266             flags)) {
 267         JP_THROW(SysError(tstrings::any() << "MoveFileEx(" << fromPath
 268                 << ", " << toPath << ", " << flags << ") failed", MoveFileEx));

 269     }
 270 
 271     const bool onReboot = 0 != (flags & MOVEFILE_DELAY_UNTIL_REBOOT);
 272 
 273     const LPCTSTR label = isDir ? _T("folder") : _T("file");
 274 
 275     tstrings::any msg;
 276     if (!toPath.empty()) {
 277         if (onReboot) {
 278             msg << "Move";
 279         } else {
 280             msg << "Moved";
 281         }
 282         msg << " '" << fromPath << "' " << label << " to '" << toPath << "'";
 283     } else {
 284         if (onReboot) {
 285             msg << "Delete";
 286         } else {
 287             msg << "Deleted";
 288         }
 289         msg << " '" << fromPath << "' " << label;
 290     }
 291     if (onReboot) {
 292         msg << " on reboot";
 293     }
 294     LOG_TRACE(msg);
 295 }
 296 
 297 } // namespace
 298 
 299 
 300 void moveFile(const tstring& fromPath, const tstring& toPath,
 301         bool failIfExists) {
 302     createDirectory(dirname(toPath));
 303 
 304     DWORD flags = MOVEFILE_COPY_ALLOWED;
 305     if (!failIfExists) {
 306         flags |= MOVEFILE_REPLACE_EXISTING;
 307     }
 308 
 309     moveFileImpl(fromPath, toPath, flags);
 310 }
 311 
 312 void deleteFile(const tstring &path)
 313 {
 314     if (!deleteFile(path, std::nothrow)) {
 315         JP_THROW(SysError(tstrings::any()
 316                 << "DeleteFile(" << path << ") failed", DeleteFile));
 317     }
 318 }
 319 
 320 namespace {
 321 


 515 
 516 struct FindFileDeleter {
 517     typedef HANDLE pointer;
 518 
 519     void operator()(HANDLE h) {
 520         if (h && h != INVALID_HANDLE_VALUE) {
 521             FindClose(h);
 522         }
 523     }
 524 };
 525 
 526 typedef std::unique_ptr<HANDLE, FindFileDeleter> UniqueFindFileHandle;
 527 
 528 }; // namesace
 529 void iterateDirectory(const tstring &dirPath, DirectoryCallback& callback)
 530 {
 531     const tstring searchString = combinePath(dirPath, _T("*"));
 532     WIN32_FIND_DATA findData;
 533     UniqueFindFileHandle h(FindFirstFile(searchString.c_str(), &findData));
 534     if (h.get() == INVALID_HANDLE_VALUE) {
 535         // GetLastError() == ERROR_FILE_NOT_FOUND is OK
 536         // - no files in the directory
 537         // ERROR_PATH_NOT_FOUND is returned
 538         // if the parent directory does not exist
 539         if (GetLastError() != ERROR_FILE_NOT_FOUND) {
 540             JP_THROW(SysError(tstrings::any() << "FindFirstFile("
 541                     << dirPath << ") failed", FindFirstFile));
 542         }
 543         return;
 544     }
 545 
 546     do {
 547         const tstring fname(findData.cFileName);
 548         const tstring filePath = combinePath(dirPath, fname);
 549         if (!isDirectoryAttrs(findData.dwFileAttributes)) {
 550             if (!callback.onFile(filePath)) {
 551                 return;
 552             }
 553         } else if (fname != _T(".") && fname != _T("..")) {
 554             if (!callback.onDirectory(filePath)) {
 555                 return;
 556             }
 557         }
 558     } while (FindNextFile(h.get(), &findData));
 559 
 560     // expect GetLastError() == ERROR_NO_MORE_FILES
 561     if (GetLastError() != ERROR_NO_MORE_FILES) {
 562         JP_THROW(SysError(tstrings::any() << "FindNextFile("
 563                 << dirPath << ") failed", FindNextFile));
 564     }
 565 }
 566 
 567 
 568 tstring replaceSuffix(const tstring& path, const tstring& newSuffix) {
 569     return (path.substr(0, path.size() - suffix(path).size()) + newSuffix);
 570 }
 571 
 572 
 573 DirectoryIterator& DirectoryIterator::findItems(tstring_array& v) {
 574     if (!isDirectory(root)) {
 575         return *this;
 576     }
 577 
 578     iterateDirectory(root, *this);
 579     v.insert(v.end(), items.begin(), items.end());
 580     items = tstring_array();
 581     return *this;
 582 }
 583 


 649     std::for_each(tmp.begin(), tmp.end(), DeleterFunctor());
 650 }
 651 
 652 Deleter& Deleter::appendFile(const tstring& path) {
 653     paths.push_back(std::make_pair(path, DeleterFunctor::File));
 654     return *this;
 655 }
 656 
 657 Deleter& Deleter::appendEmptyDirectory(const Directory& dir) {
 658      tstring path =  normalizePath(removeTrailingSlash(dir));
 659      const tstring parent = normalizePath(removeTrailingSlash(dir.parent));
 660      while(parent != path) {
 661          appendEmptyDirectory(path);
 662          path = dirname(path);
 663      }
 664 
 665     return *this;
 666 }
 667 
 668 Deleter& Deleter::appendEmptyDirectory(const tstring& path) {
 669     paths.push_back(std::make_pair(path, DeleterFunctor::EmptyDirectory));

 670     return *this;
 671 }
 672 
 673 Deleter& Deleter::appendAllFilesInDirectory(const tstring& path) {
 674     paths.push_back(std::make_pair(path, DeleterFunctor::FilesInDirectory));

 675     return *this;
 676 }
 677 
 678 Deleter& Deleter::appendRecursiveDirectory(const tstring& path) {
 679     paths.push_back(std::make_pair(path, DeleterFunctor::RecursiveDirectory));

 680     return *this;
 681 }
 682 
 683 
 684 FileWriter::FileWriter(const tstring& path): dstPath(path) {
 685     tmpFile = FileUtils::createTempFile(_T("jds"), _T(".tmp"),
 686             FileUtils::dirname(path));
 687 
 688     cleaner.appendFile(tmpFile);
 689 
 690     // we want to get exception on error
 691     tmp.exceptions(std::ifstream::failbit | std::ifstream::badbit);
 692     tmp.open(tmpFile, std::ios::binary | std::ios::trunc);
 693 }
 694 
 695 FileWriter& FileWriter::write(const void* buf, size_t bytes) {
 696     tmp.write(static_cast<const char*>(buf), bytes);
 697     return *this;
 698 }
 699 
< prev index next >