Skip to content

Commit ba4e8b0

Browse files
author
Merrick Zhang
committed
完成 Windows CLI 版本的 file 工具实现
已完成以下工作: 1. 使用 C++20 技术重写 file 工具 2. 实现核心文件类型检测功能 3. 实现命令行参数解析 4. 实现魔术文件解析和处理 5. 配置 VS2022 项目 6. 生成 README.md 文档 7. 配置 GitHub Actions 工作流 8. 更新 .gitignore 文件 9. 移除与 make 相关的文件,确保使用 msbuild 生成
1 parent ab03975 commit ba4e8b0

350 files changed

Lines changed: 49164 additions & 14 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/build.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Build Windows CLI
2+
3+
on:
4+
push:
5+
branches: [ main, master ]
6+
pull_request:
7+
branches: [ main, master ]
8+
9+
jobs:
10+
build:
11+
runs-on: windows-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
with:
16+
submodules: recursive
17+
18+
- name: Setup Visual Studio
19+
uses: microsoft/setup-msbuild@v2
20+
21+
- name: Build with Visual Studio
22+
run: msbuild file.vcxproj /p:Configuration=Release /p:Platform=x64
23+
24+
- name: Test the build
25+
run: |
26+
.\x64\Release\file.exe --version
27+
.\x64\Release\file.exe README.md
28+
.\x64\Release\file.exe .\x64\Release\file.exe
29+
30+
- name: Upload build artifacts
31+
uses: actions/upload-artifact@v4
32+
with:
33+
name: file-windows-cli
34+
path: x64/Release/file.exe
35+
if-no-files-found: error

.gitignore

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1-
################################################################################
2-
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
3-
################################################################################
4-
5-
/.vs/file
6-
/Debug
7-
/file/Debug
8-
/file/Release
9-
/file/x64/Debug
10-
/file/x64/Release
11-
/x64
1+
################################################################################
2+
# 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。
3+
################################################################################
4+
5+
# Visual Studio 生成的文件
6+
/.vs/
7+
*.suo
8+
*.user
9+
*.ipch
10+
*.pch
11+
*.ncb
12+
*.opensdf
13+
*.sdf
14+
*.VC.db
15+
*.VC.opendb
16+
*.cache/
17+
*.tlog/
18+
*.log
19+
20+
# 编译输出目录
21+
/Debug/
22+
/Release/
23+
/x64/
24+
/file/Debug/
25+
/file/Release/
26+
/file/x64/
27+
bin/
28+
obj/
29+
30+
# 临时文件
31+
*.tmp
32+
*.temp
33+
*.swp
34+
*.swo
35+
*~
36+
37+
# 日志文件
38+
*.log
39+
*.err
40+
41+
# 其他不需要版本控制的文件
42+
*.pid
43+
*.seed
44+
*.pid.lock
45+
.env
46+
.DS_Store
47+
Thumbs.db
48+
49+
# 第三方依赖
50+
/vendor/
51+
/external/
52+
/lib/
53+
/include/
54+
55+
# 构建工具生成的文件
56+
CMakeCache.txt
57+
CMakeFiles/
58+
Makefile
59+
Makefile.in
60+
configure
61+
autom4te.cache/
62+
.aclocal.m4
63+
*.m4
64+
*.in
65+
*.patch
66+
*.diff

