littlefs-rust-core 0.1.0

Port of LittleFS to unsafe rust
Documentation
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! Rename. Per lfs.c lfs_rename_.

use crate::dir::commit::{lfs_dir_commit, lfs_dir_drop};
use crate::dir::fetch::lfs_dir_fetch;
use crate::dir::find::lfs_dir_find;
use crate::dir::traverse::lfs_dir_get;
use crate::dir::{LfsMdir, LfsMlist};
use crate::error::{
    LFS_ERR_INVAL, LFS_ERR_ISDIR, LFS_ERR_NAMETOOLONG, LFS_ERR_NOENT, LFS_ERR_NOTDIR,
    LFS_ERR_NOTEMPTY,
};
use crate::fs::parent::lfs_fs_pred;
use crate::fs::superblock::{lfs_fs_forceconsistency, lfs_fs_prepmove, lfs_fs_preporphans};
use crate::lfs_gstate::{lfs_gstate_hasmove, lfs_gstate_hasorphans};
use crate::lfs_type::lfs_type::{
    LFS_FROM_MOVE, LFS_TYPE_CREATE, LFS_TYPE_DELETE, LFS_TYPE_DIR, LFS_TYPE_STRUCT,
};
use crate::tag::{lfs_mattr, lfs_mktag, lfs_mktag_if, lfs_tag_id, lfs_tag_type3};
use crate::types::lfs_block_t;
use crate::util::{
    lfs_pair_cmp, lfs_pair_fromle32, lfs_path_isdir, lfs_path_islast, lfs_path_namelen,
};

