File: fileio.cc

package info (click to toggle)
apt-cacher-ng 0.8.0-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,768 kB
  • ctags: 1,640
  • sloc: cpp: 14,741; ansic: 462; perl: 376; sh: 357; makefile: 88
file content (108 lines) | stat: -rw-r--r-- 1,806 bytes parent folder | download | duplicates (5)
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

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include "config.h"
#include "fileio.h"
#include "acbuf.h"

#ifdef HAVE_LINUX_FALLOCATE
#include <linux/falloc.h>
#include <fcntl.h>

int falloc_helper(int fd, off_t start, off_t len)
{
   return fallocate(fd, FALLOC_FL_KEEP_SIZE, start, len);
}
#else
int falloc_helper(int, off_t, off_t)
{
   return 0;
}
#endif

// linking not possible? different filesystems?
bool FileCopy_generic(cmstring &from, cmstring &to)
{
	acbuf buf;
	buf.setsize(50000);
	int in(-1), out(-1);

	in=::open(from.c_str(), O_RDONLY);
	if (in<0) // error, here?!
		return false;

	while (true)
	{
		int err;
		err=buf.sysread(in);
		if (err<0)
		{
			if (err==-EAGAIN || err==-EINTR)
				continue;
			else
				goto error_copying;
		}
		else if (err==0)
			break;
		// don't open unless the input is readable, for sure
		if (out<0)
		{
			out=open(to.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 00644);
			if (out<0)
				goto error_copying;
		}
		err=buf.syswrite(out);
		if (err<=0)
		{
			if (err==-EAGAIN || err==-EINTR)
				continue;
			else
				goto error_copying;
		}

	}

	forceclose(in);
	forceclose(out);
	return true;

	error_copying:

	checkforceclose(in);
	checkforceclose(out);
	return false;
}

/*
#if defined(HAVE_LINUX_SPLICE) && defined(HAVE_PREAD)
bool FileCopy(cmstring &from, cmstring &to)
{
	int in(-1), out(-1);

	in=::open(from.c_str(), O_RDONLY);
	if (in<0) // error, here?!
		return false;

	// don't open target unless the input is readable, for sure
	uint8_t oneByte;
	ssize_t err = pread(in, &oneByte, 1, 0);
	if (err < 0 || (err == 0 && errno != EINTR))
	{
		forceclose(in);
		return false;
	}
	out = open(to.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 00644);
	if (out < 0)
	{
		forceclose(in);
		return false;
	}


	return FileCopy_generic(from, to);
}
#endif
*/