-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileStamperFunc.go
226 lines (202 loc) · 5.71 KB
/
fileStamperFunc.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// fileStamperFunc.go
// Source file auto-generated on Sat, 09 Mar 2019 03:18:51 using Gotk3ObjHandler v1.0 ©2019 H.F.M
/*
fileStamper v1.0 ©2019 H.F.M
This program comes with absolutely no warranty. See the The MIT License (MIT) for details:
https://opensource.org/licenses/mit-license.php
*/
package main
import (
"bytes"
"errors"
"fmt"
"log"
"os"
"path/filepath"
"syscall"
"time"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/gtk"
)
type timeList []fileTimeInfos
type fileTimeInfos struct {
Filename string
Atime time.Time
Mtime time.Time
Ctime time.Time
Btime time.Time // actually unused
}
// updateSb: update statusbar informations
func updateSb(state ...string) {
statusbar.CleanAll()
if len(state) != 0 {
statusbar.Set(fmt.Sprintf("%s", state[0]), 2)
}
statusbar.Set(fmt.Sprintf("%d", len(currentInFilesList)), 0)
statusbar.Set(fmt.Sprintf("%d", len(currentOutFilesList)), 1)
}
// chgTimestamp: 2 methodes, case form equal in and out filenames and another when not equal length
func chgTimestamp() (err error) {
var chgCount, stampCounter int
var startSubCounter, changed bool
var errTxt string
var lenIn = len(mainOptions.PrevInFilesTs)
var lenOut = len(mainOptions.PrevOutFilesTs)
if lenIn == lenOut {
for idx := 0; idx < lenIn; idx++ {
changed, err = doTimestampChg(mainOptions.PrevInFilesTs[idx], mainOptions.PrevOutFilesTs[idx])
if err != nil {
errTxt += err.Error() + "\n"
}
if changed {
chgCount++
changed = false
}
}
} else {
for idx := 0; idx < lenIn; idx++ {
changed, err = doTimestampChg(mainOptions.PrevInFilesTs[idx], mainOptions.PrevOutFilesTs[idx+stampCounter])
if startSubCounter {
stampCounter++
}
if err != nil {
errTxt += err.Error() + "\n"
}
if changed {
chgCount++
changed = false
}
if idx == lenOut-1 || idx+stampCounter == lenOut {
break
}
if idx == lenIn-1 {
idx = -1
startSubCounter = true
}
}
}
if len(errTxt) != 0 {
err = errors.New(errTxt)
}
updateSb(fmt.Sprintf("%d "+sts["fileModified"], chgCount))
return err
}
// doTimestampChg: perform timestamp change in file dest.
func doTimestampChg(item1, item2 fileTimeInfos) (changed bool, err error) {
var fi os.FileInfo
var atime, mtime time.Time
if fi, err = os.Stat(item2.Filename); err == nil && !(fi.IsDir() && !mainObjects.CheckbuttonIncludeDir.GetActive()) {
// if !(fi.IsDir() && !mainObjects.CheckbuttonIncludeDir.GetActive()) {
atime = item2.Atime
mtime = item2.Mtime
if mainObjects.CheckbuttonAtime.GetActive() {
atime = item1.Atime
}
if mainObjects.CheckbuttonMtime.GetActive() {
mtime = item1.Mtime
}
return true, os.Chtimes(item2.Filename, atime, mtime)
}
// }
return false, err
}
// storeAllTimestamp:
func StoreInTimestamp() (err error) {
if mainObjects.CheckbuttonSubdir.GetActive() {
if currentInFilesList, err = getSubdir(currentInFilesList); err != nil {
return err
}
}
mainOptions.PrevInFilesTs, err = storeTimes(currentInFilesList)
return err
}
func StoreOutTimestamp() (err error) {
if mainObjects.CheckbuttonSubdir.GetActive() {
if currentOutFilesList, err = getSubdir(currentOutFilesList); err != nil {
return err
}
}
mainOptions.PrevOutFilesTs, err = storeTimes(currentOutFilesList)
return err
}
// getSubdir: retreive files into subdirs and return whole files
func getSubdir(inFiles []string) (outFiles []string, err error) {
var files []string
for _, file := range inFiles {
if fi, err := os.Stat(file); fi.IsDir() && !os.IsNotExist(err) {
if files, err = ScanSubDir(file, mainObjects.CheckbuttonIncludeDir.GetActive()); err == nil {
outFiles = append(outFiles, files...)
}
}
}
return append(inFiles, outFiles...), err
}
// ScanSubDir retrieve files in a specific directory and his sub-directory.
// don't follow symlink (walk)
func ScanSubDir(root string, showDirs ...bool) (files []string, err error) {
var listDir bool
if len(showDirs) != 0 {
listDir = showDirs[0]
}
err = filepath.Walk(root,
func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
} else if listDir {
files = append(files, path)
}
return nil
})
return files, err
}
// storeTimes: store time information into structure
func storeTimes(inList []string) (fileTimeList timeList, err error) {
for _, file := range inList {
atime, mtime, ctime, err := getTimes(file)
if err == nil {
fileTimeList = append(fileTimeList, fileTimeInfos{Filename: file, Atime: atime, Mtime: mtime, Ctime: ctime})
}
}
return fileTimeList, err
}
// statTimes: get times from file
func getTimes(name string) (atime, mtime, ctime time.Time, err error) {
var fi os.FileInfo
fi, err = os.Stat(name)
if err == nil {
mtime = fi.ModTime()
stat := fi.Sys().(*syscall.Stat_t)
atime = time.Unix(int64(stat.Atim.Sec), int64(stat.Atim.Nsec))
ctime = time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec))
}
return atime, mtime, ctime, err
}
// initDropSets: configure controls to receive dndcontent.
func initDropSets() {
var targets []gtk.TargetEntry // Build dnd context
te, err := gtk.TargetEntryNew("text/uri-list", gtk.TARGET_OTHER_APP, 0)
if err != nil {
log.Fatal(err)
}
targets = append(targets, *te)
mainObjects.ButtonInFiles.DragDestSet(
gtk.DEST_DEFAULT_ALL,
targets,
gdk.ACTION_COPY)
mainObjects.ButtonOutFiles.DragDestSet(
gtk.DEST_DEFAULT_ALL,
targets,
gdk.ACTION_COPY)
}
// GetTextEOL: Get EOL from text bytes (CR, LF, CRLF) > string
func GetTextEOL(inTextBytes []byte) (outString string) {
bCR := []byte{0x0D}
bLF := []byte{0x0A}
bCRLF := []byte{0x0D, 0x0A}
if bytes.Contains(inTextBytes, bCRLF) {
return string(bCRLF)
} else if bytes.Contains(inTextBytes, bCR) {
return string(bCR)
}
return string(bLF)
}