func (input *graderBaseInput) Verify() error { stat, err := os.Stat(input.archivePath) if err != nil { return err } hash, err := common.Sha1sum(input.archivePath) if err != nil { return err } storedHash, err := input.getStoredHash() if err != nil { return err } if storedHash != fmt.Sprintf("%0x", hash) { return errors.New("Hash verification failed") } uncompressedSize, err := input.getStoredLength() if err != nil { return err } input.storedHash = storedHash input.uncompressedSize = uncompressedSize input.Commit(stat.Size()) return nil }
func (input *GraderInput) Persist() error { if err := os.MkdirAll(path.Dir(input.archivePath), 0755); err != nil { return err } tmpPath := fmt.Sprintf("%s.tmp", input.archivePath) defer os.Remove(tmpPath) uncompressedSize, err := input.createArchiveFromGit(tmpPath) if err != nil { return err } stat, err := os.Stat(tmpPath) if err != nil { return err } hash, err := common.Sha1sum(tmpPath) if err != nil { return err } hashFd, err := os.Create(fmt.Sprintf("%s.sha1", input.archivePath)) if err != nil { return err } defer hashFd.Close() if _, err := fmt.Fprintf( hashFd, "%0x *%s\n", hash, path.Base(input.archivePath), ); err != nil { return err } sizeFd, err := os.Create(fmt.Sprintf("%s.len", input.archivePath)) if err != nil { return err } defer sizeFd.Close() if _, err := fmt.Fprintf(sizeFd, "%d\n", uncompressedSize); err != nil { return err } if err := os.Rename(tmpPath, input.archivePath); err != nil { return err } input.storedHash = fmt.Sprintf("%0x", hash) input.uncompressedSize = uncompressedSize input.Commit(stat.Size()) return nil }
func (input *runnerBaseInput) Verify() error { hashes, err := input.getStoredHashes() if err != nil { return err } var size int64 = 0 for path, expectedHashStr := range hashes { actualHash, err := common.Sha1sum(path) if err != nil { return err } actualHashStr := fmt.Sprintf("%0x", actualHash) if actualHashStr != expectedHashStr { return errors.New(fmt.Sprintf( "hash mismatch for '%s' == %q, want %q", path, actualHashStr, expectedHashStr, )) } stat, err := os.Stat(path) if err != nil { return err } size += stat.Size() } settingsFd, err := os.Open(path.Join(input.path, "settings.json")) if err != nil { return err } defer settingsFd.Close() decoder := json.NewDecoder(settingsFd) if err := decoder.Decode(input.Settings()); err != nil { return err } input.Commit(size) return nil }