]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
smb/client: annotate nterr.h with DOS error codes
authorHuiwen He <hehuiwen@kylinos.cn>
Thu, 2 Apr 2026 14:18:27 +0000 (14:18 +0000)
committerSteve French <stfrench@microsoft.com>
Mon, 6 Apr 2026 00:58:40 +0000 (19:58 -0500)
commit378f75b7d6ea10f6368d414eb6b25f32224e9fc7
tree729d47a616e7dbf3b7561cfcb66a485619a44f9d
parent53cf44fa727113affda4a17207a54e0d27c7fc78
smb/client: annotate nterr.h with DOS error codes

Add comments to NT_STATUS definitions in nterr.h indicating the
corresponding DOS error class and code.

To ensure formatting consistency and facilitate automated processing,
existing human-readable comments in nterr.h were first moved to the
line preceding the #define statements.

This provides the source data for generating sorted mapping tables,
allowing the implementation of binary search for faster error mapping
lookups in later commits.

The mapping data is extracted from the existing manual
ntstatus_to_dos_map[] array in smb1maperror.c using the following
python script:

#!/usr/bin/env python3
import re
import os

MAP_FILE = "fs/smb/client/smb1maperror.c"
NTERR_FILE = "fs/smb/client/nterr.h"

def move_comments(file_path):
    """
    Moves existing inline comments (/* ... */ or // ...) to
    the preceding line to ensure formatting consistency.
    """
    if not os.path.exists(file_path):
        return
    with open(file_path, "r") as f:
        lines = f.readlines()
    new_lines = []
    # Match #define statements with inline comments
    re_str = r'^(\s*#define\s+[A-Za-z0-9_]+\s+.*?)\s*(/\*.*?\*/|//.*)$'
    pattern = re.compile(re_str)
    for line in lines:
        match = pattern.match(line.rstrip())
        if match:
            define_part, comment_part = match.groups()

            # Do not move if it's already an auto-generated mapping comment
            if re.search(r'//\s*[A-Z0-9_]+\s*,\s*[A-Za-z0-9_]+', comment_part):
                new_lines.append(line)
                continue

            indent = " " * (len(line) - len(line.lstrip()))
            # Move old comment to previous line
            new_lines.append(indent + comment_part + "\n")
            # Keep the define part
            new_lines.append(define_part.rstrip() + "\n")
        else:
            new_lines.append(line)
    with open(file_path, "w") as f:
        f.writelines(new_lines)

def annotate_nterr():
    """
    Extracts DOS error mappings from smb1maperror.c and appends them
    as comments to NT_STATUS defines in nterr.h, ensuring proper alignment.
    """
    mapping = {}
    if not os.path.exists(MAP_FILE) or not os.path.exists(NTERR_FILE):
        return

    # Extract mappings from the source mapping table
    with open(MAP_FILE, "r") as f:
        content = f.read()

        # Strip comments from source to ensure robust parsing
        content = re.sub(r'/\*.*?\*/', '', content, flags=re.DOTALL)
        content = re.sub(r'//.*', '', content)

        # Match [Class], [Code], [NT_STATUS] triplets using regex
        map_re = r'([A-Z0-9_]+)\s*,\s*([A-Za-z0-9_]+)\s*,\s*(NT_STATUS_[A-Z0-9_]+)'

        matches = re.findall(map_re, content)
        for m in matches:
            mapping[m[2]] = (m[0], m[1])

    with open(NTERR_FILE, "r") as f:
        lines = f.readlines()

    new_lines = []
    for line in lines:
        stripped = line.strip()
        if stripped.startswith("#define NT_STATUS_"):
            # Remove any existing // comments before re-annotating
            base_line = re.sub(r'\s*//.*$', '', line.rstrip())
            parts = base_line.split()
            if len(parts) >= 2:
                name = parts[1]
                # Append comment, ensuring proper alignment
                if name == "NT_STATUS_OK":
                    line = f"{base_line}\t// SUCCESS, 0\n"
                elif name in mapping:
                    d_class, d_code = mapping[name]
                    line = f"{base_line}\t// {d_class}, {d_code}\n"
                else:
                    line = f"{base_line}\t// ERRHRD, ERRgeneral\n"
        new_lines.append(line)

    with open(NTERR_FILE, "w") as f:
        f.writelines(new_lines)

if __name__ == "__main__":
    # Step 1: Clean existing inline comments and move them to separate lines
    move_comments(NTERR_FILE)
    # Step 2: Annotate with DOS codes, ensuring proper DOS codes comments
    annotate_nterr()
    print("Successfully processed nterr.h with DOS codes comments.")

Signed-off-by: Huiwen He <hehuiwen@kylinos.cn>
Reviewed-by: ChenXiaoSong <chenxiaosong@kylinos.cn>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/smb/client/nterr.h