~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_fstream.c

  • Committer: Brian Aker
  • Date: 2008-07-01 20:14:24 UTC
  • Revision ID: brian@tangent.org-20080701201424-rsof5enxl7gkr50p
More cleanup on pread()

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#define fseek(A,B,C) fseeko((A),(B),(C))
28
28
#endif
29
29
 
 
30
/*
 
31
  Read a chunk of bytes from a FILE
 
32
 
 
33
  SYNOPSIS
 
34
   my_fread()
 
35
   stream       File descriptor
 
36
   Buffer       Buffer to read to
 
37
   Count        Number of bytes to read
 
38
   MyFlags      Flags on what to do on error
 
39
 
 
40
  RETURN
 
41
    (size_t) -1 Error
 
42
    #           Number of bytes read
 
43
 */
 
44
 
 
45
size_t my_fread(FILE *stream, uchar *Buffer, size_t Count, myf MyFlags)
 
46
{
 
47
  size_t readbytes;
 
48
  DBUG_ENTER("my_fread");
 
49
  DBUG_PRINT("my",("stream: 0x%lx  Buffer: 0x%lx  Count: %u  MyFlags: %d",
 
50
                   (long) stream, (long) Buffer, (uint) Count, MyFlags));
 
51
 
 
52
  if ((readbytes= fread(Buffer, sizeof(char), Count, stream)) != Count)
 
53
  {
 
54
    DBUG_PRINT("error",("Read only %d bytes", (int) readbytes));
 
55
    if (MyFlags & (MY_WME | MY_FAE | MY_FNABP))
 
56
    {
 
57
      if (ferror(stream))
 
58
        my_error(EE_READ, MYF(ME_BELL+ME_WAITTANG),
 
59
                 my_filename(fileno(stream)),errno);
 
60
      else
 
61
      if (MyFlags & (MY_NABP | MY_FNABP))
 
62
        my_error(EE_EOFERR, MYF(ME_BELL+ME_WAITTANG),
 
63
                 my_filename(fileno(stream)),errno);
 
64
    }
 
65
    my_errno=errno ? errno : -1;
 
66
    if (ferror(stream) || MyFlags & (MY_NABP | MY_FNABP))
 
67
      DBUG_RETURN((size_t) -1);                 /* Return with error */
 
68
  }
 
69
  if (MyFlags & (MY_NABP | MY_FNABP))
 
70
    DBUG_RETURN(0);                             /* Read ok */
 
71
  DBUG_RETURN(readbytes);
 
72
} /* my_fread */
 
73
 
30
74
 
31
75
/*
32
76
  Write a chunk of bytes to a stream
42
86
    #           Number of bytes written
43
87
*/
44
88
 
45
 
size_t my_fwrite(FILE *stream, const unsigned char *Buffer, size_t Count, myf MyFlags)
 
89
size_t my_fwrite(FILE *stream, const uchar *Buffer, size_t Count, myf MyFlags)
46
90
{
47
91
  size_t writtenbytes =0;
48
92
  my_off_t seekptr;
49
93
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
50
 
  uint32_t errors;
 
94
  uint errors;
51
95
#endif
 
96
  DBUG_ENTER("my_fwrite");
 
97
  DBUG_PRINT("my",("stream: 0x%lx  Buffer: 0x%lx  Count: %u  MyFlags: %d",
 
98
                   (long) stream, (long) Buffer, (uint) Count, MyFlags));
52
99
 
53
100
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
54
101
  errors=0;
60
107
    if ((written = (size_t) fwrite((char*) Buffer,sizeof(char),
61
108
                                   Count, stream)) != Count)
62
109
    {
 
110
      DBUG_PRINT("error",("Write only %d bytes", (int) writtenbytes));
63
111
      my_errno=errno;
64
112
      if (written != (size_t) -1)
65
113
      {
71
119
#ifdef EINTR
72
120
      if (errno == EINTR)
73
121
      {
74
 
        my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0));
 
122
        VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
75
123
        continue;
76
124
      }
77
125
#endif
78
126
#if !defined(NO_BACKGROUND) && defined(USE_MY_STREAM)
 
127
#ifdef THREAD
79
128
      if (my_thread_var->abort)
80
129
        MyFlags&= ~ MY_WAIT_IF_FULL;            /* End if aborted by user */
 
130
#endif
81
131
      if ((errno == ENOSPC || errno == EDQUOT) &&
82
132
          (MyFlags & MY_WAIT_IF_FULL))
83
133
      {
84
134
        if (!(errors++ % MY_WAIT_GIVE_USER_A_MESSAGE))
85
135
          my_error(EE_DISK_FULL,MYF(ME_BELL | ME_NOREFRESH),
86
136
                   "[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));
 
137
        VOID(sleep(MY_WAIT_FOR_USER_TO_FIX_PANIC));
 
138
        VOID(my_fseek(stream,seekptr,MY_SEEK_SET,MYF(0)));
89
139
        continue;
90
140
      }
91
141
#endif
106
156
      writtenbytes+= written;
107
157
    break;
108
158
  }
109
 
  return(writtenbytes);
 
159
  DBUG_RETURN(writtenbytes);
110
160
} /* my_fwrite */
111
161
 
112
162
 
115
165
my_off_t my_fseek(FILE *stream, my_off_t pos, int whence,
116
166
                  myf MyFlags __attribute__((unused)))
117
167
{
118
 
  return(fseek(stream, (off_t) pos, whence) ?
 
168
  DBUG_ENTER("my_fseek");
 
169
  DBUG_PRINT("my",("stream: 0x%lx  pos: %lu  whence: %d  MyFlags: %d",
 
170
                   (long) stream, (long) pos, whence, MyFlags));
 
171
  DBUG_RETURN(fseek(stream, (off_t) pos, whence) ?
119
172
              MY_FILEPOS_ERROR : (my_off_t) ftell(stream));
120
173
} /* my_seek */
 
174
 
 
175
 
 
176
/* Tell current position of file */
 
177
 
 
178
my_off_t my_ftell(FILE *stream, myf MyFlags __attribute__((unused)))
 
179
{
 
180
  off_t pos;
 
181
  DBUG_ENTER("my_ftell");
 
182
  DBUG_PRINT("my",("stream: 0x%lx  MyFlags: %d", (long) stream, MyFlags));
 
183
  pos=ftell(stream);
 
184
  DBUG_PRINT("exit",("ftell: %lu",(ulong) pos));
 
185
  DBUG_RETURN((my_off_t) pos);
 
186
} /* my_ftell */