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


Golang os.SameFile函数代码示例

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


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

示例1: TestSameWindowsFile

func TestSameWindowsFile(t *testing.T) {
	temp, err := ioutil.TempDir("", "TestSameWindowsFile")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(temp)

	wd, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	err = os.Chdir(temp)
	if err != nil {
		t.Fatal(err)
	}
	defer os.Chdir(wd)

	f, err := os.Create("a")
	if err != nil {
		t.Fatal(err)
	}
	f.Close()

	ia1, err := os.Stat("a")
	if err != nil {
		t.Fatal(err)
	}

	path, err := filepath.Abs("a")
	if err != nil {
		t.Fatal(err)
	}
	ia2, err := os.Stat(path)
	if err != nil {
		t.Fatal(err)
	}
	if !os.SameFile(ia1, ia2) {
		t.Errorf("files should be same")
	}

	p := filepath.VolumeName(path) + filepath.Base(path)
	if err != nil {
		t.Fatal(err)
	}
	ia3, err := os.Stat(p)
	if err != nil {
		t.Fatal(err)
	}
	if !os.SameFile(ia1, ia3) {
		t.Errorf("files should be same")
	}
}
开发者ID:wheelcomplex,项目名称:go-1,代码行数:52,代码来源:os_windows_test.go

示例2: main

func main() {
	fi1, err := os.Stat("Hello.go")
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}
	fi2, err := os.Stat("World.go")
	if err != nil {
		fmt.Printf("Error: %v\n", err)
		return
	}
	fmt.Printf("%t\n", os.SameFile(fi1, fi2))
	fmt.Printf("%t\n", os.SameFile(fi1, fi1))
}
开发者ID:cwen-coder,项目名称:study-gopkg,代码行数:14,代码来源:SameFile.go

示例3: monitorFile

func (w *outWrapper) monitorFile(i int) {
	for {
		time.Sleep(time.Second * time.Duration(i))

		fs, err := os.Stat(w.p)
		if err != nil && os.IsNotExist(err) {
			if os.IsNotExist(err) {
				fmt.Fprintln(os.Stderr, "looks like output was deleted")
				w.reopen()
			} else {
				fmt.Fprintln(os.Stderr, "error doing stat on open file:", w.p)
			}

			continue
		}

		cs, err := w.f.Stat()
		if err != nil {
			fmt.Fprintln(os.Stderr, "error doing stat on open file:", w.p)
			continue
		}

		if !os.SameFile(fs, cs) {
			fmt.Fprintln(os.Stderr, "looks like output was rotated")
			w.reopen()
		}
	}
}
开发者ID:r3nat,项目名称:logstasher,代码行数:28,代码来源:main.go

示例4: TestGitAndRootDirs

func TestGitAndRootDirs(t *testing.T) {
	repo := test.NewRepo(t)
	repo.Pushd()
	defer func() {
		repo.Popd()
		repo.Cleanup()
	}()

	git, root, err := GitAndRootDirs()
	if err != nil {
		t.Fatal(err)
	}

	expected, err := os.Stat(git)
	if err != nil {
		t.Fatal(err)
	}

	actual, err := os.Stat(filepath.Join(root, ".git"))
	if err != nil {
		t.Fatal(err)
	}

	assert.True(t, os.SameFile(expected, actual))
}
开发者ID:zhaohaiyi,项目名称:git-lfs,代码行数:25,代码来源:git_test.go

示例5: shouldReopen

func shouldReopen(file *os.File, path string) bool {
	// Compare the current open file against the path to see
	// if there's been any file rotation or truncation.
	filestat, err := file.Stat()
	if err != nil {
		// Error in fstat? Odd
		fmt.Printf("error fstat'ing already open file '%s': %s\n", path, err)
		return false
	}

	pathstat, err := os.Stat(path)
	if err != nil {
		// Stat on the 'path' failed, keep the current file open
		fmt.Printf("error stat on path '%s': %s\n", path, err)
		return false
	}

	/* TODO(sissel): Check if path is a symlink and if it has changed */

	if os.SameFile(filestat, pathstat) {
		/* Same file, check for truncation */
		pos, _ := file.Seek(0, os.SEEK_CUR)
		if pos > filestat.Size() {
			// Current read position exceeds file length. File was truncated.
			fmt.Printf("File '%s' truncated.\n", path)
			return true
		}
	} else {
		// Not the same file; open the path again.
		fmt.Printf("Reopening '%s' (new inode/device)\n", path)
		return true
	}
	return false
} /* checkForRotation */
开发者ID:blt,项目名称:lumberjack,代码行数:34,代码来源:harvester.go

示例6: activate

