aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorflu0r1ne <flu0r1ne@flu0r1ne.net>2021-07-29 13:09:23 -0500
committerflu0r1ne <flu0r1ne@flu0r1ne.net>2021-07-29 13:09:23 -0500
commit2c10158ce92f102280d4d87406ebeea3e05bf915 (patch)
tree61797fcdea06e128a04af05ce611f9babc0a8775
parent34d7d612dd376258248ee6cae07b839951fccf32 (diff)
downloadzsu-2c10158ce92f102280d4d87406ebeea3e05bf915.tar.xz
zsu-2c10158ce92f102280d4d87406ebeea3e05bf915.zip
Added README, Licence, and fixed issue with modules
-rw-r--r--COPYING280
-rw-r--r--README.md126
-rw-r--r--cmd/cat.go24
-rw-r--r--cmd/diff.go61
-rw-r--r--cmd/list.go2
-rw-r--r--cmd/overwrite.go21
-rw-r--r--go.mod2
-rw-r--r--go.sum14
-rw-r--r--main.go18
-rw-r--r--snap/fs.go82
-rw-r--r--snap/parsing.go29
-rw-r--r--snap/parsing_test.go31
12 files changed, 576 insertions, 114 deletions
diff --git a/COPYING b/COPYING
new file mode 100644
index 0000000..d8cf7d4
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,280 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users. This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it. (Some other Free Software Foundation software is covered by
+the GNU Lesser General Public License instead.) You can apply it to
+your programs, too.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+ To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+ For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have. You must make sure that they, too, receive or can get the
+source code. And you must show them these terms so they know their
+rights.
+
+ We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+ Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software. If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+ Finally, any free program is threatened constantly by software
+patents. We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary. To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ GNU GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License. The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language. (Hereinafter, translation is included without limitation in
+the term "modification".) Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+ 1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+ 2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+ a) You must cause the modified files to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ b) You must cause any work that you distribute or publish, that in
+ whole or in part contains or is derived from the Program or any
+ part thereof, to be licensed as a whole at no charge to all third
+ parties under the terms of this License.
+
+ c) If the modified program normally reads commands interactively
+ when run, you must cause it, when started running for such
+ interactive use in the most ordinary way, to print or display an
+ announcement including an appropriate copyright notice and a
+ notice that there is no warranty (or else, saying that you provide
+ a warranty) and that users may redistribute the program under
+ these conditions, and telling the user how to view a copy of this
+ License. (Exception: if the Program itself is interactive but
+ does not normally print such an announcement, your work based on
+ the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works. But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+ a) Accompany it with the complete corresponding machine-readable
+ source code, which must be distributed under the terms of Sections
+ 1 and 2 above on a medium customarily used for software interchange; or,
+
+ b) Accompany it with a written offer, valid for at least three
+ years, to give any third party, for a charge no more than your
+ cost of physically performing source distribution, a complete
+ machine-readable copy of the corresponding source code, to be
+ distributed under the terms of Sections 1 and 2 above on a medium
+ customarily used for software interchange; or,
+
+ c) Accompany it with the information you received as to the offer
+ to distribute corresponding source code. (This alternative is
+ allowed only for noncommercial distribution and only if you
+ received the program in object code or executable form with such
+ an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it. For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable. However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License. Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+ 5. You are not required to accept this License, since you have not
+signed it. However, nothing else grants you permission to modify or
+distribute the Program or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+ 6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions. You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+ 7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all. For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices. Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+ 8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded. In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+ 9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time. Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation. If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+ 10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission. For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this. Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+ NO WARRANTY
+
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+ END OF TERMS AND CONDITIONS
diff --git a/README.md b/README.md
index 1cd5b1e..f820cef 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,122 @@
-ZFS File Diff:
-==============
+ZFS Snapshot Utils:
+===================
-Instead of operating at a dataset level, ZFS File Diff provides extra tools
+Instead of operating at a dataset level, `zsu` provides extra tools
for operating on single files within a snapshot.
This is useful when you:
-- Accedently deleted a file
-- Identify the changes file over time
+- Accidentally deleted a file
+- Wish to identify the changes within a file over time
```
-zfdiff list [--with-paths|-p] <reference>
-zfdiff cat <snap> <reference>
-zfdiff overwrite <snap> <reference>
-zfdiff diff <snap>..<snap> <reference>
-``` \ No newline at end of file
+zsu list [--with-paths|-p] <reference>
+zsu cat <reference> <snap>
+zsu diff <reference> <snap>..<snap>
+```
+
+## Build and install
+
+Requirements:
+- diff (likely installed)
+- zfs snapshots directory
+ + Enable it with `sudo zfs set snapdir=visible pool/dataset`
+
+```
+git clone path
+cd zsu
+go install
+```
+
+## Examples:
+
+The `list` subcommand will search for a pathspec in all ZFS snapshots. The `@`
+refers to the current version. It only shows snapshots where a file has been
+found at that pathspec. (E.g. if the file was moved, it will not automatically
+detect the location of the new version.) The path need not exist within the
+live version of the file system.
+
+### List
+```
+[#] zsu list cabinet/file.txt
+...
+autosnap_2021-07-29_07:00:19_hourly
+autosnap_2021-07-29_08:00:18_hourly
+autosnap_2021-07-29_09:00:18_hourly
+autosnap_2021-07-29_10:00:19_hourly
+autosnap_2021-07-29_11:00:18_hourly
+autosnap_2021-07-29_12:00:18_hourly
+autosnap_2021-07-29_13:00:19_hourly
+autosnap_2021-07-29_14:00:18_hourly
+autosnap_2021-07-29_15:00:19_frequently
+autosnap_2021-07-29_15:00:19_hourly
+autosnap_2021-07-29_15:15:18_frequently
+autosnap_2021-07-29_15:30:18_frequently
+autosnap_2021-07-29_15:45:18_frequently
+autosnap_2021-07-29_16:00:19_frequently
+autosnap_2021-07-29_16:00:19_hourly
+autosnap_2021-07-29_16:15:18_frequently
+autosnap_2021-07-29_16:30:18_frequently
+autosnap_2021-07-29_16:45:18_frequently
+@
+```
+
+```
+[#] zsu list deleted.txt
+autosnap_2021-07-01_00:00:03_monthly
+autosnap_2021-07-02_00:00:23_daily
+autosnap_2021-07-03_00:00:00_daily
+autosnap_2021-07-04_00:00:28_daily autosnap_2021-07-05_00:00:25_daily autosnap_2021-07-06_00:00:19_daily
+```
+
+### Cat
+The `cat` command will write the contents of a file to `stdout`. A snapshot can
+be specified to obtain the version within a snapshot. It behaves more like GNU
+paste than GNU cat.
+
+```
+[#] zsu cat deleted.txt autosnap_2021-07-29_15:45:18_frequently
+This is the contents of the file at 29-07-2021
+```
+
+### Specifying snapshots relative to each other
+With any commend, a snapshot can be specified a number of ways. As shown above,
+it can be specified using the name of the snapshot. A snapshot can also be specified
+relative to another. For example, `autosnap_2021-07-29_15:45:18_frequently-`
+means "the snapshot before autosnap_2021-07-29_15:45:18_frequently" and
+`autosnap_2021-07-29_15:45:18_frequently+` means "the snapshot after
+autosnap_2021-07-29_15:45:18_frequently" Any number of `+` or `-` can be appended
+to the snapshot. If the snapshot ends with a special symbol, it can be specified by name
+using quotes `'"snap-"'`. There are two special references provided for convenience. `@`
+refers to the latest snapshot at the given path. If the file exists within the
+current file system, it will refer to this file. `%` refers to the first snapshot
+including the file.
+
+Examples:
+```
+[#] zsu cat deleted.txt @ # the current file
+[#] zsu cat deleted.txt @- # the most recent snapshot
+[#] zsu cat deleted.txt @-- # the snapshot before the most recent
+[#] zsu cat deleted.txt @-2 # also the snapshot before the most recent
+[#] zsu cat deleted.txt %+ # the second snapshot which exists with this path
+[#] zsu cat deleted.txt % # the first snapshot
+```
+
+### Diff
+
+The diff subcommand shows the `diff` of two snapshots. `diff` must be present and in your `PATH`.
+
+```
+# Specify the snapshots directly
+zsu diff changing.md autosnap_2021-07-29_17:15:18_frequently..autosnap_2021-07-29_17:30:18_frequently
+27a28
+> Added this line
+70a73
+< Removed this line
+
+# Or use the relative syntax
+zsu diff changing.txt @--..@-
+27a28
+> Added this line
+70a73
+< Removed this line
+```
diff --git a/cmd/cat.go b/cmd/cat.go
index 186f7da..f3ea2bf 100644
--- a/cmd/cat.go
+++ b/cmd/cat.go
@@ -2,7 +2,7 @@ package cmd
import (
"fmt"
- "golang.flu0r1ne.net/zfdiff/snap"
+ "golang.flu0r1ne.net/zsu/snap"
"os"
"io"
)
@@ -14,34 +14,32 @@ func Cat(params []string) {
die.Fatal("Reference file is required")
}
- if(n_params > 3) {
+ if(n_params > 2) {
die.Fatal("Too many arguments provided")
}
- var reference string
- snapish := ""
+ reference := params[0]
- if(n_params == 1) {
- reference = params[0]
- } else {
- snapish, reference = params[0], params[1]
+ snapish := "@"
+ if(n_params > 1) {
+ snapish = params[1]
}
snapRef := snap.ToRelative(snapish)
oracle := snap.GetOracle(reference)
- path := oracle.ResolveRelative(snapRef)
+ path := oracle.PathTo(snapRef)
file, err := os.Open(path)
if err != nil {
- die.Fatal("Could not open snapshot %s\nError: %v", path, err)
+ die.Fatalf("Could not open snapshot %s\nError: %v", path, err)
}
defer func() {
if err = file.Close(); err != nil {
- die.Fatal("Could not close file %s\nError: %v", path, err)
+ die.Fatalf("Could not close file %s\nError: %v", path, err)
}
}()
@@ -59,7 +57,7 @@ func Cat(params []string) {
}
if err != nil {
- die.Fatal("Encountered error while reading file: %v", err)
+ die.Fatalf("Encountered error while reading file: %v", err)
}
}
-} \ No newline at end of file
+}
diff --git a/cmd/diff.go b/cmd/diff.go
index 46327b3..8d6a85f 100644
--- a/cmd/diff.go
+++ b/cmd/diff.go
@@ -1,8 +1,13 @@
package cmd
import (
+ "golang.flu0r1ne.net/zsu/snap"
+ "os/exec"
+ "os"
+ "io"
+ "log"
"fmt"
- "golang.flu0r1ne.net/zfdiff/snap"
+ "errors"
)
func Diff(params []string) {
@@ -15,9 +20,57 @@ func Diff(params []string) {
die.Fatal("Too many arguments provided")
}
- from, to := snap.ParseDiff(params[0])
+ reference := params[0]
- reference := params[1]
+ from, to := snap.ToRelative("@-"), snap.ToRelative("@")
+ if len(params) > 1 {
+ from, to = snap.ParseDiff(params[1])
+ }
+
+ oracle := snap.GetOracle(reference)
+
+ fromPath := oracle.PathTo(from)
+ toPath := oracle.PathTo(to)
+
+ _, noColor := os.LookupEnv("NO_COLOR")
+
+ colorOpt := "--color=always"
+ if noColor {
+ colorOpt = "--color=never"
+ }
+
+ diff := exec.Command("diff", colorOpt, fromPath, toPath)
+ diff.Env = os.Environ()
+
+ stderr, err := diff.StderrPipe()
+ if err != nil {
+ log.Fatal(err);
+ }
- fmt.Println(from, to, reference);
+ stdout, err := diff.StdoutPipe()
+ if err != nil {
+ log.Fatal(err);
+ }
+
+ if err := diff.Start(); err != nil {
+ log.Fatal(err)
+ }
+
+ outtext, _ := io.ReadAll(stdout)
+ errtext, _ := io.ReadAll(stderr)
+
+ fmt.Fprintf(os.Stderr, "%s", errtext)
+ fmt.Fprintf(os.Stdout, "%s", outtext)
+
+ if err := diff.Wait(); err != nil {
+ var exiterr *exec.ExitError
+
+ // GNU Diff exits 1 if the files differ
+ if errors.As(err, &exiterr) && exiterr.ExitCode() == 1 {
+ os.Exit(exiterr.ExitCode())
+ }
+
+ die.Fatalf("Error encountered while executing diff.")
+ os.Exit(2)
+ }
}
diff --git a/cmd/list.go b/cmd/list.go
index 5849975..94c18b8 100644
--- a/cmd/list.go
+++ b/cmd/list.go
@@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"flag"
- "golang.flu0r1ne.net/zfdiff/snap"
+ "golang.flu0r1ne.net/zsu/snap"
)
func List(params []string) {
diff --git a/cmd/overwrite.go b/cmd/overwrite.go
deleted file mode 100644
index 324dfb4..0000000
--- a/cmd/overwrite.go
+++ /dev/null
@@ -1,21 +0,0 @@
-package cmd
-
-import (
- "fmt"
- "golang.flu0r1ne.net/zfdiff/snap"
-)
-
-func Overwrite(params [] string) {
- if(len(params) == 0) {
- die.Fatal("Reference file is required")
- }
-
- if(len(params) > 3) {
- die.Fatal("Too many arguments provided")
- }
-
- snapRef := snap.ToRelative(params[0])
- reference := params[1]
-
- fmt.Print(snapRef, reference)
-} \ No newline at end of file
diff --git a/go.mod b/go.mod
index 91c9be7..f734cf2 100644
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,3 @@
-module golang.flu0r1ne.net/zfdiff
+module golang.flu0r1ne.net/zsu
go 1.16
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..4cc9ca7
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,14 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
+github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
+github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
diff --git a/main.go b/main.go
index 42c9f16..2a0cae4 100644
--- a/main.go
+++ b/main.go
@@ -1,10 +1,17 @@
package main
+// This software is licensed for under the Free Software Foundations's GPL v2, as retrieved
+// from https://opensource.org/licenses/gpl-2.0.php (2021).
+
import (
"os"
"io"
"fmt"
- "golang.flu0r1ne.net/zfdiff/cmd"
+ "golang.flu0r1ne.net/zsu/cmd"
+)
+
+const (
+ VERSION = "0.0.1"
)
func printUsage(w io.Writer) {
@@ -12,9 +19,8 @@ func printUsage(w io.Writer) {
fmt.Fprintln(w, " help ")
fmt.Fprintln(w, " version ")
fmt.Fprintln(w, " list [-paths|-p] <reference> ")
- fmt.Fprintln(w, " diff <snap-ish>..<snap-ish> <reference> ")
- fmt.Fprintln(w, " cat <snap-ish> <reference> ")
- fmt.Fprintln(w, " overwrite <snap-ish> <reference> ")
+ fmt.Fprintln(w, " diff <reference> <snap-ish>..<snap-ish> ")
+ fmt.Fprintln(w, " cat <reference> <snap-ish> ")
}
func dieUsage() {
@@ -34,8 +40,8 @@ func main() {
switch subcommand {
case "list":
cmd.List(subargs)
- case "overwrite":
- cmd.Overwrite(subargs)
+ case "version":
+ fmt.Printf("%s\n", VERSION)
case "cat":
cmd.Cat(subargs)
case "diff":
diff --git a/snap/fs.go b/snap/fs.go
index d0dc83d..b1cf513 100644
--- a/snap/fs.go
+++ b/snap/fs.go
@@ -5,6 +5,7 @@ import (
"path/filepath"
"os"
"sort"
+ "io"
)
type Snapshot struct {
@@ -36,6 +37,19 @@ func getZFSRoot(reference string) string {
return cursor
}
+func (snap * Snapshot) tryReference(reference string) {
+ if _, err := os.Stat(reference); err != nil {
+
+ if !os.IsNotExist(err) {
+ die.Fatalf("Error encountered while attempting to read reference file in snapshot:\n%v", err)
+ }
+
+ snap.Reference = ""
+ } else {
+ snap.Reference = reference
+ }
+}
+
func getSnapshots(reference string) []*Snapshot {
zfsRoot := getZFSRoot(reference)
@@ -43,15 +57,16 @@ func getSnapshots(reference string) []*Snapshot {
files, err := os.ReadDir(snapshotDir)
if err != nil {
- die.Fatalf("Could not find the snapshots directory inside %s", snapshotDir);
+ die.Fatalf("Could not find the snapshots directory inside \"%s\"", snapshotDir);
}
relPath, err := filepath.Rel(zfsRoot, reference)
if err != nil {
- die.Fatal("Could not find relative path to reference file: %v", err)
+ die.Fatalf("Could not find relative path to reference file: %v", err)
}
- snaps := make([]*Snapshot, len(files))
+ n_snaps := len(files) + 1
+ snaps := make([]*Snapshot, n_snaps)
for i, file := range files {
@@ -68,16 +83,17 @@ func getSnapshots(reference string) []*Snapshot {
pathInSnap := filepath.Join(snapshotDir, name, relPath)
- if _, err := os.Stat(pathInSnap); err != nil {
- if !os.IsNotExist(err) {
- die.Fatalf("Error encountered while attempting to read reference file in snapshot:\n%v", err)
- }
- } else {
- snaps[i].Reference = pathInSnap
- }
+ snaps[i].tryReference(pathInSnap)
+ }
+
+ snaps[n_snaps - 1] = &Snapshot {
+ ModTime: time.Now(),
+ Name: "@",
}
+ snaps[n_snaps - 1].tryReference(reference)
+
return snaps
}
@@ -97,6 +113,18 @@ func GetTimeseries(reference string) []*Snapshot {
return snaps
}
+func referenceMap(snapTs []*Snapshot) []int {
+ var trimmed []int
+
+ for i, snap := range snapTs {
+ if snap.Reference != "" {
+ trimmed = append(trimmed, i)
+ }
+ }
+
+ return trimmed
+}
+
func buildNameMap(snapTs []*Snapshot) map[string] int {
namemap := make(map[string] int)
@@ -123,15 +151,25 @@ func GetOracle(reference string) *SnapshotOracle {
}
}
-func (oracle *SnapshotOracle) ResolveRelative(ref *Relative) string {
+func (oracle *SnapshotOracle) PathTo(ref *Relative) string {
n_ts := len(oracle.timeseries)
- wanted := n_ts - 1
+ nthRef := referenceMap(oracle.timeseries)
- if ref.snapshot != "" {
+ if len(nthRef) == 0 {
+ die.Fatalf("The reference file is not contained in any snapshot.")
+ }
+
+ var wanted int
+ switch (ref.snapshot) {
+ case "@":
+ wanted = nthRef[len(nthRef) - 1]
+ case "%":
+ wanted = nthRef[0]
+ default:
id, ok := oracle.namemap[ref.snapshot]
if !ok {
- die.Fatal("Could not find the snapshot %s", ref.snapshot)
+ die.Fatalf("Could not find the snapshot \"%s\"", ref.snapshot)
}
wanted = id
@@ -144,4 +182,18 @@ func (oracle *SnapshotOracle) ResolveRelative(ref *Relative) string {
}
return oracle.timeseries[wanted].Reference
-} \ No newline at end of file
+}
+
+func (oracle *SnapshotOracle) ReadAll(ref *Relative) string {
+ path := oracle.PathTo(ref)
+
+ file, err := os.Open(path)
+
+ data, err := io.ReadAll(file)
+
+ if err != nil {
+ die.Fatalf("Can't read snapshot.\n%v", err)
+ }
+
+ return string(data)
+}
diff --git a/snap/parsing.go b/snap/parsing.go
index fcacec9..0d1f09f 100644
--- a/snap/parsing.go
+++ b/snap/parsing.go
@@ -18,11 +18,6 @@ func parseEscaped(snapish string) *Relative {
if isEscaped(snapish) {
snapshot := strings.Trim(snapish, `"`);
- if(snapshot == "") {
- die.Fatal("Snapshot cannot be empty \"\"")
-
- }
-
return &Relative {
snapshot: snapshot,
}
@@ -69,7 +64,7 @@ func parseRelativeSyntax(snapish, directionToken string) *Relative {
}
}
-func ToRelative(snapish string) * Relative {
+func toRelative(snapish string) * Relative {
if ref := parseEscaped(snapish); ref != nil {
return ref;
}
@@ -78,7 +73,7 @@ func ToRelative(snapish string) * Relative {
return ref;
}
- if ref := parseRelativeSyntax(snapish, "^"); ref != nil {
+ if ref := parseRelativeSyntax(snapish, "-"); ref != nil {
ref.offset = -ref.offset;
return ref;
}
@@ -88,16 +83,26 @@ func ToRelative(snapish string) * Relative {
};
}
-func ParseDiff(snapdiff string) (fromRel, toRel *Relative) {
- snapishes := strings.Split(snapdiff, "..")
+func ToRelative(snapish string) * Relative {
+ rel := toRelative(snapish)
+
+ if rel.snapshot == "" {
+ die.Fatal("Please provide a snapshot. You can use @ for the current filesystem and $ for the last snapshot.")
+ }
+
+ return rel
+}
+
+func ParseDiff(snapishdiff string) (fromRel, toRel *Relative) {
+ snapishes := strings.Split(snapishdiff, "..")
- if(len(snapishes) > 2) {
+ if len(snapishes) > 2 {
die.Fatal("Cannot diff more than two snapshots");
}
fromRel = ToRelative(snapishes[0])
- toRel = &Relative{}
- if(len(snapishes) == 2) {
+ toRel = &Relative{snapshot: "@"}
+ if len(snapishes) == 2 {
toRel = ToRelative(snapishes[1])
}
diff --git a/snap/parsing_test.go b/snap/parsing_test.go
deleted file mode 100644
index 4aa232e..0000000
--- a/snap/parsing_test.go
+++ /dev/null
@@ -1,31 +0,0 @@
-package snap
-
-import "testing"
-
-func TestRelativeParsing(t * testing.T) {
- cases := []struct {
- snapish string
- snapshot string
- offset int
- } {
- {"snapshot", "snapshot", 0},
- {"testing~~", "testing", -2},
- {"%SNAPSHOT%^+++", "%SNAPSHOT%^", 3},
- {"~~prefixed", "~~prefixed", 0},
- {"+++", "", 3},
- {"~~~", "", -3},
- {"+5", "", 5},
- {"~3", "", -3},
- {"+", "", 1},
- {"~", "", -1},
- {`"~"`, "~", 0},
- }
-
- for _, c := range cases {
- got := ToRelative(c.snapish)
-
- if got.offset != c.offset || got.snapshot != c.snapshot {
- t.Errorf("ToRelative(%s) == %+v, wanted %+v", c.snapish, got, c)
- }
- }
-} \ No newline at end of file