xref: /haiku/src/system/libroot/posix/unistd/link.c (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
1 /*
2  * Copyright 2002-2013, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <errno.h>
8 #include <unistd.h>
9 
10 #include <errno_private.h>
11 #include <syscalls.h>
12 #include <syscall_utils.h>
13 
14 
15 ssize_t
16 readlink(const char *path, char *buffer, size_t bufferSize)
17 {
18 	return readlinkat(AT_FDCWD, path, buffer, bufferSize);
19 }
20 
21 
22 ssize_t
23 readlinkat(int fd, const char *path, char *buffer, size_t bufferSize)
24 {
25 	size_t linkLen = bufferSize;
26 	status_t status = _kern_read_link(fd, path, buffer, &linkLen);
27 	if (status < B_OK) {
28 		__set_errno(status);
29 		return -1;
30 	}
31 
32 	// If the buffer is big enough, null-terminate the string. That's not
33 	// required by the standard, but helps non-conforming apps.
34 	if (linkLen < bufferSize)
35 		buffer[linkLen] = '\0';
36 
37 	// _kern_read_link() returns the length of the link, but readlinkat() is
38 	// supposed to return the number of bytes placed into buffer. If the
39 	// buffer is larger than the link contents, then linkLen is the number
40 	// of bytes written to the buffer. Otherwise, bufferSize bytes will have
41 	// been written.
42 	return min_c(linkLen, bufferSize);
43 }
44 
45 
46 int
47 symlink(const char *toPath, const char *symlinkPath)
48 {
49 	return symlinkat(toPath, AT_FDCWD, symlinkPath);
50 }
51 
52 
53 int
54 symlinkat(const char *toPath, int fd, const char *symlinkPath)
55 {
56 	RETURN_AND_SET_ERRNO(_kern_create_symlink(fd, symlinkPath, toPath, 0));
57 }
58 
59 
60 int
61 unlink(const char *path)
62 {
63 	return unlinkat(AT_FDCWD, path, 0);
64 }
65 
66 
67 int
68 unlinkat(int fd, const char *path, int flag)
69 {
70 	if ((flag & AT_REMOVEDIR) != 0)
71 		RETURN_AND_SET_ERRNO(_kern_remove_dir(fd, path));
72 	else
73 		RETURN_AND_SET_ERRNO(_kern_unlink(fd, path));
74 }
75 
76 
77 int
78 link(const char *toPath, const char *linkPath)
79 {
80 	return linkat(AT_FDCWD, toPath, AT_FDCWD, linkPath, 0);
81 }
82 
83 
84 int
85 linkat(int toFD, const char *toPath, int linkFD, const char *linkPath, int flag)
86 {
87 	int status = _kern_create_link(linkFD, linkPath, toFD, toPath,
88 		(flag & AT_SYMLINK_FOLLOW) != 0);
89 
90 	// Haiku -> POSIX error mapping
91 	if (status == B_UNSUPPORTED)
92 		status = EPERM;
93 
94 	RETURN_AND_SET_ERRNO(status);
95 }
96