aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/common.nim52
-rw-r--r--src/config.nim20
2 files changed, 57 insertions, 15 deletions
diff --git a/src/common.nim b/src/common.nim
index 11f6aef..bd9573a 100644
--- a/src/common.nim
+++ b/src/common.nim
@@ -4,6 +4,9 @@ import
"wrapper/alpm"
type
+ CacheKind* {.pure.} = enum
+ repositories
+
SyncFoundPackageInfo* = tuple[
base: string,
version: string,
@@ -245,21 +248,46 @@ proc `$`*[T: PackageTarget](target: T): string =
template tmpRoot(config: Config, dropPrivileges: bool): string =
if dropPrivileges: config.tmpRootInitial else: config.tmpRootCurrent
-proc ensureTmpOrError*(config: Config, dropPrivileges: bool): Option[string] =
- let tmpRootExists = try:
- discard config.tmpRoot(dropPrivileges).existsOrCreateDir()
- if dropPrivileges:
- let user = initialUser.get(currentUser)
- discard chown(config.tmpRoot(dropPrivileges), (Uid) user.uid, (Gid) user.gid)
- true
- except:
- false
-
- if not tmpRootExists:
- some(tr"failed to create tmp directory '$#'" % [config.tmpRoot(dropPrivileges)])
+template userCache(config: Config, dropPrivileges: bool): string =
+ if dropPrivileges: config.userCacheInitial else: config.userCacheCurrent
+
+template cache*(userCache: string, cacheKind: CacheKind): string =
+ userCache & "/" & $cacheKind
+
+proc createDirRecursive(dir: string, chownUser: Option[User]): bool =
+ let segments = dir.split("/").filter(x => not (x.len == 0 or x == "."))
+
+ proc createDirIndex(index: int): bool =
+ if index < segments.len:
+ let path = (if dir.len > 0 and dir[0] == '/': "/" else: "") &
+ segments[0 .. index].join("/")
+ try:
+ let exists = path.existsOrCreateDir()
+ if chownUser.isSome and (not exists or index == segments.len - 1):
+ discard chown(path, (Uid) chownUser.unsafeGet.uid, (Gid) chownUser.unsafeGet.gid)
+ createDirIndex(index + 1)
+ except:
+ false
+ else:
+ true
+
+ createDirIndex(0)
+
+proc ensureDirOrError(dir: string, dropPrivileges: bool): Option[string] =
+ let user = if dropPrivileges: some(initialUser.get(currentUser)) else: none(User)
+
+ if not createDirRecursive(dir, user):
+ some(tr"failed to create directory '$#'" % [dir])
else:
none(string)
+proc ensureTmpOrError*(config: Config, dropPrivileges: bool): Option[string] =
+ ensureDirOrError(config.tmpRoot(dropPrivileges), dropPrivileges)
+
+proc ensureUserCacheOrError*(config: Config, cacheKind: CacheKind,
+ dropPrivileges: bool): Option[string] =
+ ensureDirOrError(config.userCache(dropPrivileges).cache(cacheKind), dropPrivileges)
+
proc getGitFiles*(repoPath: string, gitSubdir: Option[string],
dropPrivileges: bool): seq[string] =
if gitSubdir.isSome:
diff --git a/src/config.nim b/src/config.nim
index 19dc0d6..b4b9c9a 100644
--- a/src/config.nim
+++ b/src/config.nim
@@ -29,6 +29,8 @@ type
root*: string
db*: string
cache*: string
+ userCacheInitial*: string
+ userCacheCurrent*: string
tmpRootInitial*: string
tmpRootCurrent*: string
color*: bool
@@ -119,12 +121,23 @@ proc obtainConfig*(config: PacmanConfig): Config =
let cache = config.cache
let color = config.colorMode.get
- proc obtainTmpDir(user: User): string =
- options.opt("TmpDir").get("/tmp/pakku-${USER}")
+ proc handleDirPattern(dirPattern: string, user: User): string =
+ dirPattern
.replace("${UID}", $user.uid)
.replace("${USER}", user.name)
+ .replace("${HOME}", user.home)
+ .replace("$$", "$")
+
+ proc obtainUserCacheDir(user: User): string =
+ options.opt("UserCacheDir").get("${HOME}/.cache/pakku").handleDirPattern(user)
+
+ proc obtainTmpDir(user: User): string =
+ options.opt("TmpDir").get("/tmp/pakku-${USER}").handleDirPattern(user)
- let tmpRootInitial = obtainTmpDir(initialUser.get(currentUser))
+ let initialOrCurrentUser = initialUser.get(currentUser)
+ let userCacheInitial = obtainUserCacheDir(initialOrCurrentUser)
+ let userCacheCurrent = obtainUserCacheDir(currentUser)
+ let tmpRootInitial = obtainTmpDir(initialOrCurrentUser)
let tmpRootCurrent = obtainTmpDir(currentUser)
let aurComments = options.hasKey("AurComments")
let checkIgnored = options.hasKey("CheckIgnored")
@@ -134,6 +147,7 @@ proc obtainConfig*(config: PacmanConfig): Config =
let preBuildCommand = options.opt("PreBuildCommand")
Config(root: root, db: db, cache: cache,
+ userCacheInitial: userCacheInitial, userCacheCurrent: userCacheCurrent,
tmpRootInitial: tmpRootInitial, tmpRootCurrent: tmpRootCurrent, color: color,
dbs: config.dbs, arch: config.arch, debug: config.debug, progressBar: config.progressBar,
verbosePkgList: config.verbosePkgList, pgpKeyserver: config.pgpKeyserver,