31 #if !defined(__GNUC__) || defined HAVE_MD5_H
34 #include <sasl/md5global.h>
36 #define MD5Init _sasl_MD5Init
37 #define MD5Update _sasl_MD5Update
38 #define MD5Final _sasl_MD5Final
76 const char *dest_file,
80 char tmp_file[PATH_MAX];
84 struct stat src_stats;
94 dest_len = strlen(dest_file);
95 if ((dest_len == 0) || (dest_len > PATH_MAX - 6)) {
98 "Could not copy file:\n"
101 " -> invalid destination file length: %d\n",
102 src_file, dest_file, dest_len);
107 strcpy(tmp_file, dest_file);
109 for (chrp = tmp_file + dest_len; ; chrp--) {
113 if (chrp == tmp_file)
break;
114 if (*(chrp-1) ==
'/')
break;
118 strcat(tmp_file,
".lck");
127 "Could not copy file:\n"
130 " -> could not get source file MD5\n",
131 src_file, dest_file);
139 src_fd = open(src_file, O_RDONLY);
143 "Could not copy file:\n"
146 " -> src file open error: %s\n",
147 src_file, dest_file, strerror(errno));
154 if (fstat(src_fd, &src_stats) < 0) {
157 "Could not copy file:\n"
160 " -> src file stat error: %s\n",
161 src_file, dest_file, strerror(errno));
169 tmp_fd = open(tmp_file, O_WRONLY | O_CREAT | O_TRUNC, 0600);
173 "Could not copy file:\n"
176 " -> tmp file open error: %s\n",
177 src_file, tmp_file, strerror(errno));
185 buf_size = getpagesize();
186 buf = (
char *)malloc(buf_size *
sizeof(
char));
188 while ((nread = read(src_fd, buf, buf_size)) > 0) {
190 nwritten = write(tmp_fd, buf, nread);
191 if (nwritten != nread) {
195 "Could not copy file:\n"
198 " -> write error: %s\n",
199 src_file, tmp_file, strerror(errno));
204 "Could not copy file:\n"
207 " -> bytes written (%d) does not match bytes read (%d)\n",
208 src_file, tmp_file, (
int)nwritten, (
int)nread);
222 "Could not copy file:\n"
225 " -> read error: %s\n",
226 src_file, tmp_file, strerror(errno));
241 chmod(tmp_file, src_stats.st_mode & 07777);
243 if (flags & FC_CHECK_MD5) {
250 "Could not copy file:\n"
253 " -> could not get destination file MD5\n",
262 if (strcmp(src_md5, tmp_md5) != 0) {
265 "Could not copy file:\n"
268 " -> source and destination files have different MD5s\n",
278 if (rename(tmp_file, dest_file) < 0) {
281 "Could not copy file:\n"
284 " -> tmp file rename error: %s\n",
285 src_file, dest_file, strerror(errno));
305 if (access(file, F_OK) == 0) {
335 unsigned char md5_digest[16];
338 fd = open(file, O_RDONLY);
342 "Could not get MD5 for file: %s\n"
343 " -> open error: %s\n", file, strerror(errno));
345 return((
char *)NULL);
348 MD5Init(&md5_context);
350 buf_size = getpagesize();
351 buf = (
char *)malloc(buf_size *
sizeof(
char));
353 while ((nread = read(fd, buf, buf_size)) > 0) {
354 MD5Update(&md5_context, buf, nread);
360 "Could not get MD5 for file: %s\n"
361 " -> read error: %s\n", file, strerror(errno));
365 return((
char *)NULL);
371 MD5Final(md5_digest, &md5_context);
373 for (i = 0; i < 16; i++) {
374 sprintf(hexdigest + 2 * i,
"%02x", md5_digest[i]);
406 const char *src_file,
407 const char *dest_file,
410 struct stat old_stats;
415 if (rename(src_file, dest_file) == 0) {
421 if (errno != EXDEV) {
424 "Could not move file:\n"
427 " -> rename error: %s\n",
428 src_file, dest_file, strerror(errno));
437 if (stat(src_file, &old_stats) < 0) {
440 "Could not move file:\n"
443 " -> stat error: %s\n",
444 src_file, dest_file, strerror(errno));
451 if (!
file_copy(src_file, dest_file, flags)) {
457 ut.actime = old_stats.st_atime;
458 ut.modtime = old_stats.st_mtime;
459 utime(dest_file, &ut);
463 if (unlink(src_file) < 0) {
466 "Could not unlink file: %s\n"
467 " -> %s\n", src_file, strerror(errno));
491 struct stat file_stats;
494 fd = open(file, O_RDONLY);
498 "Could not create memory map for file: %s\n"
499 " -> open error: %s\n", file, strerror(errno));
504 if (fstat(fd, &file_stats) == -1) {
507 "Could not create memory map for file: %s\n"
508 " -> fstat error: %s\n", file, strerror(errno));
514 *map_size = file_stats.st_size;
516 map_addr = mmap((
void *)NULL, *map_size, PROT_READ, MAP_SHARED, fd, 0);
517 if (map_addr == MAP_FAILED) {
520 "Could not create memory map for file: %s\n"
521 " -> mmap error: %s\n", file, strerror(errno));
547 if (munmap(map_addr, map_size) == -1) {
550 "Could not remove memory map\n"
551 " -> munmap error: %s\n", strerror(errno));