1111# include < sys/mman.h>
1212#endif
1313
14+ #ifndef _WIN32
15+ /* * Copy a file in its entirety.
16+ @param src source file descriptor
17+ @param dst target to append src to
18+ @return error code (negative)
19+ @retval 0 on success */
20+ int my_copy_file_fd (int src, int dst)
21+ {
22+ return backup::copy_file (src, dst);
23+ }
24+
25+ /* * Copy a file given a known length to be copied.
26+ @param src source file descriptor
27+ @param dst target to append src to
28+ @param size amount of data to be copied
29+ @return error code (negative)
30+ @retval 0 on success */
31+ int my_copy_file_length_fd (int src, int dst, off_t size)
32+ {
33+ return backup::copy_file (src, dst, size);
34+ }
35+
36+ #endif
1437namespace
1538{
1639#if !(defined __APPLE__ || defined _WIN32)
@@ -44,7 +67,7 @@ copy_step(int in_fd, int out_fd, size_t count, off_t *offset) noexcept
4467 return copy_file_range (in_fd, offset, out_fd, nullptr , count, 0 );
4568}
4669# define cfr (src,dst,size ) copy<copy_step>(src, dst, size)
47- # endif
70+ # endif // defined __linux__ || defined __FreeBSD__
4871# ifdef __linux__
4972/* Copy a file to a stream or to a regular file. */
5073static inline ssize_t
@@ -62,10 +85,10 @@ send_step(int in_fd, int out_fd, size_t count, off_t *offset) noexcept
6285@retval 1 if a memory mapping failed */
6386static ssize_t mmap_copy (int in_fd, int out_fd, off_t count)
6487{
65- #if SIZEOF_SIZE_T < 8
88+ # if SIZEOF_SIZE_T < 8
6689 if (count != ssize_t (count))
6790 return 1 ;
68- #endif
91+ # endif // SIZEOF_SIZE_T < 8
6992 void *p= mmap (nullptr , count, PROT_READ , MAP_SHARED , in_fd, 0 );
7093 if (p == MAP_FAILED )
7194 return 1 ;
@@ -121,56 +144,46 @@ static ssize_t pread_write(int in_fd, int out_fd, off_t count) noexcept
121144 aligned_free (b);
122145 return ret;
123146}
124- # endif
125- #endif
147+ # endif // __linux__
148+ #endif // !(defined __APPLE__ || defined _WIN32)
126149}
127150
128151namespace backup
129152{
130153
131154#ifndef _WIN32
132- /* * Copy a file.
133- @param src source file descriptor
134- @param dst target to append src to
135- @return error code (negative)
136- @retval 0 on success */
155+
137156int copy_file (int src, int dst) noexcept
138157{
139- #ifdef __APPLE__
158+ # ifdef __APPLE__
140159 return fcopyfile (src, dst, nullptr , COPYFILE_ALL | COPYFILE_CLONE );
141- #else
160+ # else
142161 return copy_file (src, dst, lseek (src, 0 , SEEK_END ));
143- #endif
162+ # endif // __APPLE__
144163}
145164
146- /* * Copy a file.
147- @param src source file descriptor
148- @param dst target to append src to
149- @param size amount of data to be copied
150- @return error code (negative)
151- @retval 0 on success */
152165int copy_file (int src, int dst, off_t size) noexcept
153166{
154- #ifdef __APPLE__
167+ # ifdef __APPLE__
155168 return fcopyfile (src, dst, nullptr , COPYFILE_ALL | COPYFILE_CLONE );
156- #else
169+ # else
157170 ssize_t ret;
158- # ifdef cfr
171+ # ifdef cfr
159172 if (!(ret= cfr (src, dst, size)))
160173 return int (ret);
161- # ifdef __linux__
174+ # ifdef __linux__
162175 if (errno == EOPNOTSUPP )
163- # endif
164- # endif
165- # ifdef __linux__ // starting with Linux 2.6.33, we can rely on sendfile(2)
176+ # endif // __linux__
177+ # endif // cfr
178+ # ifdef __linux__ // starting with Linux 2.6.33, we can rely on sendfile(2)
166179 ret= copy<send_step>(src, dst, size);
167- # else
180+ # else
168181 if ((ret= mmap_copy (src, dst, size)) == 1 )
169182 ret= pread_write (src, dst, size);
170- # endif
183+ # endif // __linux__
171184 DBUG_ASSERT (ret <= 0 );
172185 return int (ret);
173- #endif
186+ # endif // __APPLE__
187+ }
188+ #endif // _WIN32
174189}
175- #endif
176- }
0 commit comments