Rizin
unix-like reverse engineering framework and cli tools
can_clone_file.c
Go to the documentation of this file.
1 /*
2  can_clone_file.c -- does the current filesystem support cloning
3  Copyright (C) 1999-2021 Dieter Baron and Thomas Klausner
4 
5  This file is part of libzip, a library to manipulate ZIP archives.
6  The authors can be contacted at <libzip@nih.at>
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions
10  are met:
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in
15  the documentation and/or other materials provided with the
16  distribution.
17  3. The names of the authors may not be used to endorse or promote
18  products derived from this software without specific prior
19  written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS
22  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
25  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdlib.h>
35 
36 #include "config.h"
37 
38 #ifdef HAVE_CLONEFILE
39 #include <errno.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sys/attr.h>
43 #include <sys/mount.h>
44 #include <sys/param.h>
45 #include <unistd.h>
46 #elif defined(HAVE_FICLONERANGE)
47 #include <errno.h>
48 #include <linux/fs.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <sys/ioctl.h>
52 #include <unistd.h>
53 #endif
54 
55 int
56 main(int argc, char *argv[]) {
57 #ifdef HAVE_CLONEFILE
58  struct statfs fs;
59  struct attrlist attribute_list;
60  struct {
61  uint32_t size;
62  vol_capabilities_attr_t capabilities;
63  } volume_attributes;
64 
65  if (statfs(".", &fs) < 0) {
66  fprintf(stderr, "%s: can't get mount point of current directory: %s\n", argv[0], strerror(errno));
67  exit(1);
68  }
69 
70  /* Not all volumes support clonefile(). A volume can be tested for
71  clonefile() support by using getattrlist(2) to get the volume
72  capabilities attribute ATTR_VOL_CAPABILITIES, and then testing the
73  VOL_CAP_INT_CLONE flag. */
74 
75  memset(&attribute_list, 0, sizeof(attribute_list));
76  attribute_list.bitmapcount = ATTR_BIT_MAP_COUNT;
77  attribute_list.volattr = ATTR_VOL_INFO | ATTR_VOL_CAPABILITIES;
78  memset(&volume_attributes, 0, sizeof(volume_attributes));
79 
80  if (getattrlist(fs.f_mntonname, &attribute_list, &volume_attributes, sizeof(volume_attributes), 0) < 0) {
81  fprintf(stderr, "%s: can't get volume capabilities of '%s': %s\n", argv[0], fs.f_mntonname, strerror(errno));
82  exit(1);
83  }
84 
85  if (volume_attributes.capabilities.capabilities[VOL_CAPABILITIES_INTERFACES] & VOL_CAP_INT_CLONE) {
86  exit(0);
87  }
88 #elif defined(HAVE_FICLONERANGE)
89  char namea[32] = "a.fioclone.XXXXXX";
90  char nameb[32] = "b.fioclone.XXXXXX";
91  int fda, fdb, ret;
92  struct file_clone_range range;
93 
94  if ((fda = mkstemp(namea)) < 0) {
95  fprintf(stderr, "can't create temp file a: %s\n", strerror(errno));
96  exit(1);
97  }
98  if ((fdb = mkstemp(nameb)) < 0) {
99  fprintf(stderr, "can't create temp file b: %s\n", strerror(errno));
100  (void)close(fda);
101  (void)remove(namea);
102  exit(1);
103  }
104  if (write(fda, "test\n", 5) < 0) {
105  fprintf(stderr, "can't write temp file a: %s\n", strerror(errno));
106  (void)close(fda);
107  (void)remove(namea);
108  close(fdb);
109  (void)remove(nameb);
110  exit(1);
111  }
112  range.src_fd = fda;
113  range.src_offset = 0;
114  range.src_length = 0;
115  range.dest_offset = 0;
116  ret = ioctl(fdb, FICLONERANGE, &range);
117  (void)close(fda);
118  (void)close(fdb);
119  (void)remove(namea);
120  (void)remove(nameb);
121  if (ret >= 0) {
122  exit(0);
123  }
124 #endif
125 
126  exit(1);
127 }
int main(int argc, char *argv[])
static static fork write
Definition: sflib.h:33
static static fork const void static count close
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename ioctl
Definition: sflib.h:62
voidpf void uLong size
Definition: ioapi.h:138
return memset(p, 0, total)
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who statfs
Definition: sflib.h:124
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
unsigned int uint32_t
Definition: sftypes.h:29
Definition: sftypes.h:74