fix(reader): 书架scan_books改为递归扫子目录(Books/<subdir>/,深度≤2), 用户按目录组织书也能扫到 — 修复'书架为空'(书在Books/kh/子目录); reader热更 eacd0e6c 已基于此; 0warning
This commit is contained in:
1 parent
06f872faf2
commit
5e67100231
1 file changed
+34
-20
@@ -79,6 +79,39 @@ static int cmp_recent_then_name(const void *a, const void *b)
|
|||||||
return strcasecmp(x->title, y->title);
|
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)
|
static void scan_books(void)
|
||||||
{
|
{
|
||||||
const char *dir = READER_SD_ROOT "/Books";
|
const char *dir = READER_SD_ROOT "/Books";
|
||||||
@@ -89,27 +122,8 @@ static void scan_books(void)
|
|||||||
g_notice_ms = 4000;
|
g_notice_ms = 4000;
|
||||||
return;
|
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);
|
closedir(dp);
|
||||||
|
scan_dir(dir, 0); /* 递归:含 Books/<子目录>/ 下书籍(用户可能按目录组织) */
|
||||||
qsort(g_books, (size_t)g_nbooks, sizeof(shelf_book), cmp_recent_then_name);
|
qsort(g_books, (size_t)g_nbooks, sizeof(shelf_book), cmp_recent_then_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user