~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/my_chsize.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
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.
 
7
   
 
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.
 
12
   
 
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,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
#include "mysys_priv.h"
 
19
#include "mysys_err.h"
 
20
#include "m_string.h"
 
21
 
 
22
        /* Change size of file.  Truncate file if shorter,      */
 
23
        /* else expand with zero.                               */
 
24
 
 
25
int my_chsize(File fd, my_off_t newlength, myf MyFlags)
 
26
{
 
27
  DBUG_ENTER("my_chsize");
 
28
  DBUG_PRINT("my",("fd: %d  length: %lu  MyFlags: %d",fd,(ulong) newlength,
 
29
                   MyFlags));
 
30
 
 
31
#ifdef HAVE_CHSIZE
 
32
  if (chsize(fd,(off_t) newlength))
 
33
  {
 
34
    DBUG_PRINT("error",("errno: %d",errno));
 
35
    my_errno=errno;
 
36
    if (MyFlags & MY_WME)
 
37
      my_error(EE_CANT_CHSIZE,MYF(ME_BELL+ME_WAITTANG),errno);
 
38
    DBUG_RETURN(1);
 
39
  }
 
40
  DBUG_RETURN(0);
 
41
#else
 
42
  /* if file is shorter, expand with null, else fill unused part with null */
 
43
  {
 
44
    my_off_t oldsize;
 
45
    char buff[IO_SIZE];
 
46
 
 
47
    bzero(buff,IO_SIZE);
 
48
    oldsize = my_seek(fd, 0L, MY_SEEK_END, MYF(MY_WME+MY_FAE));
 
49
 
 
50
#ifdef HAVE_FTRUNCATE
 
51
    if (oldsize > newlength)
 
52
    {
 
53
      if (ftruncate(fd, (off_t) newlength))
 
54
      {
 
55
        my_errno=errno;
 
56
        DBUG_PRINT("error",("errno: %d",errno));
 
57
        if (MyFlags & MY_WME)
 
58
          my_error(EE_CANT_CHSIZE, MYF(ME_BELL+ME_WAITTANG), errno);
 
59
        DBUG_RETURN(1);
 
60
      }
 
61
      DBUG_RETURN(0);
 
62
    }
 
63
#else
 
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);
 
68
    }
 
69
#endif
 
70
    while (newlength-oldsize > IO_SIZE)
 
71
    {
 
72
      if (my_write(fd,(byte*) buff,IO_SIZE,MYF(MY_NABP)))
 
73
        goto err;
 
74
      oldsize+= IO_SIZE;
 
75
    }
 
76
    if (my_write(fd,(byte*) buff,(uint) (newlength-oldsize),MYF(MY_NABP)))
 
77
      goto err;
 
78
    DBUG_RETURN(0);
 
79
  err:
 
80
    if (MyFlags & MY_WME)
 
81
      my_error(EE_CANT_CHSIZE,MYF(ME_BELL+ME_WAITTANG),my_errno);
 
82
    DBUG_PRINT("error",("errno: %d",my_errno));
 
83
    DBUG_RETURN(1);
 
84
  }
 
85
#endif
 
86
}                               /* my_chsize */