Пример #1
0
int do_link(const char *oldpath, const char *newpath, struct stat *statp, struct config *conf, bool overwrite)
{
	/* Avoid creating too many hardlinks */
	if(statp->st_nlink >= (unsigned int)conf->max_hardlinks)
	{
		return duplicate_file(oldpath, newpath);
	}
	else if(link(oldpath, newpath))
	{
		if(overwrite && errno==EEXIST)
		{
			unlink(newpath);
			if(link(oldpath, newpath))
			{
				logp("could not hard link '%s' to '%s': %s\n",
					newpath, oldpath, strerror(errno));
				return -1;
			}
			else
			{
				logp("Successful hard link of '%s' to '%s' after unlinking the former\n", newpath, oldpath);
				return 0;
			}
		}
		return -1;
	}
	return 0;
}
Пример #2
0
Файл: link.c Проект: EmisFR/burp
int do_link(const char *oldpath, const char *newpath, struct stat *statp,
	struct conf **confs, uint8_t overwrite)
{
	/* Avoid creating too many hardlinks */
	if(confs
	  && statp->st_nlink >= (unsigned int)get_int(confs[OPT_MAX_HARDLINKS]))
	{
		return duplicate_file(oldpath, newpath);
	}
	else if(link(oldpath, newpath))
	{
		if(overwrite && errno==EEXIST)
		{
			unlink(newpath);
			if(!link(oldpath, newpath))
			{
				//logp("Successful hard link of '%s' to '%s' after unlinking the former\n", newpath, oldpath);
				return 0;
			}
		}
		logp("could not hard link '%s' to '%s': %s\n",
			newpath, oldpath, strerror(errno));
		return -1;
	}
	return 0;
}
Пример #3
0
int main(int argc, char **argv) {
#if 0
{
//system("cat /proc/self/status");
int i;
for (i = 0; i < argc; i++)
	printf("*%s* ", argv[i]);
printf("\n");
}
#endif	
	if (argc != 3) {
		fprintf(stderr, "Error fcopy: files missing\n");
		usage();
		exit(1);
	}
	
	// check the two files; remove ending /
	char *src = argv[1];
	int len = strlen(src);
	if (src[len - 1] == '/')
		src[len - 1] = '\0';
	if (strcspn(src, "\\*&!?\"'<>%^(){}[];,") != (size_t)len) {
		fprintf(stderr, "Error fcopy: invalid file name %s\n", src);
		exit(1);
	}
	
	char *dest = argv[2];
	len = strlen(dest);
	if (dest[len - 1] == '/')
		dest[len - 1] = '\0';
	if (strcspn(dest, "\\*&!?\"'<>%^(){}[];,~") != (size_t)len) {
		fprintf(stderr, "Error fcopy: invalid file name %s\n", dest);
		exit(1);
	}
	

	// the destination should be a directory; 
	struct stat s;
	if (stat(dest, &s) == -1 ||
	    !S_ISDIR(s.st_mode)) {
		fprintf(stderr, "Error fcopy: invalid destination directory\n");
		exit(1);
	}
	
	// copy files
	if (lstat(src, &s) == -1) {
		fprintf(stderr, "Error fcopy: cannot find source file\n");
		exit(1);
	}

	if (S_ISDIR(s.st_mode))
		duplicate_dir(src, dest, &s);
	else if (S_ISREG(s.st_mode))
		duplicate_file(src, dest, &s);
	else if (S_ISLNK(s.st_mode))
		duplicate_link(src, dest, &s);
	else {
		fprintf(stderr, "Error fcopy: source file unsupported\n");
		exit(1);
	}
		
	return 0;
}