func (group *TailGroup) activate(match string) {
	tail, ok := group.Tails[match]
	if !ok {
		fi, _ := os.Stat(match)
		for _, tail = range group.Tails {
			tfi, _ := tail.Stat()
			if os.SameFile(fi, tfi) {
				tail.Close()
				delete(group.Tails, tail.Path)
				off := tail.Offset()
				tail.SetOffset(0)
				tail.WriteOffset()
				base := path.Base(match)
				offset := path.Join(group.OffsetDir, base+".ptr")
				tail = NewTail(group.NewParser(base), group.maxBackfill, match, offset, off)
				group.Tails[match] = tail
				return
			}
		}
		base := path.Base(match)
		offset := path.Join(group.OffsetDir, base+".ptr")
		tail = NewTail(group.NewParser(base), group.maxBackfill, match, offset, 0)
		group.Tails[match] = tail

	}
}
开发者ID:srid,项目名称:dendrite,代码行数:26,代码来源:tail_group.go

示例7: refreshParent

func (n *navigation) refreshParent() {
	wd, err := os.Getwd()
	if err != nil {
		n.parent = newErrNavColumn(err)
		return
	}
	if wd == "/" {
		n.parent = newNavColumn(nil, nil)
	} else {
		all, err := n.loaddir("..")
		if err != nil {
			n.parent = newErrNavColumn(err)
			return
		}
		cwd, err := os.Stat(".")
		if err != nil {
			n.parent = newErrNavColumn(err)
			return
		}
		n.parent = newNavColumn(all, func(i int) bool {
			d, _ := os.Lstat("../" + all[i].text)
			return os.SameFile(d, cwd)
		})
	}
}
开发者ID:zhsj,项目名称:elvish,代码行数:25,代码来源:navigation.go

示例8: CopyFile

// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.
func CopyFile(src, dst string) (err error) {
	sfi, err := os.Stat(src)
	if err != nil {
		return
	}
	if !sfi.Mode().IsRegular() {
		// cannot copy non-regular files (e.g., directories,
		// symlinks, devices, etc.)
		return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
	}
	dfi, err := os.Stat(dst)
	if err != nil {
		if !os.IsNotExist(err) {
			return
		}
	} else {
		if !(dfi.Mode().IsRegular()) {
			return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
		}
		if os.SameFile(sfi, dfi) {
			return
		}
	}
	if err = os.Link(src, dst); err == nil {
		return
	}
	err = copyFileContents(src, dst)
	return
}
开发者ID:jkotrlik,项目名称:salsaflow,代码行数:32,代码来源:copy.go

示例9: DirectoryScanner

// DirectoryScanner implements filepath.WalkFunc, necessary to walk and
// register each file in the download directory before beginning the
// download. This lets us know which files are already downloaded, and
// which ones can be hardlinked.
//
// Deprecated; replaced by GetAllCurrentFiles().
func DirectoryScanner(path string, f os.FileInfo, err error) error {
	if f == nil { // Only exists if the directory doesn't exist beforehand.
		return err
	}

	if f.IsDir() {
		return err
	}

	if info, ok := FileTracker.m[f.Name()]; ok {
		// File exists.
		if !os.SameFile(info.FileInfo(), f) {
			os.Remove(path)
			err := os.Link(info.Path, path)
			if err != nil {
				log.Fatal(err)
			}
		}
	} else {
		// New file.
		closedChannel := make(chan struct{})
		close(closedChannel)

		FileTracker.m[f.Name()] = FileStatus{
			Name:     f.Name(),
			Path:     path,
			Priority: 0, // TODO(Liru): Add priority to file list when it is implemented
			Exists:   closedChannel,
		}

	}
	return err
}
开发者ID:Liru,项目名称:tumblr-downloader,代码行数:39,代码来源:walker.go

示例10: GetAllCurrentFiles

// GetAllCurrentFiles scans the download directory and parses the files inside
// for possible future linking, if a duplicate is found.
func GetAllCurrentFiles() {
	os.MkdirAll(cfg.DownloadDirectory, 0755)
	dirs, err := ioutil.ReadDir(cfg.DownloadDirectory)
	if err != nil {
		panic(err)
	}

	// TODO: Make GetAllCurrentFiles a LOT more stable. A lot could go wrong, but meh.

	for _, d := range dirs {
		if !d.IsDir() {
			continue
		}

		dir, err := os.Open(cfg.DownloadDirectory + string(os.PathSeparator) + d.Name())

		if err != nil {
			log.Fatal(err)
		}
		// fmt.Println(dir.Name())
		files, err := dir.Readdirnames(0)
		if err != nil {
			log.Fatal(err)
		}

		for _, f := range files {
			if info, ok := FileTracker.m[f]; ok {
				// File exists.

				p := dir.Name() + string(os.PathSeparator) + f

				checkFile, err := os.Stat(p)
				if err != nil {
					log.Fatal(err)
				}

				if !os.SameFile(info.FileInfo(), checkFile) {
					os.Remove(p)
					err := os.Link(info.Path, p)
					if err != nil {
						log.Fatal(err)
					}
				}
			} else {
				// New file.
				closedChannel := make(chan struct{})
				close(closedChannel)

				FileTracker.m[f] = FileStatus{
					Name:     f,
					Path:     dir.Name() + string(os.PathSeparator) + f,
					Priority: 0, // TODO(Liru): Add priority to file list when it is implemented
					Exists:   closedChannel,
				}

			}
		}

	}
}
开发者ID:Liru,项目名称:tumblr-downloader,代码行数:62,代码来源:walker.go

