summarylogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.SRCINFO6
-rw-r--r--PKGBUILD6
-rw-r--r--get-docker-cache-id.go82
3 files changed, 47 insertions, 47 deletions
diff --git a/.SRCINFO b/.SRCINFO
index 761a85f0805a..6991a2d9181a 100644
--- a/.SRCINFO
+++ b/.SRCINFO
@@ -1,7 +1,7 @@
pkgbase = mkinitcpio-docker-hooks
pkgdesc = mkinitcpio hooks that provides support for using docker image as root
- pkgver = 1.1
- pkgrel = 3
+ pkgver = 1.2
+ pkgrel = 1
url = https://github.com/zasdfgbnm/mkinitcpio-docker-hooks
arch = any
groups = base
@@ -16,7 +16,7 @@ pkgbase = mkinitcpio-docker-hooks
md5sums = 5ce9a3498b0af7e9fbbe830893da83e3
md5sums = b44c59a25ef2ff74e60d20aaabe683fd
md5sums = 4df2fd7306aef1e3d94ffc6b05056728
- md5sums = bcae1950b172498e34c1aa4cb55ecad0
+ md5sums = cdfc2b962060921d415134192a540dd1
pkgname = mkinitcpio-docker-hooks
diff --git a/PKGBUILD b/PKGBUILD
index e41bb705d24d..241f6d635029 100644
--- a/PKGBUILD
+++ b/PKGBUILD
@@ -1,8 +1,8 @@
# Maintainer: Xiang Gao <qasdfgtyuiop at gmail dot com>
pkgname=mkinitcpio-docker-hooks
-pkgver=1.1
-pkgrel=3
+pkgver=1.2
+pkgrel=1
pkgdesc="mkinitcpio hooks that provides support for using docker image as root"
arch=(any)
url="https://github.com/zasdfgbnm/mkinitcpio-docker-hooks"
@@ -15,7 +15,7 @@ source=('install_docker-btrfs' 'hooks_docker-btrfs' 'etc_docker-btrfs.json' 'get
md5sums=('5ce9a3498b0af7e9fbbe830893da83e3'
'b44c59a25ef2ff74e60d20aaabe683fd'
'4df2fd7306aef1e3d94ffc6b05056728'
- 'bcae1950b172498e34c1aa4cb55ecad0')
+ 'cdfc2b962060921d415134192a540dd1')
build() {
go build get-docker-cache-id.go
diff --git a/get-docker-cache-id.go b/get-docker-cache-id.go
index cbaf367cda6a..e14e1877105e 100644
--- a/get-docker-cache-id.go
+++ b/get-docker-cache-id.go
@@ -3,17 +3,17 @@ package main
import (
"encoding/json"
"fmt"
+ "io/ioutil"
"os"
"strings"
- "io/ioutil"
)
-const DOCKER_DB_DIR = "/var/lib/docker/image/btrfs"
-const REPO_FILE = DOCKER_DB_DIR + "/repositories.json"
-const CFG_FILE = "/etc/docker-btrfs.json"
+const dockerDbDir = "/var/lib/docker/image/btrfs"
+const repoFile = dockerDbDir + "/repositories.json"
+const cfgFile = "/etc/docker-btrfs.json"
func getImageTag() (string, string) {
- buf, err := ioutil.ReadFile(CFG_FILE)
+ buf, err := ioutil.ReadFile(cfgFile)
if err != nil {
panic(err)
}
@@ -25,8 +25,8 @@ func getImageTag() (string, string) {
return dat["docker_image"].(string), dat["docker_tag"].(string)
}
-func getImageId(image string, tag string) string {
- buf, err := ioutil.ReadFile(REPO_FILE)
+func getImageID(image string, tag string) string {
+ buf, err := ioutil.ReadFile(repoFile)
if err != nil {
panic(err)
}
@@ -40,9 +40,9 @@ func getImageId(image string, tag string) string {
return imageTags[image+":"+tag].(string)
}
-func getDiffIds(imgId string) []string {
- imgPath := strings.Replace(imgId, ":", "/", 1)
- buf, err := ioutil.ReadFile(DOCKER_DB_DIR + "/imagedb/content/" + imgPath)
+func getDiffIDs(imgID string) []string {
+ imgPath := strings.Replace(imgID, ":", "/", 1)
+ buf, err := ioutil.ReadFile(dockerDbDir + "/imagedb/content/" + imgPath)
if err != nil {
panic(err)
}
@@ -52,39 +52,39 @@ func getDiffIds(imgId string) []string {
panic(err)
}
rootfs := dat["rootfs"].(map[string]interface{})
- diffIdsInterface := rootfs["diff_ids"].([]interface{})
- diffIdsStr := make([]string, len(diffIdsInterface))
- for i, v := range diffIdsInterface {
- diffIdsStr[i] = strings.Replace(v.(string), "sha256:", "", 1)
+ diffIDsInterface := rootfs["diff_ids"].([]interface{})
+ diffIDsStr := make([]string, len(diffIDsInterface))
+ for i, v := range diffIDsInterface {
+ diffIDsStr[i] = strings.Replace(v.(string), "sha256:", "", 1)
}
- return diffIdsStr
+ return diffIDsStr
}
-type Node struct {
+type node struct {
parent string
- id string
- diff string
- cache string
+ id string
+ diff string
+ cache string
}
-func getNode(id string) Node {
- var node Node
+func getNode(id string) node {
+ var node node
node.id = id
- buf, err := ioutil.ReadFile(DOCKER_DB_DIR + "/layerdb/sha256/" + id + "/cache-id")
+ buf, err := ioutil.ReadFile(dockerDbDir + "/layerdb/sha256/" + id + "/cache-id")
if err != nil {
panic(err)
}
node.cache = strings.Replace(string(buf), "sha256:", "", 1)
- buf, err = ioutil.ReadFile(DOCKER_DB_DIR + "/layerdb/sha256/" + id + "/diff")
+ buf, err = ioutil.ReadFile(dockerDbDir + "/layerdb/sha256/" + id + "/diff")
if err != nil {
panic(err)
}
node.diff = strings.Replace(string(buf), "sha256:", "", 1)
- buf, err = ioutil.ReadFile(DOCKER_DB_DIR + "/layerdb/sha256/" + id + "/parent")
+ buf, err = ioutil.ReadFile(dockerDbDir + "/layerdb/sha256/" + id + "/parent")
if os.IsNotExist(err) {
node.parent = ""
} else if err != nil {
@@ -95,22 +95,22 @@ func getNode(id string) Node {
return node
}
-func readLayerInfo() map[string]Node {
- files, err := ioutil.ReadDir(DOCKER_DB_DIR + "/layerdb/sha256/")
- if err != nil {
- panic(err)
+func readLayerInfo() map[string]node {
+ files, err := ioutil.ReadDir(dockerDbDir + "/layerdb/sha256/")
+ if err != nil {
+ panic(err)
}
- nodes := make(map[string]Node)
- for i, _ := range files {
+ nodes := make(map[string]node)
+ for i := range files {
id := files[i].Name()
nodes[id] = getNode(id)
}
return nodes
}
-func searchForCacheId(diffIds []string, layers map[string]Node) string {
+func searchForCacheID(diffIDs []string, layers map[string]node) string {
candidates := make([]string, 0, len(layers))
- last := diffIds[len(diffIds)-1]
+ last := diffIDs[len(diffIDs)-1]
for i, v := range layers {
if v.diff == last {
sz := len(candidates)
@@ -122,28 +122,28 @@ func searchForCacheId(diffIds []string, layers map[string]Node) string {
for i, v := range candidates {
candidatesCurrent[i] = layers[v].parent
}
- for i := len(diffIds)-2; i>=0; i-- {
+ for i := len(diffIDs) - 2; i >= 0; i-- {
for j := 0; j < len(candidates); {
- if layers[candidatesCurrent[j]].diff == diffIds[i] {
+ if layers[candidatesCurrent[j]].diff == diffIDs[i] {
candidatesCurrent[j] = layers[candidatesCurrent[j]].parent
- j++;
+ j++
} else {
oldLen := len(candidates)
candidates[j] = candidates[oldLen-1]
candidatesCurrent[j] = candidatesCurrent[oldLen-1]
candidates = candidates[:oldLen-1]
- candidatesCurrent = candidatesCurrent[:oldLen - 1]
+ candidatesCurrent = candidatesCurrent[:oldLen-1]
}
}
}
- return candidates[0]
+ return layers[candidates[0]].cache
}
func main() {
image, tag := getImageTag()
- imgId := getImageId(image, tag)
- diffIds := getDiffIds(imgId)
+ imgID := getImageID(image, tag)
+ diffIDs := getDiffIDs(imgID)
layers := readLayerInfo()
- cacheId := searchForCacheId(diffIds, layers)
- fmt.Print(cacheId)
+ cacheID := searchForCacheID(diffIDs, layers)
+ fmt.Print(cacheID)
}