MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
my_seek.c
1 /* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "mysys_priv.h"
17 #include "mysys_err.h"
18 
19 /*
20  Seek to a position in a file.
21 
22  ARGUMENTS
23  File fd The file descriptor
24  my_off_t pos The expected position (absolute or relative)
25  int whence A direction parameter and one of
26  {SEEK_SET, SEEK_CUR, SEEK_END}
27  myf MyFlags MY_THREADSAFE must be set in case my_seek may be mixed
28  with my_pread/my_pwrite calls and fd is shared among
29  threads.
30 
31  DESCRIPTION
32  The my_seek function is a wrapper around the system call lseek and
33  repositions the offset of the file descriptor fd to the argument
34  offset according to the directive whence as follows:
35  SEEK_SET The offset is set to offset bytes.
36  SEEK_CUR The offset is set to its current location plus offset bytes
37  SEEK_END The offset is set to the size of the file plus offset bytes
38 
39  RETURN VALUE
40  my_off_t newpos The new position in the file.
41  MY_FILEPOS_ERROR An error was encountered while performing
42  the seek. my_errno is set to indicate the
43  actual error.
44 */
45 
46 my_off_t my_seek(File fd, my_off_t pos, int whence, myf MyFlags)
47 {
48  os_off_t newpos= -1;
49  DBUG_ENTER("my_seek");
50  DBUG_PRINT("my",("fd: %d Pos: %llu Whence: %d MyFlags: %d",
51  fd, (ulonglong) pos, whence, MyFlags));
52  DBUG_ASSERT(pos != MY_FILEPOS_ERROR); /* safety check */
53 
54  /*
55  Make sure we are using a valid file descriptor!
56  */
57  DBUG_ASSERT(fd != -1);
58 #if defined (_WIN32)
59  newpos= my_win_lseek(fd, pos, whence);
60 #else
61  newpos= lseek(fd, pos, whence);
62 #endif
63  if (newpos == (os_off_t) -1)
64  {
65  my_errno= errno;
66  if (MyFlags & MY_WME)
67  {
68  char errbuf[MYSYS_STRERROR_SIZE];
69  my_error(EE_CANT_SEEK, MYF(0), my_filename(fd),
70  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
71  }
72  DBUG_PRINT("error", ("lseek: %llu errno: %d", (ulonglong) newpos, errno));
73  DBUG_RETURN(MY_FILEPOS_ERROR);
74  }
75  if ((my_off_t) newpos != pos)
76  {
77  DBUG_PRINT("exit",("pos: %llu", (ulonglong) newpos));
78  }
79  DBUG_RETURN((my_off_t) newpos);
80 } /* my_seek */
81 
82 
83  /* Tell current position of file */
84  /* ARGSUSED */
85 
86 my_off_t my_tell(File fd, myf MyFlags)
87 {
88  os_off_t pos;
89  DBUG_ENTER("my_tell");
90  DBUG_PRINT("my",("fd: %d MyFlags: %d",fd, MyFlags));
91  DBUG_ASSERT(fd >= 0);
92 #if defined (HAVE_TELL) && !defined (_WIN32)
93  pos= tell(fd);
94 #else
95  pos= my_seek(fd, 0L, MY_SEEK_CUR,0);
96 #endif
97  if (pos == (os_off_t) -1)
98  {
99  my_errno= errno;
100  if (MyFlags & MY_WME)
101  {
102  char errbuf[MYSYS_STRERROR_SIZE];
103  my_error(EE_CANT_SEEK, MYF(0), my_filename(fd),
104  my_errno, my_strerror(errbuf, sizeof(errbuf), my_errno));
105  }
106  DBUG_PRINT("error", ("tell: %llu errno: %d", (ulonglong) pos, my_errno));
107  }
108  DBUG_PRINT("exit",("pos: %llu", (ulonglong) pos));
109  DBUG_RETURN((my_off_t) pos);
110 } /* my_tell */