Skip to content

Commit dcee228

Browse files
leongrossdeadprogram
authored andcommitted
add os.Link
Signed-off-by: leongross <[email protected]>
1 parent 9d6e307 commit dcee228

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/os/file_unix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ func tempDir() string {
7474
return dir
7575
}
7676

77+
// Link creates newname as a hard link to the oldname file.
78+
// If there is an error, it will be of type *LinkError.
79+
func Link(oldname, newname string) error {
80+
e := ignoringEINTR(func() error {
81+
return syscall.Link(oldname, newname)
82+
})
83+
84+
if e != nil {
85+
return &LinkError{"link", oldname, newname, e}
86+
}
87+
return nil
88+
}
89+
7790
// Symlink creates newname as a symbolic link to oldname.
7891
// On Windows, a symlink to a non-existent oldname creates a file symlink;
7992
// if oldname is later created as a directory the symlink will not work.

src/os/os_hardlink_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//go:build !windows && !baremetal && !js && !wasi && !wasip1 && !wasm_unknown
2+
3+
// Copyright 2024 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package os_test
8+
9+
import (
10+
. "os"
11+
"syscall"
12+
"testing"
13+
)
14+
15+
func TestHardlink(t *testing.T) {
16+
defer chtmpdir(t)()
17+
from, to := "hardlinktestfrom", "hardlinktestto"
18+
19+
file, err := Create(to)
20+
if err != nil {
21+
t.Fatalf("Create(%q) failed: %v", to, err)
22+
}
23+
if err = file.Close(); err != nil {
24+
t.Errorf("Close(%q) failed: %v", to, err)
25+
}
26+
err = Link(to, from)
27+
if err != nil {
28+
t.Fatalf("Link(%q, %q) failed: %v", to, from, err)
29+
}
30+
31+
tostat, err := Lstat(to)
32+
if err != nil {
33+
t.Fatalf("Lstat(%q) failed: %v", to, err)
34+
}
35+
fromstat, err := Stat(from)
36+
if err != nil {
37+
t.Fatalf("Stat(%q) failed: %v", from, err)
38+
}
39+
if !SameFile(tostat, fromstat) {
40+
t.Errorf("Symlink(%q, %q) did not create symlink", to, from)
41+
}
42+
43+
fromstat, err = Lstat(from)
44+
if err != nil {
45+
t.Fatalf("Lstat(%q) failed: %v", from, err)
46+
}
47+
// if they have the same inode, they are hard links
48+
if fromstat.Sys().(*syscall.Stat_t).Ino != tostat.Sys().(*syscall.Stat_t).Ino {
49+
t.Fatalf("Lstat(%q).Sys().Ino = %v, Lstat(%q).Sys().Ino = %v, want the same", to, tostat.Sys().(*syscall.Stat_t).Ino, from, fromstat.Sys().(*syscall.Stat_t).Ino)
50+
}
51+
52+
file.Close()
53+
}

src/syscall/syscall_libc.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ func Rename(from, to string) (err error) {
134134
return
135135
}
136136

137+
func Link(oldname, newname string) (err error) {
138+
fromdata := cstring(oldname)
139+
todata := cstring(newname)
140+
fail := int(libc_link(&fromdata[0], &todata[0]))
141+
if fail < 0 {
142+
err = getErrno()
143+
}
144+
return
145+
}
146+
137147
func Symlink(from, to string) (err error) {
138148
fromdata := cstring(from)
139149
todata := cstring(to)
@@ -416,6 +426,11 @@ func libc_rename(from, to *byte) int32
416426
//export symlink
417427
func libc_symlink(from, to *byte) int32
418428

429+
// int link(const char *oldname, *newname);
430+
//
431+
//export link
432+
func libc_link(oldname, newname *byte) int32
433+
419434
// int fsync(int fd);
420435
//
421436
//export fsync

0 commit comments

Comments
 (0)