[minor] write action to tempfile prior to signing it

This commit is contained in:
Julien Vehent 2014-11-20 13:02:41 -05:00
Родитель 98baf706fc
Коммит f864a9ea92
2 изменённых файлов: 38 добавлений и 1 удалений

Просмотреть файл

@ -14,6 +14,7 @@ import (
"io/ioutil"
"math/rand"
"mig/pgp"
"os"
"strconv"
"time"
)
@ -103,6 +104,34 @@ func ActionFromFile(path string) (Action, error) {
return a, err
}
// ToTempFile writes an action into a generated temporary file and returns its filename
func (a Action) ToTempFile() (filename string, err error) {
var (
data []byte
fd *os.File
fi os.FileInfo
)
data, err = json.Marshal(a)
if err != nil {
return
}
fd, err = ioutil.TempFile("", "migaction_")
defer fd.Close()
if err != nil {
return
}
_, err = fd.Write(data)
if err != nil {
return
}
fi, err = fd.Stat()
if err != nil {
return
}
filename = fmt.Sprintf("%s/%s", os.TempDir(), fi.Name())
return
}
const MAXINT = int(^uint(0) >> 1)
// GenID returns an ID composed of a unix timestamp and a random CRC32

Просмотреть файл

@ -529,12 +529,20 @@ func (cli Client) SignAction(a mig.Action) (signed_action mig.Action, err error)
err = fmt.Errorf("SignAction() -> %v", e)
}
}()
filename, err := a.ToTempFile()
if err != nil {
panic(err)
}
a2, err := mig.ActionFromFile(filename)
if err != nil {
panic(err)
}
secring, err := os.Open(cli.Conf.GPG.Home + "/secring.gpg")
if err != nil {
panic(err)
}
defer secring.Close()
sig, err := a.Sign(cli.Conf.GPG.KeyID, secring)
sig, err := a2.Sign(cli.Conf.GPG.KeyID, secring)
if err != nil {
panic(err)
}