Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Side by Side Diff: vpython/filesystem/filesystem.go

Issue 2703793003: vpython: Add filesystem operation / test packages. (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | vpython/filesystem/testfs/build.go » ('j') | vpython/filesystem/testfs/build.go » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2017 The LUCI Authors. All rights reserved.
2 // Use of this source code is governed under the Apache License, Version 2.0
3 // that can be found in the LICENSE file.
4
5 package filesystem
6
7 import (
8 "os"
9 "path/filepath"
10
11 "github.com/luci/luci-go/common/errors"
12 )
13
14 // MakeDirs is a convenience wrapper around os.MkdirAll that applies a 0755
15 // mask to all created directories.
16 func MakeDirs(path string) error {
17 if err := os.MkdirAll(path, 0755); err != nil {
18 return errors.Annotate(err).Err()
19 }
20 return nil
21 }
22
23 // AbsPath is a convenience wrapper around filepath.Abs that accepts a string
24 // pointer, base, and updates it on successful resolution.
25 func AbsPath(base *string) error {
26 v, err := filepath.Abs(*base)
27 if err != nil {
28 return errors.Annotate(err).Reason("unable to resolve absolute p ath").
iannucci 2017/02/21 08:25:08 did you want to include 'base' in this Reason? or
dnj 2017/02/21 18:07:49 Just chaining it here. I am printing full stacks,
29 D("base", *base).
30 Err()
31 }
32 *base = v
33 return nil
34 }
35
36 // Touch creates a new, empty file at the specified path.
37 func Touch(path string, mode os.FileMode) error {
38 fd, err := os.OpenFile(path, (os.O_CREATE | os.O_RDWR | os.O_TRUNC), mod e)
39 if err != nil {
40 return errors.Annotate(err).Err()
41 }
42 return fd.Close()
43 }
44
45 // RemoveAll is a wrapper around os.RemoveAll which makes sure all files are
46 // writeable (recursively) prior to removing them.
47 func RemoveAll(path string) error {
48 if err := MakeUserWritable(path); err != nil {
49 return errors.Annotate(err).Reason("failed to mark user-writable ").Err()
50 }
51 if err := os.RemoveAll(path); err != nil {
iannucci 2017/02/21 08:25:08 RemoveAll is pretty short: https://golang.org/src/
dnj 2017/02/21 18:07:49 Done. This is probably not a huge gain, though, si
52 return errors.Annotate(err).Err()
53 }
54 return nil
55 }
56
57 // MakeReadOnly recursively iterates through all of the files and directories
58 // starting at path and marks them read-only.
59 func MakeReadOnly(path string, filter func(string) bool) error {
60 return recursiveChmod(path, filter, func(mode os.FileMode) os.FileMode {
61 return mode & (^os.FileMode(0222))
62 })
63 }
64
65 // MakeUserWritable recursively iterates through all of the files and
66 // directories starting at path and ensures that they are user-writable.
67 func MakeUserWritable(path string) error {
68 return recursiveChmod(path, nil, func(mode os.FileMode) os.FileMode {
69 return (mode | 0200)
70 })
71 }
72
73 func recursiveChmod(path string, filter func(string) bool, chmod func(mode os.Fi leMode) os.FileMode) error {
74 if filter == nil {
75 filter = func(string) bool { return true }
76 }
77
78 err := filepath.Walk(path, func(path string, info os.FileInfo, err error ) error {
79 if err != nil {
80 return errors.Annotate(err).Err()
81 }
82
83 mode := info.Mode()
84 if (mode.IsRegular() || mode.IsDir()) && filter(path) {
85 if newMode := chmod(mode); newMode != mode {
86 if err := os.Chmod(path, newMode); err != nil {
87 return errors.Annotate(err).Reason("fail ed to set read-only: %(path)s").
iannucci 2017/02/21 08:25:08 read-only? maybe this message needs tweaking
dnj 2017/02/21 18:07:49 Done.
88 D("path", path).
89 Err()
90 }
91 }
92 }
93 return nil
94 })
95 if err != nil {
96 return errors.Annotate(err).Err()
97 }
98 return nil
99 }
OLDNEW
« no previous file with comments | « no previous file | vpython/filesystem/testfs/build.go » ('j') | vpython/filesystem/testfs/build.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698