commit 4c8a28c06203983ec3750447b8c74a39c5211d26
parent 86ab4b4cc47a6ba5e329feea32e3140f7f9ffe5f
Author: Plat <plat@stellar-nexus.ru>
Date: Sat, 26 Sep 2026 11:02:37 +0000
Fixed major issues in frag.c
Diffstat:
| M | file.c | | | 20 | ++------------------ |
| M | frag.c | | | 63 | +++++++++++++++++++++++++++++++++++++-------------------------- |
2 files changed, 39 insertions(+), 44 deletions(-)
diff --git a/file.c b/file.c
@@ -32,32 +32,16 @@ filestat(u64int filepos)
return nil;
}
- /* non-fragmented */
- if(buf[pos] == '+') {
- out->nfrags = 1;
-
- Frag *frag = calloc(1, sizeof(Frag));
- if(frag == nil) {
- werrstr("calloc: %r");
- return nil;
- }
-
- frag->block = out->dir->qid.path;
- frag->size = out->dir->length;
-
- out->frags = frag;
- }
-
/* nfrags */
out->nfrags = countfrags(filepos, 0);
- out->*frags = malloc((sizeof out->frags) * nfrags);
+ out->frags = malloc((sizeof Frag) * nfrags);
if(out->frags == nil) {
werrstr("malloc: %r");
return nil;
}
- uint *count = calloc(1, sizeof count);
+ uint *count = calloc(1, sizeof *count);
if(count == nil) {
werrstr("calloc: %r");
return nil;
diff --git a/frag.c b/frag.c
@@ -27,7 +27,7 @@ initinode(u64int inodepos)
field[FRAGSIZ-1] = '\n';
field[FRAGSIZ] = '\0';
- for(int i = 0; i <= MAXFRAGS; ++i)
+ for(int i = 0; i < MAXFRAGS; ++i)
writefield(buf + pos + FRAGSIZ*i, FRAGS, FRAGSIZ, field);
if(blkwrite(blk, buf) < 0) {
@@ -38,7 +38,7 @@ initinode(u64int inodepos)
return 0;
}
-int
+uint
addfrag(u64int inodepos, Frag *frag)
{
u64int pos; /* to avoid a conversion */
@@ -46,7 +46,7 @@ addfrag(u64int inodepos, Frag *frag)
char buf[BLKSZ];
char field[FRAGSIZ+1];
- pos = inodepos % BLKSZ;
+ pos = inodepos % BLKSZ + FRAGS;
blk = inodepos / BLKSZ;
if(blkread(blk, buf) < 0) {
@@ -55,18 +55,20 @@ addfrag(u64int inodepos, Frag *frag)
}
- char out;
+ uint out;
for(out = 0; out < MAXFRAGS; ++out, pos+=FRAGSIZ) {
- if(buf[pos+FRAGS] == 'd')
+ if(buf[pos] == 'd')
break;
}
- if(buf[pos+FRAGS] != 'd')
+ if(buf[pos] != 'd')
return -1;
buf[pos] = '+';
- writefield(buf + pos, BLOCK, BLOCKSIZ, frag->block);
- writefield(buf + pos, SIZE, SIZESIZ, frag->size);
+ seprint(field, field + sizeof(field), "%ulld", frag->block);
+ writefield(buf + pos, BLOCK, BLOCKSIZ, field);
+ seprint(field, field + sizeof(field), "%lld", frag->size);
+ writefield(buf + pos, SIZE, SIZESIZ, field);
if(blkwrite(blk, buf) < 0) {
werrstr("blkwrite: %r");
@@ -130,21 +132,30 @@ countfrags(u64int inodepos, uint nfrag)
return 0;
}
- if(buf[pos] != 'f')
+ if(buf[pos] != 'f') {
return 1;
+ }
+#ifdef DEFENSIVE
+ else if(buf[pos] == 'd') {
+ werrstr("Inconsistent status bytes");
+ return 0;
+ }
+#endif
+
getfield(buf + pos, BLOCK, BLOCKSIZ, field);
inodepos2 = strtoull(field, nil, 10);
getfield(buf + pos, SIZE, SIZESIZ, field);
- for(count = 0; count != 20 && c = field[count] != ' '; ++count) {
+ out = 0;
+ for(count = 0; count != SIZESIZ && (c = field[count]) != ' '; ++count) {
out += countfrags(inodepos2, char2num(c));
}
return out;
}
-static Frag *
+static Frag
fragstat(u64int inodepos, uint nfrag)
{
u64int pos; /* to avoid a conversion */
@@ -155,27 +166,21 @@ fragstat(u64int inodepos, uint nfrag)
pos = inodepos % BLKSZ + nfrag * FRAGSIZ;
blk = inodepos / BLKSZ;
+ Frag out;
+ memset(&out, 0, sizeof out);
+
if(blkread(blk, buf) < 0) {
werrstr("blkread: %r");
- return nil;
- }
-
- Frag *out = calloc(1, sizeof(Frag));
- if(out == nil) {
- werrstr("calloc: %r");
- return nil;
- }
-
- if(buf[pos] == 'f')
return out;
+ }
/* Block */
getfield(buf + pos, BLOCK, BLOCKSIZ, field);
- out->block = strtoull(field, nil, 10);
+ out.block = strtoull(field, nil, 10);
/* Size */
getfield(buf + pos, SIZE, SIZESIZ, field);
- out->size = strtoull(field, nil, 10);
+ out.size = strtoull(field, nil, 10);
return out;
}
@@ -201,7 +206,12 @@ buildfragarr(u64int inodepos, uint nfrag, Frag *frags, uint *fragidx)
}
if(buf[pos] != 'f') {
- frags[(*fragidx)++] = fragstat(inodepos, nfrag);
+ frags[*fragidx] = fragstat(inodepos, nfrag);
+ if(frags[*fragidx].block == 0) {
+ werrstr("fragstat: %r");
+ return 1;
+ }
+ ++(*fragidx);
return 0;
}
@@ -210,8 +220,9 @@ buildfragarr(u64int inodepos, uint nfrag, Frag *frags, uint *fragidx)
getfield(buf + pos, SIZE, SIZESIZ, field);
- for(count = 0; count != 20 && c = field[count] != ' '; ++count) {
- out += countfrags(inodepos2, char2num(c), frags, fragidx);
+ out = 0;
+ for(count = 0; count != SIZESIZ && (c = field[count]) != ' '; ++count) {
+ out += buildfragarr(inodepos2, char2num(c), frags, fragidx);
}
return out;
}