Panu Matilainen 61937a
commit cdfd0934841d4eccc26d7da7c35b23e6e9f76a9c
Panu Matilainen 61937a
Author: Panu Matilainen <pmatilai@redhat.com>
Panu Matilainen 61937a
Date:   Fri May 29 09:02:44 2009 +0300
Panu Matilainen 61937a
Panu Matilainen 61937a
    Fix calculation of hardlinked files (RhBug:503020)
Panu Matilainen 61937a
    - regression from commit 899dfb58927ec6e91014773430824462f4d0002e,
Panu Matilainen 61937a
      size of hardlinked file set is the size of one file of the set
Panu Matilainen 61937a
    - add isHardLink() internal helper to avoid a copy-paste code
Panu Matilainen 61937a
Panu Matilainen 61937a
diff --git a/build/files.c b/build/files.c
Panu Matilainen 61937a
index ef60ae2..98abedd 100644
Panu Matilainen 61937a
--- a/build/files.c
Panu Matilainen 61937a
+++ b/build/files.c
Panu Matilainen 61937a
@@ -981,6 +981,14 @@ static int isDoc(FileList fl, const char * fileName)
Panu Matilainen 61937a
     return 0;
Panu Matilainen 61937a
 }
Panu Matilainen 61937a
 
Panu Matilainen 61937a
+static int isHardLink(FileListRec flp, FileListRec tlp)
Panu Matilainen 61937a
+{
Panu Matilainen 61937a
+    return ((S_ISREG(flp->fl_mode) && S_ISREG(tlp->fl_mode)) &&
Panu Matilainen 61937a
+	    ((flp->fl_nlink > 1) && (flp->fl_nlink == tlp->fl_nlink)) &&
Panu Matilainen 61937a
+	    (flp->fl_ino == tlp->fl_ino) && 
Panu Matilainen 61937a
+	    (flp->fl_dev == tlp->fl_dev));
Panu Matilainen 61937a
+}
Panu Matilainen 61937a
+
Panu Matilainen 61937a
 /**
Panu Matilainen 61937a
  * Verify that file attributes scope over hardlinks correctly.
Panu Matilainen 61937a
  * If partial hardlink sets are possible, then add tracking dependency.
Panu Matilainen 61937a
@@ -999,14 +1007,18 @@ static int checkHardLinks(FileList fl)
Panu Matilainen 61937a
 
Panu Matilainen 61937a
 	for (j = i + 1; j < fl->fileListRecsUsed; j++) {
Panu Matilainen 61937a
 	    jlp = fl->fileList + j;
Panu Matilainen 61937a
-	    if (!S_ISREG(jlp->fl_mode))
Panu Matilainen 61937a
-		continue;
Panu Matilainen 61937a
-	    if (ilp->fl_nlink != jlp->fl_nlink)
Panu Matilainen 61937a
-		continue;
Panu Matilainen 61937a
-	    if (ilp->fl_ino != jlp->fl_ino)
Panu Matilainen 61937a
-		continue;
Panu Matilainen 61937a
-	    if (ilp->fl_dev != jlp->fl_dev)
Panu Matilainen 61937a
-		continue;
Panu Matilainen 61937a
+	    if (isHardLink(ilp, jlp)) {
Panu Matilainen 61937a
+		return 1;
Panu Matilainen 61937a
+	    }
Panu Matilainen 61937a
+	}
Panu Matilainen 61937a
+    }
Panu Matilainen 61937a
+    return 0;
Panu Matilainen 61937a
+}
Panu Matilainen 61937a
+
Panu Matilainen 61937a
+static int seenHardLink(FileList fl, FileListRec flp)
Panu Matilainen 61937a
+{
Panu Matilainen 61937a
+    for (FileListRec ilp = fl->fileList; ilp < flp; ilp++) {
Panu Matilainen 61937a
+	if (isHardLink(flp, ilp)) {
Panu Matilainen 61937a
 	    return 1;
Panu Matilainen 61937a
 	}
Panu Matilainen 61937a
     }
Panu Matilainen 61937a
@@ -1147,9 +1159,12 @@ static void genCpioListAndHeader(FileList fl,
Panu Matilainen 61937a
 	    rpm_off_t rsize32 = (rpm_off_t)flp->fl_size;
Panu Matilainen 61937a
 	    headerPutUint32(h, RPMTAG_FILESIZES, &rsize32, 1);
Panu Matilainen 61937a
 	}
Panu Matilainen 61937a
-	/* Excludes and dupes have been filtered out by now */
Panu Matilainen 61937a
-	if (S_ISREG(flp->fl_mode))
Panu Matilainen 61937a
-	    totalFileSize += flp->fl_size;
Panu Matilainen 61937a
+	/* Excludes and dupes have been filtered out by now. */
Panu Matilainen 61937a
+	if (S_ISREG(flp->fl_mode)) {
Panu Matilainen 61937a
+	    if (flp->fl_nlink == 1 || !seenHardLink(fl, flp)) {
Panu Matilainen 61937a
+		totalFileSize += flp->fl_size;
Panu Matilainen 61937a
+	    }
Panu Matilainen 61937a
+	}
Panu Matilainen 61937a
 	
Panu Matilainen 61937a
 	/*
Panu Matilainen 61937a
  	 * For items whose size varies between systems, always explicitly 
Panu Matilainen 61937a
@@ -1492,25 +1507,7 @@ static rpmRC addFile(FileList fl, const char * diskPath,
Panu Matilainen 61937a
 	flp->specdFlags = fl->currentSpecdFlags;
Panu Matilainen 61937a
 	flp->verifyFlags = fl->currentVerifyFlags;
Panu Matilainen 61937a
 
Panu Matilainen 61937a
-	/* Hard links need be counted only once. */
Panu Matilainen 61937a
-	if (S_ISREG(flp->fl_mode) && flp->fl_nlink > 1) {
Panu Matilainen 61937a
-	    FileListRec ilp;
Panu Matilainen 61937a
-	    for (i = 0;  i < fl->fileListRecsUsed; i++) {
Panu Matilainen 61937a
-		ilp = fl->fileList + i;
Panu Matilainen 61937a
-		if (!S_ISREG(ilp->fl_mode))
Panu Matilainen 61937a
-		    continue;
Panu Matilainen 61937a
-		if (flp->fl_nlink != ilp->fl_nlink)
Panu Matilainen 61937a
-		    continue;
Panu Matilainen 61937a
-		if (flp->fl_ino != ilp->fl_ino)
Panu Matilainen 61937a
-		    continue;
Panu Matilainen 61937a
-		if (flp->fl_dev != ilp->fl_dev)
Panu Matilainen 61937a
-		    continue;
Panu Matilainen 61937a
-		break;
Panu Matilainen 61937a
-	    }
Panu Matilainen 61937a
-	} else
Panu Matilainen 61937a
-	    i = fl->fileListRecsUsed;
Panu Matilainen 61937a
-
Panu Matilainen 61937a
-	if (!(flp->flags & RPMFILE_EXCLUDE) && S_ISREG(flp->fl_mode) && i >= fl->fileListRecsUsed) {
Panu Matilainen 61937a
+	if (!(flp->flags & RPMFILE_EXCLUDE) && S_ISREG(flp->fl_mode)) {
Panu Matilainen 61937a
 	    /*
Panu Matilainen 61937a
 	     * XXX Simple and stupid check for now, this needs to be per-payload
Panu Matilainen 61937a
 	     * format check once we have other payloads than good 'ole cpio.