Merge lp:~miki-tebeka/gorun/gorun-md5 into lp:gorun

Proposed by tebeka
Status: Rejected
Rejected by: Gustavo Niemeyer
Proposed branch: lp:~miki-tebeka/gorun/gorun-md5
Merge into: lp:gorun
Diff against target: 83 lines (+23/-8)
1 file modified
gorun.go (+23/-8)
To merge this branch: bzr merge lp:~miki-tebeka/gorun/gorun-md5
Reviewer Review Type Date Requested Status
Gustavo Niemeyer Pending
Review via email: mp+54112@code.launchpad.net

Description of the change

Using md5 signature instead of time stamps. This simplifies the logic of "do we need to recompile" and also protects from clock skew.

To post a comment you must log in.
Revision history for this message
Gustavo Niemeyer (niemeyer) wrote :

I believe we talked about that elsewhere at the time. Computing the md5 all the time on a file content is unnecessary.

Unmerged revisions

3. By tebeka

Using md5 instead of time

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'gorun.go'
2--- gorun.go 2011-03-12 05:24:47 +0000
3+++ gorun.go 2011-03-19 18:24:31 +0000
4@@ -22,6 +22,7 @@
5 // with this program. If not, see <http://www.gnu.org/licenses/>.
6
7 import (
8+ "crypto/md5"
9 "exec"
10 "fmt"
11 "io/ioutil"
12@@ -29,7 +30,6 @@
13 "path/filepath"
14 "runtime"
15 "strconv"
16- "strings"
17 "time"
18 )
19
20@@ -64,7 +64,7 @@
21 // causes the file to be updated on the next run.
22 now := time.Nanoseconds()
23
24- sstat, err := os.Stat(sourcefile)
25+ _, err = os.Stat(sourcefile)
26 if err != nil {
27 return err
28 }
29@@ -75,8 +75,6 @@
30 compile = true
31 case !rstat.IsRegular():
32 return os.ErrorString("not a file: " + runfile)
33- case rstat.Mtime_ns < sstat.Mtime_ns || rstat.Permission() & 0700 != 0700:
34- compile = true
35 default:
36 // We have spare cycles. Maybe remove old files.
37 if err := os.Chtimes(runfile, now, now); err == nil {
38@@ -90,8 +88,6 @@
39 if err != nil {
40 return err
41 }
42- // If sourcefile was changed, will be updated on next run.
43- os.Chtimes(runfile, sstat.Mtime_ns, sstat.Mtime_ns)
44 }
45
46 err = os.Exec(runfile, args, os.Environ())
47@@ -164,6 +160,23 @@
48 return nil
49 }
50
51+func FileMD5(sourcefile string) (string, os.Error) {
52+ content, err := ioutil.ReadFile(sourcefile)
53+ if err != nil {
54+ return "", err
55+ }
56+
57+ digest := md5.New()
58+ digest.Write(content)
59+
60+ s := ""
61+ for _, b := range digest.Sum() {
62+ s += fmt.Sprintf("%02x", b)
63+ }
64+
65+ return s, nil
66+}
67+
68 func GoRunFile(sourcefile string) (rundir, runfile string, err os.Error) {
69 rundir, err = GoRunDir()
70 if err != nil {
71@@ -173,8 +186,10 @@
72 if err != nil {
73 return "", "", err
74 }
75- runfile = strings.Replace(sourcefile, "%", "%%", -1)
76- runfile = strings.Replace(runfile, string(filepath.Separator), "%", -1)
77+ runfile, err = FileMD5(sourcefile)
78+ if err != nil {
79+ return "", "", err
80+ }
81 runfile = filepath.Join(rundir, runfile)
82 return rundir, runfile, nil
83 }

Subscribers

People subscribed via source and target branches

to all changes: