krb5_storage: normalise truncate behaviour w.r.t. file offset

We choose a semantic for the file offset during truncate operations
which is to leave the offset as is unless it is off the end of the
file in which case we pull it back in to the end.  We update fd and
stdio handling to match the {,e}mem behaviour.
This commit is contained in:
Roland C. Dowdeswell
2019-03-20 23:27:50 +00:00
committed by Nico Williams
parent b2332b9684
commit 56a32a8dbd
2 changed files with 20 additions and 1 deletions

View File

@@ -97,8 +97,21 @@ fd_seek(krb5_storage * sp, off_t offset, int whence)
static int
fd_trunc(krb5_storage * sp, off_t offset)
{
off_t tmpoff;
if (ftruncate(FD(sp), offset) == -1)
return errno;
tmpoff = lseek(FD(sp), 0, SEEK_CUR);
if (tmpoff == -1)
return errno;
if (tmpoff > offset) {
tmpoff = lseek(FD(sp), offset, SEEK_SET);
if (tmpoff == -1)
return errno;
}
return 0;
}

View File

@@ -108,14 +108,20 @@ stdio_seek(krb5_storage * sp, off_t offset, int whence)
static int
stdio_trunc(krb5_storage * sp, off_t offset)
{
off_t tmpoff;
int save_errno = errno;
if (fflush(F(sp)) == EOF)
return errno;
tmpoff = ftello(F(sp));
if (tmpoff > offset)
tmpoff = offset;
if (ftruncate(fileno(F(sp)), offset) == -1)
return errno;
if (fseeko(F(sp), offset, SEEK_SET) == -1)
if (fseeko(F(sp), 0, SEEK_END) == -1)
return errno;
if (fseeko(F(sp), tmpoff, SEEK_SET) == -1)
return errno;
errno = save_errno;
return 0;
}