Skip to content

fix: add codepage 936 for non-ASCII FAT volume labels - #234

Open
wangrong1069 wants to merge 1 commit into
linuxdeepin:masterfrom
wangrong1069:agent/agent/8989964d279f
Open

wangrong1069 wants to merge 1 commit into
linuxdeepin:masterfrom
wangrong1069:agent/agent/8989964d279f

Conversation

@wangrong1069

@wangrong1069 wangrong1069 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

FAT16::create() (service/diskoperation/filesystems/fat16.cpp:287) passes UTF-8 encoded Chinese volume labels directly to mkfs.fat -n. The mkfs.fat tool defaults to the CP850 codepage, which cannot convert UTF-8 multibyte characters (e.g., Chinese), causing the format operation to fail with exit code 1. The downstream writeLabel() (which uses mlabel and handles UTF-8) is never reached because create() returns failure.

Fix Approach

In FAT16::create(), detect whether the label contains non-ASCII characters. If it does, add --codepage=936 to the mkfs.fat command, enabling proper GBK codepage conversion for Chinese labels. ASCII-only labels and empty labels follow the original code path with no change.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Target line is not a historical bug-fix product; the change adds a conditional branch without reverting any prior fix (b4fc9c63 fixed a missing .arg() call, which is preserved)
  • Only caller PartedCore::createFileSystem() (partedcore.cpp:3992) sees no change in create() return value semantics

Business Impact Scope

  • Disk Manager — partition creation/formatting: creating FAT16/FAT32 partitions with Chinese volume labels
  • User scenario: when a user creates a FAT32/FAT16 partition and sets a Chinese label name, formatting no longer fails and the label is written correctly
  • ASCII label and no-label creation paths are unchanged (no regression)

Verification Suggestion

  • Test FAT32 partition creation with a Chinese volume label (e.g., "测试1"), verify creation succeeds and label is correct
  • Test FAT32 partition creation with an ASCII label to confirm no regression

根因分析

FAT16::create()(service/diskoperation/filesystems/fat16.cpp:287)将 UTF-8 编码的中文卷标直接通过 mkfs.fat -n 参数传递。mkfs.fat 默认使用 CP850 代码页,无法转换 UTF-8 多字节字符(如中文),导致格式化操作失败(退出码 1)。下游的 writeLabel()(使用 mlabel,支持 UTF-8)因 create() 返回失败而永远不会被执行。

修复方案

在 FAT16::create() 中检测卷标是否包含非 ASCII 字符。如果包含,则为 mkfs.fat 命令添加 --codepage=936 参数,使 GBK 代码页正确转换中文卷标。纯 ASCII 卷标和空卷标走原有代码路径,无变化。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 目标行非历史修复产物,本次改动增加条件分支,不撤销 b4fc9c63 的 .arg() 修复
  • 唯一调用者 PartedCore::createFileSystem()(partedcore.cpp:3992)所见 create() 返回值语义不变

业务影响范围

  • 磁盘管理器 — 分区创建/格式化:创建 FAT16/FAT32 分区时设置中文卷标
  • 用户场景:用户创建 FAT32/FAT16 分区并设置中文卷标名称时,格式化不再失败,卷标正确写入
  • ASCII 卷标和无卷标创建路径不变(无回归)

验证建议

  • 测试 FAT32 分区创建时设置中文卷标(如"测试1"),验证创建成功且卷标正确
  • 测试 FAT32 分区创建时设置 ASCII 卷标,确认无回归

Summary by Sourcery

Bug Fixes:

  • Fix FAT16/FAT32 formatting with non-ASCII volume labels by selecting codepage 936 for labels requiring Chinese character conversion.

1. Root cause: FAT16::create() passes UTF-8 encoded Chinese labels
   directly to mkfs.fat -n, which defaults to CP850 codepage and cannot
   convert UTF-8 multibyte characters, causing format failure (exit 1)
2. Fix: detect non-ASCII characters in the label and add --codepage=936
   to the mkfs.fat command, enabling proper GBK codepage conversion
3. Impact: ASCII label and empty label paths unchanged, only non-ASCII
   label path adds the --codepage=936 parameter

Log: Fix FAT format failure when setting Chinese volume label

Influence:
1. Test FAT32 partition creation with Chinese volume label
2. Test FAT32 partition creation with ASCII volume label (regression)
3. Test FAT16 partition creation with Chinese volume label
4. Test FAT32 partition creation without label (regression)

fix: 修复非ASCII卷标导致FAT格式化失败的问题

1. 根因:FAT16::create() 将UTF-8编码的中文卷标直接传给mkfs.fat -n,
   mkfs.fat默认使用CP850代码页,无法转换UTF-8多字节字符,导致格式化失败
2. 方案:检测卷标中的非ASCII字符,为mkfs.fat命令添加--codepage=936
   参数,使GBK代码页正确转换中文卷标
3. 影响:ASCII卷标和空卷标路径不变,仅非ASCII卷标路径新增参数

