-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
110 lines (91 loc) · 2.25 KB
/
reader.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
package main
import (
"crypto/sha512"
"encoding/binary"
"encoding/hex"
"io"
"log"
"net/http"
"os"
"strconv"
"syscall"
"unsafe"
"golang.org/x/sys/unix"
)
type imageReader struct {
fileName string
fileType string
hashSum string
totalSize int64
}
func alignTo(value int64, alignment int64) int64 {
return (value + alignment - 1) & ^(alignment - 1)
}
func (reader *imageReader) determineType(file *os.File) string {
// SquashFS
type SquashFsHeader struct {
Smagic int32
Inodes int32
MkfsZime int32
BlockSize int32
Fragments int32
Compression int16
BlockLog int16
Flags int16
NoIds int16
Smajor int16
Sminor int16
RootInode int64
BytesUsed int64
/* ignore the rest */
}
header := SquashFsHeader{}
file.Seek(0, io.SeekStart)
binary.Read(file, binary.LittleEndian, &header)
if header.Smagic == 0x73717369 {
reader.totalSize = alignTo(header.BytesUsed, 4096)
return "squashfs"
}
stat, err := file.Stat()
if err == nil {
switch stat.Mode() & os.ModeType {
case 0:
// regular file
reader.totalSize = stat.Size()
return "plain"
case os.ModeDevice:
// block device - try to determine total size
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, file.Fd(), unix.BLKGETSIZE64, uintptr(unsafe.Pointer(&reader.totalSize)))
if errno == 0 {
return "block-device"
}
}
}
return "unknown"
}
func (reader *imageReader) open(name string) error {
file, err := os.OpenFile(name, os.O_RDONLY, 0)
if err != nil {
return err
}
defer file.Close()
reader.fileName = name
reader.fileType = reader.determineType(file)
hash := sha512.New()
file.Seek(0, io.SeekStart)
io.CopyN(hash, file, reader.totalSize)
reader.hashSum = hex.EncodeToString(hash.Sum(nil))
return nil
}
func (reader *imageReader) httpHandler(writer http.ResponseWriter, request *http.Request) {
file, err := os.OpenFile(reader.fileName, os.O_RDONLY, 0)
if err != nil {
http.Error(writer, err.Error(), 500)
log.Print(err)
return
}
defer file.Close()
log.Print("Handling request from " + request.RemoteAddr + " for " + reader.fileName + " - " + request.URL.Path[1:])
io.CopyN(writer, file, reader.totalSize)
writer.Header().Set("Content-Length", strconv.FormatInt(reader.totalSize, 10))
}