From db07520913a43b809d022f418bb088d85cd629d2 Mon Sep 17 00:00:00 2001 From: KarnerF Date: Sun, 9 Aug 2026 23:16:36 +0200 Subject: [PATCH] Use statvfs for reliable USB detection on unplug --- main.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index 7e58566..de7fd39 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -2249,11 +2250,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) { char usb[32]={0}; int found=0; for(int i=0;i<=7&&!found;i++){ char p[24]; snprintf(p,sizeof(p),"/mnt/usb%d",i); - DIR *d=opendir(p); if(d){ - struct dirent *de; int n=0; - while((de=readdir(d))&&n<4) n++; - closedir(d); if(n>2){strncpy(usb,p,31);found=1;} - } + struct statvfs vfs; + if(statvfs(p,&vfs)==0&&vfs.f_blocks>0){strncpy(usb,p,31);found=1;} } mg_http_reply(c,200,"Content-Type: application/json\r\n", "{\"present\":%s,\"path\":\"%s\"}",found?"true":"false",found?usb:"");