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
=== modified file 'gorun.go'
--- gorun.go 2011-03-12 05:24:47 +0000
+++ gorun.go 2011-03-19 18:24:31 +0000
@@ -22,6 +22,7 @@
22// with this program. If not, see <http://www.gnu.org/licenses/>.22// with this program. If not, see <http://www.gnu.org/licenses/>.
2323
24import (24import (
25 "crypto/md5"
25 "exec"26 "exec"
26 "fmt"27 "fmt"
27 "io/ioutil"28 "io/ioutil"
@@ -29,7 +30,6 @@
29 "path/filepath"30 "path/filepath"
30 "runtime"31 "runtime"
31 "strconv"32 "strconv"
32 "strings"
33 "time"33 "time"
34)34)
3535
@@ -64,7 +64,7 @@
64 // causes the file to be updated on the next run.64 // causes the file to be updated on the next run.
65 now := time.Nanoseconds()65 now := time.Nanoseconds()
6666
67 sstat, err := os.Stat(sourcefile)67 _, err = os.Stat(sourcefile)
68 if err != nil {68 if err != nil {
69 return err69 return err
70 }70 }
@@ -75,8 +75,6 @@
75 compile = true75 compile = true
76 case !rstat.IsRegular():76 case !rstat.IsRegular():
77 return os.ErrorString("not a file: " + runfile)77 return os.ErrorString("not a file: " + runfile)
78 case rstat.Mtime_ns < sstat.Mtime_ns || rstat.Permission() & 0700 != 0700:
79 compile = true
80 default:78 default:
81 // We have spare cycles. Maybe remove old files.79 // We have spare cycles. Maybe remove old files.
82 if err := os.Chtimes(runfile, now, now); err == nil {80 if err := os.Chtimes(runfile, now, now); err == nil {
@@ -90,8 +88,6 @@
90 if err != nil {88 if err != nil {
91 return err89 return err
92 }90 }
93 // If sourcefile was changed, will be updated on next run.
94 os.Chtimes(runfile, sstat.Mtime_ns, sstat.Mtime_ns)
95 }91 }
9692
97 err = os.Exec(runfile, args, os.Environ())93 err = os.Exec(runfile, args, os.Environ())
@@ -164,6 +160,23 @@
164 return nil160 return nil
165}161}
166162
163func FileMD5(sourcefile string) (string, os.Error) {
164 content, err := ioutil.ReadFile(sourcefile)
165 if err != nil {
166 return "", err
167 }
168
169 digest := md5.New()
170 digest.Write(content)
171
172 s := ""
173 for _, b := range digest.Sum() {
174 s += fmt.Sprintf("%02x", b)
175 }
176
177 return s, nil
178}
179
167func GoRunFile(sourcefile string) (rundir, runfile string, err os.Error) {180func GoRunFile(sourcefile string) (rundir, runfile string, err os.Error) {
168 rundir, err = GoRunDir()181 rundir, err = GoRunDir()
169 if err != nil {182 if err != nil {
@@ -173,8 +186,10 @@
173 if err != nil {186 if err != nil {
174 return "", "", err187 return "", "", err
175 }188 }
176 runfile = strings.Replace(sourcefile, "%", "%%", -1)189 runfile, err = FileMD5(sourcefile)
177 runfile = strings.Replace(runfile, string(filepath.Separator), "%", -1)190 if err != nil {
191 return "", "", err
192 }
178 runfile = filepath.Join(rundir, runfile)193 runfile = filepath.Join(rundir, runfile)
179 return rundir, runfile, nil194 return rundir, runfile, nil
180}195}

Subscribers

People subscribed via source and target branches

to all changes: