{
auto statptr = getStat(fpath);
if (statptr == nullptr) {
- // target doesn't exist
- return nullptr;
+ ThrowExc(FileDoNotExist, "file not exist: " << fpath);
} else if (!S_ISREG(statptr->st_mode)) {
- INFO("File type is not regular: " << fpath);
- return nullptr;
+ ThrowExc(FileSystemError, "file type is not reguler: " << fpath);
} else if (modifiedSince != -1 && statptr->st_mtim.tv_sec < modifiedSince) {
DEBUG("file[" << fpath << "] isn't modified since[" << modifiedSince << "]");
return nullptr;
FsVisitorPtr FsVisitor::create(const std::string &dirpath, time_t modifiedSince)
{
auto statptr = getStat(dirpath);
- if (statptr == nullptr) {
- // target doesn't exist
- return nullptr;
- } else if (!S_ISDIR(statptr->st_mode)) {
- INFO("File type is not directory: " << dirpath);
- return nullptr;
- } else {
+ if (statptr == nullptr)
+ ThrowExc(FileDoNotExist, "directory not exist: " << dirpath);
+ else if (!S_ISDIR(statptr->st_mode))
+ ThrowExc(FileSystemError, "file type is not directory: " << dirpath);
+ else
return FsVisitorPtr(new FsVisitor(dirpath, modifiedSince));
- }
}
FsVisitor::FsVisitor(const std::string &dirpath, time_t modifiedSince) :
while (readdir_r(m_dirptr.get(), m_entryBuf, &result) == 0) {
if (result == nullptr) { // end of dir stream
m_dirs.pop();
- while (!m_dirs.empty() && !(m_dirptr = openDir(m_dirs.front()))) {
+ while (!m_dirs.empty() && !(m_dirptr = openDir(m_dirs.front())))
m_dirs.pop();
- }
if (m_dirs.empty())
return nullptr;
std::string filepath(result->d_name);
if (result->d_type == DT_DIR) {
- if (filepath.compare(".") == 0 || filepath.compare("..") == 0)
- continue;
- else
+ if (filepath.compare(".") != 0 && filepath.compare("..") != 0)
m_dirs.emplace(
dir + ((filepath.back() == '/') ? filepath : (filepath + '/')));
} else if (result->d_type == DT_REG) {
- auto fileptr = File::create(dir + filepath, m_since);
-
- if (!fileptr)
- continue;
- else
- return fileptr;
+ try {
+ auto fileptr = File::create(dir + filepath, m_since);
+
+ if (fileptr)
+ return fileptr;
+ } catch (const FileDoNotExist &e) {
+ WARN("file not exist: " << dir << filepath <<
+ " msg: " << e.what());
+ } catch (const FileSystemError &e) {
+ WARN("file type is not regular...? can it be happened?"
+ " :" << dir << filepath << " msg: " << e.what());
+ }
}
}
return scanFileHelper(context, filepath);
DEBUG("Scan history exist on file: " << filepath);
- if (File::create(filepath, static_cast<time_t>(history->ts))) {
- DEBUG("file[" << filepath << "] is modified since the detected time. "
- "let's remove history and re-scan");
- m_db->deleteDetectedMalware(filepath);
- return scanFileHelper(context, filepath);
+
+ try {
+ if (File::create(filepath, static_cast<time_t>(history->ts))) {
+ DEBUG("file[" << filepath << "] is modified since the detected time. "
+ "let's remove history and re-scan");
+ m_db->deleteDetectedMalware(filepath);
+ return scanFileHelper(context, filepath);
+ }
+ } catch (const FileSystemError &e) {
+ ERROR("file doesn't exist or type isn't regular: " << filepath <<
+ " " << e.what());
+ return BinaryQueue::Serialize(CSR_ERROR_INVALID_PARAMETER, CsDetected()).pop();
}
DEBUG("file[" << filepath << "] isn't modified since the detected time. "
m_db->setDetectedMalwareIgnored(filepath, true);
break;
- case CSR_CS_REMOVE: {
- auto file = File::create(filepath);
- if (!file && !file->remove()) {
- ERROR("Failed to remove filepath: " << filepath);
- return BinaryQueue::Serialize(CSR_ERROR_REMOVE_FAILED, CsDetected()).pop();
+ case CSR_CS_REMOVE:
+ try {
+ auto file = File::create(filepath);
+ if (!file && !file->remove()) {
+ ERROR("Failed to remove filepath: " << filepath);
+ return BinaryQueue::Serialize(CSR_ERROR_REMOVE_FAILED, CsDetected()).pop();
+ }
+ } catch (const FileDoNotExist &) {
+ WARN("File already removed... : " << filepath);
+ } catch (const FileSystemError &) {
+ WARN("File type is changed... it's considered as different file "
+ "in same path: " << filepath);
}
m_db->deleteDetectedMalware(filepath);
break;
- }
case CSR_CS_SKIP:
break;
RawBuffer Logic::getScannableFiles(const std::string &dir)
{
auto lastScanTime = m_db->getLastScanTime(dir, m_csDataVersion);
- auto visitor = FsVisitor::create(dir, lastScanTime);
StrSet fileset;
- while (auto file = visitor->next()) {
- DEBUG("In dir[" << dir << "], Scannable file[" << file->getPath() << "]");
- fileset.insert(file->getPath());
+ try {
+ auto visitor = FsVisitor::create(dir, lastScanTime);
+
+ if (visitor == nullptr)
+ return BinaryQueue::Serialize(CSR_ERROR_INVALID_PARAMETER, StrSet()).pop();
+
+ while (auto file = visitor->next()) {
+ DEBUG("In dir[" << dir << "], Scannable file[" << file->getPath() << "]");
+ fileset.insert(file->getPath());
+ }
+ } catch (const FileDoNotExist &) {
+ WARN("Directory isn't exist: " << dir << " return success with empty file set "
+ "to skip it softly.");
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, StrSet()).pop();
+ } catch (const FileSystemError &) {
+ WARN("Directory isn't directory... file type changed: " << dir << " return "
+ "empty file set to skip it softly.");
+ return BinaryQueue::Serialize(CSR_ERROR_NONE, StrSet()).pop();
}
if (lastScanTime != -1) {
return BinaryQueue::Serialize(CSR_ERROR_INVALID_PARAMETER).pop();
}
- if (File::create(filepath, static_cast<time_t>(history->ts))) {
- ERROR("Target modified since db delta inserted. name: " << filepath);
- m_db->deleteDetectedMalware(filepath);
- // TODO: is it okay to just refresh db and return success?
- return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
+ try {
+ if (File::create(filepath, static_cast<time_t>(history->ts))) {
+ ERROR("Target modified since db delta inserted. name: " << filepath);
+ m_db->deleteDetectedMalware(filepath);
+ // TODO: is it okay to just refresh db and return success?
+ return BinaryQueue::Serialize(CSR_ERROR_NONE).pop();
+ }
+ } catch (const FileSystemError &e) {
+ ERROR("file doesn't exist or type isn't regular: " << filepath <<
+ " " << e.what());
+ return BinaryQueue::Serialize(CSR_ERROR_INVALID_PARAMETER).pop();
}
switch (action) {
- case CSR_CS_ACTION_REMOVE: {
- auto file = File::create(filepath);
- if (!file && !file->remove()) {
- ERROR("Failed to remove filepath: " << filepath);
- return BinaryQueue::Serialize(CSR_ERROR_REMOVE_FAILED).pop();
+ case CSR_CS_ACTION_REMOVE:
+ try {
+ auto file = File::create(filepath);
+ if (!file && !file->remove()) {
+ ERROR("Failed to remove filepath: " << filepath);
+ return BinaryQueue::Serialize(CSR_ERROR_REMOVE_FAILED).pop();
+ }
+ } catch (const FileDoNotExist &) {
+ WARN("File already removed... : " << filepath);
+ } catch (const FileSystemError &) {
+ WARN("File type is changed... it's considered as different file "
+ "in same path: " << filepath);
}
m_db->deleteDetectedMalware(filepath);
break;
- }
case CSR_CS_ACTION_IGNORE:
m_db->setDetectedMalwareIgnored(filepath, true);