summaryrefslogtreecommitdiff
path: root/cmd/srv9p/static.c
blob: a6da53f4bbbf76901140691d86725a9e37aafce0 (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
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
#include <assert.h>

#include "static.h"

#define UNUSED(name) /* name __attribute__((unused)) */
#define p9_str(cstr)    ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr })
#define p9_nulstr       ((struct lib9p_s){ .len = 0, .utf8 = NULL })

/******************************************************************************/

static struct lib9p_srv_file *static_dir_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	return &file->header;
}

static void static_dir_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	/* do nothing */
}

static uint32_t static_dir_io(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, lib9p_o_t UNUSED(flags)) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	return 1;
}

static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	return (struct lib9p_stat){
		.kern_type                      = 0,
		.kern_dev                       = 0,
		.file_qid                       = {
			.type = LIB9P_QT_DIR,
			.vers = 1,
			.path = file->pathnum,
		},
		.file_mode                = LIB9P_DM_DIR | (file->perm & 0555),
		.file_atime               = file->atime,
		.file_mtime               = file->mtime,
		.file_size                = 0,
		.file_name                = p9_str(file->name),
		.file_owner_uid           = p9_str(file->u_name),
		.file_owner_gid           = p9_str(file->g_name),
		.file_last_modified_uid   = p9_str(file->m_name),
		.file_extension           = p9_nulstr,
		.file_owner_n_uid         = file->u_num,
		.file_owner_n_gid         = file->g_num,
		.file_last_modified_n_uid = file->m_num,
	};
}

static void static_dir_wstat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file,
                              struct lib9p_stat UNUSED(new)) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
}

static void static_dir_remove(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) {
	assert(ctx);
	struct static_dir *file = (struct static_dir *)_file;
	assert(file);

	lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
}


static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
                                               char *UNUSED(childname)) {
	assert(ctx);
	struct static_dir *dir = (struct static_dir *)_dir;
	assert(dir);

	lib9p_error(&ctx->basectx, LINUX_EFAULT, "TODO: dopen");
	return NULL;
}

static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
                                                 char *UNUSED(childname),
                                                 lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) {
	assert(ctx);
	struct static_dir *dir = (struct static_dir *)_dir;
	assert(dir);

	lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem");
	return NULL;
}

static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir,
                               uint8_t *buf,
                               uint32_t byte_count,
                               size_t _obj_offset) {
	assert(ctx);
	struct static_dir *dir = (struct static_dir *)_dir;
	assert(dir);

	uint32_t byte_offset = 0;
	size_t obj_offset = _obj_offset;
	while (dir->members[obj_offset]) {
		struct lib9p_srv_file *filep = dir->members[obj_offset];
		struct lib9p_stat stat = filep->vtable->stat(ctx, filep);
		if (lib9p_ctx_has_error(&ctx->basectx))
			break;
		uint32_t nbytes = lib9p_marshal_stat(&ctx->basectx, byte_count-byte_offset, &stat,
		                                     &buf[byte_offset]);
		if (!nbytes) {
			if (obj_offset == _obj_offset)
				lib9p_error(&ctx->basectx,
				            LINUX_ERANGE, "stat object does not fit into negotiated max message size");
			break;
		}
		byte_offset += nbytes;
		obj_offset++;
	}
	return obj_offset - _obj_offset;
}

struct lib9p_srv_file_vtable static_dir_vtable = {
	.clone   = static_dir_clone,
	.free    = static_dir_free,

	.io      = static_dir_io,
	.stat    = static_dir_stat,
	.wstat   = static_dir_wstat,
	.remove  = static_dir_remove,

	.dopen   = static_dir_dopen,
	.dcreate = static_dir_dcreate,

	.dread   = static_dir_dread,
};