/// Translation docs: Renames a file or directory from oldpath to newpath. Handles
/// same-directory renames, cross-directory moves, and overwriting existing entries.
/// When overwriting a directory, it must be empty. On failure returns a negative error code.
///
/// C: lfs.c:3961-4138
///
/// C:
/// ```c
/// static int lfs_rename_(lfs_t *lfs, const char *oldpath, const char *newpath) {
///     // deorphan if we haven't yet, needed at most once after poweron
///     int err = lfs_fs_forceconsistency(lfs);
///     if (err) {
///         return err;
///     }
///
///     // find old entry
///     lfs_mdir_t oldcwd;
///     lfs_stag_t oldtag = lfs_dir_find(lfs, &oldcwd, &oldpath, NULL);
///     if (oldtag < 0 || lfs_tag_id(oldtag) == 0x3ff) {
///         return (oldtag < 0) ? (int)oldtag : LFS_ERR_INVAL;
///     }
///
///     // find new entry
///     lfs_mdir_t newcwd;
///     uint16_t newid;
///     lfs_stag_t prevtag = lfs_dir_find(lfs, &newcwd, &newpath, &newid);
///     if ((prevtag < 0 || lfs_tag_id(prevtag) == 0x3ff) &&
///             !(prevtag == LFS_ERR_NOENT && lfs_path_islast(newpath))) {
///         return (prevtag < 0) ? (int)prevtag : LFS_ERR_INVAL;
///     }
///
///     // if we're in the same pair there's a few special cases...
///     bool samepair = (lfs_pair_cmp(oldcwd.pair, newcwd.pair) == 0);
///     uint16_t newoldid = lfs_tag_id(oldtag);
///
///     struct lfs_mlist prevdir;
///     prevdir.next = lfs->mlist;
///     if (prevtag == LFS_ERR_NOENT) {
///         // if we're a file, don't allow trailing slashes
///         if (lfs_path_isdir(newpath)
///                 && lfs_tag_type3(oldtag) != LFS_TYPE_DIR) {
///             return LFS_ERR_NOTDIR;
///         }
///
///         // check that name fits
///         lfs_size_t nlen = lfs_path_namelen(newpath);
///         if (nlen > lfs->name_max) {
///             return LFS_ERR_NAMETOOLONG;
///         }
///
///         // there is a small chance we are being renamed in the same
///         // directory/ to an id less than our old id, the global update
///         // to handle this is a bit messy
///         if (samepair && newid <= newoldid) {
///             newoldid += 1;
///         }
///     } else if (lfs_tag_type3(prevtag) != lfs_tag_type3(oldtag)) {
///         return (lfs_tag_type3(prevtag) == LFS_TYPE_DIR)
///                 ? LFS_ERR_ISDIR
///                 : LFS_ERR_NOTDIR;
///     } else if (samepair && newid == newoldid) {
///         // we're renaming to ourselves??
///         return 0;
///     } else if (lfs_tag_type3(prevtag) == LFS_TYPE_DIR) {
///         // must be empty before removal
///         lfs_block_t prevpair[2];
///         lfs_stag_t res = lfs_dir_get(lfs, &newcwd, LFS_MKTAG(0x700, 0x3ff, 0),
///                 LFS_MKTAG(LFS_TYPE_STRUCT, newid, 8), prevpair);
///         if (res < 0) {
///             return (int)res;
///         }
///         lfs_pair_fromle32(prevpair);
///
///         // must be empty before removal
///         err = lfs_dir_fetch(lfs, &prevdir.m, prevpair);
///         if (err) {
///             return err;
///         }
///
///         if (prevdir.m.count > 0 || prevdir.m.split) {
///             return LFS_ERR_NOTEMPTY;
///         }
///
///         // mark fs as orphaned
///         err = lfs_fs_preporphans(lfs, +1);
///         if (err) {
///             return err;
///         }
///
///         // I know it's crazy but yes, dir can be changed by our parent's
///         // commit (if predecessor is child)
///         prevdir.type = 0;
///         prevdir.id = 0;
///         lfs->mlist = &prevdir;
///     }
///
///     if (!samepair) {
///         lfs_fs_prepmove(lfs, newoldid, oldcwd.pair);
///     }
///
///     // move over all attributes
///     err = lfs_dir_commit(lfs, &newcwd, LFS_MKATTRS(
///             {LFS_MKTAG_IF(prevtag != LFS_ERR_NOENT,
///                 LFS_TYPE_DELETE, newid, 0), NULL},
///             {LFS_MKTAG(LFS_TYPE_CREATE, newid, 0), NULL},
///             {LFS_MKTAG(lfs_tag_type3(oldtag),
///                 newid, lfs_path_namelen(newpath)), newpath},
///             {LFS_MKTAG(LFS_FROM_MOVE, newid, lfs_tag_id(oldtag)), &oldcwd},
///             {LFS_MKTAG_IF(samepair,
///                 LFS_TYPE_DELETE, newoldid, 0), NULL}));
///     if (err) {
///         lfs->mlist = prevdir.next;
///         return err;
///     }
///
///     // let commit clean up after move (if we're different! otherwise move
///     // logic already fixed it for us)
///     if (!samepair && lfs_gstate_hasmove(&lfs->gstate)) {
///         // prep gstate and delete move id
///         lfs_fs_prepmove(lfs, 0x3ff, NULL);
///         err = lfs_dir_commit(lfs, &oldcwd, LFS_MKATTRS(
///                 {LFS_MKTAG(LFS_TYPE_DELETE, lfs_tag_id(oldtag), 0), NULL}));
///         if (err) {
///             lfs->mlist = prevdir.next;
///             return err;
///         }
///     }
///
///     lfs->mlist = prevdir.next;
///     if (lfs_gstate_hasorphans(&lfs->gstate)) {
///         LFS_ASSERT(prevtag != LFS_ERR_NOENT
///                 && lfs_tag_type3(prevtag) == LFS_TYPE_DIR);
///
///         // fix orphan
///         err = lfs_fs_preporphans(lfs, -1);
///         if (err) {
///             return err;
///         }
///
///         err = lfs_fs_pred(lfs, prevdir.m.pair, &newcwd);
///         if (err) {
///             return err;
///         }
///
///         err = lfs_dir_drop(lfs, &newcwd, &prevdir.m);
///         if (err) {
///             return err;
///         }
///     }
///
///     return 0;
/// }
/// #endif
///
/// static lfs_ssize_t lfs_getattr_(lfs_t *lfs, const char *path,
///         uint8_t type, void *buffer, lfs_size_t size) {
///     lfs_mdir_t cwd;
///     lfs_stag_t tag = lfs_dir_find(lfs, &cwd, &path, NULL);
///     if (tag < 0) {
///         return tag;
///     }
///
///     uint16_t id = lfs_tag_id(tag);
///     if (id == 0x3ff) {
///         // special case for root
///         id = 0;
///         int err = lfs_dir_fetch(lfs, &cwd, lfs->root);
///         if (err) {
///             return err;
///         }
///     }
///
///     tag = lfs_dir_get(lfs, &cwd, LFS_MKTAG(0x7ff, 0x3ff, 0),
///             LFS_MKTAG(LFS_TYPE_USERATTR + type,
///                 id, lfs_min(size, lfs->attr_max)),
///             buffer);
///     if (tag < 0) {
///         if (tag == LFS_ERR_NOENT) {
///             return LFS_ERR_NOATTR;
///         }
///
///         return tag;
///     }
///
///     return lfs_tag_size(tag);
/// }
/// ```
fn slice_until_nul(ptr: *const u8) -> &'static [u8] {
    if ptr.is_null() {
        return &[];
    }
    unsafe {
        let mut len = 0;
        #[cfg(feature = "loop_limits")]
        const MAX_SLICE_NUL_ITER: u32 = 4096;
        #[cfg(feature = "loop_limits")]
        let mut iter: u32 = 0;
        while *ptr.add(len) != 0 {
            #[cfg(feature = "loop_limits")]
            {
                if iter >= MAX_SLICE_NUL_ITER {
                    panic!(
                        "loop_limits: MAX_SLICE_NUL_ITER ({}) exceeded",
                        MAX_SLICE_NUL_ITER
                    );
                }
                iter += 1;
            }
            len += 1;
        }
        core::slice::from_raw_parts(ptr, len)
    }
}

