Add support for empty files to files_compare() 07/31107/1
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 1 Dec 2014 11:02:13 +0000 (12:02 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 1 Dec 2014 11:16:13 +0000 (12:16 +0100)
This patch modifies approach to return values. It returns an integer
less than, equal to, or greater than zero if fd1 is found, respectively,
to be less than, to match, or be greater than fd2.

Change-Id: I9402a19a3280023ee87524cccdec36fafe52b75b

tests/libsmack-tests/test_cases.cpp

index c8e7606..7f71e8a 100644 (file)
@@ -67,8 +67,19 @@ int files_compare(int fd1, int fd2)
     RUNNER_ASSERT_ERRNO_MSG(fstat(fd1, &fs1) == 0, "fstat failed");
     RUNNER_ASSERT_ERRNO_MSG(fstat(fd2, &fs2) == 0, "fstat failed");
 
-    if (fs1.st_size != fs2.st_size) //if files are identical size will be the same
+    if (fs1.st_size < fs2.st_size) {
         return -1;
+    }
+
+    if (fs1.st_size > fs2.st_size) {
+        return 1;
+    }
+
+    //since Linux 2.6.12, mmap returns EINVAL if length is 0
+    //if both lengths are 0, files are actually the same
+    if (0 == fs1.st_size && 0 == fs2.st_size) {
+        return 0;
+    }
 
     //mapping files to process memory
     RUNNER_ASSERT_ERRNO_MSG((h1 = mmap(0, fs1.st_size, PROT_READ, MAP_SHARED, fd1, 0 )) != MAP_FAILED,