embox

Форк
0
/
kfsop.c 
163 строки · 3.2 Кб
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

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

25
#include <security/security.h>
26

27
static int create_new_node(struct path *parent, const char *name, mode_t mode) {
28
	struct path node;
29
	int retval = 0;
30

31
	if(NULL == parent->node->i_sb) {
32
		return -EINVAL;
33
	}
34

35
	if (0 != vfs_create(parent, name, mode, &node)) {
36
		return -ENOMEM;
37
	}
38

39
	if ((mode & VFS_DIR_VIRTUAL) && S_ISDIR(mode)) {
40
		node.node->i_sb = parent->node->i_sb;
41
	} else {
42
		struct super_block *sb = parent->node->i_sb;
43

44
		if (!sb || !sb->sb_iops || !sb->sb_iops->ino_create) {
45
			retval = -ENOSYS;/* FIXME EPERM ?*/
46
			goto out;
47
		}
48

49
		retval = sb->sb_iops->ino_create(node.node, parent->node, node.node->i_mode);
50
		if (retval) {
51
			goto out;
52
		}
53
	}
54
	/* XXX it's here and not in vfs since vfs node associated with drive after
55
 	 * creating. security may call driver dependent features, like setting
56
	 * xattr
57
	 */
58
	security_node_cred_fill(node.node);
59
	return 0;
60
out:
61
	vfs_del_leaf(node.node);
62
	return retval;
63
}
64

65
int kmkdir(const char *pathname, mode_t mode) {
66
	struct path node;
67
	const char *lastpath, *ch;
68
	int res;
69

70
	if (-ENOENT != (res = fs_perm_lookup(pathname, &lastpath, &node))) {
71
		errno = EEXIST;
72
		return -1;
73
	}
74

75
	if (NULL != (ch = strchr(lastpath, '/'))
76
			&& NULL != path_next(ch, NULL)) {
77
		errno = ENOENT;
78
		return -1;
79
	}
80

81
	if_mounted_follow_down(&node);
82

83
	if (0 != fs_perm_check(node.node, S_IWOTH)) {
84
		errno = EACCES;
85
		return -1;
86
	}
87

88
	if (0 != (res = security_node_create(node.node, S_IFDIR | mode))) {
89
		errno = -res;
90
		return -1;
91
	}
92

93
	if (0 != (res = create_new_node(&node, lastpath, S_IFDIR | mode))) {
94
		errno = -res;
95
		return -1;
96
	}
97

98
	return 0;
99
}
100

101
int kcreat(struct path *dir_path, const char *path, mode_t mode, struct path *child) {
102
	struct super_block *sb;
103
	int ret;
104

105
	assert(dir_path->node);
106

107
	path = path_next(path, NULL);
108

109
	if (!path) {
110
		SET_ERRNO(EINVAL);
111
		return -1;
112
	}
113

114
	if (NULL != strchr(path, '/')) {
115
		SET_ERRNO(ENOENT);
116
		return -1;
117
	}
118

119
	if (0 != (fs_perm_check(dir_path->node, S_IWOTH))) {
120
		SET_ERRNO(EACCES);
121
		return -1;
122
	}
123

124
	if (0 != (ret = security_node_create(dir_path->node, S_IFREG | mode))) {
125
		SET_ERRNO(-ret);
126
		return -1;
127
	}
128

129
	if (0 != vfs_create(dir_path, path, S_IFREG | mode, child)) {
130
		SET_ERRNO(ENOMEM);
131
		return -1;
132
	}
133

134
	child->mnt_desc = dir_path->mnt_desc;
135

136
	if(!dir_path->node || !dir_path->node->i_sb) {
137
		SET_ERRNO(EBADF);
138
		vfs_del_leaf(child->node);
139
		return -1;
140
	}
141

142
	sb = dir_path->node->i_sb;
143
	if (!sb || !sb->sb_iops || !sb->sb_iops->ino_create) {
144
		SET_ERRNO(EBADF);
145
		vfs_del_leaf(child->node);
146
		return -1;
147
	}
148
	
149
	ret = sb->sb_iops->ino_create(child->node, dir_path->node, child->node->i_mode);
150
	if (0 != ret) {
151
		SET_ERRNO(-ret);
152
		vfs_del_leaf(child->node);
153
		return -1;
154
	}
155

156
	/* XXX it's here and not in vfs since vfs node associated with drive after
157
 	 * creating. security may call driver dependent features, like setting
158
	 * xattr
159
	 */
160
	security_node_cred_fill(child->node);
161

162
	return 0;
163
}
164

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

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

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

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