1313// limitations under the License.
1414
1515#include < zvec/ailego/io/mmap_file.h>
16+ #include < zvec/ailego/utility/file_helper.h>
1617#include < zvec/core/framework/index_error.h>
1718#include < zvec/core/framework/index_logger.h>
1819#include < zvec/core/framework/index_mapping.h>
@@ -60,7 +61,8 @@ static inline bool WritePadding(ailego::File &file, size_t size) {
6061static inline int UnpackMappingSize (ailego::File &file, size_t *len) {
6162 IndexFormat::MetaHeader header;
6263 if (file.read (&header, sizeof (header)) != sizeof (header)) {
63- LOG_ERROR (" Failed to read file, errno %d, %s" , errno, std::strerror (errno));
64+ LOG_ERROR (" Failed to read file, %s" ,
65+ ailego::FileHelper::GetLastErrorString ().c_str ());
6466 return IndexError_ReadData;
6567 }
6668
@@ -93,13 +95,8 @@ int IndexMapping::open(const std::string &path, bool cow, bool full_mode) {
9395
9496 bool read_only = copy_on_write_ && !full_mode_;
9597 if (!file_.open (path.c_str (), read_only, false )) {
96- #if defined(_WIN32) || defined(_WIN64)
97- LOG_ERROR (" Failed to open file %s, win32 error %lu" , path.c_str (),
98- GetLastError ());
99- #else
100- LOG_ERROR (" Failed to open file %s, errno %d, %s" , path.c_str (), errno,
101- std::strerror (errno));
102- #endif
98+ LOG_ERROR (" Failed to open file %s, %s" , path.c_str (),
99+ ailego::FileHelper::GetLastErrorString ().c_str ());
103100 return IndexError_OpenFile;
104101 }
105102
@@ -111,8 +108,8 @@ int IndexMapping::open(const std::string &path, bool cow, bool full_mode) {
111108 }
112109
113110 if (!file_.seek (0 , ailego::File::Origin::End)) {
114- LOG_ERROR (" Failed to seek file %s, errno %d, % s" , path.c_str (), errno ,
115- std::strerror (errno ));
111+ LOG_ERROR (" Failed to seek file %s, % s" , path.c_str (),
112+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
116113 return IndexError_SeekFile;
117114 }
118115 return this ->init_index_mapping (mapping_size);
@@ -125,8 +122,8 @@ int IndexMapping::create(const std::string &path, size_t seg_meta_capacity) {
125122
126123 // write() & copying to mmap() will auto extend the file size
127124 if (!file_.create (path.c_str (), 0 )) {
128- LOG_ERROR (" Failed to create file %s, errno %d, % s" , path.c_str (), errno ,
129- std::strerror (errno ));
125+ LOG_ERROR (" Failed to create file %s, % s" , path.c_str (),
126+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
130127 return IndexError_CreateFile;
131128 }
132129 huge_page_ = Ishugetlbfs (path);
@@ -155,22 +152,22 @@ int IndexMapping::init_meta_section() {
155152 // Write index header
156153 IndexFormat::SetupMetaHeader (&meta_header, len - sizeof (meta_footer), len);
157154 if (!file_.seek (current_header_start_offset_, ailego::File::Origin::Begin)) {
158- LOG_ERROR (" Failed to seek file %s, errno %d, % s" , path.c_str (), errno ,
159- std::strerror (errno ));
155+ LOG_ERROR (" Failed to seek file %s, % s" , path.c_str (),
156+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
160157 return IndexError_SeekFile;
161158 }
162159 if (file_.write (&meta_header, sizeof (meta_header)) != sizeof (meta_header)) {
163- LOG_ERROR (" Failed to write file: %s, errno %d, % s" , path.c_str (), errno ,
164- std::strerror (errno ));
160+ LOG_ERROR (" Failed to write file: %s, % s" , path.c_str (),
161+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
165162 return IndexError_WriteData;
166163 }
167164
168165 // Write padding data
169166 uint32_t segments_meta_size =
170167 static_cast <uint32_t >(len - (sizeof (meta_header) + sizeof (meta_footer)));
171168 if (!WritePadding (file_, segments_meta_size)) {
172- LOG_ERROR (" Failed to write file: %s, errno %d, % s" , path.c_str (), errno ,
173- std::strerror (errno ));
169+ LOG_ERROR (" Failed to write file: %s, % s" , path.c_str (),
170+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
174171 return IndexError_WriteData;
175172 }
176173
@@ -180,8 +177,8 @@ int IndexMapping::init_meta_section() {
180177 meta_footer.total_size = len;
181178 IndexFormat::UpdateMetaFooter (&meta_footer, 0 );
182179 if (file_.write (&meta_footer, sizeof (meta_footer)) != sizeof (meta_footer)) {
183- LOG_ERROR (" Failed to write file: %s, errno %d, % s" , path.c_str (), errno ,
184- std::strerror (errno ));
180+ LOG_ERROR (" Failed to write file: %s, % s" , path.c_str (),
181+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
185182 return IndexError_WriteData;
186183 }
187184 return this ->init_index_mapping (len);
@@ -310,8 +307,8 @@ int IndexMapping::append(const std::string &id, size_t size) {
310307 }
311308
312309 if (!copy_on_write_ && !file_.truncate (index_size_ + size)) {
313- LOG_ERROR (" Failed to truncate file, errno %d, %s " , errno ,
314- std::strerror (errno ));
310+ LOG_ERROR (" Failed to truncate file, %s " ,
311+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
315312 return IndexError_TruncateFile;
316313 }
317314
@@ -425,8 +422,8 @@ void IndexMapping::unmap_all(void) {
425422
426423int IndexMapping::flush (void ) {
427424 if ((file_.size () < index_size_) && !file_.truncate (index_size_)) {
428- LOG_ERROR (" Failed to truncate file size %zu, errno %d, %s" , index_size_,
429- errno, std::strerror (errno ));
425+ LOG_ERROR (" Failed to truncate file size %zu, %s" , index_size_,
426+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
430427 return IndexError_TruncateFile;
431428 }
432429
@@ -443,8 +440,8 @@ int IndexMapping::flush(void) {
443440 segment_info.segment_header ->content_offset +
444441 item->meta ()->data_index ;
445442 if (file_.write (off, item->data (), segment_size) != segment_size) {
446- LOG_ERROR (" Failed to write segment, size %zu, errno %d, %s " ,
447- segment_size, errno, std::strerror (errno ));
443+ LOG_ERROR (" Failed to write segment, size %zu, %s " , segment_size ,
444+ ailego::FileHelper::GetLastErrorString (). c_str ( ));
448445 return IndexError_WriteData;
449446 }
450447 } else {
@@ -464,8 +461,9 @@ int IndexMapping::flush(void) {
464461 auto header = item.second ;
465462 if (file_.write (header_start_offset, header, header->content_offset ) !=
466463 header->content_offset ) {
467- LOG_ERROR (" Failed to write segment, size %lu, errno %d, %s" ,
468- header->content_offset , errno, std::strerror (errno));
464+ LOG_ERROR (" Failed to write segment, size %lu, %s" ,
465+ header->content_offset ,
466+ ailego::FileHelper::GetLastErrorString ().c_str ());
469467 return IndexError_WriteData;
470468 }
471469 }
@@ -487,7 +485,8 @@ int IndexMapping::init_index_mapping(size_t len) {
487485 uint8_t *start = reinterpret_cast <uint8_t *>(ailego::File::MemoryMap (
488486 file_.native_handle (), current_header_start_offset_, len, opts));
489487 if (!start) {
490- LOG_ERROR (" Failed to map file, errno %d, %s" , errno, std::strerror (errno));
488+ LOG_ERROR (" Failed to map file, %s" ,
489+ ailego::FileHelper::GetLastErrorString ().c_str ());
491490 return IndexError_MMapFile;
492491 }
493492
0 commit comments