本文整理汇总了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")
}
}
示例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))
}
}
}
示例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)
}
}
示例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)
}
示例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
}
示例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)
}
}
示例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)
}
}
示例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)
}
}
示例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))
}
}
}
示例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")
}
}
示例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)
}
}
示例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))
}
示例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)
}
}
示例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
}
示例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()
}