~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_fopen.c

  • Committer: Brian Aker
  • Date: 2008-07-06 15:03:34 UTC
  • Revision ID: brian@tangent.org-20080706150334-xv3xa202trvs0712
USE_RAID cleanup, along with ftbench tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
{
39
39
  FILE *fd;
40
40
  char type[5];
 
41
  DBUG_ENTER("my_fopen");
 
42
  DBUG_PRINT("my",("Name: '%s'  flags: %d  MyFlags: %d",
 
43
                   filename, flags, MyFlags));
41
44
  /* 
42
45
    if we are not creating, then we need to use my_access to make sure  
43
46
    the file exists since Windows doesn't handle files like "com1.sym" 
58
61
    if ((uint) fileno(fd) >= my_file_limit)
59
62
    {
60
63
      thread_safe_increment(my_stream_opened,&THR_LOCK_open);
61
 
      return(fd);                               /* safeguard */
 
64
      DBUG_RETURN(fd);                          /* safeguard */
62
65
    }
63
66
    pthread_mutex_lock(&THR_LOCK_open);
64
67
    if ((my_file_info[fileno(fd)].name = (char*)
68
71
      my_file_total_opened++;
69
72
      my_file_info[fileno(fd)].type = STREAM_BY_FOPEN;
70
73
      pthread_mutex_unlock(&THR_LOCK_open);
71
 
      return(fd);
 
74
      DBUG_PRINT("exit",("stream: 0x%lx", (long) fd));
 
75
      DBUG_RETURN(fd);
72
76
    }
73
77
    pthread_mutex_unlock(&THR_LOCK_open);
74
78
    (void) my_fclose(fd,MyFlags);
76
80
  }
77
81
  else
78
82
    my_errno=errno;
 
83
  DBUG_PRINT("error",("Got error %d on open",my_errno));
79
84
  if (MyFlags & (MY_FFNF | MY_FAE | MY_WME))
80
85
    my_error((flags & O_RDONLY) || (flags == O_RDONLY ) ? EE_FILENOTFOUND :
81
86
             EE_CANTCREATEFILE,
82
87
             MYF(ME_BELL+ME_WAITTANG), filename, my_errno);
83
 
  return((FILE*) 0);
 
88
  DBUG_RETURN((FILE*) 0);
84
89
} /* my_fopen */
85
90
 
86
91
 
89
94
int my_fclose(FILE *fd, myf MyFlags)
90
95
{
91
96
  int err,file;
 
97
  DBUG_ENTER("my_fclose");
 
98
  DBUG_PRINT("my",("stream: 0x%lx  MyFlags: %d", (long) fd, MyFlags));
92
99
 
93
100
  pthread_mutex_lock(&THR_LOCK_open);
94
101
  file=fileno(fd);
104
111
  if ((uint) file < my_file_limit && my_file_info[file].type != UNOPEN)
105
112
  {
106
113
    my_file_info[file].type = UNOPEN;
107
 
    free(my_file_info[file].name);
 
114
    my_free(my_file_info[file].name, MYF(MY_ALLOW_ZERO_PTR));
108
115
  }
109
116
  pthread_mutex_unlock(&THR_LOCK_open);
110
 
  return(err);
 
117
  DBUG_RETURN(err);
111
118
} /* my_fclose */
112
119
 
113
120
 
 
121
        /* Make a stream out of a file handle */
 
122
        /* Name may be 0 */
 
123
 
 
124
FILE *my_fdopen(File Filedes, const char *name, int Flags, myf MyFlags)
 
125
{
 
126
  FILE *fd;
 
127
  char type[5];
 
128
  DBUG_ENTER("my_fdopen");
 
129
  DBUG_PRINT("my",("Fd: %d  Flags: %d  MyFlags: %d",
 
130
                   Filedes, Flags, MyFlags));
 
131
 
 
132
  make_ftype(type,Flags);
 
133
  if ((fd = fdopen(Filedes, type)) == 0)
 
134
  {
 
135
    my_errno=errno;
 
136
    if (MyFlags & (MY_FAE | MY_WME))
 
137
      my_error(EE_CANT_OPEN_STREAM, MYF(ME_BELL+ME_WAITTANG),errno);
 
138
  }
 
139
  else
 
140
  {
 
141
    pthread_mutex_lock(&THR_LOCK_open);
 
142
    my_stream_opened++;
 
143
    if ((uint) Filedes < (uint) my_file_limit)
 
144
    {
 
145
      if (my_file_info[Filedes].type != UNOPEN)
 
146
      {
 
147
        my_file_opened--;               /* File is opened with my_open ! */
 
148
      }
 
149
      else
 
150
      {
 
151
        my_file_info[Filedes].name=  my_strdup(name,MyFlags);
 
152
      }
 
153
      my_file_info[Filedes].type = STREAM_BY_FDOPEN;
 
154
    }
 
155
    pthread_mutex_unlock(&THR_LOCK_open);
 
156
  }
 
157
 
 
158
  DBUG_PRINT("exit",("stream: 0x%lx", (long) fd));
 
159
  DBUG_RETURN(fd);
 
160
} /* my_fdopen */
 
161
 
114
162
 
115
163
/*   
116
164
  Make a fopen() typestring from a open() type bitmap
140
188
static void make_ftype(register char * to, register int flag)
141
189
{
142
190
  /* check some possible invalid combinations */  
143
 
  assert((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
144
 
  assert((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
 
191
  DBUG_ASSERT((flag & (O_TRUNC | O_APPEND)) != (O_TRUNC | O_APPEND));
 
192
  DBUG_ASSERT((flag & (O_WRONLY | O_RDWR)) != (O_WRONLY | O_RDWR));
145
193
 
146
194
  if ((flag & (O_RDONLY|O_WRONLY)) == O_WRONLY)    
147
195
    *to++= (flag & O_APPEND) ? 'a' : 'w';  
159
207
  else    
160
208
    *to++= 'r';
161
209
 
 
210
#if FILE_BINARY            /* If we have binary-files */  
 
211
  if (flag & FILE_BINARY)    
 
212
    *to++='b';
 
213
#endif  
162
214
  *to='\0';
163
215
} /* make_ftype */