commit 30f9bc6b3a71e9fad70d35c42787aa9acb90ced3
parent ed315c3f3f427c3e730d00c3082b129735d69eb8
Author: Plat <plat@stellar-nexus.ru>
Date: Thu, 24 Sep 2026 18:07:23 +0000
Reimplemented initdentry
Diffstat:
| M | dir.c | | | 151 | +++++++------------------------------------------------------------------------ |
1 file changed, 12 insertions(+), 139 deletions(-)
diff --git a/dir.c b/dir.c
@@ -67,6 +67,7 @@ writefield(char *buf, int offset, int size, char *in)
buf[offset + i] = ' ';
}
+/* do time ourselves because we don't need that much */
static int
isleap(u32int year)
{
@@ -355,126 +356,22 @@ err:
return nil;
}
-
-u64int
-dir1up(u64int dirblk, u64int dirsize,
- Qid *parent, u64int parentsize)
+int
+initdentry(u64int dentrypos, Dir *dentry)
{
- u64int parentblk = parent->path;
- u64int parentblks = parentsize / BLKSZ + (parentsize % BLKSZ > 0);
-#ifdef DEFENSIVE
- if(dirblk == super.rootblk || parentblk == SUPERBLK) {
- werrstr("Cannot extend root directory");
- return 0;
- /* stub - you should actually extend here */
- }
-#endif
-
- u64int dirnblks = dirsize / BLKSZ + (dirsize % BLKSZ > 0);
-
- u64int nextblk = dirblk + dirnblks;
- if(nextblk < super.nblocks && !bused(nextblk, nextblk)) {
- if(balloc(nextblk, nextblk) < 0) {
- werrstr("balloc: %r");
- return 0;
- }
- /* caller must fill the new block immediately and update size */
-
- return dirblk;
- }
-
-
-
-
-
- u64int newblk = bfindfree(dirnblks + 1, nextblk);
- if(newblk == 0) {
- werrstr("no space for %ulld contiguous blocks", dirnblks + 1);
- return 0;
- }
-
- if(balloc(newblk, newblk + dirnblks) < 0) {
- werrstr("balloc: %r");
- return 0;
- }
-
+ u64int pos; /* to avoid a conversion */
+ u64int blk;
char buf[BLKSZ];
- for(u64int i = 0; i < dirnblks; i++) {
- if(blkcopy(dirblk + i, newblk + i, buf) < 0) {
- werrstr("blkcopy: %r");
- if(bfree(newblk, newblk + dirnblks) < 0)
- werrstr("blkcopy and bfree: %r");
- return 0;
- }
- }
-
- u64int pos = getdentry(parentblk, parentblks, dirblk);
- if(pos == 0) {
- werrstr("getdentry: %r");
- return 0;
- }
- u64int dblk = pos / BLKSZ;
- pos = pos % BLKSZ;
- if(blkread(dblk, buf) < 0) {
- werrstr("blkread: %r");
- return 0;
- }
- char field[BLOCKSIZ + 1];
- seprint(field, field + sizeof(field), "%ulld", newblk);
- writefield(buf + pos % BLKSZ, BLOCK, BLOCKSIZ, field);
- if(blkwrite(dblk, buf) < 0) {
- werrstr("blkwrite: %r");
- return 0;
- }
- INCR_VERS(parent->vers);
-
- if(bfree(dirblk, dirblk + dirnblks - 1) < 0) {
- werrstr("bfree: %r");
- return 0;
- }
-
- return newblk;
-}
-
-int /* add a root case. also make it so that it is impossible to update the directory, but not the parent */
-initdentry(Qid *dir, u64int dirsize,
- Qid *parent, u64int parentsize,
- Dir *file)
-{
- u64int dirblk = dir->path, parentblk = parent->path;
- u64int blk, pos;
- u64int nblks = dirsize / BLKSZ + (dirsize % BLKSZ > 0);
- char buf[BLKSZ], field[STRSIZ+1];
-
- for(blk = dirblk; blk < dirblk + nblks; ++blk) {
- if(blkread(blk, buf) < 0) {
- werrstr("blkread: %r");
- return -1;
- }
-
- for(pos = 0; pos + DENTRYSIZ <= BLKSZ; pos += DENTRYSIZ) {
- if(buf[pos] == 'd')
- goto found;
- }
- }
+ char field[STRSIZ+1];
+ pos = dentrypos % BLKSZ;
+ blk = dentrypos / BLKSZ;
- dirblk = dir1up(dirblk, dirsize, parent, parentsize);
- if(dirblk == 0) {
- werrstr("dir1up: %r");
- return 0;
- }
- dir->path = dirblk;
- ++nblks;
-
- blk = dirblk + nblks - 1;
if(blkread(blk, buf) < 0) {
werrstr("blkread: %r");
- return -1;
+ return nil;
}
- pos = 0;
-
-found:
+
buf[pos] = '+';
seprint(field, field + sizeof(field), "%ulld", file->qid.path);
@@ -512,31 +409,7 @@ found:
werrstr("blkwrite: %r");
return -1;
}
- INCR_VERS(dir->vers);
-
- pos = getdentry(parentblk, parentsize, dirblk);
- if(pos == 0) {
- werrstr("getdentry: %r");
- return -1;
- }
- blk = pos / BLKSZ + (pos % BLKSZ > 0);
- pos = pos % BLKSZ;
- if(blkread(blk, buf) < 0) {
- werrstr("blkread: %r");
- return -1;
- }
-
- /* read size and write it back??? you need to update it! although that should be done inside dir1up */
- getfield(buf + pos, SIZE, SIZESIZ, field);
- u64int size = strtoull(field, nil, 10);
- seprint(field, field + sizeof(field), "%ulld", size);
- writefield(buf + pos, SIZE, SIZESIZ, field);
- if(blkwrite(blk, buf) < 0) {
- werrstr("blkwrite: %r");
- return -1;
- }
- INCR_VERS(parent->vers);
-
- return 0;
+ INCR_VERS(dir->vers);
}
+