return false;
}
+ char file_path[PATH_MAX];
+ bool ret = false;
+
while ((ep = readdir(dp))) {
- if (ep->d_type != 8)
+ if (ep->d_type != DT_REG)
continue;
struct stat st;
- char *file_path = NULL;
- if (asprintf(&file_path, "%s/%s", path, ep->d_name) == -1) {
- _E("Memory allocation error: %m");
- return false;
+ int r = snprintf(file_path, sizeof file_path, "%s/%s", path, ep->d_name);
+ if (r == -1 || r >= sizeof file_path) {
+ _E("Error while preparing report path: %m");
+ goto out;
}
if (stat(file_path, &st) != 0) {
_E("Get stats about %s error: %m", path);
- return false;
+ goto out;
}
struct tm t;
if (localtime_r(&(st.st_ctim.tv_sec), &t) == NULL) {
_E("localtime_r error: %m");
- return false;
+ goto out;
}
struct report_file *tmp_list = realloc(*list, sizeof(struct report_file)*(*list_count + 1));
- if (tmp_list == NULL) {
+ char *fname = strdup(ep->d_name);
+ char *fpath = strdup(file_path);
+ if (tmp_list == NULL || fname == NULL || fpath == NULL) {
_E("Out of memory");
- return false;
+ free(fname);
+ free(fpath);
+ free(tmp_list);
+ goto out;
}
+
*list = tmp_list;
- (*list)[*list_count].file_name = strdup(ep->d_name);
- (*list)[*list_count].file_path = strdup(file_path);
+ (*list)[*list_count].file_name = fname;
+ (*list)[*list_count].file_path = fpath;
(*list)[*list_count].ctime = st.st_ctim;
(*list_count)++;
}
+
+ ret = true;
+out:
closedir(dp);
- return true;
+ return ret;
}
static void free_record(struct report_file *record)