blob: 11d10e4a6b7dd58b1a1cbc1f5eb632f8f702f041 [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 Davison974f49e2020-07-22 12:12:18 -070057#ifdef SUPPORT_CRTIMES
58#pragma pack(push, 4)
59struct create_time {
60 uint32 length;
61 struct timespec crtime;
62};
63#pragma pack(pop)
64#endif
65
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000066#define RETURN_ERROR_IF(x,e) \
67 do { \
68 if (x) { \
69 errno = (e); \
70 return -1; \
71 } \
72 } while (0)
73
74#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
Andrew Tridgell31e12521998-03-23 13:25:30 +000075
Wayne Davison63cf5ae2006-01-29 18:52:53 +000076int do_unlink(const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000077{
78 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000079 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000080 return unlink(fname);
81}
82
Wayne Davison8e15bd82009-09-02 08:53:40 -070083#ifdef SUPPORT_LINKS
84int do_symlink(const char *lnk, const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000085{
86 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000087 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison6e310d32009-09-02 09:06:29 -070088
Wayne Davisone2c1e482011-06-18 10:12:47 -070089#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
Wayne Davison6e310d32009-09-02 09:06:29 -070090 /* For --fake-super, we create a normal file with mode 0600
91 * and write the lnk into it. */
92 if (am_root < 0) {
93 int ok, len = strlen(lnk);
94 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
95 if (fd < 0)
96 return -1;
97 ok = write(fd, lnk, len) == len;
98 if (close(fd) < 0)
99 ok = 0;
100 return ok ? 0 : -1;
101 }
102#endif
103
Wayne Davison8e15bd82009-09-02 08:53:40 -0700104 return symlink(lnk, fname);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000105}
Wayne Davison6e310d32009-09-02 09:06:29 -0700106
Wayne Davisona59a7b22011-06-18 13:42:30 -0700107#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
Wayne Davison6e310d32009-09-02 09:06:29 -0700108ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
109{
110 /* For --fake-super, we read the link from the file. */
111 if (am_root < 0) {
Wayne Davison953feea2011-09-19 09:18:18 -0700112 int fd = do_open_nofollow(path, O_RDONLY);
Wayne Davison6e310d32009-09-02 09:06:29 -0700113 if (fd >= 0) {
114 int len = read(fd, buf, bufsiz);
115 close(fd);
116 return len;
117 }
118 if (errno != ELOOP)
119 return -1;
120 /* A real symlink needs to be turned into a fake one on the receiving
121 * side, so tell the generator that the link has no length. */
122 if (!am_sender)
123 return 0;
124 /* Otherwise fall through and let the sender report the real length. */
125 }
126
127 return readlink(path, buf, bufsiz);
128}
129#endif
Wayne Davison8e15bd82009-09-02 08:53:40 -0700130#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000131
Wayne Davison5db7e4b2020-07-27 16:36:55 -0700132#if defined HAVE_LINK || defined HAVE_LINKAT
Wayne Davison3d29fa92020-06-13 11:24:30 -0700133int do_link(const char *old_path, const char *new_path)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000134{
135 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000136 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison5db7e4b2020-07-27 16:36:55 -0700137#ifdef HAVE_LINKAT
138 return linkat(AT_FDCWD, old_path, AT_FDCWD, new_path, 0);
139#else
Wayne Davison3d29fa92020-06-13 11:24:30 -0700140 return link(old_path, new_path);
Wayne Davison5db7e4b2020-07-27 16:36:55 -0700141#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000142}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000143#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000144
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000145int do_lchown(const char *path, uid_t owner, gid_t group)
146{
147 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000148 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000149#ifndef HAVE_LCHOWN
Wayne Davison05154762005-01-03 21:03:33 +0000150#define lchown chown
151#endif
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000152 return lchown(path, owner, group);
153}
154
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000155int do_mknod(const char *pathname, mode_t mode, dev_t dev)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000156{
157 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000158 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison9439c0c2007-04-24 07:32:44 +0000159
160 /* For --fake-super, we create a normal file with mode 0600. */
161 if (am_root < 0) {
162 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
163 if (fd < 0 || close(fd) < 0)
164 return -1;
165 return 0;
166 }
167
Wayne Davison4f5b0752005-02-14 00:53:43 +0000168#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000169 if (S_ISFIFO(mode))
170 return mkfifo(pathname, mode);
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000171#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000172#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000173 if (S_ISSOCK(mode)) {
174 int sock;
175 struct sockaddr_un saddr;
Wayne Davison63f91972013-10-27 10:12:53 -0700176 unsigned int len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
177 if (len >= sizeof saddr.sun_path) {
178 errno = ENAMETOOLONG;
179 return -1;
180 }
Wayne Davison4f375592006-11-21 08:37:06 +0000181#ifdef HAVE_SOCKADDR_UN_LEN
Wayne Davison63f91972013-10-27 10:12:53 -0700182 saddr.sun_len = len + 1;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000183#endif
Wayne Davison4f375592006-11-21 08:37:06 +0000184 saddr.sun_family = AF_UNIX;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000185
186 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
Wayne Davisone63ff702020-06-13 18:19:12 -0700187 || (unlink(pathname) < 0 && errno != ENOENT)
188 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000189 return -1;
190 close(sock);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000191#ifdef HAVE_CHMOD
Wayne Davison4fd842f2007-07-07 05:33:14 +0000192 return do_chmod(pathname, mode);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000193#else
194 return 0;
195#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000196 }
197#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000198#ifdef HAVE_MKNOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000199 return mknod(pathname, mode, dev);
200#else
201 return -1;
202#endif
203}
Andrew Tridgell31e12521998-03-23 13:25:30 +0000204
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000205int do_rmdir(const char *pathname)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000206{
207 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000208 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000209 return rmdir(pathname);
210}
211
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000212int do_open(const char *pathname, int flags, mode_t mode)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000213{
David Dykstra3420c8e1999-11-04 15:43:38 +0000214 if (flags != O_RDONLY) {
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000215 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000216 RETURN_ERROR_IF_RO_OR_LO;
David Dykstra3420c8e1999-11-04 15:43:38 +0000217 }
Andrew Tridgellb0f3f572000-01-23 03:00:27 +0000218
Wayne Davisonb9367412020-04-23 13:17:41 -0700219#ifdef O_NOATIME
Wayne Davison87257f82020-04-23 14:27:44 -0700220 if (open_noatime)
Wayne Davisonb9367412020-04-23 13:17:41 -0700221 flags |= O_NOATIME;
222#endif
223
Wayne Davisone106de42003-03-30 23:00:51 +0000224 return open(pathname, flags | O_BINARY, mode);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000225}
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000226
Wayne Davison4f5b0752005-02-14 00:53:43 +0000227#ifdef HAVE_CHMOD
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000228int do_chmod(const char *path, mode_t mode)
229{
Wayne Davison9dd62522020-11-29 09:33:54 -0800230 static int switch_step = 0;
David Dykstraf0b4fda2003-01-21 00:58:50 +0000231 int code;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000232 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000233 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison9dd62522020-11-29 09:33:54 -0800234 switch (switch_step) {
Wayne Davisond11f5c62005-07-27 23:30:53 +0000235#ifdef HAVE_LCHMOD
Wayne Davison9dd62522020-11-29 09:33:54 -0800236#include "case_N.h"
237 if ((code = lchmod(path, mode & CHMOD_BITS)) == 0 || errno != ENOTSUP)
238 break;
239 switch_step++;
240#endif
Wayne Davison11b02d92007-10-16 16:00:41 +0000241
Wayne Davison9dd62522020-11-29 09:33:54 -0800242#include "case_N.h"
243 if (S_ISLNK(mode)) {
244# if defined HAVE_SETATTRLIST
245 struct attrlist attrList;
246 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
247
248 memset(&attrList, 0, sizeof attrList);
249 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
250 attrList.commonattr = ATTR_CMN_ACCESSMASK;
251 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
Wayne Davison42562642009-12-31 14:10:38 -0800252# else
Wayne Davison9dd62522020-11-29 09:33:54 -0800253 code = 1;
Wayne Davison42562642009-12-31 14:10:38 -0800254# endif
Wayne Davison9dd62522020-11-29 09:33:54 -0800255 } else
256 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
257 break;
258 }
Wayne Davisone35ad792008-01-25 16:51:10 -0800259 if (code != 0 && (preserve_perms || preserve_executability))
Wayne Davisonff0e1582008-03-09 19:50:51 -0700260 return code;
David Dykstraf0b4fda2003-01-21 00:58:50 +0000261 return 0;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000262}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000263#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000264
Wayne Davison3d29fa92020-06-13 11:24:30 -0700265int do_rename(const char *old_path, const char *new_path)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000266{
267 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000268 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison3d29fa92020-06-13 11:24:30 -0700269 return rename(old_path, new_path);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000270}
271
Wayne Davison96e051c2010-11-06 09:57:23 -0700272#ifdef HAVE_FTRUNCATE
273int do_ftruncate(int fd, OFF_T size)
274{
275 int ret;
276
277 if (dry_run) return 0;
278 RETURN_ERROR_IF_RO_OR_LO;
279
280 do {
281 ret = ftruncate(fd, size);
282 } while (ret < 0 && errno == EINTR);
283
284 return ret;
285}
286#endif
287
Martin Poolbf4e7252002-03-25 03:29:47 +0000288void trim_trailing_slashes(char *name)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000289{
Martin Poolc127e8a2002-03-25 03:51:17 +0000290 int l;
Martin Poolded83472002-02-18 22:44:23 +0000291 /* Some BSD systems cannot make a directory if the name
292 * contains a trailing slash.
293 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
Wayne Davison0f78b812006-04-25 20:23:34 +0000294
Martin Poolc127e8a2002-03-25 03:51:17 +0000295 /* Don't change empty string; and also we can't improve on
296 * "/" */
Wayne Davison0f78b812006-04-25 20:23:34 +0000297
Martin Poolc127e8a2002-03-25 03:51:17 +0000298 l = strlen(name);
299 while (l > 1) {
300 if (name[--l] != '/')
301 break;
302 name[l] = '\0';
Martin Poolbf4e7252002-03-25 03:29:47 +0000303 }
304}
305
Martin Poolbf4e7252002-03-25 03:29:47 +0000306int do_mkdir(char *fname, mode_t mode)
307{
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000308 if (dry_run) return 0;
309 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison0f78b812006-04-25 20:23:34 +0000310 trim_trailing_slashes(fname);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000311 return mkdir(fname, mode);
312}
313
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000314/* like mkstemp but forces permissions */
315int do_mkstemp(char *template, mode_t perms)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000316{
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000317 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000318 RETURN_ERROR_IF(read_only, EROFS);
Wayne Davison0379c8e2007-11-03 19:27:49 +0000319 perms |= S_IWUSR;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000320
Wayne Davison4f5b0752005-02-14 00:53:43 +0000321#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000322 {
323 int fd = mkstemp(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000324 if (fd == -1)
325 return -1;
326 if (fchmod(fd, perms) != 0 && preserve_perms) {
327 int errno_save = errno;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000328 close(fd);
329 unlink(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000330 errno = errno_save;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000331 return -1;
332 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000333#if defined HAVE_SETMODE && O_BINARY
Wayne Davison3267d6a2004-10-01 02:34:22 +0000334 setmode(fd, O_BINARY);
335#endif
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000336 return fd;
337 }
338#else
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000339 if (!mktemp(template))
340 return -1;
Martin Pool25f2cb32002-01-14 00:16:51 +0000341 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000342#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000343}
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000344
345int do_stat(const char *fname, STRUCT_STAT *st)
346{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000347#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000348 return stat64(fname, st);
349#else
350 return stat(fname, st);
351#endif
352}
353
354int do_lstat(const char *fname, STRUCT_STAT *st)
355{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000356#ifdef SUPPORT_LINKS
357# ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000358 return lstat64(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000359# else
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000360 return lstat(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000361# endif
362#else
363 return do_stat(fname, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000364#endif
365}
366
367int do_fstat(int fd, STRUCT_STAT *st)
368{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000369#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000370 return fstat64(fd, st);
371#else
372 return fstat(fd, st);
373#endif
374}
Andrew Tridgell73233f01998-05-06 06:34:18 +0000375
376OFF_T do_lseek(int fd, OFF_T offset, int whence)
377{
Wayne Davisonebd33e02005-04-06 02:08:21 +0000378#ifdef HAVE_LSEEK64
Wayne Davisonf853b772005-09-03 16:56:53 +0000379#if !SIZEOF_OFF64_T
380 OFF_T lseek64();
381#else
Andrew Tridgellf28ee651998-05-06 07:35:37 +0000382 off64_t lseek64();
Wayne Davisonf853b772005-09-03 16:56:53 +0000383#endif
Andrew Tridgell73233f01998-05-06 06:34:18 +0000384 return lseek64(fd, offset, whence);
385#else
386 return lseek(fd, offset, whence);
387#endif
388}
Wayne Davison2a189352010-08-26 10:24:37 -0700389
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700390#ifdef HAVE_SETATTRLIST
Wayne Davison1c7785a2020-04-25 21:39:11 -0700391int do_setattrlist_times(const char *fname, STRUCT_STAT *stp)
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700392{
393 struct attrlist attrList;
394 struct timespec ts;
395
396 if (dry_run) return 0;
397 RETURN_ERROR_IF_RO_OR_LO;
398
Wayne Davison1c7785a2020-04-25 21:39:11 -0700399 ts.tv_sec = stp->st_mtime;
400 ts.tv_nsec = stp->ST_MTIME_NSEC;
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700401
402 memset(&attrList, 0, sizeof attrList);
403 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
404 attrList.commonattr = ATTR_CMN_MODTIME;
405 return setattrlist(fname, &attrList, &ts, sizeof ts, FSOPT_NOFOLLOW);
406}
407#endif
408
Wayne Davison974f49e2020-07-22 12:12:18 -0700409#ifdef SUPPORT_CRTIMES
410time_t get_create_time(const char *path)
411{
412 static struct create_time attrBuf;
413 struct attrlist attrList;
414
415 memset(&attrList, 0, sizeof attrList);
416 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
417 attrList.commonattr = ATTR_CMN_CRTIME;
418 if (getattrlist(path, &attrList, &attrBuf, sizeof attrBuf, FSOPT_NOFOLLOW) < 0)
419 return 0;
420 return attrBuf.crtime.tv_sec;
421}
422#endif
423
424#ifdef SUPPORT_CRTIMES
425int set_create_time(const char *path, time_t crtime)
426{
427 struct attrlist attrList;
428 struct timespec ts;
429
430 if (dry_run) return 0;
431 RETURN_ERROR_IF_RO_OR_LO;
432
433 ts.tv_sec = crtime;
434 ts.tv_nsec = 0;
435
436 memset(&attrList, 0, sizeof attrList);
437 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
438 attrList.commonattr = ATTR_CMN_CRTIME;
439 return setattrlist(path, &attrList, &ts, sizeof ts, FSOPT_NOFOLLOW);
440}
441#endif
442
Wayne Davison2a189352010-08-26 10:24:37 -0700443#ifdef HAVE_UTIMENSAT
Wayne Davisonb9367412020-04-23 13:17:41 -0700444int do_utimensat(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700445{
446 struct timespec t[2];
447
448 if (dry_run) return 0;
449 RETURN_ERROR_IF_RO_OR_LO;
450
Wayne Davisonb9367412020-04-23 13:17:41 -0700451 t[0].tv_sec = stp->st_atime;
452#ifdef ST_ATIME_NSEC
453 t[0].tv_nsec = stp->ST_ATIME_NSEC;
454#else
455 t[0].tv_nsec = 0;
456#endif
457 t[1].tv_sec = stp->st_mtime;
458#ifdef ST_MTIME_NSEC
459 t[1].tv_nsec = stp->ST_MTIME_NSEC;
460#else
461 t[1].tv_nsec = 0;
462#endif
Wayne Davison2a189352010-08-26 10:24:37 -0700463 return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
464}
465#endif
466
467#ifdef HAVE_LUTIMES
Wayne Davisonb9367412020-04-23 13:17:41 -0700468int do_lutimes(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700469{
470 struct timeval t[2];
471
472 if (dry_run) return 0;
473 RETURN_ERROR_IF_RO_OR_LO;
474
Wayne Davisonb9367412020-04-23 13:17:41 -0700475 t[0].tv_sec = stp->st_atime;
476#ifdef ST_ATIME_NSEC
477 t[0].tv_usec = stp->ST_ATIME_NSEC / 1000;
478#else
Wayne Davison2a189352010-08-26 10:24:37 -0700479 t[0].tv_usec = 0;
Wayne Davisonb9367412020-04-23 13:17:41 -0700480#endif
481 t[1].tv_sec = stp->st_mtime;
482#ifdef ST_MTIME_NSEC
483 t[1].tv_usec = stp->ST_MTIME_NSEC / 1000;
484#else
485 t[1].tv_usec = 0;
486#endif
Wayne Davison2a189352010-08-26 10:24:37 -0700487 return lutimes(fname, t);
488}
489#endif
490
491#ifdef HAVE_UTIMES
Wayne Davisonb9367412020-04-23 13:17:41 -0700492int do_utimes(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700493{
494 struct timeval t[2];
495
496 if (dry_run) return 0;
497 RETURN_ERROR_IF_RO_OR_LO;
498
Wayne Davisonb9367412020-04-23 13:17:41 -0700499 t[0].tv_sec = stp->st_atime;
500#ifdef ST_ATIME_NSEC
501 t[0].tv_usec = stp->ST_ATIME_NSEC / 1000;
502#else
Wayne Davison2a189352010-08-26 10:24:37 -0700503 t[0].tv_usec = 0;
Wayne Davisonb9367412020-04-23 13:17:41 -0700504#endif
505 t[1].tv_sec = stp->st_mtime;
506#ifdef ST_MTIME_NSEC
507 t[1].tv_usec = stp->ST_MTIME_NSEC / 1000;
508#else
509 t[1].tv_usec = 0;
510#endif
Wayne Davison2a189352010-08-26 10:24:37 -0700511 return utimes(fname, t);
512}
513
514#elif defined HAVE_UTIME
Wayne Davisonb9367412020-04-23 13:17:41 -0700515int do_utime(const char *fname, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700516{
517#ifdef HAVE_STRUCT_UTIMBUF
518 struct utimbuf tbuf;
519#else
520 time_t t[2];
521#endif
522
523 if (dry_run) return 0;
524 RETURN_ERROR_IF_RO_OR_LO;
525
526# ifdef HAVE_STRUCT_UTIMBUF
Wayne Davisonb9367412020-04-23 13:17:41 -0700527 tbuf.actime = stp->st_atime;
528 tbuf.modtime = stp->st_mtime;
Wayne Davison2a189352010-08-26 10:24:37 -0700529 return utime(fname, &tbuf);
530# else
Wayne Davisonb9367412020-04-23 13:17:41 -0700531 t[0] = stp->st_atime;
532 t[1] = stp->st_mtime;
Wayne Davison2a189352010-08-26 10:24:37 -0700533 return utime(fname, t);
534# endif
535}
536
537#else
538#error Need utimes or utime function.
539#endif
Wayne Davison28b519c2011-04-04 21:44:12 -0700540
541#ifdef SUPPORT_PREALLOCATION
Wayne Davison28b519c2011-04-04 21:44:12 -0700542#ifdef FALLOC_FL_KEEP_SIZE
543#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
544#else
545#define DO_FALLOC_OPTIONS 0
546#endif
Wayne Davisonf3873b32016-10-10 11:49:50 -0700547
548OFF_T do_fallocate(int fd, OFF_T offset, OFF_T length)
549{
Wayne Davisonc2da3802019-01-15 08:51:08 -0800550 int opts = inplace || preallocate_files ? DO_FALLOC_OPTIONS : 0;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700551 int ret;
Wayne Davison28b519c2011-04-04 21:44:12 -0700552 RETURN_ERROR_IF(dry_run, 0);
553 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700554 if (length & 1) /* make the length not match the desired length */
555 length++;
556 else
557 length--;
Wayne Davison28b519c2011-04-04 21:44:12 -0700558#if defined HAVE_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700559 ret = fallocate(fd, opts, offset, length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700560#elif defined HAVE_SYS_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700561 ret = syscall(SYS_fallocate, fd, opts, (loff_t)offset, (loff_t)length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700562#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700563 ret = posix_fallocate(fd, offset, length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700564#else
565#error Coding error in SUPPORT_PREALLOCATION logic.
566#endif
Wayne Davisonf3873b32016-10-10 11:49:50 -0700567 if (ret < 0)
568 return ret;
569 if (opts == 0) {
570 STRUCT_STAT st;
571 if (do_fstat(fd, &st) < 0)
572 return length;
Wayne Davisond1a1fec2016-10-15 11:13:28 -0700573 return st.st_blocks * S_BLKSIZE;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700574 }
575 return 0;
Wayne Davison28b519c2011-04-04 21:44:12 -0700576}
577#endif
Wayne Davison953feea2011-09-19 09:18:18 -0700578
Wayne Davisonf3873b32016-10-10 11:49:50 -0700579/* Punch a hole at pos for len bytes. The current file position must be at pos and will be
580 * changed to be at pos + len. */
Wayne Davisonc3269272020-07-07 13:12:51 -0700581int do_punch_hole(int fd, OFF_T pos, OFF_T len)
Wayne Davisonf3873b32016-10-10 11:49:50 -0700582{
583#ifdef HAVE_FALLOCATE
584# ifdef HAVE_FALLOC_FL_PUNCH_HOLE
585 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos, len) == 0) {
586 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
587 return -1;
588 return 0;
589 }
590# endif
591# ifdef HAVE_FALLOC_FL_ZERO_RANGE
592 if (fallocate(fd, FALLOC_FL_ZERO_RANGE, pos, len) == 0) {
593 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
594 return -1;
595 return 0;
596 }
597# endif
Wayne Davisonc3269272020-07-07 13:12:51 -0700598#else
599 (void)pos;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700600#endif
601 {
602 char zeros[4096];
603 memset(zeros, 0, sizeof zeros);
604 while (len > 0) {
605 int chunk = len > (int)sizeof zeros ? (int)sizeof zeros : len;
606 int wrote = write(fd, zeros, chunk);
607 if (wrote <= 0) {
608 if (wrote < 0 && errno == EINTR)
609 continue;
610 return -1;
611 }
612 len -= wrote;
613 }
614 }
615 return 0;
616}
617
Wayne Davison953feea2011-09-19 09:18:18 -0700618int do_open_nofollow(const char *pathname, int flags)
619{
620#ifndef O_NOFOLLOW
Wayne Davison79853c32011-09-20 12:54:06 -0700621 STRUCT_STAT f_st, l_st;
Wayne Davison953feea2011-09-19 09:18:18 -0700622#endif
623 int fd;
624
625 if (flags != O_RDONLY) {
626 RETURN_ERROR_IF(dry_run, 0);
627 RETURN_ERROR_IF_RO_OR_LO;
628#ifndef O_NOFOLLOW
629 /* This function doesn't support write attempts w/o O_NOFOLLOW. */
630 errno = EINVAL;
631 return -1;
632#endif
633 }
634
635#ifdef O_NOFOLLOW
636 fd = open(pathname, flags|O_NOFOLLOW);
637#else
Wayne Davisonde219102011-09-20 13:02:12 -0700638 if (do_lstat(pathname, &l_st) < 0)
639 return -1;
640 if (S_ISLNK(l_st.st_mode)) {
641 errno = ELOOP;
642 return -1;
643 }
Wayne Davison953feea2011-09-19 09:18:18 -0700644 if ((fd = open(pathname, flags)) < 0)
645 return fd;
Wayne Davison953feea2011-09-19 09:18:18 -0700646 if (do_fstat(fd, &f_st) < 0) {
647 close_and_return_error:
648 {
649 int save_errno = errno;
650 close(fd);
651 errno = save_errno;
652 }
653 return -1;
654 }
Wayne Davison953feea2011-09-19 09:18:18 -0700655 if (l_st.st_dev != f_st.st_dev || l_st.st_ino != f_st.st_ino) {
656 errno = EINVAL;
657 goto close_and_return_error;
658 }
659#endif
660
661 return fd;
662}