本文整理汇总了Golang中os.Remove函数的典型用法代码示例。如果您正苦于以下问题:Golang Remove函数的具体用法?Golang Remove怎么用?Golang Remove使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Remove函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: RemoveSnapServices
// RemoveSnapServices disables and removes service units for the applications from the snap which are services.
func RemoveSnapServices(s *snap.Info, inter interacter) error {
sysd := systemd.New(dirs.GlobalRootDir, inter)
nservices := 0
for _, app := range s.Apps {
if app.Daemon == "" {
continue
}
nservices++
serviceName := filepath.Base(app.ServiceFile())
if err := sysd.Disable(serviceName); err != nil {
return err
}
if err := os.Remove(app.ServiceFile()); err != nil && !os.IsNotExist(err) {
logger.Noticef("Failed to remove service file for %q: %v", serviceName, err)
}
if err := os.Remove(app.ServiceSocketFile()); err != nil && !os.IsNotExist(err) {
logger.Noticef("Failed to remove socket file for %q: %v", serviceName, err)
}
}
// only reload if we actually had services
if nservices > 0 {
if err := sysd.DaemonReload(); err != nil {
return err
}
}
return nil
}
示例2: Run
func (this *MemeOCR) Run(image string) (*OCRResult, error) {
imageTif := fmt.Sprintf("%s_meme.jpg", image)
outText := fmt.Sprintf("%s_meme", image)
preprocessingArgs := []string{image, "-resize", "400%", "-fill", "black", "-fuzz", "10%", "+opaque", "#FFFFFF", imageTif}
tesseractArgs := []string{"-l", "meme", imageTif, outText}
err := runProcessorCommand(GetExecPath(), preprocessingArgs)
if err != nil {
return nil, errors.New(fmt.Sprintf("Meme preprocessing command failed with error = %v", err))
}
defer os.Remove(imageTif)
err = runProcessorCommand("tesseract", tesseractArgs)
if err != nil {
return nil, errors.New(fmt.Sprintf("Meme tesseract command failed with error = %v", err))
}
defer os.Remove(outText + ".txt")
text, err := ioutil.ReadFile(outText + ".txt")
if err != nil {
return nil, err
}
result := strings.ToLower(strings.TrimSpace(string(text[:])))
return newOCRResult(this.name, result), nil
}
示例3: UnixAudienceListener
func UnixAudienceListener(sockaddr string) {
fi, err := os.Stat(sockaddr)
if err == nil {
fmode := fi.Mode()
if fmode&os.ModeType == os.ModeSocket {
o.Warn("Removing stale socket at %s", sockaddr)
os.Remove(sockaddr)
} else {
o.Fail("%s exists and is not a socket", sockaddr)
}
}
laddr, err := net.ResolveUnixAddr("unix", sockaddr)
o.MightFail(err, "Couldn't resolve audience socket address")
l, err := net.ListenUnix("unix", laddr)
o.MightFail(err, "Couldn't start audience unixsock listener")
// Fudge the permissions on the unixsock!
fi, err = os.Stat(sockaddr)
if err == nil {
os.Chmod(sockaddr, fi.Mode()|0777)
} else {
o.Warn("Couldn't fudge permission on audience socket: %s", err)
}
// make sure we clean up the unix socket when we die.
defer l.Close()
defer os.Remove(sockaddr)
AudienceListener(l)
}
示例4: makeDiskImage
func makeDiskImage() error {
log.Printf("Creating %s MB hard disk image...", B2D.DISKSIZE)
vbm("createhd", "--format", "VMDK", "--filename", B2D.DISK, "--size", B2D.DISKSIZE)
// We do the following so boot2docker vm will auto-format the disk for us
// upon first boot.
const tmp_flag_file = "format-flag.txt"
const tmp_vmdk_file = "format-flag.vmdk"
f, err := os.Create(tmp_flag_file)
if err != nil {
return err
}
err = f.Truncate(5 * 1024 * 1024) // 5MB
if err != nil {
return err
}
_, err = f.WriteString("boot2docker, please format-me\n")
if err != nil {
return err
}
err = f.Close()
if err != nil {
return err
}
vbm("convertfromraw", tmp_flag_file, tmp_vmdk_file, "--format", "VMDK")
vbm("clonehd", tmp_vmdk_file, B2D.DISK, "--existing")
vbm("closemedium", "disk", tmp_vmdk_file)
os.Remove(tmp_flag_file)
os.Remove(tmp_vmdk_file)
return nil
}
示例5: TestReadSymlinkedDirectoryToFile
// Reading a symlink to a file must fail
func TestReadSymlinkedDirectoryToFile(t *testing.T) {
var err error
var file *os.File
if file, err = os.Create("/tmp/testReadSymlinkToFile"); err != nil {
t.Fatalf("failed to create file: %s", err)
}
file.Close()
if err = os.Symlink("/tmp/testReadSymlinkToFile", "/tmp/fileLinkTest"); err != nil {
t.Errorf("failed to create symlink: %s", err)
}
var path string
if path, err = ReadSymlinkedDirectory("/tmp/fileLinkTest"); err == nil {
t.Fatalf("ReadSymlinkedDirectory on a symlink to a file should've failed")
}
if path != "" {
t.Fatalf("path should've been empty: %s", path)
}
if err = os.Remove("/tmp/testReadSymlinkToFile"); err != nil {
t.Errorf("failed to remove file: %s", err)
}
if err = os.Remove("/tmp/fileLinkTest"); err != nil {
t.Errorf("failed to remove symlink: %s", err)
}
}
示例6: TestInsertRead
func TestInsertRead(t *testing.T) {
tmp := "/tmp/tiedot_test_col"
os.Remove(tmp)
defer os.Remove(tmp)
col, err := OpenCollection(tmp)
if err != nil {
t.Fatalf("Failed to open: %v", err)
return
}
defer col.Close()
docs := [][]byte{
[]byte("abc"),
[]byte("1234")}
ids := [2]int{}
if ids[0], err = col.Insert(docs[0]); ids[0] != 0 || err != nil {
t.Fatalf("Failed to insert: %d %v", ids[0], err)
}
if ids[1], err = col.Insert(docs[1]); err != nil {
t.Fatalf("Failed to insert: %v", err)
}
if doc0 := col.Read(ids[0]); doc0 == nil || strings.TrimSpace(string(doc0)) != string(docs[0]) {
t.Fatal("Failed to read", doc0)
}
if doc1 := col.Read(ids[1]); doc1 == nil || strings.TrimSpace(string(doc1)) != string(docs[1]) {
t.Fatalf("Failed to read")
}
// it shall not panic
col.Read(col.Size)
}
示例7: run
func (m *migrateCmd) run(e *parsecli.Env) error {
c, err := parsecli.ConfigFromDir(e.Root)
if err != nil {
return err
}
c, err = m.upgradeLegacy(e, c)
if err != nil {
return err
}
localErr := parsecli.StoreConfig(e, c)
projectErr := parsecli.StoreProjectConfig(e, c)
if localErr == nil && projectErr == nil {
legacy := filepath.Join(e.Root, parsecli.LegacyConfigFile)
err := os.Remove(legacy)
if err != nil {
fmt.Fprintf(e.Err, "Could not delete: %q. Please remove this file manually.\n", legacy)
}
} else {
local := filepath.Join(e.Root, parsecli.ParseLocal)
err := os.Remove(local)
if err != nil {
fmt.Fprintf(e.Err, "Failed to clean up: %q. Please remove this file manually.\n", local)
}
project := filepath.Join(e.Root, parsecli.ParseProject)
err = os.Remove(project)
if err != nil {
fmt.Fprintf(e.Err, "Failed to clean up: %q. Please remove this file manually.\n", project)
}
}
fmt.Fprintln(e.Out, "Successfully migrated to the preferred config format.")
return nil
}
示例8: TestVerify
func TestVerify(t *testing.T) {
o := &Options{noClone: true}
db, err := CreateTemp("", "temp", ".db", o)
if err != nil {
t.Fatal(err)
}
defer func() {
name := db.Name()
err := db.Close()
if err := os.Remove(name); err != nil {
t.Error(err)
}
if err := os.Remove(o._WAL); err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
}()
t.Log(db.Name(), o._WAL)
if err := db.Verify(nil, nil); err != nil {
t.Error(err)
}
}
示例9: TestCreate
func TestCreate(t *testing.T) {
const name = "_testdata/tempname"
os.Remove(name)
o := &Options{noClone: true}
db, err := Create(name, o)
if err != nil {
t.Fatal(err)
}
defer func() {
err := db.Close()
if err := os.Remove(name); err != nil {
t.Error(err)
}
if err := os.Remove(o._WAL); err != nil {
t.Error(err)
}
if err != nil {
t.Error(err)
}
}()
if _, err = Create(name, &Options{noClone: true}); err == nil {
t.Error("unexpected success")
return
}
if _, err = Open(name, &Options{noClone: true}); err == nil {
t.Error("unexpected success")
return
}
}
示例10: Put
func (f *fileStorageWriter) Put(name string, r io.Reader, length int64) error {
if isInternalPath(name) {
return &os.PathError{
Op: "Put",
Path: name,
Err: os.ErrPermission,
}
}
fullpath := f.fullPath(name)
dir := filepath.Dir(fullpath)
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
tmpdir := filepath.Join(f.path, ".tmp")
if err := os.MkdirAll(tmpdir, 0755); err != nil {
return err
}
defer os.Remove(tmpdir)
// Write to a temporary file first, and then move (atomically).
file, err := ioutil.TempFile(tmpdir, "juju-filestorage-")
if err != nil {
return err
}
_, err = io.CopyN(file, r, length)
file.Close()
if err != nil {
os.Remove(file.Name())
return err
}
return utils.ReplaceFile(file.Name(), fullpath)
}
示例11: TestWALName
func TestWALName(t *testing.T) {
db, err := CreateTemp("", "kv-wal-name", ".test", &Options{noClone: true})
if err != nil {
t.Fatal(err)
}
defer func(n, wn string) {
if _, err := os.Stat(n); err != nil {
t.Error(err)
} else {
if err := os.Remove(n); err != nil {
t.Error(err)
}
}
if _, err := os.Stat(wn); err != nil {
t.Error(err)
} else {
if err := os.Remove(wn); err != nil {
t.Error(err)
}
}
t.Logf("%q\n%q", n, wn)
}(db.Name(), db.WALName())
if err := db.Close(); err != nil {
t.Error(err)
return
}
if n := db.WALName(); n != "" {
t.Error(n)
}
}
示例12: Display
func (eogHandler) Display(ctx *context.T, mimetype string, r io.ReadCloser) (func(), error) {
// eog cannot read from a pipe, so we have to write the file to
// the filesystem before displaying it.
defer r.Close()
tmp, err := ioutil.TempFile("", "")
if err != nil {
return nil, err
}
if _, err := io.Copy(tmp, r); err != nil {
os.Remove(tmp.Name())
return nil, err
}
tmp.Close()
cmd := exec.Command("eog", "--display", ":0", "-f", tmp.Name())
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
stop := func() {
if err := cmd.Process.Kill(); err != nil {
vlog.Errorf("Could not kill eog: %v", err)
}
cmd.Wait()
os.Remove(tmp.Name())
}
if err := cmd.Start(); err != nil {
return stop, err
}
return stop, nil
}
示例13: TestSystem
func TestSystem(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
sys := NewSystem(SystemConfig{BuildTempDir: tmpdir})
{
tmpFile1, err := sys.SmallTempFile()
if assert.NoError(t, err) {
defer os.Remove(tmpFile1.Name())
defer tmpFile1.Close()
tmpFile2, err := sys.SmallTempFile()
if assert.NoError(t, err) {
defer os.Remove(tmpFile2.Name())
defer tmpFile2.Close()
assert.NotEqual(t, tmpFile1.Name(), tmpFile2.Name())
}
}
}
{
tmpDir1, err := sys.TempDirForBuild()
if assert.NoError(t, err) {
assert.True(t, strings.HasPrefix(tmpDir1, tmpdir))
tmpDir2, err := sys.TempDirForBuild()
if assert.NoError(t, err) {
assert.True(t, strings.HasPrefix(tmpDir2, tmpdir))
assert.NotEqual(t, tmpDir1, tmpDir2)
}
}
}
}
示例14: TestCanonicalCircular
func TestCanonicalCircular(t *testing.T) {
tmp1, err := tmpfile("circular")
if err != nil {
t.Fatal(err)
}
tmp2, err := tmpfile("circular")
if err != nil {
t.Fatal(nonil(err, os.Remove(tmp1)))
}
defer removeall(tmp1, tmp2)
// Symlink tmp1 -> tmp2.
if err = nonil(os.Remove(tmp1), os.Symlink(tmp2, tmp1)); err != nil {
t.Fatal(err)
}
// Symlnik tmp2 -> tmp1.
if err = nonil(os.Remove(tmp2), os.Symlink(tmp1, tmp2)); err != nil {
t.Fatal(err)
}
if _, err = canonical(tmp1); err == nil {
t.Fatalf("want canonical(%q)!=nil", tmp1)
}
if _, ok := err.(*os.PathError); !ok {
t.Fatalf("want canonical(%q)=os.PathError; got %T", tmp1, err)
}
}
示例15: BenchmarkFirst16
func BenchmarkFirst16(b *testing.B) {
const n = 5000
g := runtime.GOMAXPROCS(0)
defer runtime.GOMAXPROCS(g)
o := &Options{noClone: true}
db, err := CreateTemp("_testdata", "temp", ".db", o)
if err != nil {
b.Fatal(err)
}
dbname := db.Name()
defer func(n string) {
db.Close()
os.Remove(n)
os.Remove(o._WAL)
}(dbname)
rng := fc()
for i := 0; i < n; i++ {
if err := db.Set(n2b(rng.Next()), n2b(rng.Next())); err != nil {
b.Fatal(err)
}
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
db.First()
}
b.StopTimer()
}