From 5e671002310cecdf4ea010b9c298a373f5495f90 Mon Sep 17 00:00:00 2001 From: AskaEth Date: Fri, 4 Sep 2026 20:48:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(reader):=20=E4=B9=A6=E6=9E=B6scan=5Fbooks?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E9=80=92=E5=BD=92=E6=89=AB=E5=AD=90=E7=9B=AE?= =?UTF-8?q?=E5=BD=95(Books//,=E6=B7=B1=E5=BA=A6=E2=89=A42),=20?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E6=8C=89=E7=9B=AE=E5=BD=95=E7=BB=84=E7=BB=87?= =?UTF-8?q?=E4=B9=A6=E4=B9=9F=E8=83=BD=E6=89=AB=E5=88=B0=20=E2=80=94=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D'=E4=B9=A6=E6=9E=B6=E4=B8=BA=E7=A9=BA'(?= =?UTF-8?q?=E4=B9=A6=E5=9C=A8Books/kh/=E5=AD=90=E7=9B=AE=E5=BD=95);=20read?= =?UTF-8?q?er=E7=83=AD=E6=9B=B4=20eacd0e6c=20=E5=B7=B2=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=E6=AD=A4;=200warning?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- patch_v2/reader_app/app/shelf.c | 54 +++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 20 deletions(-) diff --git a/patch_v2/reader_app/app/shelf.c b/patch_v2/reader_app/app/shelf.c index 7798a9a..3ff5dcb 100644 --- a/patch_v2/reader_app/app/shelf.c +++ b/patch_v2/reader_app/app/shelf.c @@ -79,6 +79,39 @@ static int cmp_recent_then_name(const void *a, const void *b) return strcasecmp(x->title, y->title); } +/* 递归收集 Books/ 下(含子目录,深度 ≤2)的书。只在 scan_books 里调用。 */ +static void scan_dir(const char *dir, int depth) +{ + if (depth > 2) return; /* 防深嵌套/循环 */ + DIR *dp = opendir(dir); + if (!dp) return; + struct dirent *de; + while ((de = readdir(dp)) && g_nbooks < 256) { + if (de->d_name[0] == '.') continue; + char path[256]; + if (snprintf(path, sizeof path, "%s/%s", dir, de->d_name) >= (int)sizeof path) continue; + struct stat st; + if (stat(path, &st) != 0) continue; + if (S_ISDIR(st.st_mode)) { scan_dir(path, depth + 1); continue; } + int f = fmt_from_ext(de->d_name); + if (f < 0) continue; + shelf_book *b = &g_books[g_nbooks]; + snprintf(b->path, sizeof b->path, "%s", path); + strncpy(b->title, de->d_name, sizeof b->title - 1); b->title[sizeof b->title - 1] = 0; + char *dot = strrchr(b->title, '.'); + if (dot) *dot = 0; /* 标题去扩展名 */ + b->fmt = f; + b->size = (long)st.st_size; + rdprog p; + b->ts = (rdprog_load(b->path, &p) == 1) ? p.ts : 0; + char sz[24]; fmt_size(sz, sizeof sz, b->size); + snprintf(b->sub, sizeof b->sub, "%s · %s%s", fmt_name(f), sz, + b->ts ? " · 读" : ""); + g_nbooks++; + } + closedir(dp); +} + static void scan_books(void) { const char *dir = READER_SD_ROOT "/Books"; @@ -89,27 +122,8 @@ static void scan_books(void) g_notice_ms = 4000; return; } - struct dirent *de; - while ((de = readdir(dp)) && g_nbooks < 256) { - if (de->d_name[0] == '.') continue; - int f = fmt_from_ext(de->d_name); - if (f < 0) continue; - shelf_book *b = &g_books[g_nbooks]; - if (snprintf(b->path, sizeof b->path, "%s/%s", dir, de->d_name) >= (int)sizeof b->path) continue; - strncpy(b->title, de->d_name, sizeof b->title - 1); b->title[sizeof b->title - 1] = 0; - char *dot = strrchr(b->title, '.'); - if (dot) *dot = 0; /* 标题去扩展名 */ - b->fmt = f; - struct stat st; - b->size = (stat(b->path, &st) == 0) ? (long)st.st_size : 0; - rdprog p; - b->ts = (rdprog_load(b->path, &p) == 1) ? p.ts : 0; - char sz[24]; fmt_size(sz, sizeof sz, b->size); - snprintf(b->sub, sizeof b->sub, "%s · %s%s", fmt_name(f), sz, - b->ts ? " · 读" : ""); - g_nbooks++; - } closedir(dp); + scan_dir(dir, 0); /* 递归:含 Books/<子目录>/ 下书籍(用户可能按目录组织) */ qsort(g_books, (size_t)g_nbooks, sizeof(shelf_book), cmp_recent_then_name); }