embox

Форк
0
/
kremove.c 
126 строк · 2.1 Кб
1
/*
2
 * @file
3
 *
4
 * @date Nov 29, 2012
5
 * @author: Anton Bondarev
6
 */
7

8
#include <unistd.h>
9
#include <fcntl.h>
10
#include <stdio.h>
11
#include <stdlib.h>
12
#include <errno.h>
13
#include <utime.h>
14

15
#include <fs/dir_context.h>
16
#include <fs/fs_driver.h>
17
#include <fs/hlpr_path.h>
18
#include <fs/inode.h>
19
#include <fs/super_block.h>
20
#include <fs/inode_operation.h>
21
#include <fs/kfsop.h>
22
#include <fs/perm.h>
23
#include <fs/vfs.h>
24
#include <fs/path.h>
25

26
#include <security/security.h>
27

28
int kremove(const char *pathname) {
29
	struct path node;
30
	int res;
31

32
	if (0 != (res = fs_perm_lookup(pathname, NULL, &node))) {
33
		errno = -res;
34
		return -1;
35
	}
36

37
	if (S_ISDIR(node.node->i_mode)) {
38
		return krmdir(pathname);
39
	}
40
	else {
41
		return kunlink(pathname);
42
	}
43
}
44

45
int kunlink(const char *pathname) {
46
	struct path node, parent;
47
	struct super_block *sb;
48
	int res;
49

50
	if (0 != (res = fs_perm_lookup(pathname, NULL, &node))) {
51
		errno = -res;
52
		return -1;
53
	}
54

55
	vfs_get_parent(&node, &parent);
56
	if (0 != fs_perm_check(parent.node, S_IWOTH)) {
57
		errno = EACCES;
58
		return -1;
59
	}
60

61
	if (0 != (res = security_node_delete(parent.node, node.node))) {
62
		errno = -res;
63
		return -1;
64
	}
65

66
	sb = node.node->i_sb;
67

68
	if (NULL == sb->sb_iops->ino_remove) {
69
		errno = EPERM;
70
		return -1;
71
	}
72

73
	if (0 != (res = sb->sb_iops->ino_remove(parent.node, node.node))) {
74
		errno = -res;
75
		return -1;
76
	}
77

78
	vfs_del_leaf(node.node);
79

80
	return 0;
81

82
}
83

84
int krmdir(const char *pathname) {
85
	struct path node, parent, child;
86
	struct super_block *sb;
87
	int res;
88

89
	if (0 != (res = fs_perm_lookup(pathname, NULL, &node))) {
90
		errno = -res;
91
		return -1;
92
	}
93

94
	if (0 != (res = fs_perm_check(node.node, S_IWOTH))) {
95
		errno = EACCES;
96
		return -1;
97
	}
98

99
	/* Remove directory only if it's empty */
100
	if (0 == vfs_get_child_next(&node, NULL, &child)) {
101
		errno = ENOTEMPTY;
102
		return -1;
103
	}
104

105
	vfs_get_parent(&node, &parent);
106
	if (0 != (res = security_node_delete(parent.node, node.node))) {
107
		return res;
108
	}
109

110
	sb = node.node->i_sb;
111

112
	if (NULL == sb->sb_iops->ino_remove) {
113
		errno = EPERM;
114
		return -1;
115
	}
116

117
	if (0 != (res = sb->sb_iops->ino_remove(parent.node, node.node))) {
118
		errno = -res;
119
		return -1;
120
	}
121

122
	vfs_del_leaf(node.node);
123

124
	return 0;
125

126
}
127

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.