summaryrefslogtreecommitdiff
path: root/src/rmdir.c
blob: 06a9b5ad600843b14607c4d1ba2e4c41b4afdfd9 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
*  C Implementation: rmdir
*
* Description: unionfs rmdir() call
*              If the directory to remove 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 <sys/types.h>
#include <dirent.h>

#include "unionfs.h"
#include "opts.h"
#include "debug.h"
#include "cow.h"
#include "general.h"
#include "findbranch.h"
#include "string.h"
#include "readdir.h"
#include "usyslog.h"

/**
  * If the branch that has the directory to be removed is in read-write mode,
  * we can really delete the file.
  */
static int rmdir_rw(const char *path, int branch_rw) {
	DBG("%s\n", path);

	char p[PATHLEN_MAX];
	if (BUILD_PATH(p, uopt.branches[branch_rw].path, path)) return ENAMETOOLONG;

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

	return 0;
}

/**
  * If the branch that has the directory to be removed 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 directory.
  */
static int rmdir_ro(const char *path, int branch_ro) {
	DBG("%s\n", path);

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

	if (branch_rw < 0) return -EACCES;

	
	DBG("Calling hide_dir\n");
	if (hide_dir(path, branch_rw) == -1) {
		switch (errno) {
		case (EEXIST):
		case (ENOTDIR):
		case (ENOTEMPTY):
			// catch errors not allowed for rmdir()
			USYSLOG (LOG_ERR, "%s: Creating the whiteout failed: %s\n",
				__func__, strerror(errno));
			errno = EFAULT;
		}
		return errno;
	}

	return 0;
}

/**
  * rmdir() call
  */
int unionfs_rmdir(const char *path) {
	DBG("%s\n", path);

	if (dir_not_empty(path)) return -ENOTEMPTY;

	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 = rmdir_ro(path, i);
	} else {
		// read-write branch
		res = rmdir_rw(path, i);
		if (res == 0) {
			// No need to be root, whiteouts are created as root!
			maybe_whiteout(path, i, WHITEOUT_DIR);
		}
	}

	return -res;
}