func (fs *FileDiffStore) createRadixPath(baseDir, fileName string) (string, error) { targetPath := fileutil.TwoLevelRadixPath(baseDir, fileName) radixDir, _ := filepath.Split(targetPath) if err := os.MkdirAll(radixDir, 0700); err != nil { return "", err } return targetPath, nil }
func TestPurgeDigests(t *testing.T) { fds := getTestFileDiffStore(t, TESTDATA_DIR, true) _, err := fds.Get(TEST_DIGEST1, []string{TEST_DIGEST2, TEST_DIGEST3}) assert.Nil(t, err) _, err = fds.Get(TEST_DIGEST2, []string{TEST_DIGEST1, TEST_DIGEST3}) assert.Nil(t, err) d_12 := getDiffBasename(TEST_DIGEST1, TEST_DIGEST2) d_13 := getDiffBasename(TEST_DIGEST1, TEST_DIGEST3) d_23 := getDiffBasename(TEST_DIGEST2, TEST_DIGEST3) awaitDiffMetricsGoroutine(fds.getDiffMetricPath(d_12), 3*time.Second) awaitDiffMetricsGoroutine(fds.getDiffMetricPath(d_13), 3*time.Second) awaitDiffMetricsGoroutine(fds.getDiffMetricPath(d_23), 3*time.Second) assertFileExists(fds.getDiffMetricPath(d_12), t) assertFileExists(fds.getDiffMetricPath(d_13), t) assertFileExists(fds.getDiffMetricPath(d_23), t) assert.Nil(t, fds.PurgeDigests([]string{TEST_DIGEST1}, false)) // Removed from image cache assertFileNotExists(fileutil.TwoLevelRadixPath(fds.localImgDir, fds.getImageBaseName(TEST_DIGEST1)), t) assertFileExists(fileutil.TwoLevelRadixPath(fds.localImgDir, fds.getImageBaseName(TEST_DIGEST2)), t) assertFileExists(fileutil.TwoLevelRadixPath(fds.localImgDir, fds.getImageBaseName(TEST_DIGEST3)), t) // Removed from diffMetrics caches assertFileNotExists(fds.getDiffMetricPath(d_12), t) assertFileNotExists(fds.getDiffMetricPath(d_13), t) assertFileExists(fds.getDiffMetricPath(d_23), t) _, ok := fds.diffCache.Get(d_12) assert.False(t, ok) _, ok = fds.diffCache.Get(d_13) assert.False(t, ok) _, ok = fds.diffCache.Get(d_23) assert.True(t, ok) // Removed from cache _, ok = fds.imageCache.Get(TEST_DIGEST1) assert.False(t, ok) _, ok = fds.imageCache.Get(TEST_DIGEST2) assert.True(t, ok) _, ok = fds.imageCache.Get(TEST_DIGEST3) assert.True(t, ok) }
func main() { defer common.LogPanic() common.Init() fileInfos, err := ioutil.ReadDir(".") if err != nil { glog.Fatalf("Unable to read directory.") } // Get the directory for _, info := range fileInfos { if info.IsDir() { continue } fileName := info.Name() outFileName := fileutil.TwoLevelRadixPath(fileName) if fileName == outFileName { glog.Infof("Excluding %s -> %s", fileName, outFileName) continue } if !fileutil.FileExists(outFileName) { // Create the path if it doesn't exist. targetDir, _ := filepath.Split(outFileName) if err = os.MkdirAll(targetDir, 0700); err != nil { glog.Errorf("Unable to run create path: %s", targetDir) } if err := os.Rename(fileName, outFileName); err != nil { glog.Errorf("Unable to run mv %s %s", fileName, outFileName) } } } }
func NewURLAwareFileServer(baseDir, baseUrl string) *URLAwareFileServer { absPath, err := filepath.Abs(baseDir) if err != nil { glog.Fatalf("Unable to get abs path of %s. Got error: %s", baseDir, err) } fileHandler := http.StripPrefix(baseUrl, http.FileServer(http.Dir(absPath))) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // TODO(stephana): Expose a handler in the diff store so we don't have // to mangle the URL from the outside. r.URL.Path = fileutil.TwoLevelRadixPath(r.URL.Path) // Cache images for 12 hours. w.Header().Set("Cache-control", "public, max-age=43200") fileHandler.ServeHTTP(w, r) }) return &URLAwareFileServer{ baseDir: absPath, baseUrl: baseUrl, Handler: handler, } }
// getDiffMetricPath returns the filename where the diffmetric should be // cached. func (fs *FileDiffStore) getDiffMetricPath(baseName string) string { return fileutil.TwoLevelRadixPath(fs.localDiffMetricsDir, fmt.Sprintf("%s.%s", baseName, DIFFMETRICS_EXTENSION)) }
// getDigestPath returns the filepath where the image corresponding to the // give digests should be stored. func (fs *FileDiffStore) getDigestImagePath(digest string) string { return fileutil.TwoLevelRadixPath(fs.localImgDir, fmt.Sprintf("%s.%s", digest, IMG_EXTENSION)) }