From e301fefe660f3cb1f27f924e04d25db5db9f28a6 Mon Sep 17 00:00:00 2001 From: kitsunyan Date: Sat, 19 May 2018 07:46:13 +0300 Subject: Add user cache dir --- doc/pakku.conf.5.txt | 4 ++++ pakku.conf | 1 + src/common.nim | 52 ++++++++++++++++++++++++++++++++++++++++------------ src/config.nim | 20 +++++++++++++++++--- 4 files changed, 62 insertions(+), 15 deletions(-) diff --git a/doc/pakku.conf.5.txt b/doc/pakku.conf.5.txt index 9b03b13..c9666a4 100644 --- a/doc/pakku.conf.5.txt +++ b/doc/pakku.conf.5.txt @@ -16,6 +16,10 @@ divided into sections. Only options section is used. Options ------- +*UserCacheDir =* path/to/user/cache/dir:: + Set the cache directory in which pakku will cache repositories. + The default value is +$\{HOME\}/.cache/pakku+. + *TmpDir =* path/to/tmp/dir:: Set the temporary directory in which pakku will perform all building operations. The default value is +/tmp/pakku-$\{USER\}+. diff --git a/pakku.conf b/pakku.conf index dcb0686..9b781ca 100644 --- a/pakku.conf +++ b/pakku.conf @@ -1,6 +1,7 @@ # See the pakku.conf(5) manpage [options] +#UserCacheDir = ${HOME}/.cache/pakku #TmpDir = /tmp/pakku-${USER} AurComments 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, -- cgit v1.2.3-70-g09d2