The engine core had some unfortunate bugs that were the result of some early design errors when I wasn't as familiar with channels. I've finally rewritten most of the bad parts, and I think it's much more logical and stable now. This also simplifies the resource API, since more of the work is done completely in the engine, and hidden from view. Lastly, this adds a few new metaparameters and associated code. There are still some open problems left to solve, but hopefully this brings us one step closer.
52 lines
2.0 KiB
Go
52 lines
2.0 KiB
Go
// Mgmt
|
|
// Copyright (C) 2013-2018+ James Shubin and the project contributors
|
|
// Written by James Shubin <james@shubin.ca> and the project contributors
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package graph
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
|
|
errwrap "github.com/pkg/errors"
|
|
)
|
|
|
|
// varDir returns the path to a working directory for the resource. It will try
|
|
// and create the directory first, and return an error if this failed. The dir
|
|
// should be cleaned up by the resource on Close if it wishes to discard the
|
|
// contents. If it does not, then a future resource with the same kind and name
|
|
// may see those contents in that directory. The resource should clean up the
|
|
// contents before use if it is important that nothing exist. It is always
|
|
// possible that contents could remain after an abrupt crash, so do not store
|
|
// overly sensitive data unless you're aware of the risks.
|
|
func (obj *State) varDir(extra string) (string, error) {
|
|
// Using extra adds additional dirs onto our namespace. An empty extra
|
|
// adds no additional directories.
|
|
if obj.Prefix == "" { // safety
|
|
return "", fmt.Errorf("the VarDir prefix is empty")
|
|
}
|
|
|
|
// an empty string at the end has no effect
|
|
p := fmt.Sprintf("%s/", path.Join(obj.Prefix, extra))
|
|
if err := os.MkdirAll(p, 0770); err != nil {
|
|
return "", errwrap.Wrapf(err, "can't create prefix in: %s", p)
|
|
}
|
|
|
|
// returns with a trailing slash as per the mgmt file res convention
|
|
return p, nil
|
|
}
|