Log: 修复设置中文卷标时FAT格式化失败的问题

Influence:
1. 测试FAT32分区创建时设置中文卷标
2. 测试FAT32分区创建时设置ASCII卷标(回归)
3. 测试FAT16分区创建时设置中文卷标
4. 测试FAT32分区创建时不设置卷标(回归)

PMS: BUG-362663
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: wangrong1069

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Updates FAT16 formatting to pass --codepage=936 for non-ASCII labels, allowing Chinese volume labels to be converted successfully while preserving existing behavior for ASCII and empty labels.

Sequence diagram for FAT formatting with non-ASCII volume labels

sequenceDiagram
    participant PartedCore
    participant FAT16
    participant mkfs_fat
    participant writeLabel

    PartedCore->>FAT16: create(new_partition)
    alt label contains non-ASCII characters
        FAT16->>mkfs_fat: format with --codepage=936 -n label
        mkfs_fat-->>FAT16: success
    else label is ASCII or empty
        FAT16->>mkfs_fat: format with existing arguments
        mkfs_fat-->>FAT16: success or existing result
    end
    FAT16->>writeLabel: writeLabel()
Loading

File-Level Changes

Change Details Files
Conditionally select GBK codepage conversion for non-ASCII FAT volume labels.
  • Read the filesystem label once and scan for characters outside ASCII.
  • Add --codepage=936 to mkfs.fat only when the label contains non-ASCII characters.
  • Preserve the existing command path for ASCII and empty labels.
service/diskoperation/filesystems/fat16.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="service/diskoperation/filesystems/fat16.cpp" line_range="297" />
<code_context>
+        }
+        if (hasNonAscii) {
+            cmd = QString("mkfs.fat -F %1 -v -I --codepage=936 -n %2 %3")
+                    .arg(fat_size).arg(label).arg(new_partition.getPath());
+        } else {
+            cmd = QString("mkfs.fat -F %1 -v -I -n %2 %3")
</code_context>
<issue_to_address>
**Labels with spaces fail formatting**

When a non-ASCII volume label contains whitespace, `FAT16::create()` inserts the label unquoted into the combined command, so command parsing splits it into separate arguments and `mkfs.fat` receives the wrong `-n` value and an extra operand; formatting fails.

Pass the label as a single argument instead of interpolating it unquoted into the combined command.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

}
if (hasNonAscii) {
cmd = QString("mkfs.fat -F %1 -v -I --codepage=936 -n %2 %3")
.arg(fat_size).arg(label).arg(new_partition.getPath());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Labels with spaces fail formatting

When a non-ASCII volume label contains whitespace, FAT16::create() inserts the label unquoted into the combined command, so command parsing splits it into separate arguments and mkfs.fat receives the wrong -n value and an extra operand; formatting fails.

Pass the label as a single argument instead of interpolating it unquoted into the combined command.

Prompt for AI agents
In `service/diskoperation/filesystems/fat16.cpp` at line 297:

**Labels with spaces fail formatting**

When a non-ASCII volume label contains whitespace, `FAT16::create()` inserts the label unquoted into the combined command, so command parsing splits it into separate arguments and `mkfs.fat` receives the wrong `-n` value and an extra operand; formatting fails.

Pass the label as a single argument instead of interpolating it unquoted into the combined command.

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 98 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次 PR 在 FAT16::create() 函数中添加了对非 ASCII 卷标的 codepage 936 支持, 语法逻辑正确,性能无影响,未引入安全漏洞。仅存在轻微的代码重复问题 (两条分支的命令构造几乎相同,仅 --codepage=936 不同),建议重构以提升可维护性。 四维度均通过,总分 98 分。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['无语法逻辑问题,代码逻辑清晰正确。']


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['建议将 codepage 参数提取为条件变量,合并两条分支,例如:QString codepage = hasNonAscii ? "--codepage=936 " : ""; 然后统一构造命令字符串,减少重复代码。']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['无性能问题,break 提前退出循环的优化合理。']


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: ['无安全漏洞。建议后续考虑对 label 中的 shell 元字符进行转义校验,作为防御性编程的改进方向(非本次 PR 引入的问题)。']


💡 改进建议代码示例

// 建议重构:消除 if/else 分支的代码重复
QString label = new_partition.getFileSystemLabel();
bool hasNonAscii = false;
for (const QChar &ch : label) {
    if (ch.unicode() > 127) {
        hasNonAscii = true;
        break;
    }
}
// 将 codepage 参数提取为条件变量,合并命令构造
QString codepage = hasNonAscii ? "--codepage=936 " : "";
cmd = QString("mkfs.fat -F %1 -v -I %2-n %3 %4")
        .arg(fat_size)
        .arg(codepage)
        .arg(label)
        .arg(new_partition.getPath());

本报告由 AI 代码审查工具自动生成

@deepin-bot

deepin-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.28
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #251

@deepin-bot

deepin-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.29
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #259

@deepin-bot

deepin-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.30
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #261

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants