@@ -539,7 +539,62 @@ func (m *Manager) listSnapshots(pool string) ([]*ListEntry, error) {
539539 dsType : snapshotType ,
540540 }
541541
542- return m .listDetails (filter )
542+ listEntries , err := m .listDetails (filter )
543+ if err != nil {
544+ return nil , err
545+ }
546+
547+ m .calculateEntrySize (listEntries )
548+
549+ return listEntries , err
550+ }
551+ func (m * Manager ) calculateEntrySize (listEntries []* ListEntry ) {
552+ // TODO: The `go-libzfs` library might be useful to avoid a lot of command actions: https://github.com/bicomsystems/go-libzfs
553+ const preCloneParts = 2
554+
555+ for _ , entry := range listEntries {
556+ // Extract the pre-clone name.
557+ splitEntry := strings .SplitN (entry .Name , "@" , preCloneParts )
558+ if len (splitEntry ) < preCloneParts {
559+ continue
560+ }
561+
562+ preClone := splitEntry [0 ]
563+
564+ if ! strings .Contains (preClone , "_pre" ) {
565+ continue
566+ }
567+
568+ // Get the pre-clone origin.
569+ preSnapshot , err := m .runner .Run (buildOriginCommand (preClone ), false )
570+ if err != nil {
571+ log .Err ("failed to get pre-clone origin" , err .Error ())
572+ continue
573+ }
574+
575+ // Get the pre-snapshot size.
576+ sizeStr , err := m .runner .Run (buildSizeCommand (preSnapshot ), false )
577+ if err != nil {
578+ log .Err ("failed to get pre-snapshot size:" , err .Error ())
579+ continue
580+ }
581+
582+ usedByPreSnapshot , err := util .ParseBytes (sizeStr )
583+ if err != nil {
584+ log .Err ("cannot parse the extracted size of a pre-snapshot:" , err )
585+ continue
586+ }
587+
588+ entry .Used += usedByPreSnapshot
589+ }
590+ }
591+
592+ func buildOriginCommand (clone string ) string {
593+ return "zfs get -H -o value origin " + clone
594+ }
595+
596+ func buildSizeCommand (snapshot string ) string {
597+ return "zfs get -H -p -o value used " + snapshot
543598}
544599
545600// listDetails lists all ZFS types.
0 commit comments