Add new util function

This commit is contained in:
topjohnwu
2019-06-23 03:53:41 -07:00
parent 1058aeb04f
commit 4cc7aced15
4 changed files with 38 additions and 33 deletions

View File

@@ -1,15 +1,15 @@
/* file.cpp - Contains all files related utilities
*/
#include <sys/sendfile.h>
#include <sys/mman.h>
#include <linux/fs.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <libgen.h>
#include <sys/sendfile.h>
#include <sys/mman.h>
#include <linux/fs.h>
#include <magisk.h>
#include <utils.h>
@@ -396,3 +396,14 @@ void parse_prop_file(const char *file, const function<bool (string_view, string_
return fn(line, eql + 1);
}, true);
}
void parse_mnt(const char *file, const function<bool (mntent*)> &fn) {
unique_ptr<FILE, decltype(&fclose)> fp(xfopen(file, "rce"), &fclose);
if (fp) {
mntent *mentry;
while ((mentry = getmntent(fp.get())) != nullptr) {
if (!fn(mentry))
break;
}
}
}

View File

@@ -3,13 +3,14 @@
#pragma once
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdio.h>
#include <dirent.h>
#include <pthread.h>
#include <poll.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <mntent.h>
#ifdef __cplusplus
extern "C" {
@@ -173,6 +174,7 @@ void parse_prop_file(const char *file, const std::function
void *__mmap(const char *filename, size_t *size, bool rw);
void frm_rf(int dirfd, std::initializer_list<const char *> excl = std::initializer_list<const char *>());
void clone_dir(int src, int dest, bool overwrite = true);
void parse_mnt(const char *file, const std::function<bool (mntent*)> &fn);
template <typename B>
void mmap_ro(const char *filename, B &buf, size_t &sz) {