-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqttfs_rename.c
186 lines (158 loc) · 5.47 KB
/
mqttfs_rename.c
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (C) 2022 Mikhail Burakov. This file is part of mqttfs.
*
* mqttfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mqttfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mqttfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <fuse.h>
#include <search.h>
#include <stdio.h>
#include <string.h>
#include <threads.h>
#include "log.h"
#include "mqtt.h"
#include "mqttfs.h"
#include "node.h"
#include "str.h"
#ifndef RENAME_NOREPLACE
// mburakov: musl does not define this.
#define RENAME_NOREPLACE (1 << 0)
#endif // RENAME_NOREPLACE
#ifndef RENAME_EXCHANGE
// mburakov: musl does not define this.
#define RENAME_EXCHANGE (1 << 1)
#endif // RENAME_EXCHANGE
static int RenameExchange(struct Context* context, struct Node** from_nodep,
void** to_nodep) {
// mburakov: Check below reproduces behavior described in man 2 rename.
struct Node* from_node = *from_nodep;
struct Node* to_node = *to_nodep;
if (from_node->is_dir != to_node->is_dir)
return from_node->is_dir ? -ENOTDIR : -EISDIR;
// mburakov: Publish payload with the updated topic.
if (!from_node->is_dir) {
if (!MqttPublish(context->mqtt, &to_node->path, from_node->data,
from_node->size)) {
LOG(ERR, "failed to publish topic");
return -EIO;
}
// mburakov: Delivery might not be done yet, try to cancel.
MqttCancel(context->mqtt, &from_node->path);
}
// mburakov: Exchange names first.
struct Str temp = from_node->path;
from_node->path = to_node->path;
to_node->path = temp;
// mburakov: Exchange nodes afterwards.
*from_nodep = to_node;
*to_nodep = from_node;
return 0;
}
static int RenameNoreplace(struct Context* context, struct Node** from_nodep,
void** to_nodep, struct Str* to_view) {
if (!to_nodep) {
LOG(ERR, "failed to search node: %s", strerror(errno));
return -EIO;
}
if (*to_nodep != to_view) return -EEXIST;
// TODO(mburakov): Should recursive creation be allowed?
// TODO(mburakov): Should parent node type be verified?
int result;
struct Str to_copy;
if (!StrCopy(&to_copy, to_view)) {
LOG(ERR, "failed to copy string: %s", strerror(errno));
result = -EIO;
goto rollback_tsearch;
}
// mburakov: Publish payload with the updated topic.
struct Node* from_node = *from_nodep;
if (!from_node->is_dir) {
if (!MqttPublish(context->mqtt, to_view, from_node->data,
from_node->size)) {
LOG(ERR, "failed to publish topic");
result = -EIO;
goto rollback_str_copy;
}
// mburakov: Delivery might not be done yet, try to cancel.
MqttCancel(context->mqtt, &from_node->path);
}
// mburakov: First, remove the node.
tdelete(from_node, &context->root_node, NodeCompare);
// mburakov: Second, update the node path.
StrFree(&from_node->path);
from_node->path = to_copy;
// mburakov: Finally, put the node back.
*to_nodep = from_node;
return 0;
rollback_str_copy:
StrFree(&to_copy);
rollback_tsearch:
tdelete(to_view, &context->root_node, NodeCompare);
return result;
}
static int RenameNormal(struct Context* context, struct Node** from_nodep,
void** to_nodep, struct Str* to_view) {
if (!to_nodep) {
LOG(ERR, "failed to search node: %s", strerror(errno));
return -EIO;
}
if (*to_nodep == to_view)
return RenameNoreplace(context, from_nodep, to_nodep, to_view);
int result = RenameExchange(context, from_nodep, to_nodep);
if (result) return result;
// TODO(mburakov): This cleans up original remains of target node. But what if
// it is open or polled? I have no idea how is this supposed to work...
struct Node* from_node = *from_nodep;
tdelete(from_node, &context->root_node, NodeCompare);
MqttCancel(context->mqtt, &from_node->path);
NodeDestroy(from_node);
return 0;
}
int MqttfsRename(const char* from, const char* to, unsigned int flags) {
struct Context* context = fuse_get_context()->private_data;
if (mtx_lock(&context->root_mutex) != thrd_success) {
LOG(ERR, "failed to lock nodes mutex: %s", strerror(errno));
return -EIO;
}
int result;
struct Str from_view = StrView(from + 1);
struct Node** from_nodep =
tfind(&from_view, &context->root_node, NodeCompare);
if (!from_nodep) {
result = -ENOENT;
goto rollback_mtx_lock;
}
void** to_nodep;
struct Str to_view = StrView(to + 1);
switch (flags) {
case 0:
to_nodep = tsearch(&to_view, &context->root_node, NodeCompare);
result = RenameNormal(context, from_nodep, to_nodep, &to_view);
break;
case RENAME_EXCHANGE:
to_nodep = tfind(&to_view, &context->root_node, NodeCompare);
result = RenameExchange(context, from_nodep, to_nodep);
break;
case RENAME_NOREPLACE:
to_nodep = tsearch(&to_view, &context->root_node, NodeCompare);
result = RenameNoreplace(context, from_nodep, to_nodep, &to_view);
break;
default:
result = -EINVAL;
break;
}
rollback_mtx_lock:
mtx_unlock(&context->root_mutex);
return result;
}