Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 1 | // Package binary provides a client-side library for the binary |
| 2 | // repository. |
| 3 | // |
| 4 | // TODO(jsimsa): Implement parallel download and upload. |
| 5 | package binary |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
| 9 | "crypto/md5" |
| 10 | "encoding/hex" |
| 11 | "io" |
| 12 | "io/ioutil" |
| 13 | "os" |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 14 | "time" |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 15 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 16 | "veyron.io/veyron/veyron2/context" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 17 | "veyron.io/veyron/veyron2/services/mgmt/binary" |
| 18 | "veyron.io/veyron/veyron2/services/mgmt/repository" |
| 19 | "veyron.io/veyron/veyron2/verror" |
| 20 | "veyron.io/veyron/veyron2/vlog" |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | var ( |
| 24 | errOperationFailed = verror.Internalf("operation failed") |
Tilak Sharma | 492e8e9 | 2014-09-18 10:58:14 -0700 | [diff] [blame] | 25 | errNotExist = verror.NoExistf("binary does not exist") |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | const ( |
| 29 | nAttempts = 2 |
| 30 | partSize = 1 << 22 |
| 31 | subpartSize = 1 << 12 |
| 32 | ) |
| 33 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 34 | func Delete(ctx context.T, name string) error { |
| 35 | ctx, cancel := ctx.WithTimeout(time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 36 | defer cancel() |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 37 | if err := repository.BinaryClient(name).Delete(ctx); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 38 | vlog.Errorf("Delete() failed: %v", err) |
| 39 | return err |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 44 | func download(ctx context.T, w io.WriteSeeker, von string) error { |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 45 | client := repository.BinaryClient(von) |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame] | 46 | // TODO(rthellend): Use the media type. |
| 47 | parts, _, err := client.Stat(ctx) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 48 | if err != nil { |
| 49 | vlog.Errorf("Stat() failed: %v", err) |
| 50 | return err |
| 51 | } |
| 52 | for _, part := range parts { |
| 53 | if part.Checksum == binary.MissingChecksum { |
| 54 | return errNotExist |
| 55 | } |
| 56 | } |
| 57 | offset, whence := int64(0), 0 |
| 58 | for i, part := range parts { |
| 59 | success := false |
| 60 | download: |
| 61 | for j := 0; !success && j < nAttempts; j++ { |
| 62 | if _, err := w.Seek(offset, whence); err != nil { |
| 63 | vlog.Errorf("Seek(%v, %v) failed: %v", offset, whence, err) |
| 64 | continue |
| 65 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 66 | stream, err := client.Download(ctx, int32(i)) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 67 | if err != nil { |
| 68 | vlog.Errorf("Download(%v) failed: %v", i, err) |
| 69 | continue |
| 70 | } |
| 71 | h, nreceived := md5.New(), 0 |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 72 | rStream := stream.RecvStream() |
| 73 | for rStream.Advance() { |
| 74 | bytes := rStream.Value() |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 75 | if _, err := w.Write(bytes); err != nil { |
| 76 | vlog.Errorf("Write() failed: %v", err) |
| 77 | stream.Cancel() |
| 78 | continue download |
| 79 | } |
| 80 | h.Write(bytes) |
| 81 | nreceived += len(bytes) |
| 82 | } |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 83 | |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 84 | if err := rStream.Err(); err != nil { |
| 85 | vlog.Errorf("Advance() failed: %v", err) |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 86 | stream.Cancel() |
| 87 | continue download |
| 88 | |
| 89 | } |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 90 | if err := stream.Finish(); err != nil { |
| 91 | vlog.Errorf("Finish() failed: %v", err) |
| 92 | continue |
| 93 | } |
| 94 | if expected, got := part.Checksum, hex.EncodeToString(h.Sum(nil)); expected != got { |
| 95 | vlog.Errorf("Unexpected checksum: expected %v, got %v", expected, got) |
| 96 | continue |
| 97 | } |
| 98 | if expected, got := part.Size, int64(nreceived); expected != got { |
| 99 | vlog.Errorf("Unexpected size: expected %v, got %v", expected, got) |
| 100 | continue |
| 101 | } |
| 102 | success = true |
| 103 | } |
| 104 | if !success { |
| 105 | return errOperationFailed |
| 106 | } |
| 107 | offset += part.Size |
| 108 | } |
| 109 | return nil |
| 110 | } |
| 111 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 112 | func Download(ctx context.T, von string) ([]byte, error) { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 113 | dir, prefix := "", "" |
| 114 | file, err := ioutil.TempFile(dir, prefix) |
| 115 | if err != nil { |
| 116 | vlog.Errorf("TempFile(%v, %v) failed: %v", dir, prefix, err) |
| 117 | return nil, errOperationFailed |
| 118 | } |
| 119 | defer os.Remove(file.Name()) |
| 120 | defer file.Close() |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 121 | ctx, cancel := ctx.WithTimeout(time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 122 | defer cancel() |
| 123 | if err := download(ctx, file, von); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 124 | return nil, errOperationFailed |
| 125 | } |
| 126 | bytes, err := ioutil.ReadFile(file.Name()) |
| 127 | if err != nil { |
| 128 | vlog.Errorf("ReadFile(%v) failed: %v", file.Name(), err) |
| 129 | return nil, errOperationFailed |
| 130 | } |
| 131 | return bytes, nil |
| 132 | } |
| 133 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 134 | func DownloadToFile(ctx context.T, von, path string) error { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 135 | dir, prefix := "", "" |
| 136 | file, err := ioutil.TempFile(dir, prefix) |
| 137 | if err != nil { |
| 138 | vlog.Errorf("TempFile(%v, %v) failed: %v", dir, prefix, err) |
| 139 | return errOperationFailed |
| 140 | } |
| 141 | defer file.Close() |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 142 | ctx, cancel := ctx.WithTimeout(time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 143 | defer cancel() |
| 144 | if err := download(ctx, file, von); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 145 | if err := os.Remove(file.Name()); err != nil { |
| 146 | vlog.Errorf("Remove(%v) failed: %v", file.Name(), err) |
| 147 | } |
| 148 | return errOperationFailed |
| 149 | } |
| 150 | perm := os.FileMode(0700) |
| 151 | if err := file.Chmod(perm); err != nil { |
| 152 | vlog.Errorf("Chmod(%v) failed: %v", perm, err) |
| 153 | if err := os.Remove(file.Name()); err != nil { |
| 154 | vlog.Errorf("Remove(%v) failed: %v", file.Name(), err) |
| 155 | } |
| 156 | return errOperationFailed |
| 157 | } |
| 158 | if err := os.Rename(file.Name(), path); err != nil { |
| 159 | vlog.Errorf("Rename(%v, %v) failed: %v", file.Name(), path, err) |
| 160 | if err := os.Remove(file.Name()); err != nil { |
| 161 | vlog.Errorf("Remove(%v) failed: %v", file.Name(), err) |
| 162 | } |
| 163 | return errOperationFailed |
| 164 | } |
| 165 | return nil |
| 166 | } |
| 167 | |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 168 | func upload(ctx context.T, r io.ReadSeeker, von string) error { |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 169 | client := repository.BinaryClient(von) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 170 | offset, whence := int64(0), 2 |
| 171 | size, err := r.Seek(offset, whence) |
| 172 | if err != nil { |
| 173 | vlog.Errorf("Seek(%v, %v) failed: %v", offset, whence, err) |
| 174 | return errOperationFailed |
| 175 | } |
| 176 | nparts := (size-1)/partSize + 1 |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame] | 177 | // TODO(rthellend): Determine the actual media type. |
| 178 | mediaType := "application/octet-stream" |
| 179 | if err := client.Create(ctx, int32(nparts), mediaType); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 180 | vlog.Errorf("Create() failed: %v", err) |
| 181 | return err |
| 182 | } |
| 183 | for i := 0; int64(i) < nparts; i++ { |
| 184 | success := false |
| 185 | upload: |
| 186 | for j := 0; !success && j < nAttempts; j++ { |
| 187 | offset, whence := int64(i*partSize), 0 |
| 188 | if _, err := r.Seek(offset, whence); err != nil { |
| 189 | vlog.Errorf("Seek(%v, %v) failed: %v", offset, whence, err) |
| 190 | continue |
| 191 | } |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 192 | stream, err := client.Upload(ctx, int32(i)) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 193 | if err != nil { |
| 194 | vlog.Errorf("Upload(%v) failed: %v", i, err) |
| 195 | continue |
| 196 | } |
| 197 | buffer := make([]byte, partSize) |
| 198 | if int64(i+1) == nparts { |
| 199 | buffer = buffer[:(size % partSize)] |
| 200 | } |
| 201 | nread := 0 |
| 202 | for nread < len(buffer) { |
| 203 | n, err := r.Read(buffer[nread:]) |
| 204 | nread += n |
| 205 | if err != nil && (err != io.EOF || nread < len(buffer)) { |
| 206 | vlog.Errorf("Read() failed: %v", err) |
| 207 | stream.Cancel() |
| 208 | continue upload |
| 209 | } |
| 210 | } |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 211 | sender := stream.SendStream() |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 212 | for from := 0; from < len(buffer); from += subpartSize { |
| 213 | to := from + subpartSize |
| 214 | if to > len(buffer) { |
| 215 | to = len(buffer) |
| 216 | } |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 217 | if err := sender.Send(buffer[from:to]); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 218 | vlog.Errorf("Send() failed: %v", err) |
| 219 | stream.Cancel() |
| 220 | continue upload |
| 221 | } |
| 222 | } |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 223 | if err := sender.Close(); err != nil { |
| 224 | vlog.Errorf("Close() failed: %v", err) |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame] | 225 | parts, _, statErr := client.Stat(ctx) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 226 | if statErr != nil { |
| 227 | vlog.Errorf("Stat() failed: %v", statErr) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 228 | if deleteErr := client.Delete(ctx); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 229 | vlog.Errorf("Delete() failed: %v", deleteErr) |
| 230 | } |
| 231 | return err |
| 232 | } |
| 233 | if parts[i].Checksum == binary.MissingChecksum { |
| 234 | stream.Cancel() |
| 235 | continue |
| 236 | } |
| 237 | } |
| 238 | if err := stream.Finish(); err != nil { |
| 239 | vlog.Errorf("Finish() failed: %v", err) |
Robin Thellend | 64178ed | 2014-11-20 13:16:22 -0800 | [diff] [blame] | 240 | parts, _, statErr := client.Stat(ctx) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 241 | if statErr != nil { |
| 242 | vlog.Errorf("Stat() failed: %v", statErr) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 243 | if deleteErr := client.Delete(ctx); err != nil { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 244 | vlog.Errorf("Delete() failed: %v", deleteErr) |
| 245 | } |
| 246 | return err |
| 247 | } |
| 248 | if parts[i].Checksum == binary.MissingChecksum { |
| 249 | continue |
| 250 | } |
| 251 | } |
| 252 | success = true |
| 253 | } |
| 254 | if !success { |
| 255 | return errOperationFailed |
| 256 | } |
| 257 | } |
| 258 | return nil |
| 259 | } |
| 260 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 261 | func Upload(ctx context.T, von string, data []byte) error { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 262 | buffer := bytes.NewReader(data) |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 263 | ctx, cancel := ctx.WithTimeout(time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 264 | defer cancel() |
| 265 | return upload(ctx, buffer, von) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 268 | func UploadFromFile(ctx context.T, von, path string) error { |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 269 | file, err := os.Open(path) |
| 270 | defer file.Close() |
| 271 | if err != nil { |
| 272 | vlog.Errorf("Open(%v) failed: %v", err) |
| 273 | return errOperationFailed |
| 274 | } |
Matt Rosencrantz | c2ed03e | 2014-11-25 15:40:48 -0800 | [diff] [blame] | 275 | ctx, cancel := ctx.WithTimeout(time.Minute) |
Matt Rosencrantz | 137b8d2 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 276 | defer cancel() |
| 277 | return upload(ctx, file, von) |
Jiri Simsa | 51d78fc | 2014-07-09 18:34:08 -0700 | [diff] [blame] | 278 | } |