github.com/cloud-foundations/dominator@v0.0.0-20221004181915-6e4fee580046/cmd/subtool/pushFile.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/Cloud-Foundations/Dominator/lib/filesystem"
     8  	"github.com/Cloud-Foundations/Dominator/lib/log"
     9  	objclient "github.com/Cloud-Foundations/Dominator/lib/objectserver/client"
    10  	"github.com/Cloud-Foundations/Dominator/lib/srpc"
    11  	"github.com/Cloud-Foundations/Dominator/lib/triggers"
    12  	"github.com/Cloud-Foundations/Dominator/lib/wsyscall"
    13  	"github.com/Cloud-Foundations/Dominator/proto/sub"
    14  	"github.com/Cloud-Foundations/Dominator/sub/client"
    15  )
    16  
    17  func pushFileSubcommand(args []string, logger log.DebugLogger) error {
    18  	srpcClient := getSubClient(logger)
    19  	defer srpcClient.Close()
    20  	if err := pushFile(srpcClient, args[0], args[1]); err != nil {
    21  		return fmt.Errorf("Error pushing file: %s", err)
    22  	}
    23  	return nil
    24  }
    25  
    26  func pushFile(srpcClient *srpc.Client, source, dest string) error {
    27  	var sourceStat wsyscall.Stat_t
    28  	if err := wsyscall.Stat(source, &sourceStat); err != nil {
    29  		return err
    30  	}
    31  	sourceFile, err := os.Open(source)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	defer sourceFile.Close()
    36  	objClient := objclient.AttachObjectClient(srpcClient)
    37  	defer objClient.Close()
    38  	hashVal, _, err := objClient.AddObject(sourceFile, uint64(sourceStat.Size),
    39  		nil)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	newRegularInode := &filesystem.RegularInode{
    44  		Mode:             filesystem.FileMode(sourceStat.Mode),
    45  		Uid:              sourceStat.Uid,
    46  		Gid:              sourceStat.Gid,
    47  		MtimeNanoSeconds: int32(sourceStat.Mtim.Nsec),
    48  		MtimeSeconds:     sourceStat.Mtim.Sec,
    49  		Size:             uint64(sourceStat.Size),
    50  		Hash:             hashVal}
    51  	newInode := sub.Inode{Name: dest, GenericInode: newRegularInode}
    52  	var updateRequest sub.UpdateRequest
    53  	var updateReply sub.UpdateResponse
    54  	updateRequest.Wait = true
    55  	updateRequest.InodesToMake = append(updateRequest.InodesToMake, newInode)
    56  	if *triggersFile != "" {
    57  		updateRequest.Triggers, err = triggers.Load(*triggersFile)
    58  		if err != nil {
    59  			return err
    60  		}
    61  	} else if *triggersString != "" {
    62  		updateRequest.Triggers, err = triggers.Decode([]byte(*triggersString))
    63  		if err != nil {
    64  			return err
    65  		}
    66  	}
    67  	startTime := showStart("Subd.Update()")
    68  	err = client.CallUpdate(srpcClient, updateRequest, &updateReply)
    69  	showTimeTaken(startTime)
    70  	return err
    71  }