当前位置: 首页>>代码示例>>Golang>>正文


Golang os.Lstat函数代码示例

本文整理汇总了Golang中os.Lstat函数的典型用法代码示例。如果您正苦于以下问题:Golang Lstat函数的具体用法?Golang Lstat怎么用?Golang Lstat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Lstat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: testGitFS

func testGitFS(mnt string, t *testing.T) {
	fi, err := os.Lstat(mnt + "/file")
	if err != nil {
		t.Fatalf("Lstat: %v", err)
	} else if fi.IsDir() {
		t.Fatalf("got mode %v, want file", fi.Mode())
	} else if fi.Size() != 5 {
		t.Fatalf("got size %d, want file size 5", fi.Size())
	}

	if fi, err := os.Lstat(mnt + "/dir"); err != nil {
		t.Fatalf("Lstat: %v", err)
	} else if !fi.IsDir() {
		t.Fatalf("got %v, want dir", fi)
	}

	if fi, err := os.Lstat(mnt + "/dir/subfile"); err != nil {
		t.Fatalf("Lstat: %v", err)
	} else if fi.IsDir() || fi.Size() != 5 || fi.Mode()&0x111 == 0 {
		t.Fatalf("got %v, want +x file size 5", fi)
	}

	if fi, err := os.Lstat(mnt + "/link"); err != nil {
		t.Fatalf("Lstat: %v", err)
	} else if fi.Mode()&os.ModeSymlink == 0 {
		t.Fatalf("got %v, want symlink", fi.Mode())
	}

	if content, err := ioutil.ReadFile(mnt + "/file"); err != nil {
		t.Fatalf("ReadFile: %v", err)
	} else if string(content) != "hello" {
		t.Errorf("got %q, want %q", content, "hello")
	}
}
开发者ID:hanwen,项目名称:gitfs,代码行数:34,代码来源:fs_test.go

示例2: TestUnionFsDelete

func TestUnionFsDelete(t *testing.T) {
	wd, clean := setupUfs(t)
	defer clean()

	writeToFile(wd+"/ro/file", "a")
	_, err := os.Lstat(wd + "/mnt/file")
	CheckSuccess(err)

	err = os.Remove(wd + "/mnt/file")
	CheckSuccess(err)

	_, err = os.Lstat(wd + "/mnt/file")
	if err == nil {
		t.Fatal("should have disappeared.")
	}
	delPath := wd + "/rw/" + testOpts.DeletionDirName
	names := dirNames(delPath)
	if len(names) != 1 {
		t.Fatal("Should have 1 deletion", names)
	}

	for k, _ := range names {
		c, err := ioutil.ReadFile(delPath + "/" + k)
		CheckSuccess(err)
		if string(c) != "file" {
			t.Fatal("content mismatch", string(c))
		}
	}
}
开发者ID:taruti,项目名称:go-fuse,代码行数:29,代码来源:unionfs_test.go

示例3: TestUnionFsLink

func TestUnionFsLink(t *testing.T) {
	wd, clean := setupUfs(t)
	defer clean()

	content := "blabla"
	fn := wd + "/ro/file"
	err := ioutil.WriteFile(fn, []byte(content), 0666)
	CheckSuccess(err)
	freezeRo(wd + "/ro")

	err = os.Link(wd+"/mnt/file", wd+"/mnt/linked")
	CheckSuccess(err)

	fi2, err := os.Lstat(wd + "/mnt/linked")
	CheckSuccess(err)

	fi1, err := os.Lstat(wd + "/mnt/file")
	CheckSuccess(err)
	if fi1.Ino != fi2.Ino {
		t.Errorf("inode numbers should be equal for linked files %v, %v", fi1.Ino, fi2.Ino)
	}
	c, err := ioutil.ReadFile(wd + "/mnt/linked")
	if string(c) != content {
		t.Errorf("content mismatch got %q want %q", string(c), content)
	}
}
开发者ID:taruti,项目名称:go-fuse,代码行数:26,代码来源:unionfs_test.go

示例4: DirMove

// DirMove moves src directory to this remote using server side move
// operations.
//
// Will only be called if src.Fs().Name() == f.Name()
//
// If it isn't possible then return fs.ErrorCantDirMove
//
// If destination exists then return fs.ErrorDirExists
func (f *Fs) DirMove(src fs.Fs) error {
	srcFs, ok := src.(*Fs)
	if !ok {
		fs.Debug(srcFs, "Can't move directory - not same remote type")
		return fs.ErrorCantDirMove
	}
	// Check if source exists
	sstat, err := os.Lstat(srcFs.root)
	if err != nil {
		return err
	}
	// And is a directory
	if !sstat.IsDir() {
		return fs.ErrorCantDirMove
	}

	// Check if destination exists
	_, err = os.Lstat(f.root)
	if !os.IsNotExist(err) {
		return fs.ErrorDirExists
	}

	// Do the move
	return os.Rename(srcFs.root, f.root)
}
开发者ID:pombredanne,项目名称:rclone,代码行数:33,代码来源:local.go

