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

Proposed by Bin Li
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 Needs Fixing
You-Sheng Yang 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

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

Revision history for this message
Bin Li (binli) wrote :

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

Revision history for this message
Simon Fels (morphis) wrote :

Comments in line.

review: Needs Fixing
Revision history for this message
Bin Li (binli) :

Unmerged commits

556b327... by Bin Li

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
diff --git a/hybris/common/hooks.c b/hybris/common/hooks.c
index a7c761f..2520611 100644
--- a/hybris/common/hooks.c
+++ b/hybris/common/hooks.c
@@ -1386,6 +1386,77 @@ static int my_readdir_r(DIR *dir, struct bionic_dirent *entry,
1386 return res;1386 return res;
1387}1387}
13881388
1389static int _hybris_hook_alphasort(struct bionic_dirent **a,
1390 struct bionic_dirent **b)
1391{
1392 return strcoll((*a)->d_name, (*b)->d_name);
1393}
1394static int _hybris_hook_versionsort(struct bionic_dirent **a,
1395 struct bionic_dirent **b)
1396{
1397 return strverscmp((*a)->d_name, (*b)->d_name);
1398}
1399
1400static struct bionic_dirent *_hybris_hook_scandirat(int fd, DIR *dirp, struct bionic_dirent ***namelist,
1401 int (*filter)(const struct bionic_dirent *),
1402 int (*compar)(const struct bionic_dirent **, const struct bionic_dirent **))
1403{
1404 struct dirent **namelist_r;
1405 static struct bionic_dirent **result;
1406 struct bionic_dirent *filter_r;
1407
1408 int i = 0;
1409 size_t nItems = 0;
1410
1411 TRACE_HOOK("dirp %p", dirp);
1412
1413 int res = scandirat(fd, dirp, &namelist_r, NULL, NULL);
1414
1415 if (res != 0 && namelist_r != NULL) {
1416
1417 result = malloc(res * sizeof(struct bionic_dirent));
1418 if (!result)
1419 return -1;
1420
1421 for (i = 0; i < res; i++) {
1422 filter_r = malloc(sizeof(struct bionic_dirent));
1423 if (!filter_r) {
1424 while (i-- > 0)
1425 free(result[i]);
1426 free(result);
1427 return -1;
1428 }
1429 filter_r->d_ino = namelist_r[i]->d_ino;
1430 filter_r->d_off = namelist_r[i]->d_off;
1431 filter_r->d_reclen = namelist_r[i]->d_reclen;
1432 filter_r->d_type = namelist_r[i]->d_type;
1433
1434 strcpy(filter_r->d_name, namelist_r[i]->d_name);
1435 filter_r->d_name[sizeof(namelist_r[i]->d_name) - 1] = '\0';
1436
1437 if (filter != NULL && !(*filter)(filter_r)) {//apply filter
1438 free(filter_r);
1439 continue;
1440 }
1441
1442 result[nItems++] = filter_r;
1443 }
1444 if (nItems && compar != NULL) // sort
1445 qsort(result, nItems, sizeof(struct bionic_dirent *), compar);
1446
1447 *namelist = result;
1448 }
1449
1450 return res;
1451}
1452
1453static struct bionic_dirent *_hybris_hook_scandir(DIR *dirp, struct bionic_dirent ***namelist,
1454 int (*filter)(const struct bionic_dirent *),
1455 int (*compar)(const struct bionic_dirent **, const struct bionic_dirent **))
1456{
1457 return _hybris_hook_scandirat(AT_FDCWD, dirp, namelist, filter, compar);
1458}
1459
1389static inline void swap(void **a, void **b)1460static inline void swap(void **a, void **b)
1390{1461{
1391 void *tmp = *a;1462 void *tmp = *a;
@@ -1634,9 +1705,6 @@ static struct _hook hooks[] = {
1634 {"__sprintf_chk", __sprintf_chk},1705 {"__sprintf_chk", __sprintf_chk},
1635 {"__snprintf_chk", __snprintf_chk},1706 {"__snprintf_chk", __snprintf_chk},
1636 {"strncasecmp",strncasecmp},1707 {"strncasecmp",strncasecmp},
1637 /* dirent.h */
1638 {"opendir", opendir},
1639 {"closedir", closedir},
1640 /* pthread.h */1708 /* pthread.h */
1641 {"getauxval", getauxval},1709 {"getauxval", getauxval},
1642 {"gettid", my_gettid},1710 {"gettid", my_gettid},
@@ -1803,9 +1871,13 @@ static struct _hook hooks[] = {
1803 {"seekdir", seekdir},1871 {"seekdir", seekdir},
1804 {"telldir", telldir},1872 {"telldir", telldir},
1805 {"dirfd", dirfd},1873 {"dirfd", dirfd},
1874 {"scandir", _hybris_hook_scandir},
1875 {"scandirat", _hybris_hook_scandirat},
1876 {"alphasort,", _hybris_hook_alphasort},
1877 {"versionsort,", _hybris_hook_versionsort},
1878 {"scandir64", scandir},
1806 /* fcntl.h */1879 /* fcntl.h */
1807 {"open", my_open},1880 {"open", my_open},
1808 // TODO: scandir, scandirat, alphasort, versionsort
1809 {"__get_tls_hooks", __get_tls_hooks},1881 {"__get_tls_hooks", __get_tls_hooks},
1810 {"sscanf", sscanf},1882 {"sscanf", sscanf},
1811 {"scanf", scanf},1883 {"scanf", scanf},

Subscribers

People subscribed via source and target branches

to all changes: