Skip to content

Commit 5bda4c5

Browse files
authored
Merge pull request #10 from HuolalaTech/fix/close-fd
fix(c++): close fd while flushing in incremental mode
2 parents 15979ff + 58035ca commit 5bda4c5

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Core/Glog.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ bool Glog::appendLogFile(uint8_t &maxRecursionDepth, time_t epochSeconds) {
419419
if (--maxRecursionDepth <= 0) {
420420
InternalError("appendLogFile() reach recursion upper limit");
421421
m_cacheFile->closeFile();
422+
closeFile(fd, filepath.c_str());
422423
return false;
423424
}
424425
return appendLogFile(maxRecursionDepth, epochSeconds);
@@ -450,13 +451,15 @@ bool Glog::appendLogFile(uint8_t &maxRecursionDepth, time_t epochSeconds) {
450451
InternalError("fail to truncate [%s] to size %zu, %s", filepath.c_str(), mmapOffset + mmapSize,
451452
::strerror(errno));
452453
m_cacheFile->closeFile();
454+
closeFile(fd, filepath.c_str());
453455
return false;
454456
}
455457

456458
if (!GlogFile::zeroFillFile(fd, fileSize, copySize)) {
457459
InternalError("fail to zeroFile [%s] to size %zu, %s", filepath.c_str(), mmapOffset + mmapSize,
458460
strerror(errno));
459461
m_cacheFile->closeFile();
462+
closeFile(fd, filepath.c_str());
460463
return false;
461464
}
462465

@@ -465,6 +468,7 @@ bool Glog::appendLogFile(uint8_t &maxRecursionDepth, time_t epochSeconds) {
465468
if (mmapPtr == MAP_FAILED) {
466469
InternalError("fail to mmap [%s], %s", filepath.c_str(), strerror(errno));
467470
m_cacheFile->closeFile();
471+
closeFile(fd, filepath.c_str());
468472
return false;
469473
}
470474

@@ -473,6 +477,7 @@ bool Glog::appendLogFile(uint8_t &maxRecursionDepth, time_t epochSeconds) {
473477
if (src == nullptr || src == MAP_FAILED) {
474478
InternalError("fail to get cache file [%s] mmap ptr", filepath.c_str());
475479
m_cacheFile->closeFile();
480+
closeFile(fd, filepath.c_str());
476481
return false;
477482
}
478483
memcpy((uint8_t *) mmapPtr + dstOffset, src + headerSize, copySize);
@@ -493,6 +498,7 @@ bool Glog::appendLogFile(uint8_t &maxRecursionDepth, time_t epochSeconds) {
493498
InternalError("fail to truncate [%s] to size %zu, %s", filepath.c_str(), fileSize + copySize,
494499
::strerror(errno));
495500
m_cacheFile->closeFile();
501+
closeFile(fd, filepath.c_str());
496502
return false;
497503
}
498504

@@ -502,8 +508,11 @@ bool Glog::appendLogFile(uint8_t &maxRecursionDepth, time_t epochSeconds) {
502508
// m_cacheFile->resetInternalState();
503509

504510
m_cacheFile->closeFile();
511+
closeFile(fd, filepath.c_str());
505512
if (::remove(m_cacheFile->getPath().c_str()) < 0) {
506513
InternalError("fail to remove file [%s] %s", m_cacheFile->getPath().c_str(), ::strerror(errno));
514+
m_cacheFile->closeFile();
515+
closeFile(fd, filepath.c_str());
507516
return false;
508517
} else {
509518
InternalDebug("remove file [%s]", m_cacheFile->getPath().c_str());

Core/utilities.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,15 @@ int readSpecifySize(int fd, void *buf, size_t size) {
232232
return static_cast<int>(bytes);
233233
}
234234

235+
bool closeFile(int fd, const char *filepath) {
236+
if (fd < 0) {
237+
return false;
238+
}
239+
if (::close(fd) != 0) {
240+
InternalError("fail to close [%s], %s", filepath ? filepath : "Unknown", strerror(errno));
241+
return false;
242+
}
243+
return true;
244+
}
245+
235246
} // namespace glog

Core/utilities.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ int char2int(char input);
7676
bool str2Hex(const string &s, uint8_t *data);
7777

7878
int readSpecifySize(int fd, void *buf, size_t size);
79+
80+
bool closeFile(int fd, const char *filepath = nullptr);
7981
} // namespace glog
8082
#endif //CORE__UTILITIES_H_

0 commit comments

Comments
 (0)