示例5: createFileAndFolderIfNeeded

func (rw *rollingFileWriter) createFileAndFolderIfNeeded() error {
	var err error

	if len(rw.currentDirPath) != 0 {
		err = os.MkdirAll(rw.currentDirPath, defaultDirectoryPermissions)

		if err != nil {
			return err
		}
	}

	rw.fileName = rw.self.getCurrentModifiedFileName(rw.originalFileName)
	filePath := filepath.Join(rw.currentDirPath, rw.fileName)

	// If exists
	stat, err := os.Lstat(filePath)
	if err == nil {
		rw.currentFile, err = os.OpenFile(filePath, os.O_WRONLY|os.O_APPEND, defaultFilePermissions)

		stat, err = os.Lstat(filePath)
		if err != nil {
			return err
		}

		rw.currentFileSize = stat.Size()
	} else {
		rw.currentFile, err = os.Create(filePath)
		rw.currentFileSize = 0
	}
	if err != nil {
		return err
	}

	return nil
}
开发者ID:ling-zhou,项目名称:seelog,代码行数:35,代码来源:writers_rollingfilewriter.go

示例6: TestConfigPerm

func (s *suite) TestConfigPerm(c *gc.C) {
	testing.MakeSampleJujuHome(c)

	path := gitjujutesting.HomePath(".juju")
	info, err := os.Lstat(path)
	c.Assert(err, jc.ErrorIsNil)
	oldPerm := info.Mode().Perm()
	env := `
environments:
    only:
        type: dummy
        state-server: false
        authorized-keys: i-am-a-key
`
	outfile, err := environs.WriteEnvirons("", env)
	c.Assert(err, jc.ErrorIsNil)

	info, err = os.Lstat(outfile)
	c.Assert(err, jc.ErrorIsNil)
	// Windows is not fully POSIX compliant. Normal permission
	// checking will yield unexpected results
	if runtime.GOOS != "windows" {
		c.Assert(info.Mode().Perm(), gc.Equals, os.FileMode(0600))
	}

	info, err = os.Lstat(filepath.Dir(outfile))
	c.Assert(err, jc.ErrorIsNil)
	if runtime.GOOS != "windows" {
		c.Assert(info.Mode().Perm(), gc.Equals, oldPerm)
	}

}
开发者ID:imoapps,项目名称:juju,代码行数:32,代码来源:config_test.go

示例7: TestExplicitScan

func TestExplicitScan(t *testing.T) {
	wd, clean := setup(t)
	defer clean()

	err := os.Mkdir(wd+"/store/backing1", 0755)
	CheckSuccess(err)
	os.Symlink(wd+"/ro", wd+"/store/backing1/READONLY")
	CheckSuccess(err)

	fi, _ := os.Lstat(wd + "/mnt/backing1")
	if fi != nil {
		t.Error("Should not have file:", fi)
	}

	scan := wd + "/mnt/config/" + _SCAN_CONFIG
	_, err = os.Lstat(scan)
	if err != nil {
		t.Error(".scan_config missing:", err)
	}

	err = ioutil.WriteFile(scan, []byte("something"), 0644)
	if err != nil {
		t.Error("error writing:", err)
	}

	_, err = os.Lstat(wd + "/mnt/backing1")
	if err != nil {
		t.Error("Should have workspace backing1:", err)
	}
}
开发者ID:crazy2be,项目名称:go-fuse,代码行数:30,代码来源:autounion_test.go

示例8: TestUnionFsCheckHiddenFiles

func TestUnionFsCheckHiddenFiles(t *testing.T) {
	wd, clean := setupUfs(t)
	defer clean()

	err := ioutil.WriteFile(wd+"/ro/hidden", []byte("bla"), 0644)
	if err != nil {
		t.Fatalf("WriteFile failed: %v", err)
	}
	err = ioutil.WriteFile(wd+"/ro/not_hidden", []byte("bla"), 0644)
	if err != nil {
		t.Fatalf("WriteFile failed: %v", err)
	}
	setRecursiveWritable(t, wd+"/ro", false)

	fi, _ := os.Lstat(wd + "/mnt/hidden")
	if fi != nil {
		t.Fatal("Lstat() should have failed", fi)
	}
	_, err = os.Lstat(wd + "/mnt/not_hidden")
	if err != nil {
		t.Fatalf("Lstat failed: %v", err)
	}

	names, err := Readdirnames(wd + "/mnt")
	if err != nil {
		t.Fatalf("Readdirnames failed: %v", err)
	}
	if len(names) != 1 || names[0] != "not_hidden" {
		t.Fatal("unexpected names", names)
	}
}
开发者ID:eric-autoref,项目名称:go-fuse,代码行数:31,代码来源:unionfs_test.go

