-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathficlone.c
More file actions
124 lines (104 loc) · 2.73 KB
/
ficlone.c
File metadata and controls
124 lines (104 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
This file is part of rmw<https://theimpossibleastronaut.github.io/rmw-website/>
Copyright (C) 2012-2024 Andy Alt (arch_stanton5995@proton.me)
Other authors: https://github.com/theimpossibleastronaut/rmw/blob/master/AUTHORS.md
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INC_GLOBALS_H
#define INC_GLOBALS_H
#include "globals.h"
#endif
#ifdef HAVE_FICLONE
#include <fcntl.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/statfs.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "ficlone.h"
#include "messages.h"
bool
is_ficlone_fs(const char *path)
{
#ifdef HAVE_FICLONE
struct statfs buf;
if (statfs(path, &buf) == -1)
{
print_msg_error();
perror("statfs");
exit(EXIT_FAILURE);
}
return buf.f_type == BTRFS_SUPER_MAGIC;
#else
(void) path;
return false;
#endif
}
int
do_ficlone(const char *source, const char *dest, int *save_errno)
{
#ifdef HAVE_FICLONE
int src_fd, dest_fd;
struct stat src_stat;
// Open the source file in read-only mode
src_fd = open(source, O_RDONLY);
if (src_fd == -1)
{
perror("open source");
return src_fd;
}
// Retrieve source file permissions
if (fstat(src_fd, &src_stat) == -1)
{
perror("fstat source");
close(src_fd);
return -1;
}
// Ensure no umask setting interferes with the permissions
mode_t old_umask = umask(0);
// Open or create the destination file with the same permissions as the source
dest_fd = open(dest, O_WRONLY | O_CREAT, src_stat.st_mode & 0777);
umask(old_umask);
if (dest_fd == -1)
{
perror("open destination");
close(src_fd);
return dest_fd;
}
int res = ioctl(dest_fd, FICLONE, src_fd);
*save_errno = errno;
close(src_fd);
close(dest_fd);
if (res == -1)
{
if (*save_errno != EXDEV)
fprintf(stderr, "ioctl: %s in %s\n", strerror(*save_errno), __func__);
// Clone failed, remove the destination file
if (unlink(dest) != 0)
fprintf(stderr, "unlink: %s in %s\n", strerror(errno), __func__);
return res;
}
res = unlink(source);
if (res == -1)
{
perror("unlink source");
return res;
}
return 0;
#else
(void) source;
(void) dest;
(void) save_errno;
return 0;
#endif
}