fix(reader): M27 r2 — 扫描深度提到 6 + 按 Books 第一级目录分组
真机真实布局暴露两处问题(评审 P1):
- Books/400+本高质量完本合集/<分类>/<作品>/<书名>.txt 在 depth 3-4,旧 depth≤2 让 413 本 3.8GB 不可见
→ SHELF_SCAN_MAX_DEPTH=2→6;report 新增 depth_skipped,超深书籍文件计数并在 g_notice 可见提示
(有 N 个文件因目录层数超过 6 层未显示),不再静默
- 按完整相对路径分组会把 413 本书炸成 410 个每条 1 书的段头
→ 分组键改为 Books/ 第一级目录(shelf_book.top);完整相对路径仍在 group[],
第一级之后的子路径(分类/作品)放进副题 sub(如 01、玄幻/01、斗破 · TXT · 2.1MB)
→ 真机书库只剩 3 组:400+本高质量完本合集 / kh / 轻小说
- 单测 50 断言:depth3/4 收录、depth7 计入 depth_skipped、四种真实路径的组名、
a/b 与 a/c 同组 a、副题含子路径、format 回归、根组未分类、path/进度不变
This commit is contained in:
1 parent
409fd1edf7
commit
63ead3ec1e
5 files changed
+211
-82
No files matched your search
@@ -7,8 +7,6 @@
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define SHELF_SCAN_MAX_DEPTH 2 /* 子目录深度上限(Books/a/b 有书,Books/a/b/c 不扫) */
|
||||
|
||||
int shelf_fmt_from_ext(const char *name)
|
||||
{
|
||||
const char *d = strrchr(name, '.');
|
||||
@@ -69,11 +67,33 @@ typedef struct scan_ctx {
|
||||
int cap;
|
||||
int n;
|
||||
int truncated;
|
||||
int depth_skipped;
|
||||
shelf_ts_fn ts_fn;
|
||||
} scan_ctx;
|
||||
|
||||
/* 递归收集:rel = 相对 root 的目录路径(root 自身为 "")。 */
|
||||
static void scan_dir(scan_ctx *c, const char *dir, int depth, const char *rel)
|
||||
/* 已超过深度上限的目录:只数其中还能识别的书籍文件,不出书(不静默跳过)。
|
||||
* 自身再留 8 层硬上限,防符号链接/异常深目录把计数也拖死。 */
|
||||
static void count_books_under(scan_ctx *c, const char *dir, int depth)
|
||||
{
|
||||
if (depth > SHELF_SCAN_MAX_DEPTH + 8) return;
|
||||
DIR *dp = opendir(dir);
|
||||
if (!dp) return;
|
||||
struct dirent *de;
|
||||
while ((de = readdir(dp))) {
|
||||
if (de->d_name[0] == '.') continue;
|
||||
char path[400];
|
||||
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)) { count_books_under(c, path, depth + 1); continue; }
|
||||
if (shelf_fmt_from_ext(de->d_name) >= 0) c->depth_skipped++;
|
||||
}
|
||||
closedir(dp);
|
||||
}
|
||||
|
||||
/* 递归收集。rel = 相对 root 的完整目录路径(root 自身为 ""),
|
||||
* top = 第一级目录名(root 自身/根下文件为 "")。 */
|
||||
static void scan_dir(scan_ctx *c, const char *dir, int depth, const char *rel, const char *top)
|
||||
{
|
||||
if (depth > SHELF_SCAN_MAX_DEPTH) return; /* 防深嵌套/循环 */
|
||||
if (c->n >= c->cap) return; /* 已满:不再进新目录 */
|
||||
@@ -87,10 +107,14 @@ static void scan_dir(scan_ctx *c, const char *dir, int depth, const char *rel)
|
||||
struct stat st;
|
||||
if (stat(path, &st) != 0) continue;
|
||||
if (S_ISDIR(st.st_mode)) {
|
||||
char subrel[SHELF_GROUP_MAX * 4];
|
||||
if (snprintf(subrel, sizeof subrel, "%s%s%s", rel, rel[0] ? "/" : "", de->d_name)
|
||||
>= (int)sizeof subrel) continue;
|
||||
scan_dir(c, path, depth + 1, subrel);
|
||||
if (depth + 1 > SHELF_SCAN_MAX_DEPTH) {
|
||||
count_books_under(c, path, depth + 1); /* 超深:计数→可见提示 */
|
||||
continue;
|
||||
}
|
||||
char subrel[SHELF_GROUP_MAX * 16];
|
||||
snprintf(subrel, sizeof subrel, "%s%s%s", rel, rel[0] ? "/" : "", de->d_name);
|
||||
/* 第一级目录名 = top;更深层沿用。de->d_name 在递归返回前有效。 */
|
||||
scan_dir(c, path, depth + 1, subrel, depth == 0 ? de->d_name : top);
|
||||
continue;
|
||||
}
|
||||
int f = shelf_fmt_from_ext(de->d_name);
|
||||
@@ -102,13 +126,24 @@ static void scan_dir(scan_ctx *c, const char *dir, int depth, const char *rel)
|
||||
char *dot = strrchr(b->title, '.');
|
||||
if (dot) *dot = 0; /* 标题去扩展名 */
|
||||
shelf_copy_utf8(b->group, (int)sizeof b->group, rel);
|
||||
shelf_copy_utf8(b->top, (int)sizeof b->top, top);
|
||||
b->fmt = f;
|
||||
b->size = (long)st.st_size;
|
||||
b->ts = c->ts_fn ? c->ts_fn(b->path) : 0;
|
||||
char sz[24];
|
||||
shelf_fmt_size(sz, sizeof sz, b->size);
|
||||
snprintf(b->sub, sizeof b->sub, "%s · %s%s", shelf_fmt_name(f), sz,
|
||||
b->ts ? " · 读" : "");
|
||||
/* 副题:第一级之后的子路径(分类/作品)· 格式 · 大小 [· 读] */
|
||||
const char *rest = strchr(rel, '/');
|
||||
if (rest) rest++;
|
||||
if (rest && rest[0]) {
|
||||
char pre[100];
|
||||
shelf_copy_utf8(pre, (int)sizeof pre, rest);
|
||||
snprintf(b->sub, sizeof b->sub, "%s · %s · %s%s", pre, shelf_fmt_name(f), sz,
|
||||
b->ts ? " · 读" : "");
|
||||
} else {
|
||||
snprintf(b->sub, sizeof b->sub, "%s · %s%s", shelf_fmt_name(f), sz,
|
||||
b->ts ? " · 读" : "");
|
||||
}
|
||||
c->n++;
|
||||
}
|
||||
closedir(dp);
|
||||
@@ -117,7 +152,7 @@ static void scan_dir(scan_ctx *c, const char *dir, int depth, const char *rel)
|
||||
int shelf_scan_books(const char *root, shelf_book *books, int cap,
|
||||
shelf_ts_fn ts_fn, shelf_scan_report *rep)
|
||||
{
|
||||
if (rep) { rep->nbooks = 0; rep->truncated = 0; rep->root_missing = 0; }
|
||||
if (rep) { rep->nbooks = 0; rep->truncated = 0; rep->depth_skipped = 0; rep->root_missing = 0; }
|
||||
if (!root || !books || cap <= 0) {
|
||||
if (rep) rep->root_missing = 1;
|
||||
return 0;
|
||||
@@ -130,22 +165,40 @@ int shelf_scan_books(const char *root, shelf_book *books, int cap,
|
||||
closedir(dp);
|
||||
|
||||
scan_ctx c;
|
||||
c.books = books; c.cap = cap; c.n = 0; c.truncated = 0; c.ts_fn = ts_fn;
|
||||
scan_dir(&c, root, 0, "");
|
||||
c.books = books; c.cap = cap; c.n = 0; c.truncated = 0; c.depth_skipped = 0; c.ts_fn = ts_fn;
|
||||
scan_dir(&c, root, 0, "", "");
|
||||
qsort(books, (size_t)c.n, sizeof(shelf_book), cmp_recent_then_name);
|
||||
if (rep) { rep->nbooks = c.n; rep->truncated = c.truncated; }
|
||||
if (rep) {
|
||||
rep->nbooks = c.n;
|
||||
rep->truncated = c.truncated;
|
||||
rep->depth_skipped = c.depth_skipped;
|
||||
}
|
||||
return c.n;
|
||||
}
|
||||
|
||||
/* ---- 行序列 ---- */
|
||||
|
||||
/* 段头排序:根组(group=="")恒置顶,其余按目录名不区分大小写排序。 */
|
||||
/* 分组键 = top(Books 第一级目录名)。长度截到第一个 '/',排序时忽略大小写。 */
|
||||
static int top_len(const char *s)
|
||||
{
|
||||
const char *p = strchr(s, '/');
|
||||
return p ? (int)(p - s) : (int)strlen(s);
|
||||
}
|
||||
static int top_cmp(const char *a, const char *b)
|
||||
{
|
||||
int la = top_len(a), lb = top_len(b);
|
||||
if (!la != !lb) return la ? 1 : -1; /* 根组 "" 恒置顶 */
|
||||
int m = la < lb ? la : lb;
|
||||
int c = strncasecmp(a, b, (size_t)m);
|
||||
if (c) return c;
|
||||
return la - lb;
|
||||
}
|
||||
static int top_eq(const char *a, const char *b) { return top_cmp(a, b) == 0; }
|
||||
|
||||
/* 段头排序:根组(top=="")恒置顶,其余按第一级目录名不区分大小写排序。 */
|
||||
static int cmp_group_key(const void *pa, const void *pb)
|
||||
{
|
||||
const char *a = *(const char *const *)pa;
|
||||
const char *b = *(const char *const *)pb;
|
||||
if (!a[0] != !b[0]) return a[0] ? 1 : -1; /* "" 在前 */
|
||||
return strcasecmp(a, b);
|
||||
return top_cmp(*(const char *const *)pa, *(const char *const *)pb);
|
||||
}
|
||||
|
||||
static int build_rows_format(const shelf_book *books, int nbooks,
|
||||
@@ -178,14 +231,14 @@ static int build_rows_format(const shelf_book *books, int nbooks,
|
||||
static int build_rows_folder(const shelf_book *books, int nbooks,
|
||||
shelf_row *rows, int rowcap)
|
||||
{
|
||||
/* 去重目录键(""=根组)。键是指向 books[].group 的字符串,books 生存期覆盖 rows。 */
|
||||
/* 去重分组键(books[].top,""=根组)。键指向 books 内字符串,生存期覆盖 rows。 */
|
||||
static const char *keys[SHELF_MAX_BOOKS];
|
||||
int nkeys = 0;
|
||||
for (int i = 0; i < nbooks; i++) {
|
||||
const char *k = books[i].group;
|
||||
const char *k = books[i].top;
|
||||
int seen = 0;
|
||||
for (int j = 0; j < nkeys; j++) {
|
||||
if (strcmp(keys[j], k) == 0) { seen = 1; break; }
|
||||
if (top_eq(keys[j], k)) { seen = 1; break; }
|
||||
}
|
||||
if (!seen) keys[nkeys++] = k;
|
||||
}
|
||||
@@ -200,7 +253,7 @@ static int build_rows_folder(const shelf_book *books, int nbooks,
|
||||
rows[nrows].label = keys[ki][0] ? keys[ki] : SHELF_GROUP_ROOT;
|
||||
nrows++;
|
||||
for (int i = 0; i < nbooks; i++) {
|
||||
if (strcmp(books[i].group, keys[ki]) != 0) continue;
|
||||
if (!top_eq(books[i].top, keys[ki])) continue;
|
||||
if (nrows >= rowcap) break;
|
||||
rows[nrows].section = 0;
|
||||
rows[nrows].fmt = books[i].fmt;
|
||||
|
||||
Reference in new issue
Block a user