Example #1
0
// Create creates the file name within the file system fs
func Create(fs fs.FS, name string) (File, error) {
	f, err := fs.Create(name)
	if err != nil {
		return nil, err
	}
	return &file{name: name, file: f}, nil
}
Example #2
0
// Open opens the file name within the file system fs
func Open(fs fs.FS, name string) (File, error) {
	f, err := fs.OpenFile(name, os.O_RDWR, 0600) // perm = u+rw
	if err != nil {
		return nil, err
	}
	if _, err = f.Seek(0, os.SEEK_SET); err != nil {
		return nil, err
	}
	return &file{name: name, file: f}, nil
}
Example #3
0
// Mount opens a write-ahead log file system
func Mount(disk fs.FS) (*Disk, error) {
	dir, err := disk.Open("/")
	if err != nil {
		return nil, err
	}
	defer dir.Close()
	sfi, err := dir.Readdir(0)
	if err != nil {
		return nil, err
	}
	var m string
	for _, fi := range sfi {
		n := fi.Name()
		if !strings.HasPrefix(n, "master") {
			continue
		}
		if n > m {
			m = n
		}
	}
	d := &Disk{disk: disk}
	var mf File
	if m == "" {
		m = d.makeName()
		mf, err = Create(disk, m)
	} else {
		if d.seqno, err = parseMasterName(m); err != nil {
			return nil, errors.New("foreign files on disk: " + m)
		}
		mf, err = Open(disk, m)
	}
	if err != nil {
		return nil, err
	}
	d.master = mf
	return d, nil
}