-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkat.html
604 lines (433 loc) · 15.6 KB
/
linkat.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:26:13 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>LINK</title>
</head>
<body>
<h1 align="center">LINK</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#VERSIONS">VERSIONS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#BUGS">BUGS</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">link, linkat -
make a new name for a file</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<unistd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
link(const char *</b><i>oldpath</i><b>, const char
*</b><i>newpath</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>#include
<fcntl.h></b> /* Definition of AT_* constants */
<b><br>
#include <unistd.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
linkat(int</b> <i>olddirfd</i><b>, const char
*</b><i>oldpath</i><b>, <br>
int</b> <i>newdirfd</i><b>, const char
*</b><i>newpath</i><b>, int</b> <i>flags</i><b>);</b></p>
<p style="margin-left:5%; margin-top: 1em">Feature Test
Macro Requirements for glibc (see
<b>feature_test_macros</b>(7)):</p>
<p style="margin-left:11%; margin-top: 1em"><b>linkat</b>():</p>
<p style="margin-left:17%;">Since glibc 2.10:</p>
<p style="margin-left:23%;">_POSIX_C_SOURCE >= 200809L</p>
<p style="margin-left:17%;">Before glibc 2.10:</p>
<p style="margin-left:23%;">_ATFILE_SOURCE</p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>link</b>()
creates a new link (also known as a hard link) to an
existing file.</p>
<p style="margin-left:11%; margin-top: 1em">If
<i>newpath</i> exists, it will <i>not</i> be
overwritten.</p>
<p style="margin-left:11%; margin-top: 1em">This new name
may be used exactly as the old one for any operation; both
names refer to the same file (and so have the same
permissions and ownership) and it is impossible to tell
which name was the "original".</p>
<p style="margin-left:11%; margin-top: 1em"><b>linkat()</b>
<br>
The <b>linkat</b>() system call operates in exactly the same
way as <b>link</b>(), except for the differences described
here.</p>
<p style="margin-left:11%; margin-top: 1em">If the pathname
given in <i>oldpath</i> is relative, then it is interpreted
relative to the directory referred to by the file descriptor
<i>olddirfd</i> (rather than relative to the current working
directory of the calling process, as is done by
<b>link</b>() for a relative pathname).</p>
<p style="margin-left:11%; margin-top: 1em">If
<i>oldpath</i> is relative and <i>olddirfd</i> is the
special value <b>AT_FDCWD</b>, then <i>oldpath</i> is
interpreted relative to the current working directory of the
calling process (like <b>link</b>()).</p>
<p style="margin-left:11%; margin-top: 1em">If
<i>oldpath</i> is absolute, then <i>olddirfd</i> is
ignored.</p>
<p style="margin-left:11%; margin-top: 1em">The
interpretation of <i>newpath</i> is as for <i>oldpath</i>,
except that a relative pathname is interpreted relative to
the directory referred to by the file descriptor
<i>newdirfd</i>.</p>
<p style="margin-left:11%; margin-top: 1em">The following
values can be bitwise ORed in <i>flags</i>: <b><br>
AT_EMPTY_PATH</b> (since Linux 2.6.39)</p>
<p style="margin-left:22%;">If <i>oldpath</i> is an empty
string, create a link to the file referenced by
<i>olddirfd</i> (which may have been obtained using the
<b>open</b>(2) <b>O_PATH</b> flag). In this case,
<i>olddirfd</i> can refer to any type of file except a
directory. This will generally not work if the file has a
link count of zero (files created with <b>O_TMPFILE</b> and
without <b>O_EXCL</b> are an exception). The caller must
have the <b>CAP_DAC_READ_SEARCH</b> capability in order to
use this flag. This flag is Linux-specific; define
<b>_GNU_SOURCE</b> to obtain its definition.</p>
<p style="margin-left:11%;"><b>AT_SYMLINK_FOLLOW</b> (since
Linux 2.6.18)</p>
<p style="margin-left:22%;">By default, <b>linkat</b>(),
does not dereference <i>oldpath</i> if it is a symbolic link
(like <b>link</b>()). The flag <b>AT_SYMLINK_FOLLOW</b> can
be specified in <i>flags</i> to cause <i>oldpath</i> to be
dereferenced if it is a symbolic link. If procfs is mounted,
this can be used as an alternative to <b>AT_EMPTY_PATH</b>,
like this:</p>
<p style="margin-left:28%; margin-top: 1em">linkat(AT_FDCWD,
"/proc/self/fd/<fd>", newdirfd, <br>
newname, AT_SYMLINK_FOLLOW);</p>
<p style="margin-left:11%; margin-top: 1em">Before kernel
2.6.18, the <i>flags</i> argument was unused, and had to be
specified as 0.</p>
<p style="margin-left:11%; margin-top: 1em">See
<b>openat</b>(2) for an explanation of the need for
<b>linkat</b>().</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success,
zero is returned. On error, -1 is returned, and <i>errno</i>
is set appropriately.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EACCES</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">Write access to the directory
containing <i>newpath</i> is denied, or search permission is
denied for one of the directories in the path prefix of
<i>oldpath</i> or <i>newpath</i>. (See also
<b>path_resolution</b>(7).)</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EDQUOT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The user’s quota of disk blocks on the filesystem
has been exhausted.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EEXIST</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>newpath</i> already exists.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><i>oldpath</i> or <i>newpath</i> points outside your
accessible address space.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EIO</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An I/O error occurred.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ELOOP</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Too many symbolic links were encountered in resolving
<i>oldpath</i> or <i>newpath</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EMLINK</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The file referred to by <i>oldpath</i> already has the
maximum number of links to it. For example, on an
<b>ext4</b>(5) filesystem that does not employ the
<i>dir_index</i> feature, the limit on the number of hard
links to a file is 65,000; on <b>btrfs</b>(5), the limit is
65,535 links.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>ENAMETOOLONG</b></p>
<p style="margin-left:22%;"><i>oldpath</i> or
<i>newpath</i> was too long.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOENT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>A directory component in <i>oldpath</i> or
<i>newpath</i> does not exist or is a dangling symbolic
link.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOMEM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>Insufficient kernel memory was available.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOSPC</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The device containing the file has no room for the new
directory entry.</p></td></tr>
</table>
<p style="margin-left:11%;"><b>ENOTDIR</b></p>
<p style="margin-left:22%;">A component used as a directory
in <i>oldpath</i> or <i>newpath</i> is not, in fact, a
directory.</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EPERM</b></p></td>
<td width="4%"></td>
<td width="78%">
<p><i>oldpath</i> is a directory.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EPERM</b></p></td>
<td width="4%"></td>
<td width="78%">
<p>The filesystem containing <i>oldpath</i> and
<i>newpath</i> does not support the creation of hard
links.</p> </td></tr>
</table>
<p style="margin-left:11%;"><b>EPERM</b> (since Linux
3.6)</p>
<p style="margin-left:22%;">The caller does not have
permission to create a hard link to this file (see the
description of <i>/proc/sys/fs/protected_hardlinks</i> in
<b>proc</b>(5)).</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EPERM</b></p></td>
<td width="4%"></td>
<td width="78%">
<p><i>oldpath</i> is marked immutable or append-only. (See
<b>ioctl_iflags</b>(2).)</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EROFS</b></p></td>
<td width="4%"></td>
<td width="78%">
<p>The file is on a read-only filesystem.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EXDEV</b></p></td>
<td width="4%"></td>
<td width="78%">
<p><i>oldpath</i> and <i>newpath</i> are not on the same
mounted filesystem. (Linux permits a filesystem to be
mounted at multiple points, but <b>link</b>() does not work
across different mount points, even if the same filesystem
is mounted on both.)</p></td></tr>
</table>
<p style="margin-left:11%; margin-top: 1em">The following
additional errors can occur for <b>linkat</b>():</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EBADF</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em"><i>olddirfd</i> or
<i>newdirfd</i> is not a valid file descriptor.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An invalid flag value was specified in <i>flags</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOENT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p><b>AT_EMPTY_PATH</b> was specified in <i>flags</i>, but
the caller did not have the <b>CAP_DAC_READ_SEARCH</b>
capability.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ENOENT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>An attempt was made to link to the
<i>/proc/self/fd/NN</i> file corresponding to a file
descriptor created with</p></td></tr>
</table>
<p style="margin-left:22%; margin-top: 1em">open(path,
O_TMPFILE | O_EXCL, mode);</p>
<p style="margin-left:22%; margin-top: 1em">See
<b>open</b>(2).</p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>ENOENT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em"><i>oldpath</i> is a relative
pathname and <i>olddirfd</i> refers to a directory that has
been deleted, or <i>newpath</i> is a relative pathname and
<i>newdirfd</i> refers to a directory that has been
deleted.</p> </td></tr>
</table>
<p style="margin-left:11%;"><b>ENOTDIR</b></p>
<p style="margin-left:22%;"><i>oldpath</i> is relative and
<i>olddirfd</i> is a file descriptor referring to a file
other than a directory; or similar for <i>newpath</i> and
<i>newdirfd</i></p>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="7%">
<p><b>EPERM</b></p></td>
<td width="4%"></td>
<td width="78%">
<p><b>AT_EMPTY_PATH</b> was specified in <i>flags</i>,
<i>oldpath</i> is an empty string, and <i>olddirfd</i>
refers to a directory.</p></td></tr>
</table>
<h2>VERSIONS
<a name="VERSIONS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>linkat</b>()
was added to Linux in kernel 2.6.16; library support was
added to glibc in version 2.4.</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>link</b>():
SVr4, 4.3BSD, POSIX.1-2001 (but see NOTES),
POSIX.1-2008.</p>
<p style="margin-left:11%; margin-top: 1em"><b>linkat</b>():
POSIX.1-2008.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">Hard links, as
created by <b>link</b>(), cannot span filesystems. Use
<b>symlink</b>(2) if this is required.</p>
<p style="margin-left:11%; margin-top: 1em">POSIX.1-2001
says that <b>link</b>() should dereference <i>oldpath</i> if
it is a symbolic link. However, since kernel 2.0, Linux does
not do so: if <i>oldpath</i> is a symbolic link, then
<i>newpath</i> is created as a (hard) link to the same
symbolic link file (i.e., <i>newpath</i> becomes a symbolic
link to the same file that <i>oldpath</i> refers to). Some
other implementations behave in the same manner as Linux.
POSIX.1-2008 changes the specification of <b>link</b>(),
making it implementation-dependent whether or not
<i>oldpath</i> is dereferenced if it is a symbolic link. For
precise control over the treatment of symbolic links when
creating a link, use <b>linkat</b>().</p>
<p style="margin-left:11%; margin-top: 1em"><b>Glibc
notes</b> <br>
On older kernels where <b>linkat</b>() is unavailable, the
glibc wrapper function falls back to the use of
<b>link</b>(), unless the <b>AT_SYMLINK_FOLLOW</b> is
specified. When <i>oldpath</i> and <i>newpath</i> are
relative pathnames, glibc constructs pathnames based on the
symbolic links in <i>/proc/self/fd</i> that correspond to
the <i>olddirfd</i> and <i>newdirfd</i> arguments.</p>
<h2>BUGS
<a name="BUGS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On NFS
filesystems, the return code may be wrong in case the NFS
server performs the link creation and dies before it can say
so. Use <b>stat</b>(2) to find out if the link got
created.</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>ln</b>(1),
<b>open</b>(2), <b>rename</b>(2), <b>stat</b>(2),
<b>symlink</b>(2), <b>unlink</b>(2),
<b>path_resolution</b>(7), <b>symlink</b>(7)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>