add function get_statfs_by_path

use_iouring
YuQing 2024-10-31 10:31:43 +08:00
parent aad48cc03d
commit 21366a4a2e
4 changed files with 68 additions and 25 deletions

View File

@ -1,4 +1,8 @@
Version 1.76 2024-10-31
* get_mounted_filesystems act as program df
* add function get_statfs_by_path
Version 1.75 2024-09-22 Version 1.75 2024-09-22
* task init callback support extra argument * task init callback support extra argument
* connection pool performance optimization * connection pool performance optimization

View File

@ -334,6 +334,52 @@ int get_mounted_filesystems(struct fast_statfs *stats,
#endif #endif
} }
int get_statfs_by_path(const char *path, FastStatFS *statfs)
{
#define MAX_STATFS_COUNT 256
int result;
int count;
int path_len;
int mnt_len;
int matched_len;
char resolved_path[PATH_MAX];
struct fast_statfs stats[MAX_STATFS_COUNT];
struct fast_statfs *entry;
struct fast_statfs *end;
if ((result=get_mounted_filesystems(stats, MAX_STATFS_COUNT,
&count)) != 0)
{
return result;
}
realpath(path, resolved_path);
path_len = strlen(resolved_path);
matched_len = 0;
end = stats + count;
for (entry=stats; entry<end; entry++)
{
mnt_len = strlen(entry->f_mntonname);
if (mnt_len <= path_len && memcmp(resolved_path, entry->
f_mntonname, mnt_len) == 0)
{
if ((mnt_len > 1 && mnt_len < path_len) &&
*(resolved_path + mnt_len) != '/')
{
continue;
}
if (mnt_len > matched_len)
{
matched_len = mnt_len;
*statfs = *entry;
}
}
}
return (matched_len > 0 ? 0 : ENOENT);
}
#if defined(OS_LINUX) || defined(OS_FREEBSD) #if defined(OS_LINUX) || defined(OS_FREEBSD)
typedef struct fast_process_array { typedef struct fast_process_array {

View File

@ -151,6 +151,15 @@ int get_boot_time(struct timeval *boot_time);
int get_mounted_filesystems(struct fast_statfs *stats, int get_mounted_filesystems(struct fast_statfs *stats,
const int size, int *count); const int size, int *count);
/** get statfs by path
* parameters:
* path: the path
* statfs: return the statfs
* return: error no, 0 success, != 0 fail
*/
int get_statfs_by_path(const char *path, FastStatFS *statfs);
#if defined(OS_LINUX) || defined(OS_FREEBSD) #if defined(OS_LINUX) || defined(OS_FREEBSD)
/** get processes /** get processes
* parameters: * parameters:

View File

@ -64,6 +64,8 @@ int main(int argc, char *argv[])
volatile bool continue_flag = true; volatile bool continue_flag = true;
FastIFConfig if_configs[32]; FastIFConfig if_configs[32];
struct fast_statfs stats[32]; struct fast_statfs stats[32];
char *path;
FastStatFS statfs;
start_time = get_current_time_ms(); start_time = get_current_time_ms();
srand(time(NULL)); srand(time(NULL));
@ -92,6 +94,13 @@ int main(int argc, char *argv[])
stats[i].f_ffree); stats[i].f_ffree);
} }
path = "/root";
if (get_statfs_by_path(path, &statfs) == 0) {
printf("\ninput path: %s, %s => %s, type: %s\n\n", path,
statfs.f_mntfromname, statfs.f_mntonname,
statfs.f_fstypename);
}
#if defined(OS_LINUX) || defined(OS_FREEBSD) #if defined(OS_LINUX) || defined(OS_FREEBSD)
{ {
FastProcessInfo *processes; FastProcessInfo *processes;
@ -139,31 +148,6 @@ int main(int argc, char *argv[])
#endif #endif
/*
{
int result;
char *filename;
IniContext iniContext;
if (argc > 1) {
filename = argv[1];
} else {
filename = "/etc/mc/worker.conf";
}
if ((result=iniLoadFromFile(filename, &iniContext)) != 0) {
logError("file: "__FILE__", line: %d, "
"load conf file \"%s\" fail, ret code: %d",
__LINE__, filename, result);
return result;
}
//iniPrintItems(&iniContext);
iniFreeContext(&iniContext);
}
*/
sched_enable_delay_task(); sched_enable_delay_task();
scheduleArray.entries = scheduleEntries; scheduleArray.entries = scheduleEntries;
scheduleArray.count = 0; scheduleArray.count = 0;