File: file.c

package info (click to toggle)
cowdancer 0.89
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 648 kB
  • sloc: ansic: 4,596; sh: 407; makefile: 144; cpp: 5
file content (183 lines) | stat: -rw-r--r-- 4,028 bytes parent folder | download | duplicates (4)
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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 *  qemubuilder: pbuilder with qemu
 *  Basic file operations code.
 *
 *  Copyright (C) 2009 Junichi Uekawa
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  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
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

/**
   copy file contents to temporary filesystem, so that it can be used
   inside qemu.

   return -1
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <alloca.h>
#include <errno.h>
#include "file.h"
#include "log.h"

/**
 * Copy file from orig to dest.
 *
 * returns 0 on success, -1 on failure.
 */
int copy_file(const char *orig, const char *dest) {
	const int buffer_size = 4096;
	char *buf = malloc(buffer_size);
	int ret = -1;
	int fin = -1;
	int fout = -1;
	size_t count;
	struct stat st;

	if (!buf) {
		log_printf(log_error, "Out of memory");
		goto out;
	}

	if (-1 == (fin = open(orig, O_RDONLY))) {
		log_perror("file copy: open for read");
		goto out;
	}
	if (-1 == (fout = creat(dest, 0666))) {
		log_perror("file copy: open for write");
		goto out;
	}
	while ((count = read(fin, buf, buffer_size)) > 0) {
		if (-1 == write(fout, buf, count)) { /* error */
			log_perror("file copy: write");
			log_printf(log_error, "%i", fin);
			log_printf(log_error, "%i %i", fout, (int)count);
			goto out;
		}
	}
	if (count == -1) {
		log_perror("file copy: read ");
		goto out;
	}
	if (-1 == close(fin) || -1 == close(fout)) {
		log_perror("file copy: close ");
		goto out;
	}

	if (0 > stat(orig, &st)) {
		log_printf(
			log_error, "Error calling stat '%s': %s", orig, strerror(errno));
		goto out;
	}

	if (chmod(dest, st.st_mode)) {
		log_printf(log_error,
				   "Error calling chmod('%s' 0%llo): %s",
				   orig,
				   (unsigned long long)st.st_mode,
				   strerror(errno));
		goto out;
	}

	ret = 0;
out:
	free(buf);
	return ret;
}

/**
   Create sparse file of specified size.

   returns 0 on success, 1 on fail.
 */
int create_sparse_file(const char *filename, unsigned long int size) {
	int fd = creat(filename, 0660);
	if (-1 == fd) {
		log_perror("creat");
		log_printf(log_error, "Could not create %s", filename);
		return 1;
	}

	const off_t seeksize = 1 << 30; /* try with 30-bit seeks (1GB / seek) */
	assert(size > 1);
	size--;
	if (-1 == lseek(fd, 0, SEEK_SET)) {
		log_perror("initial lseek");
		return 1;
	}

	while (size > seeksize) {
		if (-1 == lseek(fd, seeksize, SEEK_CUR)) {
			log_perror("intermediate lseek");
			return 1;
		}
		size -= seeksize;
	}
	if (-1 == lseek(fd, size - 1, SEEK_CUR)) {
		log_perror("final lseek");
		return 1;
	}

	if (-1 ==
		write(fd, "", 1)) /* A string consists of \0, write 0 to end of file */
	{
		log_perror("write");
		return 1;
	}

	if (-1 == close(fd)) {
		log_perror("close");
		return 1;
	}
	return 0;
}

/* mknod with prepended pathname
   return -1 on failure.
 */
int mknod_inside_chroot(const char *chroot,
						const char *pathname,
						mode_t mode,
						dev_t dev) {
	char *p = alloca(strlen(chroot) + strlen(pathname) + 2);
	int ret;

	if (!p) {
		log_printf(log_error, "error on alloca");
		return -1;
	}

	if (-1 == sprintf(p, "%s/%s", chroot, pathname)) {
		log_printf(log_error, "error on sprintf");
		return -1;
	}

	ret = mknod(p, mode, dev);

	if (ret == -1) {
		/* output the error message for debug, but ignore it here. */
		log_perror(p);
	}

	return ret;
}