示例11: CopyFile

func CopyFile(srcPath string, destPath string) error {
	srcFile, err := os.Stat(srcPath)

	if err != nil {
		return err
	}

	if !srcFile.Mode().IsRegular() {
		return fmt.Errorf("Unregular source file: %s (%s)", srcFile.Name(), srcFile.Mode().String())
	}

	destFile, err := os.Stat(destPath)

	if err != nil {
		if !os.IsNotExist(err) {
			return err
		}
	} else {
		if !(destFile.Mode().IsRegular()) {
			return fmt.Errorf("Unregular source file: %s (%s)", srcFile.Name(), srcFile.Mode().String())
		}
		if os.SameFile(srcFile, destFile) {
			return nil
		}
	}

	if err := os.Link(srcPath, destPath); err == nil {
		return err
	}

	return copyFileContents(srcPath, destPath)

}
开发者ID:btfidelis,项目名称:gync,代码行数:33,代码来源:iomanager.go

示例12: pollForRotations

// pollForRotations hits the filesystem every interval looking to see if the file at `filename` path is different from what it was previously
func (t *File) pollForRotations(d time.Duration) {
	previousFile, err := t.file.Stat()
	if err != nil {
		t.errc <- err
	}

	for !t.closed {
		currentFile, err := os.Stat(t.filename)
		switch err {
		case nil:
			switch os.SameFile(currentFile, previousFile) {
			case true:
				if t.checkForTruncate() {
					if err := t.reopenFile(); err != nil {
						t.errc <- err
					}
				}
			case false:
				previousFile = currentFile
				if err := t.reopenFile(); err != nil {
					t.errc <- err
				}
			}
		default:
			// Filename doens't seem to be there (or something else weird), wait for it to re-appear
		}

		time.Sleep(d)
	}
}
开发者ID:thomaso-mirodin,项目名称:tailer,代码行数:31,代码来源:polling.go

示例13: testRename

// 重命名
func testRename() {
	fp1 := "config/config.txt"
	fp2 := "config/config1.txt"
	fp3 := "config/temp.txt"

	err := os.Rename(fp1, fp2)
	if err != nil {
		if os.IsExist(err) {
			fmt.Println("fp2 is exist", err)
		}
	}
	os.Rename(fp1, fp3)
	fmt.Println("Rename Ok!!")
	os.Rename(fp3, fp1)
	var fi1 os.FileInfo
	var fi2 os.FileInfo
	fi1, err = os.Stat(fp1)
	if err != nil {
		fmt.Println("Error : ", err)
		return
	}
	fi2, err = os.Stat(fp2)
	if err != nil {
		fmt.Println("Error : ", err)
		return
	}
	fmt.Println("os.SameFile : ", os.SameFile(fi1, fi2))
	fmt.Println("fi1 : ", fi1)
	fmt.Println("fi2 : ", fi2)
}
开发者ID:shawnpan,项目名称:learnGo,代码行数:31,代码来源:testOS.go

示例14: Copy

// Copy file (hard link)
func Copy(oldname, newname string) (err error) {

	oldfile, err := os.Stat(oldname)
	checkError(err)
	if !oldfile.Mode().IsRegular() {
		return fmt.Errorf("Copy: non-regular old file %s (%q)", oldfile.Name(), oldfile.Mode().String())
	}

	newfile, err := os.Stat(newname)
	if err != nil {
		if !os.IsNotExist(err) {
			return
		}
	} else {
		if !(newfile.Mode().IsRegular()) {
			return fmt.Errorf("Copy: non-regular new file %s (%q)", oldfile.Name(), oldfile.Mode().String())
		}
		if os.SameFile(oldfile, newfile) {
			return
		}
	}

	if err = os.Link(oldname, newname); err != nil {
		return
	}

	err = copyFileContents(oldname, newname)

	return
}
开发者ID:foysal-mamun,项目名称:files-with-go,代码行数:31,代码来源:files.go

示例15: onFileFound

func (me *fileScannerImpl) onFileFound(newFile *FileAttr) {
	// Update map[SHA256]...
	if list, ok := me.scannedFiles[newFile.SHA256]; ok {
		for _, existing := range list {
			// 1. Check file size here to avoid hash collision.
			// 2. If the two paths are the same, then skip.
			// 3. If the two paths point to the same file, then skip.
			if existing.Size != newFile.Size ||
				SamePath(existing.Path, newFile.Path) ||
				os.SameFile(existing.Details, newFile.Details) {
				return
			}
		}

		me.scannedFiles[newFile.SHA256] = append(list, newFile)
	} else {
		me.scannedFiles[newFile.SHA256] = []*FileAttr{newFile}
	}

	me.updater.Log(LOG_TRACE, "%v (%v)", newFile.Path, &newFile.SHA256)

	// Update total count.
	me.totalFiles++
	me.totalBytes += newFile.Size
}
开发者ID:toalexjin,项目名称:dedup,代码行数:25,代码来源:scan.go


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