2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-10-27 11:04:17 +00:00
|
|
|
import chronicles
|
|
|
|
import stew/io2
|
|
|
|
export io2
|
|
|
|
|
|
|
|
when defined(windows):
|
|
|
|
import stew/[windows/acl]
|
|
|
|
|
|
|
|
proc secureCreatePath*(path: string): IoResult[void] =
|
|
|
|
when defined(windows):
|
|
|
|
let sres = createFoldersUserOnlySecurityDescriptor()
|
|
|
|
if sres.isErr():
|
|
|
|
error "Could not allocate security descriptor", path = path,
|
|
|
|
errorMsg = ioErrorMsg(sres.error), errorCode = $sres.error
|
|
|
|
err(sres.error)
|
|
|
|
else:
|
|
|
|
var sd = sres.get()
|
2020-10-30 00:36:47 +00:00
|
|
|
createPath(path, 0o700, secDescriptor = sd.getDescriptor())
|
2020-10-27 11:04:17 +00:00
|
|
|
else:
|
2020-10-30 00:36:47 +00:00
|
|
|
createPath(path, 0o700)
|
2020-10-27 11:04:17 +00:00
|
|
|
|
|
|
|
proc secureWriteFile*[T: byte|char](path: string,
|
|
|
|
data: openArray[T]): IoResult[void] =
|
|
|
|
when defined(windows):
|
|
|
|
let sres = createFilesUserOnlySecurityDescriptor()
|
|
|
|
if sres.isErr():
|
|
|
|
error "Could not allocate security descriptor", path = path,
|
|
|
|
errorMsg = ioErrorMsg(sres.error), errorCode = $sres.error
|
|
|
|
err(sres.error)
|
|
|
|
else:
|
|
|
|
var sd = sres.get()
|
|
|
|
writeFile(path, data, 0o600, secDescriptor = sd.getDescriptor())
|
|
|
|
else:
|
|
|
|
writeFile(path, data, 0o600)
|