示例9: TestUnionFsDelete

func TestUnionFsDelete(t *testing.T) {
	wd, clean := setupUfs(t)
	defer clean()

	WriteFile(t, wd+"/ro/file", "a")
	_, err := os.Lstat(wd + "/mnt/file")
	if err != nil {
		t.Fatalf("Lstat failed: %v", err)
	}

	err = os.Remove(wd + "/mnt/file")
	if err != nil {
		t.Fatalf("Remove failed: %v", err)
	}

	_, err = os.Lstat(wd + "/mnt/file")
	if err == nil {
		t.Fatal("should have disappeared.")
	}
	delPath := wd + "/rw/" + testOpts.DeletionDirName
	names := dirNames(t, delPath)
	if len(names) != 1 {
		t.Fatal("Should have 1 deletion", names)
	}

	for k := range names {
		c, err := ioutil.ReadFile(delPath + "/" + k)
		if err != nil {
			t.Fatalf("ReadFile failed: %v", err)
		}
		if string(c) != "file" {
			t.Fatal("content mismatch", string(c))
		}
	}
}
开发者ID:eric-autoref,项目名称:go-fuse,代码行数:35,代码来源:unionfs_test.go

示例10: TestChmod

func TestChmod(t *testing.T) {
	wd, clean := setupUfs(t)
	defer clean()

	ro_fn := wd + "/ro/file"
	m_fn := wd + "/mount/file"
	writeToFile(ro_fn, "a")
	err := os.Chmod(m_fn, 07070)
	CheckSuccess(err)

	err = os.Chown(m_fn, 0, 0)
	code := fuse.OsErrorToErrno(err)
	if code != fuse.EPERM {
		t.Error("Unexpected error code", code, err)
	}

	fi, err := os.Lstat(m_fn)
	CheckSuccess(err)
	if fi.Mode&07777 != 07272 {
		t.Errorf("Unexpected mode found: %o", fi.Mode)
	}
	_, err = os.Lstat(wd + "/rw/file")
	if err != nil {
		t.Errorf("File not promoted")
	}
}
开发者ID:klimek,项目名称:go-fuse,代码行数:26,代码来源:unionfs_test.go

示例11: TestCopyChmod

func TestCopyChmod(t *testing.T) {
	t.Log("TestCopyChmod")
	wd, clean := setupUfs(t)
	defer clean()

	contents := "hello"
	fn := wd + "/mount/y"
	err := ioutil.WriteFile(fn, []byte(contents), 0644)
	CheckSuccess(err)

	err = os.Chmod(fn, 0755)
	CheckSuccess(err)

	fi, err := os.Lstat(fn)
	CheckSuccess(err)
	if fi.Mode&0111 == 0 {
		t.Errorf("1st attr error %o", fi.Mode)
	}
	time.Sleep(entryTtl * 1.1e9)
	fi, err = os.Lstat(fn)
	CheckSuccess(err)
	if fi.Mode&0111 == 0 {
		t.Errorf("uncached attr error %o", fi.Mode)
	}
}
开发者ID:klimek,项目名称:go-fuse,代码行数:25,代码来源:unionfs_test.go

示例12: TestDailyRotationAtOpen

func TestDailyRotationAtOpen(t *testing.T) {

	path := "test_daily.log"

	stat, _ := os.Lstat(path)
	if stat != nil {
		os.Remove(path)
	}

	rotator := NewDailyRotator(path)
	rotator.WriteString("FIRST LOG")
	rotator.Close()

	now := time.Now()

	// simulate next day
	rotator = NewDailyRotator(path)
	defer rotator.Close()

	rotator.Now = time.Unix(now.Unix()+86400, 0)
	rotator.WriteString("NEXT LOG")

	stat, _ = os.Lstat(path + "." + now.Format(dateFormat))

	assert.NotNil(t, stat)

	os.Remove(path)
	os.Remove(path + "." + now.Format(dateFormat))
}
开发者ID:shizhh,项目名称:lantern,代码行数:29,代码来源:daily_rotator_test.go

示例13: TestEntryNotify

