~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
#ifdef THREAD
51
    if (my_thread_var->abort)
52
      MyFlags&= ~ MY_WAIT_IF_FULL;		/* End if aborted by user */
53
#endif
54
    if ((my_errno == ENOSPC || my_errno == EDQUOT) &&
55
        (MyFlags & MY_WAIT_IF_FULL))
56
    {
57
      if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
58
	my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
59
		 my_filename(Filedes),my_errno,MY_WAIT_FOR_USER_TO_FIX_PANIC);
60
      VOID(sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC));
61
      continue;
62
    }
63
64
    if ((writenbytes == 0 || writenbytes == (size_t) -1))
65
    {
66
      if (my_errno == EINTR)
67
      {
68
        DBUG_PRINT("debug", ("my_write() was interrupted and returned %ld",
69
                             (long) writenbytes));
70
        continue;                               /* Interrupted */
71
      }
72
73
      if (!writenbytes && !errors++)		/* Retry once */
74
      {
75
        /* We may come here if the file quota is exeeded */
76
        errno=EFBIG;				/* Assume this is the error */
77
        continue;
78
      }
79
    }
80
    else
81
      continue;					/* Retry */
82
#endif
83
    if (MyFlags & (MY_NABP | MY_FNABP))
84
    {
85
      if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
86
      {
87
	my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
88
		 my_filename(Filedes),my_errno);
89
      }
90
      DBUG_RETURN(MY_FILE_ERROR);		/* Error on read */
91
    }
92
    else
93
      break;					/* Return bytes written */
94
  }
95
  if (MyFlags & (MY_NABP | MY_FNABP))
96
    DBUG_RETURN(0);			/* Want only errors */
97
  DBUG_RETURN(writenbytes+written);
98
} /* my_write */