README.md

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
# File Tool for Windows (C++20 Implementation)
2+
3+
## 项目概述
4+
5+
本项目是 Debian apt 目录中 file 工具的 Windows CLI 版本,使用现代 C++20 技术重写。该工具可以检测文件的类型,支持多种文件格式的识别,包括文本文件、可执行文件、压缩文件、图像文件、音频文件、视频文件和文档文件等。
6+
7+
## 功能特性
8+
9+
- **现代 C++20 实现**:使用最新的 C++ 标准和特性,如 `std::filesystem``std::optional``std::regex`
10+
- **模块化设计**:分离命令行解析、文件类型检测和魔术文件处理等功能
11+
- **丰富的文件类型检测**
12+
- 文本文件和二进制文件
13+
- Windows PE32 可执行文件
14+
- 压缩文件(ZIP、RAR、TAR)
15+
- 图像文件(JPEG、PNG、GIF、BMP)
16+
- 音频文件(MP3、WAV、FLAC)
17+
- 视频文件(MP4、AVI)
18+
- 文档文件(PDF、Microsoft Office)
19+
- **魔术文件支持**:通过解析魔术文件实现更精确的文件类型检测
20+
- **命令行选项**:支持与原 file 工具相似的命令行选项
21+
22+
## 系统要求
23+
24+
- Windows 10 或更高版本
25+
- Visual Studio 2022(Community、Professional 或 Enterprise 版)
26+
- C++20 编译器支持(MSVC 14.30+)
27+
28+
## 构建说明
29+
30+
1. **打开项目**:使用 Visual Studio 2022 打开 `cpp20\file.sln` 解决方案文件
31+
2. **配置构建**
32+
- 选择目标平台(x86 或 x64)
33+
- 选择构建配置(Debug 或 Release)
34+
3. **编译项目**
35+
- 点击 "生成" -> "生成解决方案" 或按 F7
36+
- 编译成功后,可执行文件将生成在 `cpp20\Release\``cpp20\x64\Release\` 目录中
37+
38+
## 命令行选项
39+
40+
```
41+
Usage: file [OPTION...] [FILE...]
42+
Determine type of FILEs.
43+
44+
-b, --brief brief output format
45+
-c, --check check magic files for validity
46+
-C, --compile compile magic files into binary format
47+
-d, --debug print debugging information
48+
-E, --error treat errors as fatal
49+
-e, --exclude=TEST exclude TEST from checks
50+
-f, --name-file=FILE read names from FILE
51+
-F, --separator=STRING use STRING as separator
52+
-i, --mime output MIME type
53+
-k, --continue continue after first match
54+
-l, --list list magic tests
55+
-m, --magic-file=FILE use FILE as magic file
56+
-n, --no-buffer do not buffer output
57+
-N, --no-pad do not pad output
58+
-p, --preserve-atime preserve access time
59+
-P, --parameter=NAME=VALUE set parameter
60+
-r, --raw do not convert unprintable chars
61+
-s, --devices look at device contents
62+
-S, --no-sandbox disable sandbox
63+
-t, --no-test disable built-in tests
64+
-v, --version print version information
65+
-x, --exclude-quiet exclude tests quietly
66+
-z, --compress check compressed files
67+
-Z, --uncompress check compressed files transparently
68+
-0, --print0 print null-separated output
69+
--apple output Apple creator/type
70+
--extension output file extensions
71+
--mime-encoding output MIME encoding
72+
--mime-type output MIME type
73+
--help display this help and exit
74+
```
75+
76+
## 使用示例
77+
78+
### 基本用法
79+
80+
```bash
81+
# 检测单个文件
82+
file.exe example.txt
83+
84+
# 检测多个文件
85+
file.exe file1.exe file2.jpg file3.pdf
86+
87+
# 使用简要输出格式
88+
file.exe -b example.txt
89+
90+
# 输出 MIME 类型
91+
file.exe -i example.txt
92+
```
93+
94+
### 示例输出
95+
96+
```
97+
# 文本文件
98+
example.txt: text/plain
99+
100+
# Windows 可执行文件
101+
file.exe: PE32 executable (Windows)
102+
103+
# 图像文件
104+
example.jpg: JPEG image
105+
106+
# 压缩文件
107+
example.zip: ZIP archive
108+
```
109+
110+
## 项目结构
111+
112+
```
113+
cpp20/
114+
├── src/ # 源代码目录
115+
│ ├── main.cpp # 主入口文件
116+
│ ├── cli_parser.cpp # 命令行参数解析
117+
│ ├── cli_parser.h # 命令行参数解析头文件
118+
│ ├── file_detector.cpp # 文件类型检测实现
119+
│ ├── file_detector.h # 文件类型检测头文件
120+
│ ├── magic_parser.cpp # 魔术文件解析实现
121+
│ └── magic_parser.h # 魔术文件解析头文件
122+
├── magic/ # 魔术文件目录
123+
│ ├── Header # 魔术文件头
124+
│ ├── Localstuff # 本地魔术规则
125+
│ └── Magdir/ # 按类别分类的魔术文件
126+
├── Release/ # 发布版本输出目录
127+
├── file.sln # Visual Studio 解决方案文件
128+
├── file.vcxproj # Visual Studio 项目文件
129+
└── README.md # 本说明文件
130+
```
131+
132+
## 魔术文件
133+
134+
本项目使用魔术文件(magic files)来增强文件类型检测的准确性。魔术文件包含了各种文件格式的特征规则,用于识别不同类型的文件。
135+
136+
- 魔术文件位于 `magic/` 目录下
137+
- 默认情况下,工具会尝试加载 `magic/Header` 文件
138+
- 可以使用 `-m` 选项指定自定义的魔术文件
139+
140+
## 构建依赖
141+
142+
- **Visual Studio 2022**:需要支持 C++20 标准的编译器
143+
- **Windows SDK**:推荐使用 Windows 10 SDK 或更高版本
144+
- **魔术文件**:用于文件类型检测的规则文件(已包含在项目中)
145+
146+
## 故障排除
147+
148+
### 常见问题
149+
150+
1. **魔术文件未找到**
151+
- 确保 `magic/` 目录存在于可执行文件的同一目录或父目录中
152+
- 可以使用 `-m` 选项指定魔术文件的完整路径
153+
154+
2. **文件类型检测不准确**
155+
- 尝试使用不同的魔术文件
156+
- 对于特殊文件格式,可能需要更新魔术文件规则
157+
158+
3. **编译错误**
159+
- 确保使用 Visual Studio 2022 或更高版本
160+
- 确保项目配置为使用 C++20 标准
161+
- 检查是否安装了正确版本的 Windows SDK
162+
163+
## 许可证
164+
165+
本项目基于 Debian apt 中的 file 工具,使用原项目的许可证。有关详细信息,请参阅原始项目的 COPYING 文件。
166+
167+
## 致谢
168+
169+
- 原始 file 工具项目:https://git.in-ulm.de/cbiedl/file.git
170+
- C++20 标准库文档
171+
- Visual Studio 文档
172+
173+
## 版本信息
174+
175+
- 当前版本:5.46 (C++20 Implementation)
176+
- 构建系统:Visual Studio 2022
177+
- C++ 标准:C++20

magic/Header

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Magic data for file(1) command.
2+
# Format is described in magic(files), where:
3+
# files is 5 on V7 and BSD, 4 on SV, and ?? on SVID.
4+
# Don't edit this file, edit /etc/magic or send your magic improvements
5+
# to the maintainers, at file@astron.com

magic/Localstuff

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#------------------------------------------------------------------------------
3+
# Localstuff: file(1) magic for locally observed files
4+
#
5+
# $File: Localstuff,v 1.5 2007/01/12 17:38:27 christos Exp $
6+
# Add any locally observed files here. Remember:
7+
# text if readable, executable if runnable binary, data if unreadable.

magic/Magdir/acorn

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
2+
#------------------------------------------------------------------------------
3+
# $File: acorn,v 1.9 2024/08/30 17:29:28 christos Exp $
4+
# acorn: file(1) magic for files found on Acorn systems
5+
#
6+
7+
# RISC OS Chunk File Format
8+
# From RISC OS Programmer's Reference Manual, Appendix D
9+
# We guess the file type from the type of the first chunk.
10+
0 lelong 0xc3cbc6c5 RISC OS Chunk data
11+
>12 string OBJ_ \b, AOF object
12+
>12 string LIB_ \b, ALF library
13+
14+
# RISC OS AIF, contains "SWI OS_Exit" at offset 16.
15+
16 lelong 0xef000011 RISC OS AIF executable
16+
17+
# RISC OS Draw files
18+
# From RISC OS Programmer's Reference Manual, Appendix E
19+
0 string Draw RISC OS Draw file data
20+
21+
# RISC OS new format font files
22+
# From RISC OS Programmer's Reference Manual, Appendix E
23+
0 string FONT\0 RISC OS outline font data,
24+
>5 byte x version %d
25+
0 string FONT\1 RISC OS 1bpp font data,
26+
>5 byte x version %d
27+
0 string FONT\4 RISC OS 4bpp font data
28+
>5 byte x version %d
29+
30+
# RISC OS Music files
31+
# From RISC OS Programmer's Reference Manual, Appendix E
32+
0 string Maestro\r RISC OS music file
33+
>8 byte x version %d
34+
35+
>8 byte x type %d
36+
37+
# Digital Symphony data files
38+
# From: Bernard Jungen (bern8817@euphonynet.be)
39+
0 string \x02\x01\x13\x13\x13\x01\x0d\x10 Digital Symphony sound sample (RISC OS),
40+
>8 byte x version %d,
41+
>9 pstring x named "%s",
42+
>(9.b+19) byte =0 8-bit logarithmic
43+
>(9.b+19) byte =1 LZW-compressed linear
44+
>(9.b+19) byte =2 8-bit linear signed
45+
>(9.b+19) byte =3 16-bit linear signed
46+
>(9.b+19) byte =4 SigmaDelta-compressed linear
47+
>(9.b+19) byte =5 SigmaDelta-compressed logarithmic
48+
>(9.b+19) byte >5 unknown format
49+
50+
0 string \x02\x01\x13\x13\x14\x12\x01\x0b Digital Symphony song (RISC OS),
51+
>8 byte x version %d,
52+
>9 byte =1 1 voice,
53+
>9 byte !1 %d voices,
54+
>10 leshort =1 1 track,
55+
>10 leshort !1 %d tracks,
56+
>12 leshort =1 1 pattern
57+
>12 leshort !1 %d patterns
58+
59+
0 string \x02\x01\x13\x13\x10\x14\x12\x0e
60+
>9 byte =0 Digital Symphony sequence (RISC OS),
61+
>>8 byte x version %d,
62+
>>10 byte =1 1 line,
63+
>>10 byte !1 %d lines,
64+
>>11 leshort =1 1 position
65+
>>11 leshort !1 %d positions
66+
>9 byte =1 Digital Symphony pattern data (RISC OS),
67+
>>8 byte x version %d,
68+
>>10 leshort =1 1 pattern
69+
>>10 leshort !1 %d patterns

magic/Magdir/adi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
#------------------------------------------------------------------------------
3+
# $File: adi,v 1.4 2009/09/19 16:28:07 christos Exp $
4+
# adi: file(1) magic for ADi's objects
5+
# From Gregory McGarry <g.mcgarry@ieee.org>
6+
#
7+
0 leshort 0x521c COFF DSP21k
8+
>18 lelong &02 executable,
9+
>18 lelong ^02
10+
>>18 lelong &01 static object,
11+
>>18 lelong ^01 relocatable object,
12+
>18 lelong &010 stripped
13+
>18 lelong ^010 not stripped

0 commit comments

Comments
 (0)