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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
/*
* Copyright (c) 2009 - 2011 Miek Gieben
* See LICENSE for the license
* rdup-up -- update an directory tree with
* and rdup archive
*
* File related functions
*
*/
#include "rdup-up.h"
#include "protocol.h"
extern int sig;
extern gboolean opt_dry;
extern gboolean opt_table;
extern gboolean opt_quiet;
extern guint opt_strip;
extern gint opt_verbose;
extern GSList *hlink_list;
/* signal.c */
void got_sig(int signal);
static gboolean
mk_time(struct rdup *e)
{
struct utimbuf ut;
/* we don't carry the a_time, how cares anyway with noatime? */
ut.actime = ut.modtime = e->f_mtime;
if (utime(e->f_name, &ut) == -1)
msgd(__func__, __LINE__,_("Failed to set mtime '%s\': %s"), e->f_name, strerror(errno));
return TRUE;
}
static gboolean
mk_chown(struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
uid_t u; gid_t g;
u = lookup_uid(uidhash, e->f_user, e->f_uid);
g = lookup_gid(gidhash, e->f_group, e->f_gid);
/* Capabilities under Linux?? TODO */
if (getuid() == 0)
if (lchown(e->f_name, u, g) == -1 && ! opt_quiet)
msgd(__func__, __LINE__,_("Failed to chown `%s\': %s"), e->f_name, strerror(errno));
return TRUE;
}
static gboolean
mk_mode(struct rdup *e)
{
chmod(e->f_name, e->f_mode);
return TRUE;
}
static gboolean
mk_meta(struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
mk_mode(e);
mk_chown(e, uidhash, gidhash);
mk_time(e);
return TRUE;
}
static gboolean
mk_dev(struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
gchar *parent;
struct stat *st;
if (opt_dry)
return TRUE;
if (!rm(e->f_name))
return FALSE;
if (mknod(e->f_name, e->f_mode, e->f_rdev) == -1) {
if (errno == EACCES) {
parent = dir_parent(e->f_name);
st = dir_write(parent);
if (mknod(e->f_name, e->f_mode, e->f_rdev) == -1) {
msgd(__func__, __LINE__, _("Failed to make device `%s\': %s"), e->f_name, strerror(errno));
dir_restore(parent, st);
g_free(parent);
return FALSE;
}
dir_restore(parent, st);
g_free(parent);
} else {
msgd(__func__, __LINE__, _("Failed to make device `%s\': %s"), e->f_name, strerror(errno));
return FALSE;
}
}
mk_meta(e, uidhash, gidhash);
return TRUE;
}
static gboolean
mk_sock(struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
gchar *parent;
struct stat *st;
if (opt_dry)
return TRUE;
if (!rm(e->f_name))
return FALSE;
if (mkfifo(e->f_name, e->f_mode) == -1) {
if (errno == EACCES) {
parent = dir_parent(e->f_name);
st = dir_write(parent);
if (mkfifo(e->f_name, e->f_mode) == -1) {
msgd(__func__, __LINE__, _("Failed to make socket `%s\': %s"), e->f_name, strerror(errno));
dir_restore(parent, st);
g_free(parent);
return FALSE;
}
dir_restore(parent, st);
g_free(parent);
} else {
msgd(__func__, __LINE__, _("Failed to make socket `%s\': %s"), e->f_name, strerror(errno));
return FALSE;
}
}
mk_meta(e, uidhash, gidhash);
return TRUE;
}
static gboolean
mk_link(struct rdup *e, char *p, GHashTable *uidhash, GHashTable *gidhash)
{
struct stat *st;
gchar *t;
gchar *parent;
if (opt_dry)
return TRUE;
if (!rm(e->f_name))
return FALSE;
/* symlink */
if (S_ISLNK(e->f_mode)) {
if (symlink(e->f_target, e->f_name) == -1) {
if (errno == EACCES) {
parent = dir_parent(e->f_name);
st = dir_write(parent);
if (symlink(e->f_target, e->f_name) == -1) {
msgd(__func__, __LINE__, _("Failed to make symlink `%s -> %s\': %s"), e->f_name, e->f_target, strerror(errno));
dir_restore(parent, st);
g_free(parent);
return FALSE;
}
dir_restore(parent, st);
g_free(parent);
} else {
msgd(__func__, __LINE__, _("Failed to make symlink `%s -> %s\': %s"), e->f_name, e->f_target, strerror(errno));
return FALSE;
}
}
mk_chown(e, uidhash, gidhash);
return TRUE;
}
/* hardlink */
/* target must also fall in backup dir */
t = g_strdup_printf("%s%s", p, e->f_target);
e->f_target = t;
hlink_list = g_slist_append(hlink_list, e);
return TRUE;
}
static gboolean
mk_reg(FILE *in, struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
FILE *out = NULL;
char *buf;
gchar *parent;
size_t bytes;
gboolean ok = TRUE;
gboolean old_dry = opt_dry;
struct stat *st;
/* with opt_dry we can't just return TRUE; as we may
* need to suck in the file's content - which is thrown
* away in that case */
if (! e->f_name) {
/* fake an opt_dry */
opt_dry = TRUE;
}
if (!opt_dry) {
if (!rm(e->f_name)) {
opt_dry = old_dry;
return FALSE;
}
}
if (!opt_dry && !(out = fopen(e->f_name, "w"))) {
if (errno == EACCES) {
parent = dir_parent(e->f_name);
st = dir_write(parent);
if (!(out = fopen(e->f_name, "w"))) {
msgd(__func__, __LINE__, _("Failed to open file `%s\': %s"), e->f_name, strerror(errno));
ok = FALSE;
}
dir_restore(parent, st);
} else {
msgd(__func__, __LINE__, _("Failed to open file `%s\': %s"), e->f_name, strerror(errno));
ok = FALSE;
}
}
/* we need to read the input to not upset
* the flow into rdup-up, but we are not
* creating anything when opt_dry is active
*/
buf = g_malloc(BUFSIZE + 1);
while ((bytes = block_in_header(in)) > 0) {
if (block_in(in, bytes, buf) == -1) {
if (out)
fclose(out);
opt_dry = old_dry;
return FALSE;
}
if (ok && !opt_dry) {
if (fwrite(buf, sizeof(char), bytes, out) != bytes) {
msgd(__func__, __LINE__, _("Write failure `%s\': %s"), e->f_name, strerror(errno));
if (out)
fclose(out);
opt_dry = old_dry;
return FALSE;
}
}
}
g_free(buf);
if (ok && out)
fclose(out);
if (ok && !opt_dry)
mk_meta(e, uidhash, gidhash);
#ifdef DEBUG
msgd(__func__, __LINE__, "Wrote file `%s\'", e->f_name);
#endif /* DEBUG */
opt_dry = old_dry;
return TRUE;
}
static gboolean
mk_dir(struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
struct stat *s;
struct stat st;
gchar *parent;
if (opt_dry)
return TRUE;
if (lstat(e->f_name, &st) == 0) {
if (S_ISDIR(st.st_mode)) {
#if 0
msgd(__func__, __LINE__, _("Updating current dir `%s\'"), e->f_name);
#endif /* DEBUG */
/* some dir is here - update the perms and ownership */
mk_meta(e, uidhash, gidhash);
return TRUE;
}
}
/* nothing there */
if (mkdir(e->f_name, e->f_mode) == -1) {
if (errno == EACCES) {
/* make parent dir writable, and try again */
parent = dir_parent(e->f_name);
#ifdef DEBUG
msgd(__func__, __LINE__, _("EACCES for `%s\'"), parent);
#endif /* DEBUG */
s = dir_write(parent);
if (!s)
msgd(__func__, __LINE__, _("Failed to make parent writable"));
if (mkdir(e->f_name, e->f_mode) == -1) {
msgd(__func__, __LINE__, _("Failed to create directory `%s\': %s"), e->f_name, strerror(errno));
dir_restore(parent, s);
g_free(parent);
return FALSE;
}
dir_restore(parent, s);
g_free(parent);
} else {
msgd(__func__, __LINE__, _("Failed to create directory `%s\': %s"), e->f_name, strerror(errno));
return FALSE;
}
}
mk_meta(e, uidhash, gidhash);
return TRUE;
}
/* make (or delete) an object in the filesystem */
gboolean
mk_obj(FILE *in, char *p, struct rdup *e, GHashTable *uidhash, GHashTable *gidhash)
{
if (opt_verbose == 1 && e->f_name) {
if (S_ISLNK(e->f_mode) || e->f_lnk)
fprintf(stdout, "%s -> %s\n", e->f_name, e->f_target);
else
fprintf(stdout, "%s\n", e->f_name);
}
if (opt_table)
rdup_write_table(e, stdout);
if (opt_verbose == 2 && e->f_name)
fprintf(stdout, "%c %d %d %s\n",
e->plusmin == PLUS ? '+' : '-',
e->f_uid, e->f_gid, e->f_name);
/* split here - or above - return when path is zero length
* for links check that the f_size is zero */
switch(e->plusmin) {
case MINUS:
if (opt_dry || ! e->f_name)
return TRUE;
return rm(e->f_name);
case PLUS:
/* opt_dry handled within the subfunctions */
/* only files, no hardlinks! */
if (S_ISREG(e->f_mode) && ! e->f_lnk )
return mk_reg(in, e, uidhash, gidhash);
/* no name, we can exit here - for files this is handled
* in mk_reg, because we may need to suck in data */
if (e->f_name == NULL)
return TRUE;
if (S_ISDIR(e->f_mode))
return mk_dir(e, uidhash, gidhash);
/* First sym and hardlinks and then regular files */
if (S_ISLNK(e->f_mode) || e->f_lnk)
return mk_link(e, p, uidhash, gidhash);
if (S_ISBLK(e->f_mode) || S_ISCHR(e->f_mode))
return mk_dev(e, uidhash, gidhash);
if (S_ISSOCK(e->f_mode))
return mk_sock(e, uidhash, gidhash);
if (S_ISFIFO(e->f_mode))
return mk_sock(e, uidhash, gidhash);
}
/* only reached during the heat death of the universe */
return TRUE;
}
/* Create the remaining hardlinks in the target directory */
gboolean
mk_hlink(GSList *h)
{
struct rdup *e;
GSList *p;
struct stat *st;
gchar *parent;
if (opt_dry)
return TRUE;
for (p = g_slist_nth(h, 0); p; p = p->next) {
e = (struct rdup *)p->data;
if (link(e->f_target, e->f_name) == -1) {
if (errno == EACCES) {
parent = dir_parent(e->f_name);
st = dir_write(parent);
if (link(e->f_target, e->f_name) == -1) {
msgd(__func__, __LINE__, _("Failed to create hardlink `%s -> %s\': %s"),
e->f_name, e->f_target, strerror(errno));
dir_restore(parent, st);
g_free(parent);
return FALSE;
}
dir_restore(parent, st);
g_free(parent);
return TRUE;
} else {
msgd(__func__, __LINE__, _("Failed to create hardlink `%s -> %s\': %s"),
e->f_name, e->f_target, strerror(errno));
return FALSE;
}
}
}
return TRUE;
}
|