blob: 1ed36f3c2752df80250f077ecf0d03ce8a1a4a16 [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 Davisonb3bf9b92009-01-03 10:57:14 -08007 * Copyright (C) 2003-2009 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;
David Dykstraf0b4fda2003-01-21 00:58:50 +000041extern int preserve_perms;
Wayne Davisone35ad792008-01-25 16:51:10 -080042extern int preserve_executability;
Andrew Tridgellf0fca041998-05-09 13:58:54 +000043
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000044#define RETURN_ERROR_IF(x,e) \
45 do { \
46 if (x) { \
47 errno = (e); \
48 return -1; \
49 } \
50 } while (0)
51
52#define RETURN_ERROR_IF_RO_OR_LO RETURN_ERROR_IF(read_only || list_only, EROFS)
Andrew Tridgell31e12521998-03-23 13:25:30 +000053
Wayne Davison63cf5ae2006-01-29 18:52:53 +000054int do_unlink(const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000055{
56 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000057 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +000058 return unlink(fname);
59}
60
Wayne Davison8e15bd82009-09-02 08:53:40 -070061#ifdef SUPPORT_LINKS
62int do_symlink(const char *lnk, const char *fname)
Andrew Tridgell31e12521998-03-23 13:25:30 +000063{
64 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +000065 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison6e310d32009-09-02 09:06:29 -070066
67#ifdef NO_SYMLINK_XATTRS
68 /* For --fake-super, we create a normal file with mode 0600
69 * and write the lnk into it. */
70 if (am_root < 0) {
71 int ok, len = strlen(lnk);
72 int fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
73 if (fd < 0)
74 return -1;
75 ok = write(fd, lnk, len) == len;
76 if (close(fd) < 0)
77 ok = 0;
78 return ok ? 0 : -1;
79 }
80#endif
81
Wayne Davison8e15bd82009-09-02 08:53:40 -070082 return symlink(lnk, fname);
Andrew Tridgell31e12521998-03-23 13:25:30 +000083}
Wayne Davison6e310d32009-09-02 09:06:29 -070084
85#ifdef NO_SYMLINK_XATTRS
86ssize_t do_readlink(const char *path, char *buf, size_t bufsiz)
87{
88 /* For --fake-super, we read the link from the file. */
89 if (am_root < 0) {
90 int fd = open(path, O_RDONLY|O_NOFOLLOW);
91 if (fd >= 0) {
92 int len = read(fd, buf, bufsiz);
93 close(fd);
94 return len;
95 }
96 if (errno != ELOOP)
97 return -1;
98 /* A real symlink needs to be turned into a fake one on the receiving
99 * side, so tell the generator that the link has no length. */
100 if (!am_sender)
101 return 0;
102 /* Otherwise fall through and let the sender report the real length. */
103 }
104
105 return readlink(path, buf, bufsiz);
106}
107#endif
Wayne Davison8e15bd82009-09-02 08:53:40 -0700108#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000109
Wayne Davison4f5b0752005-02-14 00:53:43 +0000110#ifdef HAVE_LINK
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000111int do_link(const char *fname1, const char *fname2)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000112{
113 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000114 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000115 return link(fname1, fname2);
116}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000117#endif
Andrew Tridgell31e12521998-03-23 13:25:30 +0000118
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000119int do_lchown(const char *path, uid_t owner, gid_t group)
120{
121 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000122 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000123#ifndef HAVE_LCHOWN
Wayne Davison05154762005-01-03 21:03:33 +0000124#define lchown chown
125#endif
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000126 return lchown(path, owner, group);
127}
128
Wayne Davison4a19c3b2006-11-19 00:23:21 +0000129int do_mknod(const char *pathname, mode_t mode, dev_t dev)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000130{
131 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000132 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison9439c0c2007-04-24 07:32:44 +0000133
134 /* For --fake-super, we create a normal file with mode 0600. */
135 if (am_root < 0) {
136 int fd = open(pathname, O_WRONLY|O_CREAT|O_TRUNC, S_IWUSR|S_IRUSR);
137 if (fd < 0 || close(fd) < 0)
138 return -1;
139 return 0;
140 }
141
Wayne Davison4f5b0752005-02-14 00:53:43 +0000142#if !defined MKNOD_CREATES_FIFOS && defined HAVE_MKFIFO
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000143 if (S_ISFIFO(mode))
144 return mkfifo(pathname, mode);
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000145#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000146#if !defined MKNOD_CREATES_SOCKETS && defined HAVE_SYS_UN_H
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000147 if (S_ISSOCK(mode)) {
148 int sock;
149 struct sockaddr_un saddr;
Wayne Davison4f375592006-11-21 08:37:06 +0000150#ifdef HAVE_SOCKADDR_UN_LEN
151 unsigned int len =
152#endif
153 strlcpy(saddr.sun_path, pathname, sizeof saddr.sun_path);
Wayne Davison4f5b0752005-02-14 00:53:43 +0000154#ifdef HAVE_SOCKADDR_UN_LEN
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000155 saddr.sun_len = len >= sizeof saddr.sun_path
156 ? sizeof saddr.sun_path : len + 1;
157#endif
Wayne Davison4f375592006-11-21 08:37:06 +0000158 saddr.sun_family = AF_UNIX;
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000159
160 if ((sock = socket(PF_UNIX, SOCK_STREAM, 0)) < 0
161 || (unlink(pathname) < 0 && errno != ENOENT)
162 || (bind(sock, (struct sockaddr*)&saddr, sizeof saddr)) < 0)
163 return -1;
164 close(sock);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000165#ifdef HAVE_CHMOD
Wayne Davison4fd842f2007-07-07 05:33:14 +0000166 return do_chmod(pathname, mode);
Wayne Davisond11f5c62005-07-27 23:30:53 +0000167#else
168 return 0;
169#endif
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000170 }
171#endif
Wayne Davison4f5b0752005-02-14 00:53:43 +0000172#ifdef HAVE_MKNOD
Wayne Davisonda6eb9d2004-10-01 06:56:14 +0000173 return mknod(pathname, mode, dev);
174#else
175 return -1;
176#endif
177}
Andrew Tridgell31e12521998-03-23 13:25:30 +0000178
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000179int do_rmdir(const char *pathname)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000180{
181 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000182 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell31e12521998-03-23 13:25:30 +0000183 return rmdir(pathname);
184}
185
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000186int do_open(const char *pathname, int flags, mode_t mode)
Andrew Tridgell31e12521998-03-23 13:25:30 +0000187{
David Dykstra3420c8e1999-11-04 15:43:38 +0000188 if (flags != O_RDONLY) {
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000189 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000190 RETURN_ERROR_IF_RO_OR_LO;
David Dykstra3420c8e1999-11-04 15:43:38 +0000191 }
Andrew Tridgellb0f3f572000-01-23 03:00:27 +0000192
Wayne Davisone106de42003-03-30 23:00:51 +0000193 return open(pathname, flags | O_BINARY, mode);
Andrew Tridgell31e12521998-03-23 13:25:30 +0000194}
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000195
Wayne Davison4f5b0752005-02-14 00:53:43 +0000196#ifdef HAVE_CHMOD
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000197int do_chmod(const char *path, mode_t mode)
198{
David Dykstraf0b4fda2003-01-21 00:58:50 +0000199 int code;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000200 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000201 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davisond11f5c62005-07-27 23:30:53 +0000202#ifdef HAVE_LCHMOD
Wayne Davison42562642009-12-31 14:10:38 -0800203 code = lchmod(path, mode & CHMOD_BITS);
204#else
205 if (S_ISLNK(mode)) {
206# if defined HAVE_SETATTRLIST
Wayne Davison11b02d92007-10-16 16:00:41 +0000207 struct attrlist attrList;
208 uint32_t m = mode & CHMOD_BITS; /* manpage is wrong: not mode_t! */
209
210 memset(&attrList, 0, sizeof attrList);
211 attrList.bitmapcount = ATTR_BIT_MAP_COUNT;
212 attrList.commonattr = ATTR_CMN_ACCESSMASK;
213 code = setattrlist(path, &attrList, &m, sizeof m, FSOPT_NOFOLLOW);
Wayne Davison42562642009-12-31 14:10:38 -0800214# else
Wayne Davisond11f5c62005-07-27 23:30:53 +0000215 code = 1;
Wayne Davison42562642009-12-31 14:10:38 -0800216# endif
Wayne Davisond11f5c62005-07-27 23:30:53 +0000217 } else
Wayne Davisonaa0e6b92008-03-17 07:34:22 -0700218 code = chmod(path, mode & CHMOD_BITS); /* DISCOURAGED FUNCTION */
Wayne Davison42562642009-12-31 14:10:38 -0800219#endif /* !HAVE_LCHMOD */
Wayne Davisone35ad792008-01-25 16:51:10 -0800220 if (code != 0 && (preserve_perms || preserve_executability))
Wayne Davisonff0e1582008-03-09 19:50:51 -0700221 return code;
David Dykstraf0b4fda2003-01-21 00:58:50 +0000222 return 0;
Andrew Tridgell7308bd61998-03-24 06:39:16 +0000223}
Andrew Tridgellf92ef571998-03-24 06:42:11 +0000224#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000225
Wayne Davison63cf5ae2006-01-29 18:52:53 +0000226int do_rename(const char *fname1, const char *fname2)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000227{
228 if (dry_run) return 0;
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000229 RETURN_ERROR_IF_RO_OR_LO;
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000230 return rename(fname1, fname2);
231}
232
Wayne Davison96e051c2010-11-06 09:57:23 -0700233#ifdef HAVE_FTRUNCATE
234int do_ftruncate(int fd, OFF_T size)
235{
236 int ret;
237
238 if (dry_run) return 0;
239 RETURN_ERROR_IF_RO_OR_LO;
240
241 do {
242 ret = ftruncate(fd, size);
243 } while (ret < 0 && errno == EINTR);
244
245 return ret;
246}
247#endif
248
Martin Poolbf4e7252002-03-25 03:29:47 +0000249void trim_trailing_slashes(char *name)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000250{
Martin Poolc127e8a2002-03-25 03:51:17 +0000251 int l;
Martin Poolded83472002-02-18 22:44:23 +0000252 /* Some BSD systems cannot make a directory if the name
253 * contains a trailing slash.
254 * <http://www.opensource.apple.com/bugs/X/BSD%20Kernel/2734739.html> */
Wayne Davison0f78b812006-04-25 20:23:34 +0000255
Martin Poolc127e8a2002-03-25 03:51:17 +0000256 /* Don't change empty string; and also we can't improve on
257 * "/" */
Wayne Davison0f78b812006-04-25 20:23:34 +0000258
Martin Poolc127e8a2002-03-25 03:51:17 +0000259 l = strlen(name);
260 while (l > 1) {
261 if (name[--l] != '/')
262 break;
263 name[l] = '\0';
Martin Poolbf4e7252002-03-25 03:29:47 +0000264 }
265}
266
Martin Poolbf4e7252002-03-25 03:29:47 +0000267int do_mkdir(char *fname, mode_t mode)
268{
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000269 if (dry_run) return 0;
270 RETURN_ERROR_IF_RO_OR_LO;
Wayne Davison0f78b812006-04-25 20:23:34 +0000271 trim_trailing_slashes(fname);
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000272 return mkdir(fname, mode);
273}
274
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000275/* like mkstemp but forces permissions */
276int do_mkstemp(char *template, mode_t perms)
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000277{
Wayne Davison7a27e9b2004-02-18 22:33:21 +0000278 RETURN_ERROR_IF(dry_run, 0);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000279 RETURN_ERROR_IF(read_only, EROFS);
Wayne Davison0379c8e2007-11-03 19:27:49 +0000280 perms |= S_IWUSR;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000281
Wayne Davison4f5b0752005-02-14 00:53:43 +0000282#if defined HAVE_SECURE_MKSTEMP && defined HAVE_FCHMOD && (!defined HAVE_OPEN64 || defined HAVE_MKSTEMP64)
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000283 {
284 int fd = mkstemp(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000285 if (fd == -1)
286 return -1;
287 if (fchmod(fd, perms) != 0 && preserve_perms) {
288 int errno_save = errno;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000289 close(fd);
290 unlink(template);
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000291 errno = errno_save;
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000292 return -1;
293 }
Wayne Davison4f5b0752005-02-14 00:53:43 +0000294#if defined HAVE_SETMODE && O_BINARY
Wayne Davison3267d6a2004-10-01 02:34:22 +0000295 setmode(fd, O_BINARY);
296#endif
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000297 return fd;
298 }
299#else
Wayne Davisoncfeed4d2004-02-17 23:00:00 +0000300 if (!mktemp(template))
301 return -1;
Martin Pool25f2cb32002-01-14 00:16:51 +0000302 return do_open(template, O_RDWR|O_EXCL|O_CREAT, perms);
Andrew Tridgellf62c17e2001-05-02 08:33:18 +0000303#endif
Andrew Tridgell1b2d7331998-04-05 06:26:24 +0000304}
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000305
306int do_stat(const char *fname, STRUCT_STAT *st)
307{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000308#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000309 return stat64(fname, st);
310#else
311 return stat(fname, st);
312#endif
313}
314
315int do_lstat(const char *fname, STRUCT_STAT *st)
316{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000317#ifdef SUPPORT_LINKS
318# ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000319 return lstat64(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000320# else
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000321 return lstat(fname, st);
Wayne Davison0957a742005-01-19 19:29:20 +0000322# endif
323#else
324 return do_stat(fname, st);
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000325#endif
326}
327
328int do_fstat(int fd, STRUCT_STAT *st)
329{
Wayne Davison4f5b0752005-02-14 00:53:43 +0000330#ifdef USE_STAT64_FUNCS
Andrew Tridgellbcacc181998-05-06 05:43:36 +0000331 return fstat64(fd, st);
332#else
333 return fstat(fd, st);
334#endif
335}
Andrew Tridgell73233f01998-05-06 06:34:18 +0000336
337OFF_T do_lseek(int fd, OFF_T offset, int whence)
338{
Wayne Davisonebd33e02005-04-06 02:08:21 +0000339#ifdef HAVE_LSEEK64
Wayne Davisonf853b772005-09-03 16:56:53 +0000340#if !SIZEOF_OFF64_T
341 OFF_T lseek64();
342#else
Andrew Tridgellf28ee651998-05-06 07:35:37 +0000343 off64_t lseek64();
Wayne Davisonf853b772005-09-03 16:56:53 +0000344#endif
Andrew Tridgell73233f01998-05-06 06:34:18 +0000345 return lseek64(fd, offset, whence);
346#else
347 return lseek(fd, offset, whence);
348#endif
349}
Wayne Davison2a189352010-08-26 10:24:37 -0700350
351#ifdef HAVE_UTIMENSAT
352int do_utimensat(const char *fname, time_t modtime, uint32 mod_nsec)
353{
354 struct timespec t[2];
355
356 if (dry_run) return 0;
357 RETURN_ERROR_IF_RO_OR_LO;
358
359 t[0].tv_sec = 0;
360 t[0].tv_nsec = UTIME_NOW;
361 t[1].tv_sec = modtime;
362 t[1].tv_nsec = mod_nsec;
363 return utimensat(AT_FDCWD, fname, t, AT_SYMLINK_NOFOLLOW);
364}
365#endif
366
367#ifdef HAVE_LUTIMES
368int do_lutimes(const char *fname, time_t modtime, uint32 mod_nsec)
369{
370 struct timeval t[2];
371
372 if (dry_run) return 0;
373 RETURN_ERROR_IF_RO_OR_LO;
374
375 t[0].tv_sec = time(NULL);
376 t[0].tv_usec = 0;
377 t[1].tv_sec = modtime;
378 t[1].tv_usec = mod_nsec / 1000;
379 return lutimes(fname, t);
380}
381#endif
382
383#ifdef HAVE_UTIMES
384int do_utimes(const char *fname, time_t modtime, uint32 mod_nsec)
385{
386 struct timeval t[2];
387
388 if (dry_run) return 0;
389 RETURN_ERROR_IF_RO_OR_LO;
390
391 t[0].tv_sec = time(NULL);
392 t[0].tv_usec = 0;
393 t[1].tv_sec = modtime;
394 t[1].tv_usec = mod_nsec / 1000;
395 return utimes(fname, t);
396}
397
398#elif defined HAVE_UTIME
399int do_utime(const char *fname, time_t modtime, UNUSED(uint32 mod_nsec))
400{
401#ifdef HAVE_STRUCT_UTIMBUF
402 struct utimbuf tbuf;
403#else
404 time_t t[2];
405#endif
406
407 if (dry_run) return 0;
408 RETURN_ERROR_IF_RO_OR_LO;
409
410# ifdef HAVE_STRUCT_UTIMBUF
411 tbuf.actime = time(NULL);
412 tbuf.modtime = modtime;
413 return utime(fname, &tbuf);
414# else
415 t[0] = time(NULL);
416 t[1] = modtime;
417 return utime(fname, t);
418# endif
419}
420
421#else
422#error Need utimes or utime function.
423#endif
Wayne Davison28b519c2011-04-04 21:44:12 -0700424
425#ifdef SUPPORT_PREALLOCATION
426int do_fallocate(int fd, OFF_T offset, OFF_T length)
427{
428#ifdef FALLOC_FL_KEEP_SIZE
429#define DO_FALLOC_OPTIONS FALLOC_FL_KEEP_SIZE
430#else
431#define DO_FALLOC_OPTIONS 0
432#endif
433 RETURN_ERROR_IF(dry_run, 0);
434 RETURN_ERROR_IF_RO_OR_LO;
435#if defined HAVE_FALLOCATE
436 return fallocate(fd, DO_FALLOC_OPTIONS, offset, length);
437#elif defined HAVE_SYS_FALLOCATE
438 return syscall(SYS_fallocate, fd, DO_FALLOC_OPTIONS, (loff_t)offset, (loff_t)length);
439#elif defined HAVE_EFFICIENT_POSIX_FALLOCATE
440 return posix_fallocate(fd, offset, length);
441#else
442#error Coding error in SUPPORT_PREALLOCATION logic.
443#endif
444}
445#endif