The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/contrib/openzfs/tests/zfs-tests/cmd/get_diff.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*
    2  * This file and its contents are supplied under the terms of the
    3  * Common Development and Distribution License ("CDDL"), version 1.0.
    4  * You may only use this file in accordance with the terms of version
    5  * 1.0 of the CDDL.
    6  *
    7  * A full copy of the text of the CDDL should have accompanied this
    8  * source.  A copy of the CDDL is also available via the Internet at
    9  * http://www.illumos.org/license/CDDL.
   10  */
   11 
   12 /*
   13  * Copyright (c) 2018 by Delphix. All rights reserved.
   14  */
   15 
   16 #include <stdio.h>
   17 #include <fcntl.h>
   18 #include <unistd.h>
   19 #include <stdlib.h>
   20 #include <string.h>
   21 #include <assert.h>
   22 #include <sys/param.h>
   23 #include <sys/types.h>
   24 #include <sys/stat.h>
   25 #include <errno.h>
   26 
   27 static void
   28 usage(const char *msg, int exit_value)
   29 {
   30         (void) fprintf(stderr, "usage: get_diff file redacted_file\n%s\n", msg);
   31         exit(exit_value);
   32 }
   33 
   34 /*
   35  * This utility compares two files, an original and its redacted counterpart
   36  * (in that order). It compares the files 512 bytes at a time, printing out
   37  * any ranges (as offset and length) where the redacted file does not match
   38  * the original. This output is used to verify that the expected ranges of
   39  * a redacted file do not contain the original data.
   40  */
   41 int
   42 main(int argc, char *argv[])
   43 {
   44         off_t           diff_off = 0, diff_len = 0, off = 0;
   45         int             fd1, fd2;
   46         char            *fname1, *fname2;
   47         char            buf1[DEV_BSIZE], buf2[DEV_BSIZE];
   48         ssize_t         bytes;
   49 
   50         if (argc != 3)
   51                 usage("Incorrect number of arguments.", 1);
   52 
   53         if ((fname1 = argv[1]) == NULL)
   54                 usage("Filename missing.", 1);
   55         if ((fd1 = open(fname1, O_LARGEFILE | O_RDONLY)) < 0) {
   56                 perror("open1 failed");
   57                 exit(1);
   58         }
   59 
   60         if ((fname2 = argv[2]) == NULL)
   61                 usage("Redacted filename missing.", 1);
   62         if ((fd2 = open(fname2, O_LARGEFILE | O_RDONLY)) < 0) {
   63                 perror("open2 failed");
   64                 exit(1);
   65         }
   66 
   67         while ((bytes = pread(fd1, buf1, DEV_BSIZE, off)) > 0) {
   68                 if (pread(fd2, buf2, DEV_BSIZE, off) < 0) {
   69                         if (errno == EIO) {
   70                                 /*
   71                                  * A read in a redacted section of a file will
   72                                  * fail with EIO. If we get EIO, continue on
   73                                  * but ensure that a comparison of buf1 and
   74                                  * buf2 will fail, indicating a redacted block.
   75                                  */
   76                                 buf2[0] = ~buf1[0];
   77                         } else {
   78                                 perror("pread failed");
   79                                 exit(1);
   80                         }
   81                 }
   82                 if (memcmp(buf1, buf2, bytes) == 0) {
   83                         if (diff_len != 0) {
   84                                 (void) fprintf(stdout, "%lld,%lld\n",
   85                                     (long long)diff_off, (long long)diff_len);
   86                                 assert(off == diff_off + diff_len);
   87                                 diff_len = 0;
   88                         }
   89                         diff_off = 0;
   90                 } else {
   91                         if (diff_len == 0)
   92                                 diff_off = off;
   93                         assert(off == diff_off + diff_len);
   94                         diff_len += bytes;
   95                 }
   96                 off += bytes;
   97         }
   98 
   99         if (diff_len != 0) {
  100                 (void) fprintf(stdout, "%lld,%lld\n", (long long)diff_off,
  101                     (long long)diff_len);
  102         }
  103 
  104         (void) close(fd1);
  105         (void) close(fd2);
  106 
  107         return (0);
  108 }

Cache object: 19ea8d5d955a74b7f838d582486c566d


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.