-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnsfile.go
More file actions
126 lines (106 loc) · 2.61 KB
/
Copy pathnsfile.go
File metadata and controls
126 lines (106 loc) · 2.61 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
// Copyrigh 2018 Rustam Gilyazov. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package siebns currently only allows fixing the encoded file size
// in Siebel Gateway Naming file after making manual modifications to it.
package siebns
import (
"encoding/base64"
"encoding/binary"
"errors"
"io"
"os"
)
// other constants
const (
headerSize = 82
checksumSz = 12
signature = "Siebel Name Server Backing File"
)
type format struct {
unicode bool
dos bool
}
type offsets struct {
checksum int64
}
type version struct {
siebel string
nsfile string
}
//nsHeader contains the information from NS file header
type nsHeader struct {
byteOrder binary.ByteOrder
format format
offsets offsets
version version
}
type nsDisker interface {
io.Reader
io.Writer
io.Seeker
io.Closer
Name() string
Stat() (os.FileInfo, error)
}
// NSFile Name Server File descriptor
type NSFile struct {
header *nsHeader // ns information
nsDisker // file handle
}
var (
// bom is Unicode BOM
bom = [3]byte{0xef, 0xbb, 0xbf}
// crlf is dos carriage return and line feed
crlf = []byte{0x0d, 0x0a}
// coding is the encoding used
coding = base64.StdEncoding
)
var (
errNotInitialised = errors.New("internal error: structure not initialised")
errNotSiebns = errors.New("not a siebel gateway file")
errByteOrderNil = errors.New("internal error: byteOrder is nil")
errZeroFilesize = errors.New("zero file size")
errChecksumCorrupt = errors.New("Checksum part is corrupt. Please fix it " +
"manually by\nopening the file in the editor and deleting " +
"data from line 4 (leaving the\nline 4 empty).")
)
// FixSize fixes the size in header regardless of whether
// NSFile.CorrectionNeeded is true or false. Sets NSFile.CorrectionNeeded to
// false
func (ns *NSFile) FixSize() (int, error) {
return ns.header.writeEncodedSize(ns, ns.Size())
}
// IsHeaderCorrect returns true if the file header doesn't need adjustment
func (ns *NSFile) IsHeaderCorrect() bool {
size, _ := ns.header.readEncodedSize(ns)
return (ns.Size() == size)
}
// Open opens existing nsfile
func Open(path string) (*NSFile, error) {
fileInfo, err := os.Stat(path)
if err != nil {
return nil, err
}
if fileInfo.Size() < headerSize {
return nil, errNotSiebns
}
f, err := os.OpenFile(path, os.O_RDWR, 0644)
if err != nil {
return nil, err
}
hdr, err := readHeader(f)
ns := &NSFile{
nsDisker: f,
header: hdr,
}
return ns, err
}
// Size returns the file size
func (ns *NSFile) Size() int64 {
fi, err := ns.Stat()
if err != nil {
panic(err)
}
return fi.Size()
}