~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include "mysys_priv.h"
17
#include "mysys_err.h"
18
#include <errno.h>
19
20
21
	/* Write a chunk of bytes to a file */
22
23
size_t my_write(int Filedes, const uchar *Buffer, size_t Count, myf MyFlags)
24
{
25
  size_t writenbytes, written;
26
  uint errors;
27
  DBUG_ENTER("my_write");
28
  DBUG_PRINT("my",("Fd: %d  Buffer: 0x%lx  Count: %lu  MyFlags: %d",
29
		   Filedes, (long) Buffer, (ulong) Count, MyFlags));
30
  errors=0; written=0;
31
32
  /* The behavior of write(fd, buf, 0) is not portable */
33
  if (unlikely(!Count))
34
    DBUG_RETURN(0);
35
  
36
  for (;;)
37
  {
38
    if ((writenbytes= write(Filedes, Buffer, Count)) == Count)
39
      break;
40
    if (writenbytes != (size_t) -1)
41
    {						/* Safeguard */
42
      written+=writenbytes;
43
      Buffer+=writenbytes;
44
      Count-=writenbytes;
45
    }
46
    my_errno=errno;
47
    DBUG_PRINT("error",("Write only %ld bytes, error: %d",
48
			(long) writenbytes, my_errno));
49
#ifndef NO_BACKGROUND
50
    if (my_thread_var->abort)
51
      MyFlags&= ~ MY_WAIT_IF_FULL;		/* End if aborted by user */
52
    if ((my_errno == ENOSPC || my_errno == EDQUOT) &&
53
        (MyFlags & MY_WAIT_IF_FULL))
54
    {
55
      if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
56
	my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
57
		 my_filename(Filedes),my_errno,MY_WAIT_FOR_USER_TO_FIX_PANIC);
58
      VOID(sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC));
59
      continue;
60
    }
61
62
    if ((writenbytes == 0 || writenbytes == (size_t) -1))
63
    {
64
      if (my_errno == EINTR)
65
      {
66
        DBUG_PRINT("debug", ("my_write() was interrupted and returned %ld",
67
                             (long) writenbytes));
68
        continue;                               /* Interrupted */
69
      }
70
71
      if (!writenbytes && !errors++)		/* Retry once */
72
      {
73
        /* We may come here if the file quota is exeeded */
74
        errno=EFBIG;				/* Assume this is the error */
75
        continue;
76
      }
77
    }
78
    else
79
      continue;					/* Retry */
80
#endif
81
    if (MyFlags & (MY_NABP | MY_FNABP))
82
    {
83
      if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
84
      {
85
	my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
86
		 my_filename(Filedes),my_errno);
87
      }
88
      DBUG_RETURN(MY_FILE_ERROR);		/* Error on read */
89
    }
90
    else
91
      break;					/* Return bytes written */
92
  }
93
  if (MyFlags & (MY_NABP | MY_FNABP))
94
    DBUG_RETURN(0);			/* Want only errors */
95
  DBUG_RETURN(writenbytes+written);
96
} /* my_write */