本文整理汇总了Golang中os.Link函数的典型用法代码示例。如果您正苦于以下问题:Golang Link函数的具体用法?Golang Link怎么用?Golang Link使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Link函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateSnapshot
// CreateSnapshot will create hardlinks for all tsm and tombstone files
// in the path provided
func (f *FileStore) CreateSnapshot() (string, error) {
f.traceLogger.Printf("Creating snapshot in %s", f.dir)
files := f.Files()
f.mu.Lock()
f.currentTempDirID += 1
f.mu.Unlock()
f.mu.RLock()
defer f.mu.RUnlock()
// get a tmp directory name
tmpPath := fmt.Sprintf("%s/%d.tmp", f.dir, f.currentTempDirID)
err := os.Mkdir(tmpPath, 0777)
if err != nil {
return "", err
}
for _, tsmf := range files {
newpath := filepath.Join(tmpPath, filepath.Base(tsmf.Path()))
if err := os.Link(tsmf.Path(), newpath); err != nil {
return "", fmt.Errorf("error creating tsm hard link: %q", err)
}
// Check for tombstones and link those as well
for _, tf := range tsmf.TombstoneFiles() {
newpath := filepath.Join(tmpPath, filepath.Base(tf.Path))
if err := os.Link(tf.Path, newpath); err != nil {
return "", fmt.Errorf("error creating tombstone hard link: %q", err)
}
}
}
return tmpPath, nil
}
示例2: forceLink
func forceLink(oldname, newname string) error {
err := os.Link(oldname, newname)
if err == nil {
return nil
}
if os.IsPermission(err) {
// Blindly attempt to remove immutable attributes.
MakeMutable(oldname, newname)
}
return os.Link(oldname, newname)
}
示例3: 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
}
示例4: ChecksumForGraphID
func (c *checksums) ChecksumForGraphID(id, parent, oldTarDataPath, newTarDataPath string) (diffID layer.DiffID, size int64, err error) {
defer func() {
if err != nil {
logrus.Debugf("could not get checksum for %q with tar-split: %q. Attempting fallback.", id, err)
diffID, size, err = c.checksumForGraphIDNoTarsplit(id, parent, newTarDataPath)
}
}()
if oldTarDataPath == "" {
err = errors.New("no tar-split file")
return
}
tarDataFile, err := os.Open(oldTarDataPath)
if err != nil {
return
}
defer tarDataFile.Close()
uncompressed, err := gzip.NewReader(tarDataFile)
if err != nil {
return
}
dgst := digest.Canonical.New()
err = c.assembleTarTo(id, uncompressed, &size, dgst.Hash())
if err != nil {
return
}
diffID = layer.DiffID(dgst.Digest())
os.RemoveAll(newTarDataPath)
err = os.Link(oldTarDataPath, newTarDataPath)
return
}
示例5: TestLinkExisting
// Deal correctly with hard links implied by matching client inode
// numbers.
func TestLinkExisting(t *testing.T) {
tc := NewTestCase(t)
defer tc.Cleanup()
c := RandomData(5)
err := ioutil.WriteFile(tc.orig+"/file1", c, 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
err = os.Link(tc.orig+"/file1", tc.orig+"/file2")
if err != nil {
t.Fatalf("Link failed: %v", err)
}
var s1, s2 syscall.Stat_t
err = syscall.Lstat(tc.mnt+"/file1", &s1)
if err != nil {
t.Fatalf("Lstat failed: %v", err)
}
err = syscall.Lstat(tc.mnt+"/file2", &s2)
if err != nil {
t.Fatalf("Lstat failed: %v", err)
}
if s1.Ino != s2.Ino {
t.Errorf("linked files should have identical inodes %v %v", s1.Ino, s2.Ino)
}
back, err := ioutil.ReadFile(tc.mnt + "/file1")
if err != nil {
t.Fatalf("ReadFile failed: %v", err)
}
CompareSlices(t, back, c)
}
示例6: SaveConfig
func (config *Config) SaveConfig(path string) error {
path = os.ExpandEnv(path)
os.Remove(path + ".new")
os.Mkdir(filepath.Dir(path), 0700)
file, err := os.Create(path + ".new")
if err != nil {
return err
}
defer file.Close()
defer os.Remove(path + ".new")
data, err := yaml.Marshal(config)
if err != nil {
return err
}
if _, err := file.Write(data); err != nil {
return err
}
os.Remove(path + ".bak")
if err := os.Link(path, path+".bak"); err != nil && !os.IsNotExist(err) {
return err
}
file.Close()
os.Remove(path)
return os.Rename(path+".new", path)
}
示例7: 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()
}
示例8: TestLinkExisting
// Deal correctly with hard links implied by matching client inode
// numbers.
func TestLinkExisting(t *testing.T) {
me := NewTestCase(t)
defer me.Cleanup()
c := "hello"
err := ioutil.WriteFile(me.orig+"/file1", []byte(c), 0644)
CheckSuccess(err)
err = os.Link(me.orig+"/file1", me.orig+"/file2")
CheckSuccess(err)
f1, err := os.Lstat(me.mnt + "/file1")
CheckSuccess(err)
f2, err := os.Lstat(me.mnt + "/file2")
CheckSuccess(err)
if f1.Ino != f2.Ino {
t.Errorf("linked files should have identical inodes %v %v", f1.Ino, f2.Ino)
}
c1, err := ioutil.ReadFile(me.mnt + "/file1")
CheckSuccess(err)
if string(c1) != c {
t.Errorf("Content mismatch relative to original.")
}
}
示例9: copyFile
func copyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return err
}
if !sfi.Mode().IsRegular() {
return fmt.Errorf("non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return nil
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return nil
}
}
if err = os.Link(src, dst); err == nil {
return err
}
return copyFileContents(src, dst)
}
示例10: copyFile
func copyFile(source string, target string) {
source_info, err := os.Stat(source)
if err != nil {
log.Println(err)
return
}
os.MkdirAll(filepath.Dir(target), 0666)
target_info, err := os.Stat(target)
if err != nil {
os.Link(source, target)
} else if !os.SameFile(source_info, target_info) {
unsetReadonly(target)
os.Remove(target)
os.Link(source, target)
}
}
示例11: TestMemUnionFsLink
func TestMemUnionFsLink(t *testing.T) {
wd, _, clean := setupMemUfs(t)
defer clean()
content := "blabla"
fn := wd + "/ro/file"
err := ioutil.WriteFile(fn, []byte(content), 0666)
CheckSuccess(err)
err = os.Link(wd+"/mnt/file", wd+"/mnt/linked")
CheckSuccess(err)
var st2 syscall.Stat_t
err = syscall.Lstat(wd+"/mnt/linked", &st2)
CheckSuccess(err)
var st1 syscall.Stat_t
err = syscall.Lstat(wd+"/mnt/file", &st1)
CheckSuccess(err)
if st1.Ino != st2.Ino {
t.Errorf("inode numbers should be equal for linked files %v, %v", st1.Ino, st2.Ino)
}
c, err := ioutil.ReadFile(wd + "/mnt/linked")
if string(c) != content {
t.Errorf("content mismatch got %q want %q", string(c), content)
}
}
示例12: Exec
func (ln *SomeLn) Exec(inPipe io.Reader, outPipe io.Writer, errPipe io.Writer) error {
if ln.IsSymbolic {
return os.Symlink(ln.target, ln.linkName)
} else {
return os.Link(ln.target, ln.linkName)
}
}
示例13: TestLinkExisting
// Deal correctly with hard links implied by matching client inode
// numbers.
func TestLinkExisting(t *testing.T) {
tc := NewTestCase(t)
defer tc.Cleanup()
c := "hello"
err := ioutil.WriteFile(tc.orig+"/file1", []byte(c), 0644)
CheckSuccess(err)
err = os.Link(tc.orig+"/file1", tc.orig+"/file2")
CheckSuccess(err)
var s1, s2 syscall.Stat_t
err = syscall.Lstat(tc.mnt+"/file1", &s1)
CheckSuccess(err)
err = syscall.Lstat(tc.mnt+"/file2", &s2)
CheckSuccess(err)
if s1.Ino != s2.Ino {
t.Errorf("linked files should have identical inodes %v %v", s1.Ino, s2.Ino)
}
c1, err := ioutil.ReadFile(tc.mnt + "/file1")
CheckSuccess(err)
if string(c1) != c {
t.Errorf("Content mismatch relative to original.")
}
}
示例14: 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,
}
}
}
}
}
示例15: testLink
func (me *testCase) testLink() {
me.tester.Log("Testing hard links.")
me.writeOrigFile()
err := os.Mkdir(me.origSubdir, 0777)
CheckSuccess(err)
// Link.
err = os.Link(me.mountFile, me.mountSubfile)
CheckSuccess(err)
fi, err := os.Lstat(me.mountFile)
if fi.Nlink != 2 {
me.tester.Errorf("Expect 2 links: %v", fi)
}
f, err := os.Open(me.mountSubfile)
var buf [1024]byte
slice := buf[:]
n, err := f.Read(slice)
f.Close()
strContents := string(slice[:n])
if strContents != contents {
me.tester.Errorf("Content error: %v", slice[:n])
}
me.removeMountSubdir()
me.removeMountFile()
}