blob: d92074aaadc20f595c6d381a24d6eefb0f90d416 [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 Davisonc3b553a2022-01-15 17:21:01 -08007 * Copyright (C) 2003-2022 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
Wayne Davison291a0422021-07-08 18:47:21 -070058#ifdef HAVE_GETATTRLIST
Wayne Davison974f49e2020-07-22 12:12:18 -070059#pragma pack(push, 4)
60struct create_time {
61 uint32 length;
62 struct timespec crtime;
63};
64#pragma pack(pop)
Wayne Davison291a0422021-07-08 18:47:21 -070065#elif defined __CYGWIN__
66#include <windows.h>
67#endif
Wayne Davison974f49e2020-07-22 12:12:18 -070068#endif
69
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000070#define RETURN_ERROR_IF(x,e) \
71 do { \
72 if (x) { \
73 errno = (e); \
74 return -1; \
75 } \
76 } while (0)
77
78#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
Andrew Tridgell31e12521998-03-23 13:25:30 +000079
Wayne Davison452ef782021-10-10 09:53:35 -070080int do_unlink(const char *path)
Andrew Tridgell31e12521998-03-23 13:25:30 +000081{
82 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000083 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison452ef782021-10-10 09:53:35 -070084 return unlink(path);
Andrew Tridgell31e12521998-03-23 13:25:30 +000085}
86
Wayne Davison8e15bd82009-09-02 08:53:40 -070087#ifdef SUPPORT_LINKS
Wayne Davison452ef782021-10-10 09:53:35 -070088int do_symlink(const char *lnk, const char *path)
Andrew Tridgell31e12521998-03-23 13:25:30 +000089{
90 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000091 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison6e310d32009-09-02 09:06:29 -070092
Wayne Davisone2c1e482011-06-18 10:12:47 -070093#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
Wayne Davison6e310d32009-09-02 09:06:29 -070094 /* For --fake-super, we create a normal file with mode 0600
95 * and write the lnk into it. */
96 if (am_root < 0) {
97 int ok, len = strlen(lnk);
Wayne Davison452ef782021-10-10 09:53:35 -070098 int fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
Wayne Davison6e310d32009-09-02 09:06:29 -070099 if (fd < 0)
100 return -1;
101 ok = write(fd, lnk, len) == len;
102 if (close(fd) < 0)
103 ok = 0;
104 return ok ? 0 : -1;
105 }
106#endif
107
Wayne Davison452ef782021-10-10 09:53:35 -0700108 return symlink(lnk, path);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000109}
Wayne Davison6e310d32009-09-02 09:06:29 -0700110
Wayne Davisona59a7b22011-06-18 13:42:30 -0700111#if defined NO_SYMLINK_XATTRS || defined NO_SYMLINK_USER_XATTRS
Wayne Davison6e310d32009-09-02 09:06:29 -0700112ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
113{
114 /* For --fake-super, we read the link from the file. */
115 if (am_root < 0) {
Wayne Davison953feea2011-09-19 09:18:18 -0700116 int fd = do_open_nofollow(path, O_RDONLY);
Wayne Davison6e310d32009-09-02 09:06:29 -0700117 if (fd >= 0) {
118 int len = read(fd, buf, bufsiz);
119 close(fd);
120 return len;
121 }
122 if (errno != ELOOP)
123 return -1;
124 /* A real symlink needs to be turned into a fake one on the receiving
125 * side, so tell the generator that the link has no length. */
126 if (!am_sender)
127 return 0;
128 /* Otherwise fall through and let the sender report the real length. */
129 }
130
131 return readlink(path, buf, bufsiz);
132}
133#endif
Wayne Davison8e15bd82009-09-02 08:53:40 -0700134#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000135
Wayne Davison5db7e4b2020-07-27 16:36:55 -0700136#if defined HAVE_LINK || defined HAVE_LINKAT
Wayne Davison3d29fa92020-06-13 11:24:30 -0700137int do_link(const char *old_path, const char *new_path)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000138{
139 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000140 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison5db7e4b2020-07-27 16:36:55 -0700141#ifdef HAVE_LINKAT
142 return linkat(AT_FDCWD, old_path, AT_FDCWD, new_path, 0);
143#else
Wayne Davison3d29fa92020-06-13 11:24:30 -0700144 return link(old_path, new_path);
Wayne Davison5db7e4b2020-07-27 16:36:55 -0700145#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000146}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000147#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000148
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000149int do_lchown(const char *path, uid_t owner, gid_t group)
150{
151 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000152 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000153#ifndef HAVE_LCHOWN
Wayne Davison05154762005-01-03 21:03:33 +0000154#define lchown chown
155#endif
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000156 return lchown(path, owner, group);
157}
158
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000159int do_mknod(const char *pathname, mode_t mode, dev_t dev)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000160{
161 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000162 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison9439c0c2007-04-24 07:32:44 +0000163
164 /* For --fake-super, we create a normal file with mode 0600. */
165 if (am_root < 0) {
166 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
167 if (fd < 0 || close(fd) < 0)
168 return -1;
169 return 0;
170 }
171
Wayne Davison4f5b0752005-02-14 00:53:43 +0000172#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000173 if (S_ISFIFO(mode))
174 return mkfifo(pathname, mode);
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000175#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000176#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000177 if (S_ISSOCK(mode)) {
178 int sock;
179 struct sockaddr_un saddr;
Wayne Davison63f91972013-10-27 10:12:53 -0700180 unsigned int len = strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
181 if (len >= sizeof saddr.sun_path) {
182 errno = ENAMETOOLONG;
183 return -1;
184 }
Wayne Davison4f375592006-11-21 08:37:06 +0000185#ifdef HAVE_SOCKADDR_UN_LEN
Wayne Davison63f91972013-10-27 10:12:53 -0700186 saddr.sun_len = len + 1;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000187#endif
Wayne Davison4f375592006-11-21 08:37:06 +0000188 saddr.sun_family = AF_UNIX;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000189
190 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
Wayne Davisone63ff702020-06-13 18:19:12 -0700191 || (unlink(pathname) < 0 && errno != ENOENT)
192 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000193 return -1;
194 close(sock);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000195#ifdef HAVE_CHMOD
Wayne Davison4fd842f2007-07-07 05:33:14 +0000196 return do_chmod(pathname, mode);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000197#else
198 return 0;
199#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000200 }
201#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000202#ifdef HAVE_MKNOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000203 return mknod(pathname, mode, dev);
204#else
205 return -1;
206#endif
207}
Andrew Tridgell31e12521998-03-23 13:25:30 +0000208
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000209int do_rmdir(const char *pathname)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000210{
211 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000212 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000213 return rmdir(pathname);
214}
215
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000216int do_open(const char *pathname, int flags, mode_t mode)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000217{
David Dykstra3420c8e1999-11-04 15:43:38 +0000218 if (flags != O_RDONLY) {
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000219 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000220 RETURN_ERROR_IF_RO_OR_LO;
David Dykstra3420c8e1999-11-04 15:43:38 +0000221 }
Andrew Tridgellb0f3f572000-01-23 03:00:27 +0000222
Wayne Davisonb9367412020-04-23 13:17:41 -0700223#ifdef O_NOATIME
Wayne Davison87257f82020-04-23 14:27:44 -0700224 if (open_noatime)
Wayne Davisonb9367412020-04-23 13:17:41 -0700225 flags |= O_NOATIME;
226#endif
227
Wayne Davisone106de42003-03-30 23:00:51 +0000228 return open(pathname, flags | O_BINARY, mode);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000229}
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000230
Wayne Davison4f5b0752005-02-14 00:53:43 +0000231#ifdef HAVE_CHMOD
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000232int do_chmod(const char *path, mode_t mode)
233{
Wayne Davison0d1b4882021-10-10 09:20:13 -0700234 static int switch_step = 0;
David Dykstraf0b4fda2003-01-21 00:58:50 +0000235 int code;
Wayne Davison296352e2021-10-10 11:45:14 -0700236
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000237 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000238 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison296352e2021-10-10 11:45:14 -0700239
Wayne Davison0d1b4882021-10-10 09:20:13 -0700240 switch (switch_step) {
Wayne Davisond11f5c62005-07-27 23:30:53 +0000241#ifdef HAVE_LCHMOD
Wayne Davison11a9b622021-10-10 10:04:54 -0700242 case 0:
Wayne Davison0d1b4882021-10-10 09:20:13 -0700243 if ((code = lchmod(path, mode & CHMOD_BITS)) == 0)
244 break;
245 if (errno == ENOSYS)
246 switch_step++;
247 else if (errno != ENOTSUP)
248 break;
Wayne Davison9dd62522020-11-29 09:33:54 -0800249#endif
Wayne Davisonb7b387b2022-03-13 09:31:44 -0700250 /* FALLTHROUGH */
Wayne Davison11a9b622021-10-10 10:04:54 -0700251 default:
Wayne Davison9dd62522020-11-29 09:33:54 -0800252 if (S_ISLNK(mode)) {
253# if defined HAVE_SETATTRLIST
254 struct attrlist attrList;
255 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
256
257 memset(&attrList, 0, sizeof attrList);
258 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
259 attrList.commonattr = ATTR_CMN_ACCESSMASK;
Wayne Davison142aba02022-01-17 22:12:21 -0800260 if ((code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW)) == 0)
261 break;
262 if (errno == ENOTSUP)
263 code = 1;
Wayne Davison42562642009-12-31 14:10:38 -0800264# else
Wayne Davison9dd62522020-11-29 09:33:54 -0800265 code = 1;
Wayne Davison42562642009-12-31 14:10:38 -0800266# endif
Wayne Davison9dd62522020-11-29 09:33:54 -0800267 } else
268 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
Wayne Davison0d1b4882021-10-10 09:20:13 -0700269 break;
Wayne Davison9dd62522020-11-29 09:33:54 -0800270 }
Wayne Davisone35ad792008-01-25 16:51:10 -0800271 if (code != 0 && (preserve_perms || preserve_executability))
Wayne Davisonff0e1582008-03-09 19:50:51 -0700272 return code;
David Dykstraf0b4fda2003-01-21 00:58:50 +0000273 return 0;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000274}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000275#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000276
Wayne Davison3d29fa92020-06-13 11:24:30 -0700277int do_rename(const char *old_path, const char *new_path)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000278{
279 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000280 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison3d29fa92020-06-13 11:24:30 -0700281 return rename(old_path, new_path);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000282}
283
Wayne Davison96e051c2010-11-06 09:57:23 -0700284#ifdef HAVE_FTRUNCATE
285int do_ftruncate(int fd, OFF_T size)
286{
287 int ret;
288
289 if (dry_run) return 0;
290 RETURN_ERROR_IF_RO_OR_LO;
291
292 do {
293 ret = ftruncate(fd, size);
294 } while (ret < 0 && errno == EINTR);
295
296 return ret;
297}
298#endif
299
Martin Poolbf4e7252002-03-25 03:29:47 +0000300void trim_trailing_slashes(char *name)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000301{
Martin Poolc127e8a2002-03-25 03:51:17 +0000302 int l;
Martin Poolded83472002-02-18 22:44:23 +0000303 /* Some BSD systems cannot make a directory if the name
304 * contains a trailing slash.
305 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
Wayne Davison0f78b812006-04-25 20:23:34 +0000306
Martin Poolc127e8a2002-03-25 03:51:17 +0000307 /* Don't change empty string; and also we can't improve on
308 * "/" */
Wayne Davison0f78b812006-04-25 20:23:34 +0000309
Martin Poolc127e8a2002-03-25 03:51:17 +0000310 l = strlen(name);
311 while (l > 1) {
312 if (name[--l] != '/')
313 break;
314 name[l] = '\0';
Martin Poolbf4e7252002-03-25 03:29:47 +0000315 }
316}
317
Wayne Davison452ef782021-10-10 09:53:35 -0700318int do_mkdir(char *path, mode_t mode)
Martin Poolbf4e7252002-03-25 03:29:47 +0000319{
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000320 if (dry_run) return 0;
321 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison452ef782021-10-10 09:53:35 -0700322 trim_trailing_slashes(path);
323 return mkdir(path, mode);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000324}
325
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000326/* like mkstemp but forces permissions */
327int do_mkstemp(char *template, mode_t perms)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000328{
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000329 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000330 RETURN_ERROR_IF(read_only, EROFS);
Wayne Davison0379c8e2007-11-03 19:27:49 +0000331 perms |= S_IWUSR;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000332
Wayne Davison4f5b0752005-02-14 00:53:43 +0000333#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000334 {
335 int fd = mkstemp(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000336 if (fd == -1)
337 return -1;
338 if (fchmod(fd, perms) != 0 && preserve_perms) {
339 int errno_save = errno;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000340 close(fd);
341 unlink(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000342 errno = errno_save;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000343 return -1;
344 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000345#if defined HAVE_SETMODE && O_BINARY
Wayne Davison3267d6a2004-10-01 02:34:22 +0000346 setmode(fd, O_BINARY);
347#endif
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000348 return fd;
349 }
350#else
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000351 if (!mktemp(template))
352 return -1;
Martin Pool25f2cb32002-01-14 00:16:51 +0000353 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000354#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000355}
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000356
Wayne Davison452ef782021-10-10 09:53:35 -0700357int do_stat(const char *path, STRUCT_STAT *st)
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000358{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000359#ifdef USE_STAT64_FUNCS
Wayne Davison452ef782021-10-10 09:53:35 -0700360 return stat64(path, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000361#else
Wayne Davison452ef782021-10-10 09:53:35 -0700362 return stat(path, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000363#endif
364}
365
Wayne Davison452ef782021-10-10 09:53:35 -0700366int do_lstat(const char *path, STRUCT_STAT *st)
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000367{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000368#ifdef SUPPORT_LINKS
369# ifdef USE_STAT64_FUNCS
Wayne Davison452ef782021-10-10 09:53:35 -0700370 return lstat64(path, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000371# else
Wayne Davison452ef782021-10-10 09:53:35 -0700372 return lstat(path, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000373# endif
374#else
Wayne Davison452ef782021-10-10 09:53:35 -0700375 return do_stat(path, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000376#endif
377}
378
379int do_fstat(int fd, STRUCT_STAT *st)
380{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000381#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000382 return fstat64(fd, st);
383#else
384 return fstat(fd, st);
385#endif
386}
Andrew Tridgell73233f01998-05-06 06:34:18 +0000387
388OFF_T do_lseek(int fd, OFF_T offset, int whence)
389{
Wayne Davisonebd33e02005-04-06 02:08:21 +0000390#ifdef HAVE_LSEEK64
Wayne Davisonf853b772005-09-03 16:56:53 +0000391#if !SIZEOF_OFF64_T
392 OFF_T lseek64();
393#else
Andrew Tridgellf28ee651998-05-06 07:35:37 +0000394 off64_t lseek64();
Wayne Davisonf853b772005-09-03 16:56:53 +0000395#endif
Andrew Tridgell73233f01998-05-06 06:34:18 +0000396 return lseek64(fd, offset, whence);
397#else
398 return lseek(fd, offset, whence);
399#endif
400}
Wayne Davison2a189352010-08-26 10:24:37 -0700401
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700402#ifdef HAVE_SETATTRLIST
Wayne Davison452ef782021-10-10 09:53:35 -0700403int do_setattrlist_times(const char *path, STRUCT_STAT *stp)
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700404{
405 struct attrlist attrList;
Wayne Davison78b5bc62021-10-02 10:16:58 -0700406 struct timespec ts[2];
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700407
408 if (dry_run) return 0;
409 RETURN_ERROR_IF_RO_OR_LO;
410
Wayne Davison78b5bc62021-10-02 10:16:58 -0700411 /* Yes, this is in the opposite order of utime and similar. */
412 ts[0].tv_sec = stp->st_mtime;
413 ts[0].tv_nsec = stp->ST_MTIME_NSEC;
414
415 ts[1].tv_sec = stp->st_atime;
416 ts[1].tv_nsec = stp->ST_ATIME_NSEC;
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700417
418 memset(&attrList, 0, sizeof attrList);
419 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
Wayne Davison78b5bc62021-10-02 10:16:58 -0700420 attrList.commonattr = ATTR_CMN_MODTIME | ATTR_CMN_ACCTIME;
Wayne Davison452ef782021-10-10 09:53:35 -0700421 return setattrlist(path, &attrList, ts, sizeof ts, FSOPT_NOFOLLOW);
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700422}
Wayne Davison296352e2021-10-10 11:45:14 -0700423
424#ifdef SUPPORT_CRTIMES
425int do_setattrlist_crtime(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}
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700441#endif
Wayne Davison296352e2021-10-10 11:45:14 -0700442#endif /* HAVE_SETATTRLIST */
Wayne Davisonb7799aa2017-08-31 08:22:14 -0700443
Wayne Davison974f49e2020-07-22 12:12:18 -0700444#ifdef SUPPORT_CRTIMES
Wayne Davison291a0422021-07-08 18:47:21 -0700445time_t get_create_time(const char *path, STRUCT_STAT *stp)
Wayne Davison974f49e2020-07-22 12:12:18 -0700446{
Wayne Davison291a0422021-07-08 18:47:21 -0700447#ifdef HAVE_GETATTRLIST
Wayne Davison974f49e2020-07-22 12:12:18 -0700448 static struct create_time attrBuf;
449 struct attrlist attrList;
450
Wayne Davison291a0422021-07-08 18:47:21 -0700451 (void)stp;
Wayne Davison974f49e2020-07-22 12:12:18 -0700452 memset(&attrList, 0, sizeof attrList);
453 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
454 attrList.commonattr = ATTR_CMN_CRTIME;
455 if (getattrlist(path, &attrList, &attrBuf, sizeof attrBuf, FSOPT_NOFOLLOW) < 0)
456 return 0;
457 return attrBuf.crtime.tv_sec;
Wayne Davison291a0422021-07-08 18:47:21 -0700458#elif defined __CYGWIN__
459 (void)path;
460 return stp->st_birthtime;
461#else
462#error Unknown crtimes implementation
Wayne Davison974f49e2020-07-22 12:12:18 -0700463#endif
Wayne Davison291a0422021-07-08 18:47:21 -0700464}
Wayne Davison974f49e2020-07-22 12:12:18 -0700465
Wayne Davison296352e2021-10-10 11:45:14 -0700466#if defined __CYGWIN__
467int do_SetFileTime(const char *path, time_t crtime)
Wayne Davison974f49e2020-07-22 12:12:18 -0700468{
Wayne Davisonf41cdc72021-10-02 11:39:41 -0700469 if (dry_run) return 0;
470 RETURN_ERROR_IF_RO_OR_LO;
471
Wayne Davison291a0422021-07-08 18:47:21 -0700472 int cnt = MultiByteToWideChar(CP_UTF8, 0, path, -1, NULL, 0);
473 if (cnt == 0)
474 return -1;
475 WCHAR *pathw = new_array(WCHAR, cnt);
476 if (!pathw)
477 return -1;
478 MultiByteToWideChar(CP_UTF8, 0, path, -1, pathw, cnt);
479 HANDLE handle = CreateFileW(pathw, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
480 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
481 free(pathw);
482 if (handle == INVALID_HANDLE_VALUE)
483 return -1;
484 int64 temp_time = Int32x32To64(crtime, 10000000) + 116444736000000000LL;
485 FILETIME birth_time;
486 birth_time.dwLowDateTime = (DWORD)temp_time;
487 birth_time.dwHighDateTime = (DWORD)(temp_time >> 32);
488 int ok = SetFileTime(handle, &birth_time, NULL, NULL);
489 CloseHandle(handle);
490 return ok ? 0 : -1;
Wayne Davison974f49e2020-07-22 12:12:18 -0700491}
Wayne Davison296352e2021-10-10 11:45:14 -0700492#endif
Wayne Davison78b5bc62021-10-02 10:16:58 -0700493#endif /* SUPPORT_CRTIMES */
Wayne Davison974f49e2020-07-22 12:12:18 -0700494
Wayne Davison2a189352010-08-26 10:24:37 -0700495#ifdef HAVE_UTIMENSAT
Wayne Davison452ef782021-10-10 09:53:35 -0700496int do_utimensat(const char *path, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700497{
498 struct timespec t[2];
499
500 if (dry_run) return 0;
501 RETURN_ERROR_IF_RO_OR_LO;
502
Wayne Davisonb9367412020-04-23 13:17:41 -0700503 t[0].tv_sec = stp->st_atime;
504#ifdef ST_ATIME_NSEC
505 t[0].tv_nsec = stp->ST_ATIME_NSEC;
506#else
507 t[0].tv_nsec = 0;
508#endif
509 t[1].tv_sec = stp->st_mtime;
510#ifdef ST_MTIME_NSEC
511 t[1].tv_nsec = stp->ST_MTIME_NSEC;
512#else
513 t[1].tv_nsec = 0;
514#endif
Wayne Davison452ef782021-10-10 09:53:35 -0700515 return utimensat(AT_FDCWD, path, t, AT_SYMLINK_NOFOLLOW);
Wayne Davison2a189352010-08-26 10:24:37 -0700516}
517#endif
518
519#ifdef HAVE_LUTIMES
Wayne Davison452ef782021-10-10 09:53:35 -0700520int do_lutimes(const char *path, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700521{
522 struct timeval t[2];
523
524 if (dry_run) return 0;
525 RETURN_ERROR_IF_RO_OR_LO;
526
Wayne Davisonb9367412020-04-23 13:17:41 -0700527 t[0].tv_sec = stp->st_atime;
528#ifdef ST_ATIME_NSEC
529 t[0].tv_usec = stp->ST_ATIME_NSEC / 1000;
530#else
Wayne Davison2a189352010-08-26 10:24:37 -0700531 t[0].tv_usec = 0;
Wayne Davisonb9367412020-04-23 13:17:41 -0700532#endif
533 t[1].tv_sec = stp->st_mtime;
534#ifdef ST_MTIME_NSEC
535 t[1].tv_usec = stp->ST_MTIME_NSEC / 1000;
536#else
537 t[1].tv_usec = 0;
538#endif
Wayne Davison452ef782021-10-10 09:53:35 -0700539 return lutimes(path, t);
Wayne Davison2a189352010-08-26 10:24:37 -0700540}
541#endif
542
543#ifdef HAVE_UTIMES
Wayne Davison452ef782021-10-10 09:53:35 -0700544int do_utimes(const char *path, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700545{
546 struct timeval t[2];
547
548 if (dry_run) return 0;
549 RETURN_ERROR_IF_RO_OR_LO;
550
Wayne Davisonb9367412020-04-23 13:17:41 -0700551 t[0].tv_sec = stp->st_atime;
552#ifdef ST_ATIME_NSEC
553 t[0].tv_usec = stp->ST_ATIME_NSEC / 1000;
554#else
Wayne Davison2a189352010-08-26 10:24:37 -0700555 t[0].tv_usec = 0;
Wayne Davisonb9367412020-04-23 13:17:41 -0700556#endif
557 t[1].tv_sec = stp->st_mtime;
558#ifdef ST_MTIME_NSEC
559 t[1].tv_usec = stp->ST_MTIME_NSEC / 1000;
560#else
561 t[1].tv_usec = 0;
562#endif
Wayne Davison452ef782021-10-10 09:53:35 -0700563 return utimes(path, t);
Wayne Davison2a189352010-08-26 10:24:37 -0700564}
565
566#elif defined HAVE_UTIME
Wayne Davison452ef782021-10-10 09:53:35 -0700567int do_utime(const char *path, STRUCT_STAT *stp)
Wayne Davison2a189352010-08-26 10:24:37 -0700568{
569#ifdef HAVE_STRUCT_UTIMBUF
570 struct utimbuf tbuf;
571#else
572 time_t t[2];
573#endif
574
575 if (dry_run) return 0;
576 RETURN_ERROR_IF_RO_OR_LO;
577
578# ifdef HAVE_STRUCT_UTIMBUF
Wayne Davisonb9367412020-04-23 13:17:41 -0700579 tbuf.actime = stp->st_atime;
580 tbuf.modtime = stp->st_mtime;
Wayne Davison452ef782021-10-10 09:53:35 -0700581 return utime(path, &tbuf);
Wayne Davison2a189352010-08-26 10:24:37 -0700582# else
Wayne Davisonb9367412020-04-23 13:17:41 -0700583 t[0] = stp->st_atime;
584 t[1] = stp->st_mtime;
Wayne Davison452ef782021-10-10 09:53:35 -0700585 return utime(path, t);
Wayne Davison2a189352010-08-26 10:24:37 -0700586# endif
587}
588
589#else
590#error Need utimes or utime function.
591#endif
Wayne Davison28b519c2011-04-04 21:44:12 -0700592
593#ifdef SUPPORT_PREALLOCATION
Wayne Davison28b519c2011-04-04 21:44:12 -0700594#ifdef FALLOC_FL_KEEP_SIZE
595#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
596#else
597#define DO_FALLOC_OPTIONS 0
598#endif
Wayne Davisonf3873b32016-10-10 11:49:50 -0700599
600OFF_T do_fallocate(int fd, OFF_T offset, OFF_T length)
601{
Wayne Davisonc2da3802019-01-15 08:51:08 -0800602 int opts = inplace || preallocate_files ? DO_FALLOC_OPTIONS : 0;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700603 int ret;
Wayne Davison28b519c2011-04-04 21:44:12 -0700604 RETURN_ERROR_IF(dry_run, 0);
605 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700606 if (length & 1) /* make the length not match the desired length */
607 length++;
608 else
609 length--;
Wayne Davison28b519c2011-04-04 21:44:12 -0700610#if defined HAVE_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700611 ret = fallocate(fd, opts, offset, length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700612#elif defined HAVE_SYS_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700613 ret = syscall(SYS_fallocate, fd, opts, (loff_t)offset, (loff_t)length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700614#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
Wayne Davisonf3873b32016-10-10 11:49:50 -0700615 ret = posix_fallocate(fd, offset, length);
Wayne Davison28b519c2011-04-04 21:44:12 -0700616#else
617#error Coding error in SUPPORT_PREALLOCATION logic.
618#endif
Wayne Davisonf3873b32016-10-10 11:49:50 -0700619 if (ret < 0)
620 return ret;
621 if (opts == 0) {
622 STRUCT_STAT st;
623 if (do_fstat(fd, &st) < 0)
624 return length;
Wayne Davisond1a1fec2016-10-15 11:13:28 -0700625 return st.st_blocks * S_BLKSIZE;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700626 }
627 return 0;
Wayne Davison28b519c2011-04-04 21:44:12 -0700628}
629#endif
Wayne Davison953feea2011-09-19 09:18:18 -0700630
Wayne Davisonf3873b32016-10-10 11:49:50 -0700631/* Punch a hole at pos for len bytes. The current file position must be at pos and will be
632 * changed to be at pos + len. */
Wayne Davisonc3269272020-07-07 13:12:51 -0700633int do_punch_hole(int fd, OFF_T pos, OFF_T len)
Wayne Davisonf3873b32016-10-10 11:49:50 -0700634{
635#ifdef HAVE_FALLOCATE
636# ifdef HAVE_FALLOC_FL_PUNCH_HOLE
637 if (fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, pos, len) == 0) {
638 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
639 return -1;
640 return 0;
641 }
642# endif
643# ifdef HAVE_FALLOC_FL_ZERO_RANGE
644 if (fallocate(fd, FALLOC_FL_ZERO_RANGE, pos, len) == 0) {
645 if (do_lseek(fd, len, SEEK_CUR) != pos + len)
646 return -1;
647 return 0;
648 }
649# endif
Wayne Davisonc3269272020-07-07 13:12:51 -0700650#else
651 (void)pos;
Wayne Davisonf3873b32016-10-10 11:49:50 -0700652#endif
653 {
654 char zeros[4096];
655 memset(zeros, 0, sizeof zeros);
656 while (len > 0) {
657 int chunk = len > (int)sizeof zeros ? (int)sizeof zeros : len;
658 int wrote = write(fd, zeros, chunk);
659 if (wrote <= 0) {
660 if (wrote < 0 && errno == EINTR)
661 continue;
662 return -1;
663 }
664 len -= wrote;
665 }
666 }
667 return 0;
668}
669
Wayne Davison953feea2011-09-19 09:18:18 -0700670int do_open_nofollow(const char *pathname, int flags)
671{
672#ifndef O_NOFOLLOW
Wayne Davison79853c32011-09-20 12:54:06 -0700673 STRUCT_STAT f_st, l_st;
Wayne Davison953feea2011-09-19 09:18:18 -0700674#endif
675 int fd;
676
677 if (flags != O_RDONLY) {
678 RETURN_ERROR_IF(dry_run, 0);
679 RETURN_ERROR_IF_RO_OR_LO;
680#ifndef O_NOFOLLOW
681 /* This function doesn't support write attempts w/o O_NOFOLLOW. */
682 errno = EINVAL;
683 return -1;
684#endif
685 }
686
687#ifdef O_NOFOLLOW
688 fd = open(pathname, flags|O_NOFOLLOW);
689#else
Wayne Davisonde219102011-09-20 13:02:12 -0700690 if (do_lstat(pathname, &l_st) < 0)
691 return -1;
692 if (S_ISLNK(l_st.st_mode)) {
693 errno = ELOOP;
694 return -1;
695 }
Wayne Davison953feea2011-09-19 09:18:18 -0700696 if ((fd = open(pathname, flags)) < 0)
697 return fd;
Wayne Davison953feea2011-09-19 09:18:18 -0700698 if (do_fstat(fd, &f_st) < 0) {
699 close_and_return_error:
700 {
701 int save_errno = errno;
702 close(fd);
703 errno = save_errno;
704 }
705 return -1;
706 }
Wayne Davison953feea2011-09-19 09:18:18 -0700707 if (l_st.st_dev != f_st.st_dev || l_st.st_ino != f_st.st_ino) {
708 errno = EINVAL;
709 goto close_and_return_error;
710 }
711#endif
712
713 return fd;
714}