1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This library is free software; you can redistribute it and/or
4
modify it under the terms of the GNU Library General Public
5
License as published by the Free Software Foundation; either
6
version 2 of the License, or (at your option) any later version.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public
14
License along with this library; if not, write to the Free
15
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18
#include "mysys_priv.h"
19
#include "mysys_err.h"
22
/* Change size of file. Truncate file if shorter, */
23
/* else expand with zero. */
25
int my_chsize(File fd, my_off_t newlength, myf MyFlags)
27
DBUG_ENTER("my_chsize");
28
DBUG_PRINT("my",("fd: %d length: %lu MyFlags: %d",fd,(ulong) newlength,
32
if (chsize(fd,(off_t) newlength))
34
DBUG_PRINT("error",("errno: %d",errno));
37
my_error(EE_CANT_CHSIZE,MYF(ME_BELL+ME_WAITTANG),errno);
42
/* if file is shorter, expand with null, else fill unused part with null */
48
oldsize = my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
51
if (oldsize > newlength)
53
if (ftruncate(fd, (off_t) newlength))
56
DBUG_PRINT("error",("errno: %d",errno));
58
my_error(EE_CANT_CHSIZE, MYF(ME_BELL+ME_WAITTANG), errno);
64
if (oldsize > newlength)
65
{ /* Fill diff with null */
66
VOID(my_seek(fd, newlength, MY_SEEK_SET, MYF(MY_WME+MY_FAE)));
67
swap(long, newlength, oldsize);
70
while (newlength-oldsize > IO_SIZE)
72
if (my_write(fd,(byte*) buff,IO_SIZE,MYF(MY_NABP)))
76
if (my_write(fd,(byte*) buff,(uint) (newlength-oldsize),MYF(MY_NABP)))
81
my_error(EE_CANT_CHSIZE,MYF(ME_BELL+ME_WAITTANG),my_errno);
82
DBUG_PRINT("error",("errno: %d",my_errno));