From: Michihiro NAKAJIMA Date: Wed, 3 Oct 2012 00:05:57 +0000 (+0900) Subject: Add --older,--older-than,--older-mtime and --older-mtime-than to bsdtar to X-Git-Tag: v3.1.0~40^2~119 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=58a5bbef0a5f2e44abb73526942e072e0ecb3147;p=thirdparty%2Flibarchive.git Add --older,--older-than,--older-mtime and --older-mtime-than to bsdtar to enable only including files and directories older than specified date or file. --- diff --git a/Makefile.am b/Makefile.am index 3e40ab743..70d4ec28e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/tar/bsdtar.1 b/tar/bsdtar.1 index 8b46ea754..efa55c874 100644 --- a/tar/bsdtar.1 +++ b/tar/bsdtar.1 @@ -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. diff --git a/tar/bsdtar.c b/tar/bsdtar.c index b92ada1ec..2c83d0bf7 100644 --- a/tar/bsdtar.c +++ b/tar/bsdtar.c @@ -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; diff --git a/tar/bsdtar.h b/tar/bsdtar.h index 2387c44a3..8a11b1e16 100644 --- a/tar/bsdtar.h +++ b/tar/bsdtar.h @@ -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, diff --git a/tar/cmdline.c b/tar/cmdline.c index 83bbda136..329d8e5c7 100644 --- a/tar/cmdline.c +++ b/tar/cmdline.c @@ -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 }, diff --git a/tar/test/CMakeLists.txt b/tar/test/CMakeLists.txt index 9cdb68b7f..6d74530da 100644 --- a/tar/test/CMakeLists.txt +++ b/tar/test/CMakeLists.txt @@ -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 index 000000000..1313aed55 --- /dev/null +++ b/tar/test/test_option_older_than.c @@ -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(".."); +}