-
Notifications
You must be signed in to change notification settings - Fork 15
/
fhdr_stamp.go
113 lines (98 loc) · 2.47 KB
/
fhdr_stamp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmbbs
import (
"fmt"
"math/rand"
"os"
"github.com/Ptt-official-app/go-pttbbs/ptttype"
"github.com/Ptt-official-app/go-pttbbs/types"
)
func fhdrStamp(boardFilename string, header *ptttype.FileHeaderRaw, theType ptttype.StampType) (fullFilename string, err error) {
nowTS := types.NowTS()
filename := ""
switch theType {
case ptttype.STAMP_FILE:
for {
rnd := rand.Intn(0xfff + 1)
nowTS++
filename = fmt.Sprintf("M.%d.A.%3.3X", nowTS, rnd)
fullFilename = boardFilename + string(os.PathSeparator) + filename
isValid, err := fhdrStampIsValidFilename(fullFilename)
if err != nil {
return "", err
}
if isValid {
break
}
}
case ptttype.STAMP_DIR:
for {
nowTS++
filename = fmt.Sprintf("D%X", nowTS&0o7777)
fullFilename = boardFilename + string(os.PathSeparator) + filename
isValid, err := fhdrStampIsValidDir(fullFilename)
if err != nil {
return "", err
}
if isValid {
break
}
}
case ptttype.STAMP_LINK:
for {
nowTS++
filename = fmt.Sprintf("S%X", nowTS)
fullFilename = boardFilename + string(os.PathSeparator) + filename
isValid, err := fhdrStampIsValidLink(fullFilename)
if err != nil {
return "", err
}
if isValid {
break
}
}
default:
return "", ptttype.ErrInvalidType
}
copy(header.Filename[:], []byte(filename))
theDate := nowTS.Cdatemd()
copy(header.Date[:], []byte(theDate))
return fullFilename, nil
}
func fhdrStampIsValidFilename(fullFilename string) (isValid bool, err error) {
file, err := types.OpenCreate(fullFilename, os.O_WRONLY|os.O_EXCL)
if os.IsExist(err) {
return false, nil
}
if err != nil {
return false, err
}
defer file.Close()
return true, nil
}
func fhdrStampIsValidDir(fullFilename string) (isValid bool, err error) {
err = types.Mkdir(fullFilename)
if os.IsExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func fhdrStampIsValidLink(fullFilename string) (isValid bool, err error) {
err = types.Symlink("temp", fullFilename)
if os.IsExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func Stampfile(boardFilename string, header *ptttype.FileHeaderRaw) (filename string, err error) {
*header = ptttype.EMPTY_FILE_HEADER_RAW
return fhdrStamp(boardFilename, header, ptttype.STAMP_FILE)
}
func StampfileU(boardFilename string, header *ptttype.FileHeaderRaw) (filename string, err error) {
return fhdrStamp(boardFilename, header, ptttype.STAMP_FILE)
}