[common] Extract OS-specific code into separate files.

This commit is contained in:
Ciprian Dorin Craciun 2021-12-21 21:32:40 +02:00
parent 6c70880476
commit d6fc059c97
7 changed files with 174 additions and 52 deletions

View file

@ -19,7 +19,6 @@ import "runtime"
import "runtime/debug" import "runtime/debug"
import "sort" import "sort"
import "strings" import "strings"
import "syscall"
import "time" import "time"
import "github.com/colinmarc/cdb" import "github.com/colinmarc/cdb"
@ -75,22 +74,17 @@ func archiveFile (_context *context, _pathResolved string, _pathInArchive string
var _fileDev uint64 var _fileDev uint64
var _fileInode uint64 var _fileInode uint64
var _fileSize uint64 var _fileSize uint64
var _fileTimestamp [2]uint64 var _fileTimestamp uint64
if _stat, _error := os.Stat (_pathResolved); _error == nil { if _stat, _error := os.Stat (_pathResolved); _error == nil {
_stat := _stat.Sys() _fileDev, _fileInode, _fileSize, _fileTimestamp, _error = statExtract (_stat)
if _stat, _ok := _stat.(*syscall.Stat_t); _ok { if _error != nil {
_fileDev = uint64 (_stat.Dev) return _error
_fileInode = uint64 (_stat.Ino)
_fileSize = uint64 (_stat.Size)
_fileTimestamp = [2]uint64 { uint64 (_stat.Mtim.Sec), uint64 (_stat.Mtim.Nsec) }
} else {
return fmt.Errorf ("[6578d2d7] failed `stat`-ing: `%s`!", _pathResolved)
} }
} else { } else {
return _error return _error
} }
_fileIdText := fmt.Sprintf ("%d.%d-%d-%d.%d", _fileDev, _fileInode, _fileSize, _fileTimestamp[0], _fileTimestamp[1]) _fileIdText := fmt.Sprintf ("%d.%d.%d.%d", _fileDev, _fileInode, _fileSize, _fileTimestamp)
_fileIdRaw := blake3.Sum256 ([]byte (_fileIdText)) _fileIdRaw := blake3.Sum256 ([]byte (_fileIdText))
_fileId := hex.EncodeToString (_fileIdRaw[:]) _fileId := hex.EncodeToString (_fileIdRaw[:])
@ -121,18 +115,16 @@ func archiveFile (_context *context, _pathResolved string, _pathInArchive string
} }
defer _file.Close () defer _file.Close ()
if _stat, _error := _file.Stat (); _error == nil { if _stat, _error := _file.Stat (); _error == nil {
_stat := _stat.Sys() _fileDev_0, _fileInode_0, _fileSize_0, _fileTimestamp_0, _error := statExtract (_stat)
if _stat, _ok := _stat.(*syscall.Stat_t); _ok { if _error != nil {
if ( return nil, _error
(_fileDev != uint64 (_stat.Dev)) ||
(_fileInode != uint64 (_stat.Ino)) ||
(_fileSize != uint64 (_stat.Size)) ||
(_fileTimestamp[0] != uint64 (_stat.Mtim.Sec)) ||
(_fileTimestamp[1] != uint64 (_stat.Mtim.Nsec))) {
return nil, fmt.Errorf ("[3a07643b] file changed while reading: `%s`!", _pathResolved)
} }
} else { if (
return nil, fmt.Errorf ("[4daf593a] failed `stat`-ing: `%s`!", _pathResolved) (_fileDev != _fileDev_0) ||
(_fileInode != _fileInode_0) ||
(_fileSize != _fileSize_0) ||
(_fileTimestamp != _fileTimestamp_0)) {
return nil, fmt.Errorf ("[3a07643b] file changed while reading: `%s`!", _pathResolved)
} }
} else { } else {
return nil, _error return nil, _error
@ -144,16 +136,14 @@ func archiveFile (_context *context, _pathResolved string, _pathInArchive string
return nil, _error return nil, _error
} }
if _stat, _error := _file.Stat (); _error == nil { if _stat, _error := _file.Stat (); _error == nil {
_stat := _stat.Sys() _, _, _fileSize_0, _fileTimestamp_0, _error := statExtract (_stat)
if _stat, _ok := _stat.(*syscall.Stat_t); _ok { if _error != nil {
if ( return nil, _error
(_fileSize != uint64 (_stat.Size)) ||
(_fileTimestamp[0] != uint64 (_stat.Mtim.Sec)) ||
(_fileTimestamp[1] != uint64 (_stat.Mtim.Nsec))) {
return nil, fmt.Errorf ("[9689146e] file changed while reading: `%s`!", _pathResolved)
} }
} else { if (
return nil, fmt.Errorf ("[523fa3d1] failed `stat`-ing: `%s`!", _pathResolved) (_fileSize != _fileSize_0) ||
(_fileTimestamp != _fileTimestamp_0)) {
return nil, fmt.Errorf ("[9689146e] file changed while reading: `%s`!", _pathResolved)
} }
} else { } else {
return nil, _error return nil, _error

View file

@ -0,0 +1,26 @@
//go:build freebsd || darwin
package archiver
import "fmt"
import "os"
import "syscall"
func statExtract (_stat os.FileInfo) (_dev uint64, _inode uint64, _size uint64, _timestamp uint64, _error error) {
if _stat, _ok := _stat.Sys () .(*syscall.Stat_t); _ok {
_dev = uint64 (_stat.Dev)
_inode = uint64 (_stat.Ino)
_size = uint64 (_stat.Size)
_timestamp = (uint64 (_stat.Mtimespec.Sec) * 1000000) + (uint64 (_stat.Mtimespec.Nsec) / 1000)
} else {
_error = fmt.Errorf ("[7739aed3] failed `stat`-ing`!")
}
return
}

View file

@ -0,0 +1,26 @@
//go:build linux || openbsd
package archiver
import "fmt"
import "os"
import "syscall"
func statExtract (_stat os.FileInfo) (_dev uint64, _inode uint64, _size uint64, _timestamp uint64, _error error) {
if _stat, _ok := _stat.Sys () .(*syscall.Stat_t); _ok {
_dev = uint64 (_stat.Dev)
_inode = uint64 (_stat.Ino)
_size = uint64 (_stat.Size)
_timestamp = (uint64 (_stat.Mtim.Sec) * 1000000) + (uint64 (_stat.Mtim.Nsec) / 1000)
} else {
_error = fmt.Errorf ("[6578d2d7] failed `stat`-ing`!")
}
return
}

View file

@ -0,0 +1,35 @@
//go:build freebsd
package server
import "syscall"
func setrlimit (_limitMemory uint) (error) {
{
_limitMb := (2 * _limitMemory) + (1 * 1024)
_limit := syscall.Rlimit {
Cur : int64 (_limitMb) * 1024 * 1024,
Max : int64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_AS, &_limit); _error != nil {
return _error
}
}
{
_limitMb := _limitMemory
_limit := syscall.Rlimit {
Cur : int64 (_limitMb) * 1024 * 1024,
Max : int64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_DATA, &_limit); _error != nil {
return _error
}
}
return nil
}

View file

@ -0,0 +1,36 @@
//go:build linux || darwin
package server
import "syscall"
func setrlimit (_limitMemory uint) (error) {
{
_limitMb := (2 * _limitMemory) + (1 * 1024)
_limit := syscall.Rlimit {
Cur : uint64 (_limitMb) * 1024 * 1024,
Max : uint64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_AS, &_limit); _error != nil {
return _error
}
}
{
_limitMb := _limitMemory
_limit := syscall.Rlimit {
Cur : uint64 (_limitMb) * 1024 * 1024,
Max : uint64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_DATA, &_limit); _error != nil {
return _error
}
}
return nil
}

View file

@ -0,0 +1,26 @@
//go:build openbsd
package server
import "syscall"
func setrlimit (_limitMemory uint) (error) {
{
_limitMb := _limitMemory
_limit := syscall.Rlimit {
Cur : uint64 (_limitMb) * 1024 * 1024,
Max : uint64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_DATA, &_limit); _error != nil {
return _error
}
}
return nil
}

View file

@ -898,27 +898,10 @@ func main_0 () (error) {
if !_quiet && _isMaster { if !_quiet && _isMaster {
log.Printf ("[ii] [2c130d70] limiting memory to %d MiB;\n", _limitMemory) log.Printf ("[ii] [2c130d70] limiting memory to %d MiB;\n", _limitMemory)
} }
{ if _error := setrlimit (_limitMemory); _error != nil {
_limitMb := (2 * _limitMemory) + (1 * 1024)
_limit := syscall.Rlimit {
Cur : uint64 (_limitMb) * 1024 * 1024,
Max : uint64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_AS, &_limit); _error != nil {
AbortError (_error, "[4da96378] failed to configure memory limit!") AbortError (_error, "[4da96378] failed to configure memory limit!")
} }
} }
{
_limitMb := _limitMemory
_limit := syscall.Rlimit {
Cur : uint64 (_limitMb) * 1024 * 1024,
Max : uint64 (_limitMb) * 1024 * 1024,
}
if _error := syscall.Setrlimit (syscall.RLIMIT_DATA, &_limit); _error != nil {
AbortError (_error, "[f661b4fe] failed to configure memory limit!")
}
}
}