auto fullpath = (parent_dirpath.back() == '/') ?
(parent_dirpath + name) : (parent_dirpath + "/" + name);
- if (result->d_type == DT_DIR) {
+ INFO("start to traverse file: " << fullpath);
+
+ if (result->d_type == DT_LNK) {
+ if (!currentdir->isInApp())
+ continue;
+
+ auto stat = getStat(fullpath);
+ if (S_ISREG(stat->st_mode))
+ this->visitFile(fullpath, currentdir);
+ else if (S_ISDIR(stat->st_mode))
+ this->visitDir(fullpath, currentdir);
+
+ } else if (result->d_type == DT_DIR) {
if ((name_size == 1 && name[0] == '.') ||
(name_size == 2 && name[0] == '.' && name[1] == '.'))
continue;
- FilePtr ncurrentdir;
- try {
- ncurrentdir = File::create(fullpath, currentdir);
- } catch (const Exception &e) {
- if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST) {
- WARN("Perm denied to create file on pkg path: " << fullpath);
- continue;
- } else {
- throw;
- }
- }
-
- if (this->m_isBasedOnName && ncurrentdir->isInApp()) {
- this->m_targetHandler(ncurrentdir);
- } else {
- auto ndirptr = openDir(fullpath);
- if (ndirptr == nullptr) {
- WARN("Failed to open dir: " << fullpath << " with errno: " << errno);
- continue;
- }
-
- DEBUG("recurse dir : " << fullpath);
- this->run(ndirptr, ncurrentdir);
- }
+ this->visitDir(fullpath, currentdir);
} else if (result->d_type == DT_REG) {
- try {
- auto fileptr = File::createIfModified(fullpath, currentdir, this->m_since);
-
- if (fileptr)
- this->m_targetHandler(fileptr);
- } catch (const Exception &e) {
- if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST)
- WARN("file not exist: " << fullpath << " msg: " << e.what());
- else if (e.error() == CSR_ERROR_FILE_SYSTEM)
- WARN("file type is not regular...? can it be happened?"
- " :" << fullpath << " msg: " << e.what());
- else
- throw;
- }
+ this->visitFile(fullpath, currentdir);
}
}
}
+void FsVisitor::visitDir(const std::string &fullpath, const FilePtr ¤tdir)
+{
+ FilePtr ncurrentdir;
+
+ try {
+ ncurrentdir = File::create(fullpath, currentdir);
+ } catch (const Exception &e) {
+ if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST) {
+ WARN("Perm denied to create file on pkg path: " << fullpath);
+ return;
+ } else {
+ throw;
+ }
+ }
+
+ if (this->m_isBasedOnName && ncurrentdir->isInApp()) {
+ this->m_targetHandler(ncurrentdir);
+ } else {
+ if (auto ndirptr = openDir(fullpath)) {
+ DEBUG("recurse dir: " << fullpath);
+ this->run(ndirptr, ncurrentdir);
+ } else {
+ WARN("Failed to open dir: " << fullpath << " with errno: " << errno);
+ }
+ }
+}
+
+void FsVisitor::visitFile(const std::string &fullpath, const FilePtr ¤tdir)
+{
+ try {
+ if (auto fileptr = File::createIfModified(fullpath, currentdir, this->m_since))
+ this->m_targetHandler(fileptr);
+ } catch (const Exception &e) {
+ if (e.error() == CSR_ERROR_FILE_DO_NOT_EXIST)
+ WARN("file not exist: " << fullpath << " msg: " << e.what());
+ else if (e.error() == CSR_ERROR_FILE_SYSTEM)
+ WARN("file type is not regular...? can it be happened?"
+ " :" << fullpath << " msg: " << e.what());
+ else
+ throw;
+ }
+}
+
void FsVisitor::run()
{
auto currentdir = File::create(this->m_path, nullptr);
int ret = CSRE_ERROR_NONE;
- if (result->d_type & (DT_REG | DT_LNK))
+ if (result->d_type == DT_LNK) {
+ // when the file type is link, check the real file's type(dir or reg file)
+ // and go ahead to traverse
+ struct stat s;
+ if (stat(fullpath.c_str(), &s) != 0)
+ continue; // failed to get stat
+
+ if (S_ISREG(s.st_mode))
+ ret = csre_cs_scan_file(handle, fullpath.c_str(), &detected);
+ else if (S_ISDIR(s.st_mode))
+ ret = csre_cs_scan_app_on_cloud(handle, fullpath.c_str(), &detected);
+ else
+ continue;
+ } else if (result->d_type == DT_REG) {
ret = csre_cs_scan_file(handle, fullpath.c_str(), &detected);
- else if ((result->d_type & DT_DIR)
- && filename.compare("..") != 0
- && filename.compare(".") != 0)
+ } else if ((result->d_type & DT_DIR) &&
+ filename.compare("..") != 0 &&
+ filename.compare(".") != 0) {
ret = csre_cs_scan_app_on_cloud(handle, fullpath.c_str(), &detected);
- else
+ } else {
continue;
+ }
if (ret != CSRE_ERROR_NONE)
return ret;