func DetectDataDir(dirpath string) (DataDirVersion, error) { names, err := fileutil.ReadDir(dirpath) if err != nil { if os.IsNotExist(err) { err = nil } // Error reading the directory return DataDirUnknown, err } nameSet := types.NewUnsafeSet(names...) if nameSet.Contains("member") { ver, err := DetectDataDir(path.Join(dirpath, "member")) if ver == DataDir2_0 { return DataDir2_0_1, nil } return ver, err } if nameSet.ContainsAll([]string{"snap", "wal"}) { // .../wal cannot be empty to exist. walnames, err := fileutil.ReadDir(path.Join(dirpath, "wal")) if err == nil && len(walnames) > 0 { return DataDir2_0, nil } } if nameSet.ContainsAll([]string{"proxy"}) { return DataDir2_0Proxy, nil } return DataDirUnknown, nil }
// identifyDataDirOrDie returns the type of the data dir. // Dies if the datadir is invalid. func identifyDataDirOrDie(dir string) dirType { names, err := fileutil.ReadDir(dir) if err != nil { if os.IsNotExist(err) { return dirEmpty } plog.Fatalf("error listing data dir: %s", dir) } var m, p bool for _, name := range names { switch dirType(name) { case dirMember: m = true case dirProxy: p = true default: plog.Warningf("found invalid file/dir %s under data dir %s (Ignore this if you are upgrading etcd)", name, dir) } } if m && p { plog.Fatal("invalid datadir. Both member and proxy directories exist.") } if m { return dirMember } if p { return dirProxy } return dirEmpty }
func Exist(dirpath string) bool { names, err := fileutil.ReadDir(dirpath) if err != nil { return false } return len(names) != 0 }
// openLast opens the last wal file for read and write. func openLast(dirpath string) (*os.File, error) { names, err := fileutil.ReadDir(dirpath) if err != nil { return nil, err } names = checkWalNames(names) if len(names) == 0 { return nil, ErrFileNotFound } last := path.Join(dirpath, names[len(names)-1]) return os.OpenFile(last, os.O_RDWR, 0) }
// getSnapFilePath returns the file path for the snapshot with given index. // If the snapshot does not exist, it returns error. func (ss *snapshotStore) getSnapFilePath(index uint64) (string, error) { fns, err := fileutil.ReadDir(ss.dir) if err != nil { return "", err } wfn := fmt.Sprintf("%016x.db", index) for _, fn := range fns { if fn == wfn { return path.Join(ss.dir, fn), nil } } return "", fmt.Errorf("snapshot file doesn't exist") }