Beispiel #1
0
// Open requires a project root argument
func FileDbOpen(rootdir string) (DB, error) {

	redodir := filepath.Join(rootdir, REDO_DIR)

	if exists, err := fileutils.DirExists(redodir); err != nil {
		return nil, err
	} else if !exists {
		return nil, fmt.Errorf("FileDb redo directory [%s] not found. Forget to call redo-init?", redodir)
	}

	datadir := filepath.Join(redodir, DATA_DIR)
	if err := os.Mkdir(datadir, DIR_PERM); err != nil && !os.IsExist(err) {
		return nil, fmt.Errorf("FileDb cannot make data directory [%s]. %s", datadir, err)
	}

	return &FileDb{DataDir: datadir}, nil
}
Beispiel #2
0
// NewFile creates and returns a File instance for the given path.
// If the path is not fully qualified, it is made relative to dir.
// The newly created instance is initialized with the database specified by
// the configuration file found in its root directory or the default database.
// If a file does not have a root directory, it is initialized with a NullDb
// and HasNullDb will return true.
func NewFile(dir, path string) (f *File, err error) {

	if path == "" {
		return nil, errors.New("target path cannot be empty")
	}

	var targetPath string

	if filepath.IsAbs(path) {
		targetPath = path
	} else {
		targetPath = filepath.Clean(filepath.Join(dir, path))
	}

	if isdir, err := fileutils.IsDir(targetPath); err != nil {
		return nil, err
	} else if isdir {
		return nil, fmt.Errorf("target %s is a directory", targetPath)
	}

	f = new(File)

	f.Target = path

	rootDir, filename := splitpath(targetPath)
	relPath := &RelPath{}
	relPath.Add(filename)

	hasRoot := false

	for {
		exists, err := fileutils.DirExists(filepath.Join(rootDir, REDO_DIR))
		if err != nil {
			return nil, err
		}
		if exists {
			hasRoot = true
			break
		}
		if rootDir == "/" || rootDir == "." {
			break
		}
		rootDir, filename = splitpath(rootDir)
		relPath.Add(filename)
	}

	f.RootDir = rootDir

	f.Path = relPath.Join()

	f.PathHash = MakeHash(f.Path)

	f.Debug("@Hash %s: %s -> %s\n", f.RootDir, f.Path, f.PathHash)

	f.Dir, f.Name = splitpath(f.Fullpath())

	if hasRoot {
		// TODO(gsam): Read config file in rootDir to determine DB type, if any.
		// Default to FileDB if not specified.
		// f.Config = Config{DBType: "file"}
		f.db, err = FileDbOpen(f.RootDir)
		if err != nil {
			return nil, err
		}
	} else {
		f.Config = Config{DBType: "null"}
		f.db, err = NullDbOpen("")
		if err != nil {
			return nil, err
		}
		f.Debug("@NullDb\n")
	}

	return
}