From bba08ed9c64e8e8dad239b19e6ca8128adcaac64 Mon Sep 17 00:00:00 2001
From: Martin Wilck <mwilck@arcor.de>
Date: Fri, 20 Nov 2015 00:17:13 +0100
Subject: [PATCH 2/5] gpart: avoid superfluous seek calls

The l64opush() logic would cause 2 lseek() syscalls per guess loop.
By simply seeking back after every probing call, we do with a single
seek() call.
---
 src/gpart.c | 42 ++++++++++++++++++++++--------------------
 1 file changed, 22 insertions(+), 20 deletions(-)

diff --git src/gpart.c src/gpart.c
index 8555f66..bd3806a 100644
--- a/src/gpart.c
+++ b/src/gpart.c
@@ -13,7 +13,7 @@
  * Modified:  11.06.1999 <mb@ichabod.han.de>
  *            Handle disk read errors.
  *            Minor fixes.
- * 
+ *
  *            29.06.1999 <mb@ichabod.han.de>
  *            Made every disk read/write buffer aligned to pagesize.
  *
@@ -21,7 +21,7 @@
  *            Default scan increment now 's'.
  *            Extended ptbl boundary condition now depends on scan
  *            increment.
- * 
+ *
  *            26.02.2000 <mb@ichabod.han.de>
  *            Default scan increment 'h' again.
  *            Fixed faulty head boundary condition.
@@ -871,7 +871,7 @@ static void add_guessed_p(disk_desc *d,dos_part_entry *p,int cnt)
 		gpt = d->d_gl = (dos_guessed_pt *)alloc(sizeof(dos_guessed_pt));
 	else
 	{
-		for (gpt = d->d_gl; gpt->g_next; gpt = gpt->g_next)	
+		for (gpt = d->d_gl; gpt->g_next; gpt = gpt->g_next)
 			;
 		gpt->g_next = (dos_guessed_pt *)alloc(sizeof(dos_guessed_pt));
 		gpt = gpt->g_next;
@@ -999,15 +999,18 @@ static void do_guess_loop(disk_desc *d)
 	pr(MSG,DM_STARTSCAN);
 
 scanloop:
-	while ((rd = bread(d->d_fd,d->d_sbuf,d->d_ssize,nsecs)) == bsize)
-	{
-		int		mod, have_ext = 0;
-		g_module	*bg;
-		s64_t		sz, ofs;
-
-		d->d_nsb += incr; noffset = 0;
-		ofs = d->d_nsb; s2mb(d,ofs);
-		if (maxsec && (d->d_nsb > maxsec)) break;
+	while ((rd = bread(d->d_fd, d->d_sbuf, d->d_ssize, nsecs)) == bsize) {
+		int mod, have_ext = 0;
+		g_module *bg;
+		s64_t sz, ofs, fpos;
+
+		d->d_nsb += incr;
+		noffset = 0;
+		ofs = d->d_nsb;
+		s2mb(d, ofs);
+		fpos = d->d_nsb * d->d_ssize + bsize;
+		if (maxsec && (d->d_nsb > maxsec))
+			break;
 
 		/*
 		 * reset modules
@@ -1026,15 +1029,14 @@ guessit:
 			/*
 			 * because a gmodule is allowed to seek on
 			 * d->d_fd the current file position must be
-			 * saved.
+			 * restored after calling it.
 			 */
 
-			memset(&m->m_part,0,sizeof(dos_part_entry));
-			m->m_guess = GM_NO; l64opush(d->d_fd);
-			if ((*m->m_gfun)(d,m) && (m->m_guess * m->m_weight >= GM_PERHAPS))
+			memset(&m->m_part, 0, sizeof(dos_part_entry));
+			m->m_guess = GM_NO;
+			if ((*m->m_gfun)(d, m) && (m->m_guess * m->m_weight >= GM_PERHAPS))
 				guesses[mod++] = m;
-			if ((sz = l64opop(d->d_fd)) != l64tell(d->d_fd))
-				l64seek(d->d_fd,sz,SEEK_SET);
+			l64seek(d->d_fd, fpos, SEEK_SET);
 		}
 
 		/*
@@ -1288,7 +1290,7 @@ static void write_primary_ptbl(disk_desc *d,char *dev)
 		{
 			if ((n = number_or_quit(DM_ACTWHICHPART,1,NDOSPARTS)) < 0)
 				break;
-			if ((n >= 1) && (n <= NDOSPARTS) && 
+			if ((n >= 1) && (n <= NDOSPARTS) &&
 			    get_part_type(d->d_gpt.t_parts[n].p_typ))
 			{
 				d->d_gpt.t_parts[n - 1].p_flag = DOSPARTACTIVE;
@@ -1571,7 +1573,7 @@ static int check_partition_list(disk_desc *d)
 		if (gp->g_orph) pr(MSG,PM_G_ORPHANED);
 		if (gp->g_prim) pr(MSG,PM_G_PRIMARY);
 		if (gp->g_log)  pr(MSG,PM_G_LOGICAL);
-		pr(MSG,"\n"); 
+		pr(MSG,"\n");
 		if (f_verbose > 1)
 			print_partition(d,p,gp->g_log ? 1 : 0,0);
 	}
-- 
2.24.0

