]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Add --older,--older-than,--older-mtime and --older-mtime-than to bsdtar to
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Wed, 3 Oct 2012 00:05:57 +0000 (09:05 +0900)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Wed, 3 Oct 2012 00:05:57 +0000 (09:05 +0900)
enable only including files and directories older than specified date or file.

Makefile.am
tar/bsdtar.1
tar/bsdtar.c
tar/bsdtar.h
tar/cmdline.c
tar/test/CMakeLists.txt
tar/test/test_option_older_than.c [new file with mode: 0644]

index 3e40ab743f194200a96c8584e6b37842cf02e0da..70d4ec28e1a94d3c65f9a8416dcba9d98d8673bd 100644 (file)
@@ -716,6 +716,7 @@ bsdtar_test_SOURCES=                                                \
        tar/test/test_option_n.c                                \
        tar/test/test_option_newer_than.c                       \
        tar/test/test_option_nodump.c                           \
+       tar/test/test_option_older_than.c                       \
        tar/test/test_option_q.c                                \
        tar/test/test_option_r.c                                \
        tar/test/test_option_s.c                                \
index 8b46ea754273c0190ea0746f09fe760d177376ee..efa55c8744b01fab2a2f907c357016a092e74a83 100644 (file)
@@ -24,7 +24,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd December 24, 2011
+.Dd October 3, 2012
 .Dt TAR 1
 .Os
 .Sh NAME
@@ -381,6 +381,24 @@ the archive will be discarded.
 (c, r, u mode)
 A synonym for
 .Fl Fl format Ar ustar
+.It Fl Fl older Ar date
+(c, r, u modes only)
+Only include files and directories older than the specified date.
+This compares ctime entries.
+.It Fl Fl older-mtime Ar date
+(c, r, u modes only)
+Like
+.Fl Fl older ,
+except it compares mtime entries instead of ctime entries.
+.It Fl Fl older-than Pa file
+(c, r, u modes only)
+Only include files and directories older than the specified file.
+This compares ctime entries.
+.It Fl Fl older-mtime-than Pa file
+(c, r, u modes only)
+Like
+.Fl Fl older-than ,
+except it compares mtime entries instead of ctime entries.
 .It Fl Fl one-file-system
 (c, r, and u modes)
 Do not cross mount points.
index b92ada1ecb3ca3069efebcd0353db3fe229fbdab..2c83d0bf7cb9cf966b4a7d8032296538a61e4cbc 100644 (file)
@@ -391,7 +391,6 @@ main(int argc, char **argv)
                 *    --newer-?time='date' Only files newer than 'date'
                 *    --newer-?time-than='file' Only files newer than time
                 *         on specified file (useful for incremental backups)
