From 8457dd1176551fe81a84793f38b2dd1f69170dc6 Mon Sep 17 00:00:00 2001 From: unknown Date: Thu, 29 Jun 2006 21:43:52 +0400 Subject: [PATCH 1/3] Fix Bug#20692 rpl_* tests failure on hpux mysys/my_pread.c: Fix our implementation of pread/pwrite: we did change the file pointer in our implementation, while the normal preads/pwrites don't. The new code is based on glibc version of pread/pwrite. --- mysys/my_pread.c | 66 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/mysys/my_pread.c b/mysys/my_pread.c index 3d02e368720..eef92b9ebc6 100644 --- a/mysys/my_pread.c +++ b/mysys/my_pread.c @@ -27,7 +27,7 @@ uint my_pread(File Filedes, byte *Buffer, uint Count, my_off_t offset, myf MyFlags) { uint readbytes; - int error; + int error= 0; DBUG_ENTER("my_pread"); DBUG_PRINT("my",("Fd: %d Seek: %lu Buffer: 0x%lx Count: %u MyFlags: %d", Filedes, (ulong) offset, Buffer, Count, MyFlags)); @@ -38,17 +38,39 @@ uint my_pread(File Filedes, byte *Buffer, uint Count, my_off_t offset, errno=0; /* Linux doesn't reset this */ #endif #ifndef HAVE_PREAD + off_t old_offset; + pthread_mutex_lock(&my_file_info[Filedes].mutex); - readbytes= (uint) -1; - error= (lseek(Filedes, offset, MY_SEEK_SET) == -1L || - (readbytes = (uint) read(Filedes, Buffer, Count)) != Count); + /* + As we cannot change the file pointer, we save the old position, + before seeking to the given offset + */ + + error= (old_offset= lseek(Filedes, 0L, MY_SEEK_CUR)) == -1L || + lseek(Filedes, offset, MY_SEEK_SET) == -1L; + + if (!error) /* Seek was successful */ + { + if ((readbytes = (uint) read(Filedes, Buffer, Count)) == -1L) + my_errno= errno; + + /* + We should seek back, even if read failed. If this fails, + we will return an error. If read failed as well, we will + save the errno from read, not from lseek(). + */ + if ((error= (lseek(Filedes, old_offset, MY_SEEK_SET) == -1L)) && + readbytes != -1L) + my_errno= errno; + } + pthread_mutex_unlock(&my_file_info[Filedes].mutex); #else error=((readbytes = (uint) pread(Filedes, Buffer, Count, offset)) != Count); + my_errno= errno; #endif - if (error) + if (error || readbytes != Count) { - my_errno=errno; DBUG_PRINT("warning",("Read only %ld bytes off %ld from %d, errno: %d", readbytes,Count,Filedes,my_errno)); #ifdef THREAD @@ -89,17 +111,40 @@ uint my_pwrite(int Filedes, const byte *Buffer, uint Count, my_off_t offset, for (;;) { #ifndef HAVE_PREAD - int error; + int error= 0; + off_t old_offset; writenbytes= (uint) -1; pthread_mutex_lock(&my_file_info[Filedes].mutex); - error=(lseek(Filedes, offset, MY_SEEK_SET) != -1L && - (writenbytes = (uint) write(Filedes, Buffer, Count)) == Count); + + /* + As we cannot change the file pointer, we save the old position, + before seeking to the given offset + */ + error= ((old_offset= lseek(Filedes, 0L, MY_SEEK_CUR)) == -1L || + lseek(Filedes, offset, MY_SEEK_SET) == -1L); + + if (!error) /* Seek was successful */ + { + if ((writenbytes = (uint) write(Filedes, Buffer, Count)) == -1L) + my_errno= errno; + + /* + We should seek back, even if write failed. If this fails, + we will return an error. If write failed as well, we will + save the errno from write, not from lseek(). + */ + if ((error= (lseek(Filedes, old_offset, MY_SEEK_SET) == -1L)) && + writenbytes != -1L) + my_errno= errno; + } pthread_mutex_unlock(&my_file_info[Filedes].mutex); - if (error) + + if (!error && writenbytes == Count) break; #else if ((writenbytes = (uint) pwrite(Filedes, Buffer, Count,offset)) == Count) break; + my_errno= errno; #endif if ((int) writenbytes != -1) { /* Safegueard */ @@ -108,7 +153,6 @@ uint my_pwrite(int Filedes, const byte *Buffer, uint Count, my_off_t offset, Count-=writenbytes; offset+=writenbytes; } - my_errno=errno; DBUG_PRINT("error",("Write only %d bytes",writenbytes)); #ifndef NO_BACKGROUND #ifdef THREAD From 8299fa3f9e5d459641e01b51e9c016dba08dfbf4 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 30 Jun 2006 03:28:58 +0400 Subject: [PATCH 2/3] fix test failures mysys/my_pread.c: don't set errno without a real error --- mysys/my_pread.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/mysys/my_pread.c b/mysys/my_pread.c index eef92b9ebc6..a829caa80d2 100644 --- a/mysys/my_pread.c +++ b/mysys/my_pread.c @@ -66,8 +66,9 @@ uint my_pread(File Filedes, byte *Buffer, uint Count, my_off_t offset, pthread_mutex_unlock(&my_file_info[Filedes].mutex); #else - error=((readbytes = (uint) pread(Filedes, Buffer, Count, offset)) != Count); - my_errno= errno; + if ((error= ((readbytes = + (uint) pread(Filedes, Buffer, Count, offset)) != Count))) + my_errno= errno; #endif if (error || readbytes != Count) { From 8c89c0d2957e27554582e099cc57e842985bbbd5 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 30 Jun 2006 08:11:51 +0400 Subject: [PATCH 3/3] cleanup mysys/my_pread.c: don't set errno without a real error --- mysys/my_pread.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mysys/my_pread.c b/mysys/my_pread.c index a829caa80d2..ac52895efe9 100644 --- a/mysys/my_pread.c +++ b/mysys/my_pread.c @@ -145,7 +145,8 @@ uint my_pwrite(int Filedes, const byte *Buffer, uint Count, my_off_t offset, #else if ((writenbytes = (uint) pwrite(Filedes, Buffer, Count,offset)) == Count) break; - my_errno= errno; + else + my_errno= errno; #endif if ((int) writenbytes != -1) { /* Safegueard */