Open file as read-write when mount points to a file

This is to allow files mapped directly, like /etc/hosts, to be writable.
Closes #40

PiperOrigin-RevId: 196155920
Change-Id: Id2027e421cef5f94a0951c3e18b398a77c285bbd
This commit is contained in:
Fabricio Voznika
2018-05-10 12:38:36 -07:00
committed by Shentubot
parent 2d3c6dc2ef
commit 5a509c47a2
2 changed files with 60 additions and 2 deletions
+11 -1
View File
@@ -98,7 +98,17 @@ func (a *attachPoint) Attach(appPath string) (p9.File, error) {
}
root := filepath.Join(a.prefix, appPath)
f, err := os.OpenFile(root, openFlags|syscall.O_RDONLY, 0)
fi, err := os.Stat(root)
if err != nil {
return nil, err
}
mode := syscall.O_RDWR
if a.conf.ROMount || fi.IsDir() {
mode = syscall.O_RDONLY
}
f, err := os.OpenFile(root, mode|openFlags, 0)
if err != nil {
return nil, fmt.Errorf("unable to open file %q, err: %v", root, err)
}
+49 -1
View File
@@ -18,6 +18,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"syscall"
"testing"
@@ -102,7 +103,7 @@ func setup(ft fileType) (string, string, error) {
return "", "", fmt.Errorf("ioutil.TempDir() failed, err: %v", err)
}
// First attach with writable configuiration to setup tree.
// First attach with writable configuration to setup tree.
a := NewAttachPoint(path, Config{})
root, err := a.Attach("/")
if err != nil {
@@ -574,3 +575,50 @@ func TestReaddir(t *testing.T) {
}
})
}
// Test that attach point can be written to when it points to a file, e.g.
// /etc/hosts.
func TestAttachFile(t *testing.T) {
conf := Config{ROMount: false}
dir, err := ioutil.TempDir("", "root-")
if err != nil {
t.Fatalf("ioutil.TempDir() failed, err: %v", err)
}
defer os.RemoveAll(dir)
path := path.Join(dir, "test")
if _, err := os.Create(path); err != nil {
t.Fatalf("os.Create(%q) failed, err: %v", path, err)
}
a := NewAttachPoint(path, conf)
root, err := a.Attach("/")
if err != nil {
t.Fatalf("Attach(%q) failed, err: %v", "/", err)
}
if _, _, _, err := root.Open(p9.ReadWrite); err != nil {
t.Fatalf("Open(ReadWrite) failed, err: %v", err)
}
defer root.Close()
b := []byte("foobar")
w, err := root.WriteAt(b, 0)
if err != nil {
t.Fatalf("Write() failed, err: %v", err)
}
if w != len(b) {
t.Fatalf("Write() was partial, got: %d, expected: %d", w, len(b))
}
rBuf := make([]byte, len(b))
r, err := root.ReadAt(rBuf, 0)
if err != nil {
t.Fatalf("ReadAt() failed, err: %v", err)
}
if r != len(rBuf) {
t.Fatalf("ReadAt() was partial, got: %d, expected: %d", r, len(rBuf))
}
if string(rBuf) != "foobar" {
t.Fatalf("ReadAt() wrong data, got: %s, expected: %s", string(rBuf), "foobar")
}
}