diff --git a/src/storage.cpp b/src/storage.cpp index 54b93c270..03c3c141e 100755 --- a/src/storage.cpp +++ b/src/storage.cpp @@ -121,10 +121,31 @@ namespace return true; } + bool exists_win( const path & ph ) + { + std::wstring wsave_path(safe_convert(ph.string())); + if(::GetFileAttributes( wsave_path.c_str() ) == 0xFFFFFFFF) + { + UINT err = ::GetLastError(); + if((err == ERROR_FILE_NOT_FOUND) + || (err == ERROR_INVALID_PARAMETER) + || (err == ERROR_NOT_READY) + || (err == ERROR_PATH_NOT_FOUND) + || (err == ERROR_INVALID_NAME) + || (err == ERROR_BAD_NETPATH )) + return false; // GetFileAttributes failed because the path does not exist + // for any other error we assume the file does exist and fall through, + // this may not be the best policy though... (JM 20040330) + return true; + } + return true; + } + + std::time_t last_write_time_win( const path & ph ) { // Works for both Windows and POSIX - struct _stat path_stat; + struct _stat path_stat; std::wstring wph(safe_convert(ph.native_file_string())); if ( ::_wstat( wph.c_str(), &path_stat ) != 0 ) boost::throw_exception( filesystem_error( @@ -461,9 +482,9 @@ namespace libtorrent save_path = complete(save_path); #if defined(WIN32) && defined(UNICODE) - std::wstring wsave_path(safe_convert(save_path.native_file_string())); - if (GetFileAttributes(wsave_path.c_str()) == INVALID_FILE_ATTRIBUTES) + if (!exists_win(save_path)) { + std::wstring wsave_path(safe_convert(save_path.native_file_string())); CreateDirectory(wsave_path.c_str(), 0); } else if ((GetFileAttributes(wsave_path.c_str()) & FILE_ATTRIBUTE_DIRECTORY) == 0) @@ -483,7 +504,15 @@ namespace libtorrent { path single_file = m_pimpl->info.begin_files()->path; if (single_file.has_branch_path()) + { +#if defined(WIN32) && defined(UNICODE) + std::wstring wsave_path(safe_convert((save_path / single_file.branch_path())) + .native_directory_string()); + CreateDirectory(wsave_path.c_str(), 0); +#else create_directory(save_path / single_file.branch_path()); +#endif + } old_path = m_pimpl->save_path / single_file; new_path = save_path / m_pimpl->info.begin_files()->path; @@ -1305,11 +1334,8 @@ namespace libtorrent path dir = m_save_path / file_iter->path; #if defined(WIN32) && defined(UNICODE) - std::wstring wdir(safe_convert(dir.branch_path().native_directory_string())); - if (GetFileAttributes(wdir.c_str()) != INVALID_FILE_ATTRIBUTES) - { + if (!exists_win(dir.branch_path())) create_directories_win(dir.branch_path()); - } #else if (!exists(dir.branch_path())) create_directories(dir.branch_path());