summaryrefslogtreecommitdiff
path: root/src/unlink.c
blob: 06a7fa237330e22a6365da492821997feac5dd78 (plain)
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
/*
*  C Implementation: unlink
*
* Description: unionfs unlink() call
*              If the file to unlink exists in a lower branch, create a
*              file with a tag informing other functions that the file
*              is hidden.
*
* original implementation by Radek Podgorny
*
* License: BSD-style license
* Copyright: Radek Podgorny <radek@podgorny.cz>,
*            Bernd Schubert <bernd-schubert@gmx.de>
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <errno.h>
#include <libgen.h>

#include "unionfs.h"
#include "opts.h"
#include "debug.h"
#include "cow.h"
#include "general.h"
#include "findbranch.h"

/**
  * If the branch that has the file to be unlinked is in read-only mode,
  * we create a file with a HIDE tag in an upper level branch.
  * To other fuse functions this tag means, not to expose the 
  * lower level file.
  */
static int unlink_ro(const char *path, int branch_ro) {
	DBG_IN()	

	// find a writable branch above branch_ro
	int branch_rw = find_lowest_rw_branch(branch_ro);

	if (branch_rw < 0) return EACCES;

	if (hide_file(path, branch_rw) == -1) {
		// creating the file with the hide tag failed
		// TODO: open() error messages are not optimal on unlink()
		return errno;
	}

	return 0;
}

/**
  * If the branch that has the file to be unlinked is in read-write mode,
  * we can really delete the file.
  */
static int unlink_rw(const char *path, int branch_rw) {
	DBG_IN();
	
	char p[PATHLEN_MAX];
	snprintf(p, PATHLEN_MAX, "%s%s", uopt.branches[branch_rw].path, path);

	int res = unlink(p);
	if (res == -1) return errno;

	return 0;
}

/**
  * unlink() call
  */
int unionfs_unlink(const char *path) {
	DBG_IN();
	
	int i = find_rorw_branch(path);
	if (i == -1) return errno;

	int res;
	if (!uopt.branches[i].rw) {
		// read-only branch
		if (!uopt.cow_enabled) {
			res = EROFS;
		} else {
			res = unlink_ro(path, i);
		}
	} else {
		// read-write branch
		res = unlink_rw(path, i);
		if (res == 0) {
			// No need to be root, whiteouts are created as root!
			maybe_whiteout(path, i, WHITEOUT_FILE);
		}
	}

	return -res;
}