copyfile.c 4.01 KB
Newer Older
Bernhard Link's avatar
Bernhard Link committed
1
/*  This file is part of "reprepro"
2
 *  Copyright (C) 2003,2004,2006 Bernhard R. Link
3
 *  This program is free software; you can redistribute it and/or modify
4
 *  it under the terms of the GNU General Public License version 2 as
Bernhard Link's avatar
Bernhard Link committed
5
 *  published by the Free Software Foundation.
6 7 8 9 10 11 12 13
 *
 *  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, write to the Free Software
Bernhard Link's avatar
Bernhard Link committed
14
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02111-1301  USA
15 16 17
 */
#include <config.h>

Bernhard Link's avatar
Bernhard Link committed
18
#include <assert.h>
19 20 21 22 23 24 25 26
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
27
#include <string.h>
28 29

#include "error.h"
30
#include "strlist.h"
31 32
#include "names.h"
#include "dirs.h"
33
#include "md5sum.h"
34 35
#include "copyfile.h"

36 37
extern int verbose;

38
retvalue copy(const char *fullfilename,const char *origfile,/*@null@*/const char *md5expected,/*@null@*//*@out@*/char **calculatedmd5sum) {
39 40 41 42 43 44 45 46 47 48 49 50 51 52
	char *md5sum;
	retvalue r;

	r = md5sum_copy(origfile,fullfilename,&md5sum);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Could not open '%s'!\n",origfile);
		return RET_ERROR_MISSING;
	}
	if( r == RET_ERROR_EXIST ) {
		fprintf(stderr,"File '%s' does already exist!\n",fullfilename);
	}
	if( RET_WAS_ERROR(r) )
		return r;

53 54 55 56
	if( md5expected != NULL ) {
		if( strcmp(md5sum,md5expected) == 0 ) {
			r = RET_OK;
		} else {
Bernhard Link's avatar
Bernhard Link committed
57
			(void)unlink(fullfilename);
58 59 60
			fprintf(stderr,"WARNING: '%s' has md5sum '%s', while '%s' was expected.\n",origfile,md5sum,md5expected);
			r = RET_ERROR_WRONG_MD5;
		}
61 62
	}

63 64
	if( calculatedmd5sum != NULL )
		*calculatedmd5sum = md5sum;
Bernhard Link's avatar
Bernhard Link committed
65
	else
66 67 68 69 70
		free(md5sum);

	return r;
}

Bernhard Link's avatar
Bernhard Link committed
71
static retvalue move(const char *fullfilename,const char *origfile,/*@null@*/const char *md5expected,/*@out@*/char **md5sum) {
72 73
	retvalue r;
	// TODO: try a rename first, if md5sum is know and correct??
74

75 76 77
	r = copy(fullfilename,origfile,md5expected,md5sum);
	if( RET_IS_OK(r) ) {
		if( verbose > 15 ) {
78
			fprintf(stderr,"Deleting '%s' after copying away.\n",origfile);
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
		}
		if( unlink(origfile) != 0 ) {
			fprintf(stderr,"Error deleting '%s': %m",origfile);
		}
	}
	return r;
}

retvalue copyfile_move(const char *mirrordir,const char *filekey,const char *origfile,const char *md5expected,char **md5sum) {
	retvalue r;
	char *fullfilename;

	fullfilename = calc_dirconcat(mirrordir,filekey);
	if( fullfilename == NULL )
		return RET_ERROR_OOM;
	r = move(fullfilename,origfile,md5expected,md5sum);
	free(fullfilename);
	return r;
}
retvalue copyfile_copy(const char *mirrordir,const char *filekey,const char *origfile,const char *md5expected,char **md5sum) {
	retvalue r;
	char *fullfilename;

	fullfilename = calc_dirconcat(mirrordir,filekey);
	if( fullfilename == NULL )
		return RET_ERROR_OOM;
	r = copy(fullfilename,origfile,md5expected,md5sum);
	free(fullfilename);
	return r;
}
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
retvalue copyfile_hardlink(const char *mirrordir, const char *filekey, const char *tempfile, const char *md5sum) {
	retvalue r;
	int i,e;
	char *fullfilename = calc_fullfilename(mirrordir,filekey);
	if( fullfilename == NULL )
		return RET_ERROR_OOM;

	i = link(tempfile, fullfilename);
	e = errno;
	if( i != 0 && e == EEXIST )  {
		unlink(fullfilename);
		i = link(tempfile, fullfilename);
		e = errno;
	}
	if( i != 0 && ( e == EACCES || e == ENOENT || e == ENOTDIR ) )  {
		dirs_make_parent(fullfilename);
		i = link(tempfile, fullfilename);
		e = errno;
	}
	if( i != 0 ) {
		if( e == EXDEV || e == EPERM || e == EMLINK ) {
			r = copy(fullfilename, tempfile, md5sum, NULL);
			if( RET_WAS_ERROR(r) ) {
				free(fullfilename);
				return r;
			}
		} else {
			fprintf(stderr,
"Error creating hardlink of '%s' as '%s': %d=%s\n",
					tempfile, fullfilename, e, strerror(e));
			free(fullfilename);
			return RET_ERRNO(e);
		}
	}

	free(fullfilename);
	return RET_OK;
}