blob: b7c8f14e5dbe5aa119d823071eb6467044817314 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
Martin Poolded83472002-02-18 22:44:23 +00002 * Syscall wrappers to ensure that nothing gets done in dry_run mode
3 * and to handle system peculiarities.
Wayne Davison0f78b812006-04-25 20:23:34 +00004 *
5 * Copyright (C) 1998 Andrew Tridgell
6 * Copyright (C) 2002 Martin Pool
Wayne Davisond218be32020-04-25 23:34:16 -07007 * Copyright (C) 2003-2020 Wayne Davison
Wayne Davison0f78b812006-04-25 20:23:34 +00008 *
9 * This program is free software; you can redistribute it and/or modify
Wayne Davison8e41b682007-07-10 13:55:49 +000010 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
Wayne Davison0f78b812006-04-25 20:23:34 +000013 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
Wayne Davisone7c67062006-04-25 23:51:12 +000019 * You should have received a copy of the GNU General Public License along
Wayne Davison4fd842f2007-07-07 05:33:14 +000020 * with this program; if not, visit the http://fsf.org website.
Wayne Davison0f78b812006-04-25 20:23:34 +000021 */
Andrew Tridgell31e12521998-03-23 13:25:30 +000022
23#include "rsync.h"
24
Wayne Davison4f5b0752005-02-14 00:53:43 +000025#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000026#include <sys/un.h>
27#endif
Wayne Davison11b02d92007-10-16 16:00:41 +000028#ifdef HAVE_SYS_ATTR_H
29#include <sys/attr.h>
30#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +000031
Wayne Davison28b519c2011-04-04 21:44:12 -070032#if defined HAVE_SYS_FALLOCATE && !defined HAVE_FALLOCATE
33#include <sys/syscall.h>
34#endif
35
Andrew Tridgell31e12521998-03-23 13:25:30 +000036extern int dry_run;
Wayne Davison9439c0c2007-04-24 07:32:44 +000037extern int am_root;
Wayne Davison6e310d32009-09-02 09:06:29 -070038extern int am_sender;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000039extern int read_only;
Andrew Tridgell2b086e01998-12-05 01:56:45 +000040extern int list_only;
Wayne Davisonf3873b32016-10-10 11:49:50 -070041extern int inplace;
42extern int preallocate_files;
David Dykstraf0b4fda2003-01-21 00:58:50 +000043extern int preserve_perms;
Wayne Davisone35ad792008-01-25 16:51:10 -080044extern int preserve_executability;
Wayne Davison87257f82020-04-23 14:27:44 -070045extern int open_noatime;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000046
Wayne Davisond1a1fec2016-10-15 11:13:28 -070047#ifndef S_BLKSIZE
48# if defined hpux || defined __hpux__ || defined __hpux
49# define S_BLKSIZE 1024
50# elif defined _AIX && defined _I386
51# define S_BLKSIZE 4096
52# else
53# define S_BLKSIZE 512
54# endif
55#endif
56
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000057#define RETURN_ERROR_IF(x,e) \
58 do { \
59 if (x) { \
60 errno = (e); \
61 return -1; \
62 } \
63 } while (0)
64
65#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
Andrew Tridgell31e12521998-03-23 13:25:30 +000066
Wayne Davison63cf5ae2006-01-29 18:52:53 +000067int do_unlink(const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000068{
69 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000070 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000071 return unlink(fname);
72}
73
Wayne Davison8e15bd82009-09-02 08:53:40 -070074#ifdef SUPPORT_LINKS
75int do_symlink(const char *lnk, const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000076{
77 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000078 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison6e310d32009-09-02 09:06:29 -070079
Wayne Davisone2c1e482011-06-18 10:12:47 -070080#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
Wayne Davison6e310d32009-09-02 09:06:29 -070081 /* For --fake-super, we create a normal file with mode 0600
82 * and write the lnk into it. */
83 if (am_root < 0) {
84 int ok, len = strlen(lnk);
85 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
86 if (fd < 0)
87 return -1;
88 ok = write(fd, lnk, len) == len;
89 if (close(fd) < 0)
90 ok = 0;
91 return ok ? 0 : -1;
92 }
93#endif
94
Wayne Davison8e15bd82009-09-02 08:53:40 -070095 return symlink(lnk, fname);
Andrew Tridgell31e12521998-03-23 13:25:30 +000096}
Wayne Davison6e310d32009-09-02 09:06:29 -070097
Wayne Davisona59a7b22011-06-18 13:42:30 -070098#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
Wayne Davison6e310d32009-09-02 09:06:29 -070099ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
100{
101 /* For --fake-super, we read the link from the file. */
102 if (am_root < 0) {
Wayne Davison953feea2011-09-19 09:18:18 -0700103 int fd = do_open_nofollow(path, O_RDONLY);
Wayne Davison6e310d32009-09-02 09:06:29 -0700104 if (fd >= 0) {
105 int len = read(fd, buf, bufsiz);
106 close(fd);
107 return len;
108 }
109 if (errno != ELOOP)
110 return -1;
111 /* A real symlink needs to be turned into a fake one on the receiving
112 * side, so tell the generator that the link has no length. */
113 if (!am_sender)
114 return 0;
115 /* Otherwise fall through and let the sender report the real length. */
116 }
117
118 return readlink(path, buf, bufsiz);
119}
120#endif
Wayne Davison8e15bd82009-09-02 08:53:40 -0700121#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000122
Wayne Davison4f5b0752005-02-14 00:53:43 +0000123#ifdef HAVE_LINK
Wayne Davison3d29fa92020-06-13 11:24:30 -0700124int do_link(const char *old_path, const char *new_path)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000125{
126 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000127 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison3d29fa92020-06-13 11:24:30 -0700128 return link(old_path, new_path);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000129}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000130#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000131
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000132int do_lchown(const char *path, uid_t owner, gid_t group)
133{
134 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000135 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000136#ifndef HAVE_LCHOWN
Wayne Davison05154762005-01-03 21:03:33 +0000137#define lchown chown
138#endif
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000139 return lchown(path, owner, group);
140}
141
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000142int do_mknod(const char *pathname, mode_t mode, dev_t dev)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000143{
144 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000145 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison9439c0c2007-04-24 07:32:44 +0000146
147 /* For --fake-super, we create a normal file with mode 0600. */
148 if (am_root < 0) {
149 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
150 if (fd < 0 || close(fd) < 0)
151 return -1;
152 return 0;
153 }
154
Wayne Davison4f5b0752005-02-14 00:53:43 +0000155#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000156 if (S_ISFIFO(mode))
157 return mkfifo(pathname, mode);
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000158#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000159#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000160 if (S_ISSOCK(mode)) {
161 int sock;
162 struct sockaddr_un saddr;
Wayne Davison63f91972013-10-27 10:12:53 -0700163 unsigned int len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
164 if (len >= sizeof saddr.sun_path) {
165 errno = ENAMETOOLONG;
166 return -1;
167 }
Wayne Davison4f375592006-11-21 08:37:06 +0000168#ifdef HAVE_SOCKADDR_UN_LEN
Wayne Davison63f91972013-10-27 10:12:53 -0700169 saddr.sun_len = len + 1;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000170#endif
Wayne Davison4f375592006-11-21 08:37:06 +0000171 saddr.sun_family = AF_UNIX;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000172
173 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
Wayne Davisone63ff702020-06-13 18:19:12 -0700174 || (unlink(pathname) < 0 && errno != ENOENT)
175 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000176 return -1;
177 close(sock);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000178#ifdef HAVE_CHMOD
Wayne Davison4fd842f2007-07-07 05:33:14 +0000179 return do_chmod(pathname, mode);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000180#else
181 return 0;
182#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000183 }
184#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000185#ifdef HAVE_MKNOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000186 return mknod(pathname, mode, dev);
187#else
188 return -1;
189#endif
190}
Andrew Tridgell31e12521998-03-23 13:25:30 +0000191
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000192int do_rmdir(const char *pathname)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000193{
194 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000195 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000196 return rmdir(pathname);
197}
198
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000199int do_open(const char *pathname, int flags, mode_t mode)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000200{
David Dykstra3420c8e1999-11-04 15:43:38 +0000201 if (flags != O_RDONLY) {
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000202 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000203 RETURN_ERROR_IF_RO_OR_LO;
David Dykstra3420c8e1999-11-04 15:43:38 +0000204 }
Andrew Tridgellb0f3f572000-01-23 03:00:27 +0000205
Wayne Davisonb9367412020-04-23 13:17:41 -0700206#ifdef O_NOATIME
Wayne Davison87257f82020-04-23 14:27:44 -0700207 if (open_noatime)
Wayne Davisonb9367412020-04-23 13:17:41 -0700208 flags |= O_NOATIME;
209#endif
210
Wayne Davisone106de42003-03-30 23:00:51 +0000211 return open(pathname, flags | O_BINARY, mode);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000212}
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000213
Wayne Davison4f5b0752005-02-14 00:53:43 +0000214#ifdef HAVE_CHMOD
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000215int do_chmod(const char *path, mode_t mode)
216{
David Dykstraf0b4fda2003-01-21 00:58:50 +0000217 int code;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000218 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000219 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisond11f5c62005-07-27 23:30:53 +0000220#ifdef HAVE_LCHMOD
Wayne Davison42562642009-12-31 14:10:38 -0800221 code = lchmod(path, mode & CHMOD_BITS);
222#else
223 if (S_ISLNK(mode)) {
224# if defined HAVE_SETATTRLIST
Wayne Davison11b02d92007-10-16 16:00:41 +0000225 struct attrlist attrList;
226 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
227
228 memset(&attrList, 0, sizeof attrList);
229 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
230 attrList.commonattr = ATTR_CMN_ACCESSMASK;
231 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
Wayne Davison42562642009-12-31 14:10:38 -0800232# else
Wayne Davisond11f5c62005-07-27 23:30:53 +0000233 code = 1;
Wayne Davison42562642009-12-31 14:10:38 -0800234# endif
Wayne Davisond11f5c62005-07-27 23:30:53 +0000235 } else
Wayne Davisonaa0e6b92008-03-17 07:34:22 -0700236 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
Wayne Davison42562642009-12-31 14:10:38 -0800237#endif /* !HAVE_LCHMOD */
Wayne Davisone35ad792008-01-25 16:51:10 -0800238 if (code != 0 && (preserve_perms || preserve_executability))
Wayne Davisonff0e1582008-03-09 19:50:51 -0700239 return code;
David Dykstraf0b4fda2003-01-21 00:58:50 +0000240 return 0;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000241}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000242#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000243
Wayne Davison3d29fa92020-06-13 11:24:30 -0700244int do_rename(const char *old_path, const char *new_path)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000245{
246 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000247 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison3d29fa92020-06-13 11:24:30 -0700248 return rename(old_path, new_path);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000249}
250
Wayne Davison96e051c2010-11-06 09:57:23 -0700251#ifdef HAVE_FTRUNCATE
252int do_ftruncate(int fd, OFF_T size)
253{
254 int ret;
255
256 if (dry_run) return 0;
257 RETURN_ERROR_IF_RO_OR_LO;
258
259 do {
260 ret = ftruncate(fd, size);
261 } while (ret < 0 && errno == EINTR);
262
263 return ret;
264}
265#endif
266
Martin Poolbf4e7252002-03-25 03:29:47 +0000267void trim_trailing_slashes(char *name)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000268{
Martin Poolc127e8a2002-03-25 03:51:17 +0000269 int l;
Martin Poolded83472002-02-18 22:44:23 +0000270 /* Some BSD systems cannot make a directory if the name
271 * contains a trailing slash.
272 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
Wayne Davison0f78b812006-04-25 20:23:34 +0000273
Martin Poolc127e8a2002-03-25 03:51:17 +0000274 /* Don't change empty string; and also we can't improve on
275 * "/" */
Wayne Davison0f78b812006-04-25 20:23:34 +0000276
Martin Poolc127e8a2002-03-25 03:51:17 +0000277 l = strlen(name);
278 while (l > 1) {
279 if (name[--l] != '/')
280 break;
281 name[l] = '\0';
Martin Poolbf4e7252002-03-25 03:29:47 +0000282 }
283}
284
Martin Poolbf4e7252002-03-25 03:29:47 +0000285int do_mkdir(char *fname, mode_t mode)
286{
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000287 if (dry_run) return 0;
288 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison0f78b812006-04-25 20:23:34 +0000289 trim_trailing_slashes(fname);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000290 return mkdir(fname, mode);
291}
292
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000293/* like mkstemp but forces permissions */
294int do_mkstemp(char *template, mode_t perms)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000295{
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000296 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000297 RETURN_ERROR_IF(read_only, EROFS);
Wayne Davison0379c8e2007-11-03 19:27:49 +0000298 perms |= S_IWUSR;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000299
Wayne Davison4f5b0752005-02-14 00:53:43 +0000300#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000301 {
302 int fd = mkstemp(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000303 if (fd == -1)
304 return -1;
305 if (fchmod(fd, perms) != 0 && preserve_perms) {
306 int errno_save = errno;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000307 close(fd);
308 unlink(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000309 errno = errno_save;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000310 return -1;
311 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000312#if defined HAVE_SETMODE && O_BINARY
Wayne Davison3267d6a2004-10-01 02:34:22 +0000313 setmode(fd, O_BINARY);
314#endif
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000315 return fd;
316 }
317#else
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000318 if (!mktemp(template))
319 return -1;
Martin Pool25f2cb32002-01-14 00:16:51 +0000320 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000321#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000322}
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000323
324int do_stat(const char *fname, STRUCT_STAT *st)
325{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000326#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000327 return stat64(fname, st);
328#else
329 return stat(fname, st);
330#endif
331}
332
333int do_lstat(const char *fname, STRUCT_STAT *st)
334{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000335#ifdef SUPPORT_LINKS
336# ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000337 return lstat64(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000338# else
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000339 return lstat(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000340# endif
341#else
342 return do_stat(fname, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000343#endif
344}
345
346int do_fstat(int fd, STRUCT_STAT *st)
347{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000348#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000349 return fstat64(fd, st);
350#else
351 return fstat(fd, st);
352#endif
353}
Andrew Tridgell73233f01998-05-06 06:34:18 +0000354
355OFF_T do_lseek(int fd, OFF_T offset, int whence)
356{
Wayne Davisonebd33e02005-04-06 02:08:21 +0000357#ifdef HAVE_LSEEK64
Wayne Davisonf853b772005-09-03 16:56:53 +0000358#if !SIZEOF_OFF64_T
359 OFF_T lseek64();
360#else
Andrew Tridgellf28ee651998-05-06 07:35:37 +0000361 off64_t lseek64();
Wayne Davisonf853b772005-09-03 16:56:53 +0000362#endif
Andrew Tridgell73233f01998-05-06 06:34:18 +0000363 return lseek64(fd, offset, whence);
364#else
365 return lseek(fd, offset, whence);
366#endif
367}
Wayne Davison2a189352010-08-26 10:24:37 -0700368
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700369#ifdef HAVE_SETATTRLIST
Wayne Davison1c7785a2020-04-25 21:39:11 -0700370int do_setattrlist_times(const char *fname, STRUCT_STAT *stp)
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700371{
372 struct attrlist attrList;
373 struct timespec ts;
374
375 if (dry_run) return 0;
376 RETURN_ERROR_IF_RO_OR_LO;
377
Wayne Davison1c7785a2020-04-25 21:39:11 -0700378 ts.tv_sec = stp->st_mtime;
379 ts.tv_nsec = stp->ST_MTIME_NSEC;
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700380
381 memset(&attrList, 0, sizeof attrList);
382 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
383 attrList.commonattr = ATTR_CMN_MODTIME;
384 return setattrlist(fname, &attrList, &ts, sizeof ts, FSOPT_NOFOLLOW);
385}
386#endif
387
Wayne Davison2a189352010-08-26 10:24:37 -0700388#ifdef HAVE_UTIMENSAT
Wayne Davisonb9367412020-04-23 13:17:41 -0700389int do_utimensat(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700390{
391 struct timespec t[2];
392
393 if (dry_run) return 0;
394 RETURN_ERROR_IF_RO_OR_LO;
395
Wayne Davisonb9367412020-04-23 13:17:41 -0700396 t[0].tv_sec = stp->st_atime;
397#ifdef ST_ATIME_NSEC
398 t[0].tv_nsec = stp->ST_ATIME_NSEC;
399#else
400 t[0].tv_nsec = 0;
401#endif
402 t[1].tv_sec = stp->st_mtime;
403#ifdef ST_MTIME_NSEC
404 t[1].tv_nsec = stp->ST_MTIME_NSEC;
405#else
406 t[1].tv_nsec = 0;
407#endif
Wayne Davison2a189352010-08-26 10:24:37 -0700408 return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
409}
410#endif
411
412#ifdef HAVE_LUTIMES
Wayne Davisonb9367412020-04-23 13:17:41 -0700413int do_lutimes(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700414{
415 struct timeval t[2];
416
417 if (dry_run) return 0;
418 RETURN_ERROR_IF_RO_OR_LO;
419
Wayne Davisonb9367412020-04-23 13:17:41 -0700420 t[0].tv_sec = stp->st_atime;
421#ifdef ST_ATIME_NSEC
422 t[0].tv_usec = stp->ST_ATIME_NSEC / 1000;
423#else
Wayne Davison2a189352010-08-26 10:24:37 -0700424 t[0].tv_usec = 0;
Wayne Davisonb9367412020-04-23 13:17:41 -0700425#endif
426 t[1].tv_sec = stp->st_mtime;
427#ifdef ST_MTIME_NSEC
428 t[1].tv_usec = stp->ST_MTIME_NSEC / 1000;
429#else
430 t[1].tv_usec = 0;
431#endif
Wayne Davison2a189352010-08-26 10:24:37 -0700432 return lutimes(fname, t);
433}
434#endif
435
436#ifdef HAVE_UTIMES
Wayne Davisonb9367412020-04-23 13:17:41 -0700437int do_utimes(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700438{
439 struct timeval t[2];
440
441 if (dry_run) return 0;
442 RETURN_ERROR_IF_RO_OR_LO;
443
Wayne Davisonb9367412020-04-23 13:17:41 -0700444 t[0].tv_sec = stp->st_atime;
445#ifdef ST_ATIME_NSEC
446 t[0].tv_usec = stp->ST_ATIME_NSEC / 1000;
447#else
Wayne Davison2a189352010-08-26 10:24:37 -0700448 t[0].tv_usec = 0;
Wayne Davisonb9367412020-04-23 13:17:41 -0700449#endif
450 t[1].tv_sec = stp->st_mtime;
451#ifdef ST_MTIME_NSEC
452 t[1].tv_usec = stp->ST_MTIME_NSEC / 1000;
453#else
454 t[1].tv_usec = 0;
455#endif
Wayne Davison2a189352010-08-26 10:24:37 -0700456 return utimes(fname, t);
457}
458
459#elif defined HAVE_UTIME
Wayne Davisonb9367412020-04-23 13:17:41 -0700460int do_utime(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700461{
462#ifdef HAVE_STRUCT_UTIMBUF
463 struct utimbuf tbuf;
464#else
465 time_t t[2];
466#endif
467
468 if (dry_run) return 0;
469 RETURN_ERROR_IF_RO_OR_LO;
470
471# ifdef HAVE_STRUCT_UTIMBUF
Wayne Davisonb9367412020-04-23 13:17:41 -0700472 tbuf.actime = stp->st_atime;
473 tbuf.modtime = stp->st_mtime;
Wayne Davison2a189352010-08-26 10:24:37 -0700474 return utime(fname, &tbuf);
475# else
Wayne Davisonb9367412020-04-23 13:17:41 -0700476 t[0] = stp->st_atime;
477 t[1] = stp->st_mtime;
Wayne Davison2a189352010-08-26 10:24:37 -0700478 return utime(fname, t);
479# endif
480}
481
482#else
483#error Need utimes or utime function.
484#endif
Wayne Davison28b519c2011-04-04 21:44:12 -0700485
486#ifdef SUPPORT_PREALLOCATION
Wayne Davison28b519c2011-04-04 21:44:12 -0700487#ifdef FALLOC_FL_KEEP_SIZE
488#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
489#else
490#define DO_FALLOC_OPTIONS 0
491#endif
Wayne Davisonf3873b32016-10-10 11:49:50 -0700492
493OFF_T do_fallocate(int fd, OFF_T offset, OFF_T length)
494{
Wayne Davisonc2da3802019-01-15 08:51:08 -0800495 int opts = inplace || preallocate_files ? DO_FALLOC_OPTIONS : 0;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700496 int ret;
Wayne Davison28b519c2011-04-04 21:44:12 -0700497 RETURN_ERROR_IF(dry_run, 0);
498 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700499 if (length & 1) /* make the length not match the desired length */
500 length++;
501 else
502 length--;
Wayne Davison28b519c2011-04-04 21:44:12 -0700503#if defined HAVE_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700504 ret = fallocate(fd, opts, offset, length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700505#elif defined HAVE_SYS_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700506 ret = syscall(SYS_fallocate, fd, opts, (loff_t)offset, (loff_t)length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700507#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700508 ret = posix_fallocate(fd, offset, length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700509#else
510#error Coding error in SUPPORT_PREALLOCATION logic.
511#endif
Wayne Davisonf3873b32016-10-10 11:49:50 -0700512 if (ret < 0)
513 return ret;
514 if (opts == 0) {
515 STRUCT_STAT st;
516 if (do_fstat(fd, &st) < 0)
517 return length;
Wayne Davisond1a1fec2016-10-15 11:13:28 -0700518 return st.st_blocks * S_BLKSIZE;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700519 }
520 return 0;
Wayne Davison28b519c2011-04-04 21:44:12 -0700521}
522#endif
Wayne Davison953feea2011-09-19 09:18:18 -0700523
Wayne Davisonf3873b32016-10-10 11:49:50 -0700524/* Punch a hole at pos for len bytes. The current file position must be at pos and will be
525 * changed to be at pos + len. */
Wayne Davison888ce052020-05-25 14:01:52 -0700526int do_punch_hole(int fd, UNUSED(OFF_T pos), OFF_T len)
Wayne Davisonf3873b32016-10-10 11:49:50 -0700527{
528#ifdef HAVE_FALLOCATE
529# ifdef HAVE_FALLOC_FL_PUNCH_HOLE
530 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos, len) == 0) {
531 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
532 return -1;
533 return 0;
534 }
535# endif
536# ifdef HAVE_FALLOC_FL_ZERO_RANGE
537 if (fallocate(fd, FALLOC_FL_ZERO_RANGE, pos, len) == 0) {
538 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
539 return -1;
540 return 0;
541 }
542# endif
543#endif
544 {
545 char zeros[4096];
546 memset(zeros, 0, sizeof zeros);
547 while (len > 0) {
548 int chunk = len > (int)sizeof zeros ? (int)sizeof zeros : len;
549 int wrote = write(fd, zeros, chunk);
550 if (wrote <= 0) {
551 if (wrote < 0 && errno == EINTR)
552 continue;
553 return -1;
554 }
555 len -= wrote;
556 }
557 }
558 return 0;
559}
560
Wayne Davison953feea2011-09-19 09:18:18 -0700561int do_open_nofollow(const char *pathname, int flags)
562{
563#ifndef O_NOFOLLOW
Wayne Davison79853c32011-09-20 12:54:06 -0700564 STRUCT_STAT f_st, l_st;
Wayne Davison953feea2011-09-19 09:18:18 -0700565#endif
566 int fd;
567
568 if (flags != O_RDONLY) {
569 RETURN_ERROR_IF(dry_run, 0);
570 RETURN_ERROR_IF_RO_OR_LO;
571#ifndef O_NOFOLLOW
572 /* This function doesn't support write attempts w/o O_NOFOLLOW. */
573 errno = EINVAL;
574 return -1;
575#endif
576 }
577
578#ifdef O_NOFOLLOW
579 fd = open(pathname, flags|O_NOFOLLOW);
580#else
Wayne Davisonde219102011-09-20 13:02:12 -0700581 if (do_lstat(pathname, &l_st) < 0)
582 return -1;
583 if (S_ISLNK(l_st.st_mode)) {
584 errno = ELOOP;
585 return -1;
586 }
Wayne Davison953feea2011-09-19 09:18:18 -0700587 if ((fd = open(pathname, flags)) < 0)
588 return fd;
Wayne Davison953feea2011-09-19 09:18:18 -0700589 if (do_fstat(fd, &f_st) < 0) {
590 close_and_return_error:
591 {
592 int save_errno = errno;
593 close(fd);
594 errno = save_errno;
595 }
596 return -1;
597 }
Wayne Davison953feea2011-09-19 09:18:18 -0700598 if (l_st.st_dev != f_st.st_dev || l_st.st_ino != f_st.st_ino) {
599 errno = EINVAL;
600 goto close_and_return_error;
601 }
602#endif
603
604 return fd;
605}