This file is indexed.

/usr/share/gocode/src/github.com/dchest/safefile/safefile_test.go is in golang-github-dchest-safefile-dev 0.0~git20151022.0.855e8d9-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package safefile

import (
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"
	"time"
)

func ensureFileContains(name, data string) error {
	b, err := ioutil.ReadFile(name)
	if err != nil {
		return err
	}
	if string(b) != data {
		return fmt.Errorf("wrong data in file: expected %s, got %s", data, string(b))
	}
	return nil
}

func tempFileName(count int) string {
	return filepath.Join(os.TempDir(), fmt.Sprintf("safefile-test-%d-%x", count, time.Now().UnixNano()))
}

var testData = "Hello, this is a test data"

func testInTempDir() error {
	name := tempFileName(0)
	defer os.Remove(name)
	f, err := Create(name, 0666)
	if err != nil {
		return err
	}
	if name != f.OrigName() {
		f.Close()
		return fmt.Errorf("name %q differs from OrigName: %q", name, f.OrigName())
	}
	_, err = io.WriteString(f, testData)
	if err != nil {
		f.Close()
		return err
	}
	err = f.Commit()
	if err != nil {
		f.Close()
		return err
	}
	err = f.Close()
	if err != nil {
		return err
	}
	return ensureFileContains(name, testData)
}

func TestMakeTempName(t *testing.T) {
	// Make sure temp name is random.
	m := make(map[string]bool)
	for i := 0; i < 100; i++ {
		name, err := makeTempName("/tmp", "sf")
		if err != nil {
			t.Fatal(err)
		}
		if m[name] {
			t.Fatal("repeated file name")
		}
		m[name] = true
	}
}

func TestFile(t *testing.T) {
	err := testInTempDir()
	if err != nil {
		t.Fatal(err)
	}
}

func TestWriteFile(t *testing.T) {
	name := tempFileName(1)
	err := WriteFile(name, []byte(testData), 0666)
	if err != nil {
		t.Fatal(err)
	}
	err = ensureFileContains(name, testData)
	if err != nil {
		os.Remove(name)
		t.Fatal(err)
	}
	os.Remove(name)
}

func TestAbandon(t *testing.T) {
	name := tempFileName(2)
	f, err := Create(name, 0666)
	if err != nil {
		t.Fatal(err)
	}
	err = f.Close()
	if err != nil {
		t.Fatalf("Abandon failed: %s", err)
	}
	// Make sure temporary file doesn't exist.
	_, err = os.Stat(f.Name())
	if err != nil && !os.IsNotExist(err) {
		t.Fatal(err)
	}
}

func TestDoubleCommit(t *testing.T) {
	name := tempFileName(3)
	f, err := Create(name, 0666)
	if err != nil {
		t.Fatal(err)
	}
	err = f.Commit()
	if err != nil {
		os.Remove(name)
		t.Fatalf("First commit failed: %s", err)
	}
	err = f.Commit()
	if err != ErrAlreadyCommitted {
		os.Remove(name)
		t.Fatalf("Second commit didn't fail: %s", err)
	}
	err = f.Close()
	if err != nil {
		os.Remove(name)
		t.Fatalf("Close failed: %s", err)
	}
	os.Remove(name)
}

func TestOverwriting(t *testing.T) {
	name := tempFileName(4)
	defer os.Remove(name)

	olddata := "This is old data"
	err := ioutil.WriteFile(name, []byte(olddata), 0600)
	if err != nil {
		t.Fatal(err)
	}

	newdata := "This is new data"
	err = WriteFile(name, []byte(newdata), 0600)
	if err != nil {
		t.Fatal(err)
	}

	err = ensureFileContains(name, newdata)
	if err != nil {
		t.Fatal(err)
	}
}