pub fn lfs_rename_(lfs: *mut super::lfs::Lfs, oldpath: *const u8, newpath: *const u8) -> i32 {
    let err = lfs_fs_forceconsistency(lfs);
    if err != 0 {
        return crate::lfs_pass_err!(err);
    }

    unsafe {
        let mut oldcwd = LfsMdir {
            pair: [0, 0],
            rev: 0,
            off: 0,
            etag: 0,
            count: 0,
            erased: false,
            split: false,
            tail: [(*lfs).root[0], (*lfs).root[1]],
        };
        let mut oldpath_ptr = oldpath;
        let oldtag = lfs_dir_find(lfs, &mut oldcwd, &mut oldpath_ptr, core::ptr::null_mut());
        if oldtag < 0 || lfs_tag_id(oldtag as u32) == 0x3ff {
            return if oldtag < 0 { oldtag } else { LFS_ERR_INVAL };
        }

        let mut newcwd = LfsMdir {
            pair: [0, 0],
            rev: 0,
            off: 0,
            etag: 0,
            count: 0,
            erased: false,
            split: false,
            tail: [(*lfs).root[0], (*lfs).root[1]],
        };
        let mut newpath_ptr = newpath;
        let mut newid: u16 = 0;
        let prevtag = lfs_dir_find(lfs, &mut newcwd, &mut newpath_ptr, &mut newid);
        let newpath_slice = slice_until_nul(newpath_ptr);
        if (prevtag < 0 || lfs_tag_id(prevtag as u32) == 0x3ff)
            && !(prevtag == LFS_ERR_NOENT && lfs_path_islast(newpath_slice))
        {
            return if prevtag < 0 { prevtag } else { LFS_ERR_INVAL };
        }

        let samepair = lfs_pair_cmp(&oldcwd.pair, &newcwd.pair) == 0;
        let mut newoldid = lfs_tag_id(oldtag as u32);

        let mut prevdir = LfsMlist {
            next: (*lfs).mlist,
            id: 0,
            type_: 0,
            m: core::mem::zeroed(),
        };

        if prevtag == LFS_ERR_NOENT {
            if lfs_path_isdir(newpath_slice)
                && u32::from(lfs_tag_type3(oldtag as u32)) != LFS_TYPE_DIR
            {
                return crate::lfs_err!(LFS_ERR_NOTDIR);
            }
            let nlen = lfs_path_namelen(newpath_slice);
            if nlen > (*lfs).name_max {
                return crate::lfs_err!(LFS_ERR_NAMETOOLONG);
            }
            if samepair && newid <= newoldid {
                newoldid += 1;
            }
        } else if u32::from(lfs_tag_type3(prevtag as u32))
            != u32::from(lfs_tag_type3(oldtag as u32))
        {
            return if u32::from(lfs_tag_type3(prevtag as u32)) == LFS_TYPE_DIR {
                LFS_ERR_ISDIR
            } else {
                LFS_ERR_NOTDIR
            };
        } else if samepair && newid == newoldid {
            return 0;
        } else if u32::from(lfs_tag_type3(prevtag as u32)) == LFS_TYPE_DIR {
            let mut prevpair: [lfs_block_t; 2] = [0, 0];
            let res = lfs_dir_get(
                lfs,
                &newcwd,
                lfs_mktag(0x700, 0x3ff, 0),
                lfs_mktag(LFS_TYPE_STRUCT, newid as u32, 8),
                prevpair.as_mut_ptr() as *mut core::ffi::c_void,
            );
            if res < 0 {
                return res;
            }
            lfs_pair_fromle32(&mut prevpair);

            let err = lfs_dir_fetch(lfs, &mut prevdir.m, &prevpair);
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }
            if prevdir.m.count > 0 || prevdir.m.split {
                return crate::lfs_err!(LFS_ERR_NOTEMPTY);
            }
            let err = lfs_fs_preporphans(lfs, 1);
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }
            prevdir.type_ = 0;
            prevdir.id = 0;
            (*lfs).mlist = &prevdir as *const _ as *mut _;
        }

        if !samepair {
            lfs_fs_prepmove(lfs, newoldid as u16, &oldcwd.pair);
        }

        let nlen = lfs_path_namelen(newpath_slice);
        let attrs = [
            lfs_mattr {
                tag: lfs_mktag_if(prevtag != LFS_ERR_NOENT, LFS_TYPE_DELETE, newid as u32, 0),
                buffer: core::ptr::null(),
            },
            lfs_mattr {
                tag: lfs_mktag(LFS_TYPE_CREATE, newid as u32, 0),
                buffer: core::ptr::null(),
            },
            lfs_mattr {
                tag: lfs_mktag(u32::from(lfs_tag_type3(oldtag as u32)), newid as u32, nlen),
                buffer: newpath_ptr as *const core::ffi::c_void,
            },
            lfs_mattr {
                tag: lfs_mktag(
                    LFS_FROM_MOVE,
                    newid as u32,
                    lfs_tag_id(oldtag as u32) as u32,
                ),
                buffer: &oldcwd as *const _ as *const core::ffi::c_void,
            },
            lfs_mattr {
                tag: lfs_mktag_if(samepair, LFS_TYPE_DELETE, newoldid as u32, 0),
                buffer: core::ptr::null(),
            },
        ];
        let err = lfs_dir_commit(lfs, &mut newcwd, attrs.as_ptr() as *const _, 5);
        (*lfs).mlist = prevdir.next;
        if err != 0 {
            return crate::lfs_pass_err!(err);
        }

        if !samepair && lfs_gstate_hasmove(&(*lfs).gstate) {
            lfs_fs_prepmove(lfs, 0x3ff, core::ptr::null());
            let attrs2 = [lfs_mattr {
                tag: lfs_mktag(LFS_TYPE_DELETE, lfs_tag_id(oldtag as u32) as u32, 0),
                buffer: core::ptr::null(),
            }];
            let err = lfs_dir_commit(lfs, &mut oldcwd, attrs2.as_ptr() as *const _, 1);
            (*lfs).mlist = prevdir.next;
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }
        }

        if lfs_gstate_hasorphans(&(*lfs).gstate) {
            crate::lfs_assert!(prevtag != LFS_ERR_NOENT);
            crate::lfs_assert!(u32::from(lfs_tag_type3(prevtag as u32)) == LFS_TYPE_DIR);

            let err = lfs_fs_preporphans(lfs, -1);
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }
            let err = lfs_fs_pred(lfs, &prevdir.m.pair, &mut newcwd);
            if err != 0 {
                return crate::lfs_pass_err!(err);
            }
            lfs_dir_drop(lfs, &mut newcwd, &prevdir.m)
        } else {
            0
        }
    }
}