From d6fc059c976620d74bebf39c6216c9fca6c23f6d Mon Sep 17 00:00:00 2001 From: Ciprian Dorin Craciun Date: Tue, 21 Dec 2021 21:32:40 +0200 Subject: [PATCH] [common] Extract OS-specific code into separate files. --- sources/cmd/archiver/archiver.go | 56 +++++++++++++------------------- sources/cmd/archiver/os-bsd.go | 26 +++++++++++++++ sources/cmd/archiver/os-linux.go | 26 +++++++++++++++ sources/cmd/server/os-freebsd.go | 35 ++++++++++++++++++++ sources/cmd/server/os-linux.go | 36 ++++++++++++++++++++ sources/cmd/server/os-openbsd.go | 26 +++++++++++++++ sources/cmd/server/server.go | 21 ++---------- 7 files changed, 174 insertions(+), 52 deletions(-) create mode 100644 sources/cmd/archiver/os-bsd.go create mode 100644 sources/cmd/archiver/os-linux.go create mode 100644 sources/cmd/server/os-freebsd.go create mode 100644 sources/cmd/server/os-linux.go create mode 100644 sources/cmd/server/os-openbsd.go diff --git a/sources/cmd/archiver/archiver.go b/sources/cmd/archiver/archiver.go index e0778e9..42ba7b3 100644 --- a/sources/cmd/archiver/archiver.go +++ b/sources/cmd/archiver/archiver.go @@ -19,7 +19,6 @@ import "runtime" import "runtime/debug" import "sort" import "strings" -import "syscall" import "time" import "github.com/colinmarc/cdb" @@ -75,22 +74,17 @@ func archiveFile (_context *context, _pathResolved string, _pathInArchive string var _fileDev uint64 var _fileInode uint64 var _fileSize uint64 - var _fileTimestamp [2]uint64 + var _fileTimestamp uint64 if _stat, _error := os.Stat (_pathResolved); _error == nil { - _stat := _stat.Sys() - if _stat, _ok := _stat.(*syscall.Stat_t); _ok { - _fileDev = uint64 (_stat.Dev) - _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) + _fileDev, _fileInode, _fileSize, _fileTimestamp, _error = statExtract (_stat) + if _error != nil { + return _error } } else { 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)) _fileId := hex.EncodeToString (_fileIdRaw[:]) @@ -121,18 +115,16 @@ func archiveFile (_context *context, _pathResolved string, _pathInArchive string } defer _file.Close () if _stat, _error := _file.Stat (); _error == nil { - _stat := _stat.Sys() - if _stat, _ok := _stat.(*syscall.Stat_t); _ok { - if ( - (_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 { - return nil, fmt.Errorf ("[4daf593a] failed `stat`-ing: `%s`!", _pathResolved) + _fileDev_0, _fileInode_0, _fileSize_0, _fileTimestamp_0, _error := statExtract (_stat) + if _error != nil { + return nil, _error + } + if ( + (_fileDev != _fileDev_0) || + (_fileInode != _fileInode_0) || + (_fileSize != _fileSize_0) || + (_fileTimestamp != _fileTimestamp_0)) { + return nil, fmt.Errorf ("[3a07643b] file changed while reading: `%s`!", _pathResolved) } } else { return nil, _error @@ -144,16 +136,14 @@ func archiveFile (_context *context, _pathResolved string, _pathInArchive string return nil, _error } if _stat, _error := _file.Stat (); _error == nil { - _stat := _stat.Sys() - if _stat, _ok := _stat.(*syscall.Stat_t); _ok { - if ( - (_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 { - return nil, fmt.Errorf ("[523fa3d1] failed `stat`-ing: `%s`!", _pathResolved) + _, _, _fileSize_0, _fileTimestamp_0, _error := statExtract (_stat) + if _error != nil { + return nil, _error + } + if ( + (_fileSize != _fileSize_0) || + (_fileTimestamp != _fileTimestamp_0)) { + return nil, fmt.Errorf ("[9689146e] file changed while reading: `%s`!", _pathResolved) } } else { return nil, _error diff --git a/sources/cmd/archiver/os-bsd.go b/sources/cmd/archiver/os-bsd.go new file mode 100644 index 0000000..7832d1e --- /dev/null +++ b/sources/cmd/archiver/os-bsd.go @@ -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 +} + diff --git a/sources/cmd/archiver/os-linux.go b/sources/cmd/archiver/os-linux.go new file mode 100644 index 0000000..4665eba --- /dev/null +++ b/sources/cmd/archiver/os-linux.go @@ -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 +} + diff --git a/sources/cmd/server/os-freebsd.go b/sources/cmd/server/os-freebsd.go new file mode 100644 index 0000000..0e13227 --- /dev/null +++ b/sources/cmd/server/os-freebsd.go @@ -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 +} + diff --git a/sources/cmd/server/os-linux.go b/sources/cmd/server/os-linux.go new file mode 100644 index 0000000..f69e219 --- /dev/null +++ b/sources/cmd/server/os-linux.go @@ -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 +} + diff --git a/sources/cmd/server/os-openbsd.go b/sources/cmd/server/os-openbsd.go new file mode 100644 index 0000000..1810dd4 --- /dev/null +++ b/sources/cmd/server/os-openbsd.go @@ -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 +} + diff --git a/sources/cmd/server/server.go b/sources/cmd/server/server.go index be2c49a..d800811 100644 --- a/sources/cmd/server/server.go +++ b/sources/cmd/server/server.go @@ -898,25 +898,8 @@ func main_0 () (error) { if !_quiet && _isMaster { log.Printf ("[ii] [2c130d70] limiting memory to %d MiB;\n", _limitMemory) } - { - _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!") - } - } - { - _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!") - } + if _error := setrlimit (_limitMemory); _error != nil { + AbortError (_error, "[4da96378] failed to configure memory limit!") } }