-                * TODO: Add corresponding "older" options to reverse these.
                 */
                case OPTION_NEWER_CTIME: /* GNU tar */
                        if (archive_match_include_date(bsdtar->matching,
@@ -448,6 +447,40 @@ main(int argc, char **argv)
                case 'o': /* SUSv2 and GNU conflict here, but not fatally */
                        option_o = 1; /* Record it and resolve it later. */
                        break;
+               /*
+                * Selecting files by time:
+                *    --older-?time='date' Only files older than 'date'
+                *    --older-?time-than='file' Only files older than time
+                *         on specified file
+                */
+               case OPTION_OLDER_CTIME:
+                       if (archive_match_include_date(bsdtar->matching,
+                           ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
+                           bsdtar->argument) != ARCHIVE_OK)
+                               lafe_errc(1, 0, "Error : %s",
+                                   archive_error_string(bsdtar->matching));
+                       break;
+               case OPTION_OLDER_CTIME_THAN:
+                       if (archive_match_include_file_time(bsdtar->matching,
+                           ARCHIVE_MATCH_CTIME | ARCHIVE_MATCH_OLDER,
+                           bsdtar->argument) != ARCHIVE_OK)
+                               lafe_errc(1, 0, "Error : %s",
+                                   archive_error_string(bsdtar->matching));
+                       break;
+               case OPTION_OLDER_MTIME:
+                       if (archive_match_include_date(bsdtar->matching,
+                           ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
+                           bsdtar->argument) != ARCHIVE_OK)
+                               lafe_errc(1, 0, "Error : %s",
+                                   archive_error_string(bsdtar->matching));
+                       break;
+               case OPTION_OLDER_MTIME_THAN:
+                       if (archive_match_include_file_time(bsdtar->matching,
+                           ARCHIVE_MATCH_MTIME | ARCHIVE_MATCH_OLDER,
+                           bsdtar->argument) != ARCHIVE_OK)
+                               lafe_errc(1, 0, "Error : %s",
+                                   archive_error_string(bsdtar->matching));
+                       break;
                case OPTION_ONE_FILE_SYSTEM: /* GNU tar */
                        bsdtar->readdisk_flags |=
                            ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;
index 2387c44a354626bd63af858a462a990c5e1851ed..8a11b1e16c714b5ef59268fc8391788fef0a0596 100644 (file)
@@ -132,6 +132,10 @@ enum {
        OPTION_NO_SAME_PERMISSIONS,
        OPTION_NULL,
        OPTION_NUMERIC_OWNER,
+       OPTION_OLDER_CTIME,
+       OPTION_OLDER_CTIME_THAN,
+       OPTION_OLDER_MTIME,
+       OPTION_OLDER_MTIME_THAN,
        OPTION_ONE_FILE_SYSTEM,
        OPTION_OPTIONS,
        OPTION_POSIX,
index 83bbda13629ff4344aa8722b0a8e76488c964d06..329d8e5c76f0b5a05dbdf71a6c7eb2058a3a5711 100644 (file)
@@ -112,6 +112,12 @@ static const struct bsdtar_option {
        { "norecurse",            0, 'n' },
        { "null",                 0, OPTION_NULL },
        { "numeric-owner",        0, OPTION_NUMERIC_OWNER },
+       { "older",                1, OPTION_OLDER_CTIME },
+       { "older-ctime",          1, OPTION_OLDER_CTIME },
+       { "older-ctime-than",     1, OPTION_OLDER_CTIME_THAN },
+       { "older-mtime",          1, OPTION_OLDER_MTIME },
+       { "older-mtime-than",     1, OPTION_OLDER_MTIME_THAN },
+       { "older-than",           1, OPTION_OLDER_CTIME_THAN },
        { "one-file-system",      0, OPTION_ONE_FILE_SYSTEM },
        { "options",              1, OPTION_OPTIONS },
        { "posix",                0, OPTION_POSIX },
index 9cdb68b7fa7420f5fe39223fb81ad43a9c4ecd39..6d74530da52243f6bb7effe702a5ab73e9660977 100644 (file)
@@ -29,6 +29,7 @@ IF(ENABLE_TAR AND ENABLE_TEST)
     test_option_n.c
     test_option_newer_than.c
     test_option_nodump.c
+    test_option_older_than.c
     test_option_q.c
     test_option_r.c
     test_option_s.c
diff --git a/tar/test/test_option_older_than.c b/tar/test/test_option_older_than.c
new file mode 100644 (file)
index 0000000..1313aed
--- /dev/null
@@ -0,0 +1,82 @@
+/*-
+ * Copyright (c) 2010 Tim Kientzle
+ * Copyright (c) 2012 Michihiro NAKAJIMA
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include "test.h"
+__FBSDID("$FreeBSD$");
+
+DEFINE_TEST(test_option_older_than)
+{
+       struct stat st;
+
+       /*
+        * Basic test of --older-than.
+        * First, create three files with different mtimes.
+        * Create test1.tar with --older-than, test2.tar without.
+        */
+       assertMakeDir("test1in", 0755);
+       assertChdir("test1in");
+       assertMakeDir("a", 0755);
+       assertMakeDir("a/b", 0755);
+       assertMakeFile("old.txt", 0644, "old.txt");
+       assertMakeFile("a/b/old.txt", 0644, "old file in old directory");
+       assertEqualInt(0, stat("old.txt", &st));
+       sleepUntilAfter(st.st_mtime);
+       assertMakeFile("middle.txt", 0644, "middle.txt");
+       assertEqualInt(0, stat("middle.txt", &st));
+       sleepUntilAfter(st.st_mtime);
+       assertMakeFile("new.txt", 0644, "new");
+       assertMakeFile("a/b/new.txt", 0644, "new file in old directory");
+
+       /* Test --older-than on create */
+       assertEqualInt(0,
+               systemf("%s -cf ../test1.tar --older-than middle.txt *.txt a",
+                       testprog));
+       assertEqualInt(0, systemf("%s -cf ../test2.tar *.txt a", testprog));
+       assertChdir("..");
+
+       /* Extract test1.tar to a clean dir and verify what got archived. */
+       assertMakeDir("test1out", 0755);
+       assertChdir("test1out");
+       assertEqualInt(0, systemf("%s xf ../test1.tar", testprog));
+       assertFileNotExists("new.txt");
+       assertFileNotExists("a/b/new.txt");
+       assertFileNotExists("middle.txt");
+       assertFileExists("old.txt");
+       assertFileExists("a/b/old.txt");
+       assertChdir("..");
+
+       /* Extract test2.tar to a clean dir with --older-than and verify. */
+       assertMakeDir("test2out", 0755);
+       assertChdir("test2out");
+       assertEqualInt(0,
+               systemf("%s xf ../test2.tar --older-than ../test1in/middle.txt",
+                       testprog));
+       assertFileNotExists("new.txt");
+       assertFileNotExists("a/b/new.txt");
+       assertFileNotExists("middle.txt");
+       assertFileExists("old.txt");
+       assertFileExists("a/b/old.txt");
+       assertChdir("..");
+}