📄 operations.cpp
字号:
PULARGE_INTEGER avail, PULARGE_INTEGER total, PULARGE_INTEGER free ) { return ::GetDiskFreeSpaceExA( ph.c_str(), avail, total, free ) != 0; } template< class String > boost::filesystem::detail::space_pair space_template( String & ph ) { ULARGE_INTEGER avail, total, free; boost::filesystem::detail::space_pair result; if ( get_free_disk_space( ph, &avail, &total, &free ) ) { result.first = ok; result.second.capacity = (static_cast<boost::uintmax_t>(total.HighPart) << 32) + total.LowPart; result.second.free = (static_cast<boost::uintmax_t>(free.HighPart) << 32) + free.LowPart; result.second.available = (static_cast<boost::uintmax_t>(avail.HighPart) << 32) + avail.LowPart; } else { result.first = error_code( ::GetLastError(), system_category ); result.second.capacity = result.second.free = result.second.available = 0; } return result; } inline DWORD get_current_directory( DWORD sz, char * buf ) { return ::GetCurrentDirectoryA( sz, buf ); } template< class String > error_code get_current_path_template( String & ph ) { DWORD sz; if ( (sz = get_current_directory( 0, static_cast<typename String::value_type*>(0) )) == 0 ) { sz = 1; } typedef typename String::value_type value_type; boost::scoped_array<value_type> buf( new value_type[sz] ); if ( get_current_directory( sz, buf.get() ) == 0 ) return error_code( ::GetLastError(), system_category ); ph = buf.get(); return ok; } inline bool set_current_directory( const char * buf ) { return ::SetCurrentDirectoryA( buf ) != 0; } template< class String > error_code set_current_path_template( const String & ph ) { return error_code( set_current_directory( ph.c_str() ) ? 0 : ::GetLastError(), system_category ); } inline std::size_t get_full_path_name( const std::string & ph, std::size_t len, char * buf, char ** p ) { return static_cast<std::size_t>( ::GetFullPathNameA( ph.c_str(), static_cast<DWORD>(len), buf, p )); } const std::size_t buf_size( 128 ); template<class String> error_code get_full_path_name_template( const String & ph, String & target ) { typename String::value_type buf[buf_size]; typename String::value_type * pfn; std::size_t len = get_full_path_name( ph, buf_size , buf, &pfn ); if ( len == 0 ) return error_code( ::GetLastError(), system_category ); if ( len > buf_size ) { typedef typename String::value_type value_type; boost::scoped_array<value_type> big_buf( new value_type[len] ); if ( (len=get_full_path_name( ph, len , big_buf.get(), &pfn )) == 0 ) return error_code( ::GetLastError(), system_category ); big_buf[len] = '\0'; target = big_buf.get(); return ok; } buf[len] = '\0'; target = buf; return ok; } template<class String> error_code get_file_write_time( const String & ph, FILETIME & last_write_time ) { handle_wrapper hw( create_file( ph.c_str(), 0, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 ) ); if ( hw.handle == INVALID_HANDLE_VALUE ) return error_code( ::GetLastError(), system_category ); return error_code( ::GetFileTime( hw.handle, 0, 0, &last_write_time ) != 0 ? 0 : ::GetLastError(), system_category ); } template<class String> error_code set_file_write_time( const String & ph, const FILETIME & last_write_time ) { handle_wrapper hw( create_file( ph.c_str(), FILE_WRITE_ATTRIBUTES, FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 ) ); if ( hw.handle == INVALID_HANDLE_VALUE ) return error_code( ::GetLastError(), system_category ); return error_code( ::SetFileTime( hw.handle, 0, 0, &last_write_time ) != 0 ? 0 : ::GetLastError(), system_category ); } // these constants come from inspecting some Microsoft sample code std::time_t to_time_t( const FILETIME & ft ) { __int64 t = (static_cast<__int64>( ft.dwHighDateTime ) << 32) + ft.dwLowDateTime;# if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0 t -= 116444736000000000LL;# else t -= 116444736000000000;# endif t /= 10000000; return static_cast<std::time_t>( t ); } void to_FILETIME( std::time_t t, FILETIME & ft ) { __int64 temp = t; temp *= 10000000;# if !defined( BOOST_MSVC ) || BOOST_MSVC > 1300 // > VC++ 7.0 temp += 116444736000000000LL;# else temp += 116444736000000000;# endif ft.dwLowDateTime = static_cast<DWORD>( temp ); ft.dwHighDateTime = static_cast<DWORD>( temp >> 32 ); } template<class String> boost::filesystem::detail::time_pair last_write_time_template( const String & ph ) { FILETIME lwt; error_code ec( get_file_write_time( ph, lwt ) ); return std::make_pair( ec, to_time_t( lwt ) ); } template<class String> error_code last_write_time_template( const String & ph, const std::time_t new_time ) { FILETIME lwt; to_FILETIME( new_time, lwt ); return set_file_write_time( ph, lwt ); } bool remove_directory( const std::string & ph ) { return ::RemoveDirectoryA( ph.c_str() ) != 0; } bool delete_file( const std::string & ph ) { return ::DeleteFileA( ph.c_str() ) != 0; } template<class String> error_code remove_template( const String & ph ) { // TODO: test this code in the presence of Vista symlinks, // including dangling, self-referal, and cyclic symlinks error_code ec; fs::file_status sf( fs::detail::status_api( ph, ec ) ); if ( ec ) return ec; if ( sf.type() == fs::file_not_found ) return ok; if ( fs::is_directory( sf ) ) { if ( !remove_directory( ph ) ) return error_code(::GetLastError(), system_category); } else { if ( !delete_file( ph ) ) return error_code(::GetLastError(), system_category); } return ok; } inline bool create_directory( const std::string & dir ) { return ::CreateDirectoryA( dir.c_str(), 0 ) != 0; } template<class String> boost::filesystem::detail::query_pair create_directory_template( const String & dir_ph ) { error_code error, dummy; if ( create_directory( dir_ph ) ) return std::make_pair( error, true ); error = error_code( ::GetLastError(), system_category ); // an error here may simply mean the postcondition is already met if ( error.value() == ERROR_ALREADY_EXISTS && fs::is_directory( fs::detail::status_api( dir_ph, dummy ) ) ) return std::make_pair( ok, false ); return std::make_pair( error, false ); }#if _WIN32_WINNT >= 0x500 inline bool create_hard_link( const std::string & to_ph, const std::string & from_ph ) { return ::CreateHardLinkA( from_ph.c_str(), to_ph.c_str(), 0 ) != 0; }#endif #if _WIN32_WINNT >= 0x500 template<class String> error_code create_hard_link_template( const String & to_ph, const String & from_ph ) { return error_code( create_hard_link( to_ph.c_str(), from_ph.c_str() ) ? 0 : ::GetLastError(), system_category ); }#endif#else // BOOST_POSIX_API int posix_remove( const char * p ) {# if defined(__QNXNTO__) || (defined(__MSL__) && (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))) // Some Metrowerks C library versions fail on directories because of a // known Metrowerks coding error in ::remove. Workaround is to call // rmdir() or unlink() as indicated. // Same bug also reported for QNX, with the same fix. int err = ::unlink( p ); if ( err != EPERM ) return err; return ::rmdir( p );# else return std::remove( p );# endif }#endif} // unnamed namespacenamespace boost{ namespace filesystem { namespace detail { BOOST_FILESYSTEM_DECL system::error_code throws;// free functions ----------------------------------------------------------// BOOST_FILESYSTEM_DECL error_code not_found_error() {# ifdef BOOST_WINDOWS_API return error_code(ERROR_PATH_NOT_FOUND, system_category);# else return error_code(ENOENT, system_category); # endif } BOOST_FILESYSTEM_DECL bool possible_large_file_size_support() {# ifdef BOOST_POSIX_API struct stat lcl_stat; return sizeof( lcl_stat.st_size ) > 4;# else return true;# endif }# ifdef BOOST_WINDOWS_API BOOST_FILESYSTEM_DECL fs::file_status status_api( const std::string & ph, error_code & ec ) { return status_template( ph, ec ); }# ifndef BOOST_FILESYSTEM_NARROW_ONLY BOOST_FILESYSTEM_DECL fs::file_status status_api( const std::wstring & ph, error_code & ec ) { return status_template( ph, ec ); } BOOST_FILESYSTEM_DECL bool symbolic_link_exists_api( const std::wstring & ) { return false; } BOOST_FILESYSTEM_DECL fs::detail::query_pair is_empty_api( const std::wstring & ph ) { return is_empty_template( ph ); } BOOST_FILESYSTEM_DECL fs::detail::query_pair equivalent_api( const std::wstring & ph1, const std::wstring & ph2 ) { return equivalent_template( ph1, ph2 ); } BOOST_FILESYSTEM_DECL fs::detail::uintmax_pair file_size_api( const std::wstring & ph ) { return file_size_template( ph ); } BOOST_FILESYSTEM_DECL fs::detail::space_pair space_api( const std::wstring & ph ) { return space_template( ph ); } BOOST_FILESYSTEM_DECL error_code get_current_path_api( std::wstring & ph ) { return get_current_path_template( ph ); } BOOST_FILESYSTEM_DECL error_code set_current_path_api( const std::wstring & ph ) { return set_current_path_template( ph ); } BOOST_FILESYSTEM_DECL error_code get_full_path_name_api( const std::wstring & ph, std::wstring & target ) { return get_full_path_name_template( ph, target ); } BOOST_FILESYSTEM_DECL time_pair last_write_time_api( const std::wstring & ph ) { return last_write_time_template( ph ); } BOOST_FILESYSTEM_DECL error_code last_write_time_api( const std::wstring & ph, std::time_t new_value ) { return last_write_time_template( ph, new_value ); } BOOST_FILESYSTEM_DECL fs::detail::query_pair create_directory_api( const std::wstring & ph ) { return create_directory_template( ph ); }#if _WIN32_WINNT >= 0x500
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -