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

Proposed by Bin Li on 2016-06-28
Status: Needs review
Proposed branch: ~binli/libhybris/+git/libhybris-ubuntu:bug-1596772/add-scandir-hook
Merge into: ~morphis/libhybris/+git/libhybris-ubuntu:master
Diff against target: 107 lines (+76/-4)
1 file modified
hybris/common/hooks.c (+76/-4)
Reviewer Review Type Date Requested Status
Simon Fels 2016-06-28 Needs Fixing on 2016-06-28
You-Sheng Yang 2016-06-28 Pending
Review via email: mp+298481@code.launchpad.net

Description of the change

Add hooks to support scandir, scandirat, alphasort, versionsort, scandir64
https://bugs.launchpad.net/libhybris/+bug/1596772

To post a comment you must log in.
556b327... by Bin Li on 2016-06-28

Add hooks to support scandir, scandirat, alphasort, versionsort, scandir64
Bugs: https://bugs.launchpad.net/libhybris/+bug/1596772

Bin Li (binli) wrote :

In hooks[] there are opendir and closedir twice, so I removed one.

Simon Fels (morphis) wrote :

Comments in line.

review: Needs Fixing
Bin Li (binli) :

Unmerged commits

556b327... by Bin Li on 2016-06-28

Add hooks to support scandir, scandirat, alphasort, versionsort, scandir64
Bugs: https://bugs.launchpad.net/libhybris/+bug/1596772

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 a7c761f..2520611 100644
3--- a/hybris/common/hooks.c
4+++ b/hybris/common/hooks.c
5@@ -1386,6 +1386,77 @@ static int my_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+static int _hybris_hook_versionsort(struct bionic_dirent **a,
15+ struct bionic_dirent **b)
16+{
17+ return strverscmp((*a)->d_name, (*b)->d_name);
18+}
19+
20+static struct bionic_dirent *_hybris_hook_scandirat(int fd, DIR *dirp, struct bionic_dirent ***namelist,
21+ int (*filter)(const struct bionic_dirent *),
22+ int (*compar)(const struct bionic_dirent **, const struct bionic_dirent **))
23+{
24+ struct dirent **namelist_r;
25+ static struct bionic_dirent **result;
26+ struct bionic_dirent *filter_r;
27+
28+ int i = 0;
29+ size_t nItems = 0;
30+
31+ TRACE_HOOK("dirp %p", dirp);
32+
33+ int res = scandirat(fd, dirp, &namelist_r, NULL, NULL);
34+
35+ if (res != 0 && namelist_r != NULL) {
36+
37+ result = malloc(res * sizeof(struct bionic_dirent));
38+ if (!result)
39+ return -1;
40+
41+ for (i = 0; i < res; i++) {
42+ filter_r = malloc(sizeof(struct bionic_dirent));
43+ if (!filter_r) {
44+ while (i-- > 0)
45+ free(result[i]);
46+ free(result);
47+ return -1;
48+ }
49+ filter_r->d_ino = namelist_r[i]->d_ino;
50+ filter_r->d_off = namelist_r[i]->d_off;
51+ filter_r->d_reclen = namelist_r[i]->d_reclen;
52+ filter_r->d_type = namelist_r[i]->d_type;
53+
54+ strcpy(filter_r->d_name, namelist_r[i]->d_name);
55+ filter_r->d_name[sizeof(namelist_r[i]->d_name) - 1] = '\0';
56+
57+ if (filter != NULL && !(*filter)(filter_r)) {//apply filter
58+ free(filter_r);
59+ continue;
60+ }
61+
62+ result[nItems++] = filter_r;
63+ }
64+ if (nItems && compar != NULL) // sort
65+ qsort(result, nItems, sizeof(struct bionic_dirent *), compar);
66+
67+ *namelist = result;
68+ }
69+
70+ return res;
71+}
72+
73+static struct bionic_dirent *_hybris_hook_scandir(DIR *dirp, struct bionic_dirent ***namelist,
74+ int (*filter)(const struct bionic_dirent *),
75+ int (*compar)(const struct bionic_dirent **, const struct bionic_dirent **))
76+{
77+ return _hybris_hook_scandirat(AT_FDCWD, dirp, namelist, filter, compar);
78+}
79+
80 static inline void swap(void **a, void **b)
81 {
82 void *tmp = *a;
83@@ -1634,9 +1705,6 @@ static struct _hook hooks[] = {
84 {"__sprintf_chk", __sprintf_chk},
85 {"__snprintf_chk", __snprintf_chk},
86 {"strncasecmp",strncasecmp},
87- /* dirent.h */
88- {"opendir", opendir},
89- {"closedir", closedir},
90 /* pthread.h */
91 {"getauxval", getauxval},
92 {"gettid", my_gettid},
93@@ -1803,9 +1871,13 @@ static struct _hook hooks[] = {
94 {"seekdir", seekdir},
95 {"telldir", telldir},
96 {"dirfd", dirfd},
97+ {"scandir", _hybris_hook_scandir},
98+ {"scandirat", _hybris_hook_scandirat},
99+ {"alphasort,", _hybris_hook_alphasort},
100+ {"versionsort,", _hybris_hook_versionsort},
101+ {"scandir64", scandir},
102 /* fcntl.h */
103 {"open", my_open},
104- // TODO: scandir, scandirat, alphasort, versionsort
105 {"__get_tls_hooks", __get_tls_hooks},
106 {"sscanf", sscanf},
107 {"scanf", scanf},

Subscribers

People subscribed via source and target branches

to all changes: