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 |
||
18 |
/*
|
|
19 |
Seek to a position in a file.
|
|
20 |
||
21 |
ARGUMENTS
|
|
22 |
File fd The file descriptor
|
|
23 |
my_off_t pos The expected position (absolute or relative)
|
|
24 |
int whence A direction parameter and one of
|
|
25 |
{SEEK_SET, SEEK_CUR, SEEK_END}
|
|
26 |
myf MyFlags MY_THREADSAFE must be set in case my_seek may be mixed
|
|
27 |
with my_pread/my_pwrite calls and fd is shared among
|
|
28 |
threads.
|
|
29 |
||
30 |
DESCRIPTION
|
|
31 |
The my_seek function is a wrapper around the system call lseek and
|
|
32 |
repositions the offset of the file descriptor fd to the argument
|
|
33 |
offset according to the directive whence as follows:
|
|
34 |
SEEK_SET The offset is set to offset bytes.
|
|
35 |
SEEK_CUR The offset is set to its current location plus offset bytes
|
|
36 |
SEEK_END The offset is set to the size of the file plus offset bytes
|
|
37 |
||
38 |
RETURN VALUE
|
|
39 |
my_off_t newpos The new position in the file.
|
|
40 |
MY_FILEPOS_ERROR An error was encountered while performing
|
|
41 |
the seek. my_errno is set to indicate the
|
|
42 |
actual error.
|
|
43 |
*/
|
|
44 |
||
45 |
my_off_t my_seek(File fd, my_off_t pos, int whence, |
|
46 |
myf MyFlags __attribute__((unused))) |
|
47 |
{
|
|
48 |
register os_off_t newpos= -1; |
|
49 |
DBUG_ENTER("my_seek"); |
|
50 |
DBUG_PRINT("my",("Fd: %d Hpos: %lu Pos: %lu Whence: %d MyFlags: %d", |
|
51 |
fd, (ulong) (((ulonglong) pos) >> 32), (ulong) pos, |
|
52 |
whence, MyFlags)); |
|
53 |
DBUG_ASSERT(pos != MY_FILEPOS_ERROR); /* safety check */ |
|
54 |
||
55 |
/*
|
|
56 |
Make sure we are using a valid file descriptor!
|
|
57 |
*/
|
|
58 |
DBUG_ASSERT(fd != -1); |
|
28.1.35
by Monty Taylor
Removed all references to THREAD. |
59 |
#if !defined(HAVE_PREAD)
|
1
by brian
clean slate |
60 |
if (MyFlags & MY_THREADSAFE) |
61 |
{
|
|
62 |
pthread_mutex_lock(&my_file_info[fd].mutex); |
|
63 |
newpos= lseek(fd, pos, whence); |
|
64 |
pthread_mutex_unlock(&my_file_info[fd].mutex); |
|
65 |
}
|
|
66 |
else
|
|
67 |
#endif
|
|
68 |
newpos= lseek(fd, pos, whence); |
|
69 |
if (newpos == (os_off_t) -1) |
|
70 |
{
|
|
71 |
my_errno=errno; |
|
72 |
DBUG_PRINT("error",("lseek: %lu errno: %d", (ulong) newpos,errno)); |
|
73 |
DBUG_RETURN(MY_FILEPOS_ERROR); |
|
74 |
}
|
|
75 |
if ((my_off_t) newpos != pos) |
|
76 |
{
|
|
77 |
DBUG_PRINT("exit",("pos: %lu", (ulong) 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 __attribute__((unused))) |
|
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 |
#ifdef HAVE_TELL
|
|
93 |
pos=tell(fd); |
|
94 |
#else
|
|
95 |
pos=lseek(fd, 0L, MY_SEEK_CUR); |
|
96 |
#endif
|
|
97 |
if (pos == (os_off_t) -1) |
|
98 |
my_errno=errno; |
|
99 |
DBUG_PRINT("exit",("pos: %lu", (ulong) pos)); |
|
100 |
DBUG_RETURN((my_off_t) pos); |
|
101 |
} /* my_tell */ |