-
Notifications
You must be signed in to change notification settings - Fork 81
/
file.go
80 lines (68 loc) · 1.79 KB
/
file.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
package goque
import (
"os"
"path/filepath"
)
// goqueType defines the type of Goque data structure used.
type goqueType uint8
// The possible Goque types, used to determine compatibility when
// one stored type is trying to be opened by a different type.
const (
goqueStack goqueType = iota
goqueQueue
goquePriorityQueue
goquePrefixQueue
)
// checkGoqueType checks if the type of Goque data structure
// trying to be opened is compatible with the opener type.
//
// A file named 'GOQUE' within the data directory used by
// the structure stores the structure type, using the constants
// declared above.
//
// Stacks and Queues are 100% compatible with each other, while
// a PriorityQueue is incompatible with both.
//
// Returns true if types are compatible and false if incompatible.
func checkGoqueType(dataDir string, gt goqueType) (bool, error) {
// Set the path to 'GOQUE' file.
path := filepath.Join(dataDir, "GOQUE")
// Read 'GOQUE' file for this directory.
f, err := os.OpenFile(path, os.O_RDONLY, 0)
if os.IsNotExist(err) {
f, err = os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return false, err
}
defer f.Close()
// Create byte slice of goqueType.
gtb := make([]byte, 1)
gtb[0] = byte(gt)
_, err = f.Write(gtb)
if err != nil {
return false, err
}
return true, nil
}
if err != nil {
return false, err
}
defer f.Close()
// Get the saved type from the file.
fb := make([]byte, 1)
_, err = f.Read(fb)
if err != nil {
return false, err
}
// Convert the file byte to its goqueType.
filegt := goqueType(fb[0])
// Compare the types.
if filegt == gt {
return true, nil
} else if filegt == goqueStack && gt == goqueQueue {
return true, nil
} else if filegt == goqueQueue && gt == goqueStack {
return true, nil
}
return false, nil
}