~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_fstream.c

  • Committer: Monty Taylor
  • Date: 2008-10-10 23:04:21 UTC
  • mto: (509.1.1 codestyle)
  • mto: This revision was merged to the branch mainline in revision 511.
  • Revision ID: monty@inaugust.com-20081010230421-zohe1eppxievpw8d
RemovedĀ O_NOFOLLOW

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
/* USE_MY_STREAM isn't set because we can't thrust my_fclose! */
 
17
 
 
18
#include "mysys_priv.h"
 
19
#include "mysys_err.h"
 
20
#include <errno.h>
 
21
#include <stdio.h>
 
22
 
 
23
#ifdef HAVE_FSEEKO
 
24
#undef ftell
 
25
#undef fseek
 
26
#define ftell(A) ftello(A)
 
27
#define fseek(A,B,C) fseeko((A),(B),(C))
 
28
#endif
 
29
 
 
30
 
 
31
/*
 
32
  Write a chunk of bytes to a stream
 
33
 
 
34
   my_fwrite()
 
35
   stream       File descriptor
 
36
   Buffer       Buffer to write from
 
37
   Count        Number of bytes to write
 
38
   MyFlags      Flags on what to do on error
 
39
 
 
40
  RETURN
 
41
    (size_t) -1 Error
 
42
    #           Number of bytes written
 
43
*/
 
44
 
 
45
size_t my_fwrite(FILE *stream, const unsigned char *Buffer, size_t Count, myf MyFlags)
 
46
{
 
47
  size_t writtenbytes =0;
 
48
  my_off_t seekptr;
 
49
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
 
50
  uint32_t errors;
 
51
#endif
 
52
 
 
53
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
 
54
  errors=0;
 
55
#endif
 
56
  seekptr= ftell(stream);
 
57
  for (;;)
 
58
  {
 
59
    size_t written;
 
60
    if ((written = (size_t) fwrite((char*) Buffer,sizeof(char),
 
61
                                   Count, stream)) != Count)
 
62
    {
 
63
      my_errno=errno;
 
64
      if (written != (size_t) -1)
 
65
      {
 
66
        seekptr+=written;
 
67
        Buffer+=written;
 
68
        writtenbytes+=written;
 
69
        Count-=written;
 
70
      }
 
71
#ifdef EINTR
 
72
      if (errno == EINTR)
 
73
      {
 
74
        my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0));
 
75
        continue;
 
76
      }
 
77
#endif
 
78
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
 
79
      if (my_thread_var->abort)
 
80
        MyFlags&= ~ MY_WAIT_IF_FULL;            /* End if aborted by user */
 
81
      if ((errno == ENOSPC || errno == EDQUOT) &&
 
82
          (MyFlags & MY_WAIT_IF_FULL))
 
83
      {
 
84
        if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
 
85
          my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
 
86
                   "[stream]",my_errno,MY_WAIT_FOR_USER_TO_FIX_PANIC);
 
87
        sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC);
 
88
        my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0));
 
89
        continue;
 
90
      }
 
91
#endif
 
92
      if (ferror(stream) || (MyFlags & (MY_NABP | MY_FNABP)))
 
93
      {
 
94
        if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
 
95
        {
 
96
          my_error(EE_WRITE, MYF(ME_BELL+ME_WAITTANG),
 
97
                   my_filename(fileno(stream)),errno);
 
98
        }
 
99
        writtenbytes= (size_t) -1;        /* Return that we got error */
 
100
        break;
 
101
      }
 
102
    }
 
103
    if (MyFlags & (MY_NABP | MY_FNABP))
 
104
      writtenbytes= 0;                          /* Everything OK */
 
105
    else
 
106
      writtenbytes+= written;
 
107
    break;
 
108
  }
 
109
  return(writtenbytes);
 
110
} /* my_fwrite */
 
111
 
 
112
 
 
113
/* Seek to position in file */
 
114
 
 
115
my_off_t my_fseek(FILE *stream, my_off_t pos, int whence,
 
116
                  myf MyFlags __attribute__((unused)))
 
117
{
 
118
  return(fseek(stream, (off_t) pos, whence) ?
 
119
              MY_FILEPOS_ERROR : (my_off_t) ftell(stream));
 
120
} /* my_seek */