func TestEntryNotify(t *testing.T) {
	test := NewNotifyTest(t)
	defer test.Clean()

	dir := test.dir
	test.fs.sizeChan <- 42
	test.fs.existChan <- false

	fn := dir + "/dir/file"
	fi, _ := os.Lstat(fn)
	if fi != nil {
		t.Errorf("File should not exist, %#v", fi)
	}

	test.fs.existChan <- true
	fi, _ = os.Lstat(fn)
	if fi != nil {
		t.Errorf("negative entry should have been cached: %#v", fi)
	}

	code := test.pathfs.EntryNotify("dir", "file")
	if !code.Ok() {
		t.Errorf("EntryNotify returns error: %v", code)
	}

	fi, err := os.Lstat(fn)
	if err != nil {
		t.Fatalf("Lstat failed: %v", err)
	}
}
开发者ID:Richardphp,项目名称:noms,代码行数:30,代码来源:notify_linux_test.go

示例14: intRotate

func (w *FileLogWriter) intRotate() error {
	if w.file != nil {
		fmt.Fprint(w.file, FormatLogRecord(w.trailer, &LogRecord{Created: time.Now()}))
		w.file.Close()
	}

	if w.rotate {
		_, err := os.Lstat(w.filename)
		if err == nil {
			num := 1
			fname := ""
			// 如果设置按日切换,并且当前时间不等于上次文件打开时间
			if w.daily && time.Now().Day() != w.daily_opendate {
				// 获得昨天日期
				yesterday := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
				// 从1 到 999 找到一个空文件
				// 一天最多999个日志文件
				for ; err == nil && num <= 999; num++ {
					// 新文件名 = 文件名.昨日日期.num
					fname = w.filename + fmt.Sprintf(".%s.%03d", yesterday, num)
					_, err = os.Lstat(fname)
				}
			} else {
				// 如果不是按日,则使用当前时间
				for ; err == nil && num <= 999; num++ {
					fname = w.filename + fmt.Sprintf(".%s.%03d", time.Now().Format("2006-01-02"), num)
					_, err = os.Lstat(fname)
				}
			}

			// 如果err 为空,则表示 有文件不存在
			if err == nil {
				return fmt.Errorf("Rotate: Canot find free log number to rename %s\n", w.filename)
			}
			w.file.Close()

			// 文件重命名
			err = os.Rename(w.filename, fname)
			if err != nil {
				return fmt.Errorf("Rotate: %s\n", err)
			}
		}
	}

	// 重新打开文件
	fd, err := os.OpenFile(w.filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0660)
	if err != nil {
		return err
	}
	w.file = fd

	now := time.Now()
	fmt.Fprint(w.file, FormatLogRecord(w.header, &LogRecord{Created: now}))

	w.daily_opendate = now.Day()
	w.maxlines_curlines = 0
	w.maxsize_cursize = 0

	return nil
}
开发者ID:pengswift,项目名称:libonepiece,代码行数:60,代码来源:filelog.go

示例15: TryLock

// Try to get Lockfile lock. Returns nil, if successful and and error describing the reason, it didn't work out.
// Please note, that existing lockfiles containing pids of dead processes and lockfiles containing no pid at all
// are deleted.
func (l Lockfile) TryLock() error {
	name := string(l)

	// This has been checked by New already. If we trigger here,
	// the caller didn't use New and re-implemented it's functionality badly.
	// So panic, that he might find this easily during testing.
	if !filepath.IsAbs(string(name)) {
		panic(ErrNeedAbsPath)
	}

	tmplock, err := ioutil.TempFile(filepath.Dir(name), "")
	if err != nil {
		return err
	} else {
		defer tmplock.Close()
		defer os.Remove(tmplock.Name())
	}

	_, err = tmplock.WriteString(fmt.Sprintf("%d\n", os.Getpid()))
	if err != nil {
		return err
	}

	// return value intentionally ignored, as ignoring it is part of the algorithm
	_ = os.Link(tmplock.Name(), name)

	fiTmp, err := os.Lstat(tmplock.Name())
	if err != nil {
		return err
	}
	fiLock, err := os.Lstat(name)
	if err != nil {
		return err
	}

	// Success
	if os.SameFile(fiTmp, fiLock) {
		return nil
	}

	_, err = l.GetOwner()
	switch err {
	default:
		// Other errors -> defensively fail and let caller handle this
		return err
	case nil:
		return ErrBusy
	case ErrDeadOwner, ErrInvalidPid:
		// cases we can fix below
	}

	// clean stale/invalid lockfile
	err = os.Remove(name)
	if err != nil {
		return err
	}

	// now that we cleaned up the stale lockfile, let's recurse
	return l.TryLock()
}
开发者ID:mlafeldt,项目名称:remote_syslog2,代码行数:63,代码来源:lockfile.go


注:本文中的os.Lstat函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。