Merge ~binli/libhybris/+git/libhybris:bug-1596772/add-scandir-hook into ~libhybris-maintainers/libhybris/+git/libhybris:master

Proposed by Bin Li
Status: Merged
Merged at revision: cc5579d21badbc998809c30d2804ffce13147186
Proposed branch: ~binli/libhybris/+git/libhybris:bug-1596772/add-scandir-hook
Merge into: ~libhybris-maintainers/libhybris/+git/libhybris:master
Diff against target: 94 lines (+78/-0)
1 file modified
hybris/common/hooks.c (+78/-0)
Reviewer Review Type Date Requested Status
Simon Fels Approve
Review via email: mp+298514@code.launchpad.net

Description of the change

    hybris: common: Add hooks to support scandir

    The related functions were also added, they are scandirat,
    alphasort, versionsort, scandir64.

    Bugs: https://bugs.launchpad.net/libhybris/+bug/1596772

To post a comment you must log in.
Revision history for this message
Simon Fels (morphis) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/hybris/common/hooks.c b/hybris/common/hooks.c
2index f60f58a..937e4c0 100644
3--- a/hybris/common/hooks.c
4+++ b/hybris/common/hooks.c
5@@ -1694,6 +1694,78 @@ static int _hybris_hook_readdir_r(DIR *dir, struct bionic_dirent *entry,
6 return res;
7 }
8
9+static int _hybris_hook_alphasort(struct bionic_dirent **a,
10+ struct bionic_dirent **b)
11+{
12+ return strcoll((*a)->d_name, (*b)->d_name);
13+}
14+
15+static int _hybris_hook_versionsort(struct bionic_dirent **a,
16+ struct bionic_dirent **b)
17+{
18+ return strverscmp((*a)->d_name, (*b)->d_name);
19+}
20+
21+static struct bionic_dirent *_hybris_hook_scandirat(int fd, DIR *dirp, struct bionic_dirent ***namelist,
22+ int (*filter)(const struct bionic_dirent *),
23+ int (*compar)(const struct bionic_dirent **, const struct bionic_dirent **))
24+{
25+ struct dirent **namelist_r;
26+ static struct bionic_dirent **result;
27+ struct bionic_dirent *filter_r;
28+
29+ int i = 0;
30+ size_t nItems = 0;
31+
32+ TRACE_HOOK("dirp %p", dirp);
33+
34+ int res = scandirat(fd, dirp, &namelist_r, NULL, NULL);
35+
36+ if (res != 0 && namelist_r != NULL) {
37+
38+ result = malloc(res * sizeof(struct bionic_dirent));
39+ if (!result)
40+ return -1;
41+
42+ for (i = 0; i < res; i++) {
43+ filter_r = malloc(sizeof(struct bionic_dirent));
44+ if (!filter_r) {
45+ while (i-- > 0)
46+ free(result[i]);
47+ free(result);
48+ return -1;
49+ }
50+ filter_r->d_ino = namelist_r[i]->d_ino;
51+ filter_r->d_off = namelist_r[i]->d_off;
52+ filter_r->d_reclen = namelist_r[i]->d_reclen;
53+ filter_r->d_type = namelist_r[i]->d_type;
54+
55+ strcpy(filter_r->d_name, namelist_r[i]->d_name);
56+ filter_r->d_name[sizeof(namelist_r[i]->d_name) - 1] = '\0';
57+
58+ if (filter != NULL && !(*filter)(filter_r)) {//apply filter
59+ free(filter_r);
60+ continue;
61+ }
62+
63+ result[nItems++] = filter_r;
64+ }
65+ if (nItems && compar != NULL)
66+ qsort(result, nItems, sizeof(struct bionic_dirent *), compar);
67+
68+ *namelist = result;
69+ }
70+
71+ return res;
72+}
73+
74+static struct bionic_dirent *_hybris_hook_scandir(DIR *dirp, struct bionic_dirent ***namelist,
75+ int (*filter)(const struct bionic_dirent *),
76+ int (*compar)(const struct bionic_dirent **, const struct bionic_dirent **))
77+{
78+ return _hybris_hook_scandirat(AT_FDCWD, dirp, namelist, filter, compar);
79+}
80+
81 static inline void swap(void **a, void **b)
82 {
83 void *tmp = *a;
84@@ -2594,6 +2666,12 @@ static struct _hook hooks_mm[] = {
85 {"sigtimedwait", sigtimedwait},
86 {"sigwait", sigwait},
87 {"sigwaitinfo", sigwaitinfo},
88+ /* dirent.h */
89+ {"scandir", _hybris_hook_scandir},
90+ {"scandirat", _hybris_hook_scandirat},
91+ {"alphasort,", _hybris_hook_alphasort},
92+ {"versionsort,", _hybris_hook_versionsort},
93+ {"scandir64", scandir},
94 };
95
96

Subscribers

People subscribed via source and target branches