Abstract the use of ftruncate(2) behind mkimg_set_size().

This commit is contained in:
marcel 2014-05-07 15:08:00 +00:00
parent 8743dd6a3b
commit ef21d96a0f
5 changed files with 19 additions and 6 deletions

4
bsd.c
View File

@ -80,7 +80,9 @@ bsd_write(int fd, lba_t imgsz, void *bootcode)
memset(buf, 0, BBSIZE);
imgsz = ncyls * nheads * nsecs;
ftruncate(fd, imgsz * secsz);
error = mkimg_set_size(fd, imgsz);
if (error)
return (error);
d = (void *)(buf + secsz);
le32enc(&d->d_magic, DISKMAGIC);

View File

@ -316,6 +316,15 @@ fdcopy(int src, lba_t sblk, int dst, lba_t dblk, uint64_t *count)
return (errno);
}
int
mkimg_set_size(int fd, lba_t blk)
{
if (ftruncate(fd, blk * secsz) == -1)
return (errno);
return (0);
}
int
mkimg_write(int fd, lba_t blk, void *buf, ssize_t len)
{

View File

@ -67,6 +67,7 @@ round_block(lba_t n)
return ((n + b - 1) & ~(b - 1));
}
int mkimg_set_size(int fd, lba_t blk);
int mkimg_write(int fd, lba_t blk, void *buf, ssize_t len);
#endif /* _MKIMG_MKIMG_H_ */

View File

@ -189,9 +189,8 @@ scheme_write(int fd, lba_t end)
cylsz = nsecs * nheads;
ncyls = (end + cylsz - 1) / cylsz;
if (ftruncate(fd, end * secsz) == -1)
return (errno);
error = scheme->write(fd, end, bootcode);
error = mkimg_set_size(fd, end);
if (!error)
error = scheme->write(fd, end, bootcode);
return (error);
}

View File

@ -86,7 +86,9 @@ vtoc8_write(int fd, lba_t imgsz, void *bootcode __unused)
be16enc(&vtoc8.nsecs, nsecs);
be16enc(&vtoc8.magic, VTOC_MAGIC);
ftruncate(fd, imgsz * secsz);
error = mkimg_set_size(fd, imgsz);
if (error)
return (error);
be32enc(&vtoc8.map[VTOC_RAW_PART].nblks, imgsz);
STAILQ_FOREACH(part, &partlist, link) {