Skip to content

Commit 50fae00

Browse files
committed
v0.0.4
1 parent 91aed93 commit 50fae00

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*.exe~
77
*.dll
88
*.so
9+
*.syso
910
*.dylib
1011

1112
# Test binary, built with `go test -c`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ import (
102102
)
103103

104104
func main() {
105-
targetPath := "C:\\test\\test.txt"
105+
targetPath := "C:\\test\\test.txt"
106106

107107
eaList, err := ntfs_ea.QueryEaFile(targetPath)
108108
if err != nil {

cmd/query_file_ea/query_file_ea.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ import (
1717

1818
func main() {
1919
var targetPath, queryName string
20-
var dump, extract bool
20+
var dump, extract, stdout bool
2121

2222
flag.StringVar(&targetPath, "target-path", "", "path of the file to query EA")
2323
flag.StringVar(&queryName, "query-name", "", "names of EA to query, split by comma, if it is not given, query all EA from the file")
24+
2425
flag.BoolVar(&dump, "dump", false, "dump EA to console, this is enabled by default if no action is given")
2526
flag.BoolVar(&extract, "extract", false, "extract EA to file(s) with according EaName")
27+
flag.BoolVar(&stdout, "stdout", false, "extract EA into stdout")
2628

2729
flag.Usage = func() {
28-
fmt.Fprintf(flag.CommandLine.Output(), "%s queries EA(Extended Attribute) from a file in NTFS(New Technology File System).\nUsage: %s -query-name [eaName1],[eaName2],... -extract [target path]\n or %s -target-path [target path] -query-name [eaName1],[eaName2],... -dump -extract\nThis program is supposed to work only in Windows.\n\n", os.Args[0], os.Args[0], os.Args[0])
30+
fmt.Fprintf(flag.CommandLine.Output(), "%s queries EA(Extended Attribute) from a file in NTFS(New Technology File System).\nUsage: %s -query-name [eaName1],[eaName2],... -extract [target path]\n or %s -target-path [target path] -query-name [eaName1],[eaName2],... -dump -extract\nWrite EA value to stdout(for piping output): %s -stdout -target-path [target path] -query-name [eaName] | (process output)\n\n", os.Args[0], os.Args[0], os.Args[0], os.Args[0])
2931
flag.PrintDefaults()
3032
}
3133

@@ -66,11 +68,15 @@ func main() {
6668
}
6769

6870
if extract {
69-
err := os.WriteFile(ea.EaName, ea.EaValue, 0777)
70-
if err != nil {
71-
fmt.Fprintf(os.Stderr, "Failed to write EA for %s into file: %v\n", ea.EaName, err)
71+
if stdout {
72+
os.Stdout.Write(ea.EaValue)
7273
} else {
73-
fmt.Printf("Extracted EaValue in \"%s\"", ea.EaName)
74+
err := os.WriteFile(ea.EaName, ea.EaValue, 0777)
75+
if err != nil {
76+
fmt.Fprintf(os.Stderr, "Failed to write EA for %s into file: %v\n", ea.EaName, err)
77+
} else {
78+
fmt.Printf("Extracted EaValue in \"%s\"", ea.EaName)
79+
}
7480
}
7581
}
7682
}

cmd/query_file_ea/query_file_ea.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"OriginalFilename": "",
3030
"PrivateBuild": "",
3131
"ProductName": "query_file_ea.exe",
32-
"ProductVersion": "v0.0.3",
32+
"ProductVersion": "v0.0.4",
3333
"SpecialBuild": ""
3434
},
3535
"VarFileInfo": {

cmd/write_file_ea/write_file_ea.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@ import (
1515

1616
func main() {
1717
var srcPath, targetPath, eaName string
18-
var needEa, removeEa bool
18+
var needEa, removeEa, stdin bool
1919

2020
flag.StringVar(&targetPath, "target-path", "", "path of target file to write EA")
2121
flag.StringVar(&srcPath, "source-path", "", "path of source file to be used as content for EA")
2222
flag.StringVar(&eaName, "ea-name", "", "name of the EA")
2323

2424
flag.BoolVar(&needEa, "need-ea", false, "set flag if file needs to be interpreted with EA")
2525
flag.BoolVar(&removeEa, "remove-ea", false, "remove the EA with the given name")
26+
flag.BoolVar(&stdin, "stdin", false, "use stdin as content for EA")
2627

2728
flag.Usage = func() {
28-
fmt.Fprintf(flag.CommandLine.Output(), "%s writes EA(Extended Attribute) info a file in NTFS(New Technology File System) with the content of a given source file, if the source file is empty the EA with EaName is removed if exists.\nUsage: %s [target path] [source path] [EA name]\n or\n %s -target-path [target path] -source-path [source path] -ea-name [EA name] -need-ea\nTo remove EA with specific name, use: %s -remove-ea [target path] [EA name]\nThis program is supposed to work only in Windows.\n\n", os.Args[0], os.Args[0], os.Args[0], os.Args[0])
29+
fmt.Fprintf(flag.CommandLine.Output(), "%s writes EA(Extended Attribute) info a file in NTFS(New Technology File System) with the content of a given source file, if the source file is empty the EA with EaName is removed if exists.\nUsage: %s [target path] [source path] [EA name]\n or\n %s -target-path [target path] -source-path [source path] -ea-name [EA name] -need-ea\nWrite EA from stdin: echo \"[content for ea] | %s -stdin -target-path [target path] -ea-name [EA name]\nTo remove EA with specific name, use: %s -remove-ea [target path] [EA name]\n\n", os.Args[0], os.Args[0], os.Args[0], os.Args[0], os.Args[0])
2930
flag.PrintDefaults()
3031
}
3132

@@ -40,7 +41,7 @@ func main() {
4041
srcPath = flag.Arg(1)
4142
}
4243
if eaName == "" {
43-
if removeEa {
44+
if removeEa || stdin {
4445
eaName = flag.Arg(1)
4546
requiredArg = 2
4647
} else {
@@ -53,7 +54,7 @@ func main() {
5354
flags |= ntfs_ea.NeedEa
5455
}
5556

56-
if !(targetPath != "" && srcPath != "" && eaName != "") && !(removeEa && targetPath != "" && eaName != "") && flag.NArg() < requiredArg {
57+
if !(targetPath != "" && srcPath != "" && eaName != "") && !((removeEa || stdin) && targetPath != "" && eaName != "") && flag.NArg() < requiredArg {
5758
flag.Usage()
5859
os.Exit(1)
5960
}
@@ -73,9 +74,40 @@ func main() {
7374

7475
return
7576
}
77+
78+
if stdin {
79+
eaBuf := make([]byte, 65536)
80+
81+
n, err := os.Stdin.Read(eaBuf)
82+
if err != nil {
83+
fmt.Fprintf(os.Stderr, "Failed to read from stdin: %v", err)
84+
os.Exit(2)
85+
}
86+
87+
if n < len(eaBuf) {
88+
eaBuf = eaBuf[:n]
89+
}
90+
91+
eaToWrite := ntfs_ea.EaInfo{
92+
Flags: flags,
93+
EaName: eaName,
94+
EaValue: eaBuf,
95+
}
96+
97+
err = ntfs_ea.EaWriteFile(targetPath, eaToWrite)
98+
if err != nil {
99+
fmt.Fprintf(os.Stderr, "Failed to write EA into file: %v\n", err)
100+
os.Exit(2)
101+
}
102+
103+
fmt.Printf("Written EA info file \"%s\" from stdin with ea name \"%s\"\n", targetPath, eaName)
104+
105+
return
106+
}
107+
76108
err := ntfs_ea.WriteEaWithFile(targetPath, srcPath, eaName, flags)
77109
if err != nil {
78-
fmt.Fprintf(os.Stderr, "failed to write EA into file: %v\n", err)
110+
fmt.Fprintf(os.Stderr, "Failed to write EA into file: %v\n", err)
79111
os.Exit(2)
80112
}
81113

cmd/write_file_ea/write_file_ea.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"OriginalFilename": "",
3030
"PrivateBuild": "",
3131
"ProductName": "write_file_ea.exe",
32-
"ProductVersion": "v0.0.3",
32+
"ProductVersion": "v0.0.4",
3333
"SpecialBuild": ""
3434
},
3535
"VarFileInfo": {

0 commit comments

Comments
 (0)