├── .gitignore ├── .travis.yml ├── .vscode └── tasks.json ├── Cargo.toml ├── LICENSE ├── doc └── v2-manifesto.md └── src ├── lib.rs └── release_entry.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | /target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | rust: 3 | - nightly 4 | - beta 5 | - stable 6 | cache: cargo 7 | 8 | matrix: 9 | allow_failures: 10 | - rust: nightly 11 | 12 | before_script: 13 | - | 14 | if test "$TRAVIS_RUST_VERSION" = "nightly"; then 15 | test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update 16 | test -x $HOME/.cargo/bin/cargo-clippy || cargo install clippy 17 | cargo install-update clippy 18 | fi 19 | 20 | script: 21 | - cargo test 22 | - if test "$TRAVIS_RUST_VERSION" = "nightly"; then cargo clippy; fi 23 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.1.0", 3 | "command": "cargo", 4 | "isShellCommand": true, 5 | "showOutput": "always", 6 | "suppressTaskName": true, 7 | "tasks": [ 8 | { 9 | "taskName": "cargo build", 10 | "args": [ 11 | "build" 12 | ], 13 | "isBuildCommand": true 14 | }, 15 | { 16 | "taskName": "cargo run", 17 | "args": [ 18 | "run" 19 | ] 20 | }, 21 | { 22 | "taskName": "cargo test", 23 | "args": [ 24 | "test" 25 | ], 26 | "isTestCommand": true 27 | } 28 | ] 29 | } -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "squirrel" 3 | version = "0.1.0" 4 | authors = ["Paul Betts "] 5 | 6 | [dependencies] 7 | env_logger = "0.3" 8 | hex = "0.2" 9 | lazy_static = "0.2" 10 | log = "0.3" 11 | regex = "0.2" 12 | semver = "0.7.0" 13 | sha2 = "0.6.0" 14 | url = "1.5.1" 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /doc/v2-manifesto.md: -------------------------------------------------------------------------------- 1 | ## What sucks about Squirrel v1's Design™ 2 | 3 | * Squirrel.Windows and Squirrel.Mac are completely different 4 | - This confuses the living fuck out of people 5 | 6 | * Squirrel.Mac is missing lots of features 7 | - Delta updates 8 | - Updates via static HTTPS (i.e. S3) 9 | - Rollout updates 10 | 11 | * Squirrel.Windows uses NuGet files 12 | - Not worth it now that Electron is such a huge client 13 | - NuGet library gives us some tooling but also we're pinned to their garbage 14 | - NuGet compression / decompression library sucks out loud 15 | - Squirrel.Mac just uses Basic-Ass Zip Files, much better 16 | 17 | * Squirrel.Mac relies on lots of Xcode libraries that bitrot 18 | - RAC is cool, but Xcode breaks every library all the time 19 | - Nobody can build Squirrel.Mac anymore 20 | 21 | * Squirrel.Windows builds the tooling code and the installation code in the same library 22 | - This makes Update.exe way bigger than it needs to be, and makes the flags for Update.exe bananas. 23 | - Make code that we actually ship in the box as small as possible 24 | 25 | * Squirrel.Windows has two separate APIs 26 | - One API is through C#, the other API is via parameters to shell out to Update.exe 27 | - That's no way to live 28 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! Squirrel - the cross-platform installation and update library 2 | 3 | //#![deny(missing_docs)] 4 | #![cfg_attr(test, deny(warnings))] 5 | 6 | #[macro_use] 7 | extern crate lazy_static; 8 | 9 | /* 10 | #[macro_use] 11 | extern crate log; 12 | */ 13 | 14 | extern crate hex; 15 | extern crate regex; 16 | extern crate semver; 17 | extern crate sha2; 18 | extern crate url; 19 | 20 | pub use release_entry::{ReleaseEntry}; 21 | 22 | mod release_entry; 23 | -------------------------------------------------------------------------------- /src/release_entry.rs: -------------------------------------------------------------------------------- 1 | use hex::*; 2 | use regex::Regex; 3 | use semver::Version; 4 | use std::iter::*; 5 | use std::error::{Error}; 6 | use url::{Url}; 7 | use url::percent_encoding::{percent_decode}; 8 | 9 | /* Example lines: 10 | 11 | # SHA256 of the file Name Version Size [delta/full] release% 12 | e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 full 13 | a4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject-delta.7z 123 delta 14 | b4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject-beta.7z 34567 full 5% 15 | */ 16 | 17 | #[derive(Debug)] 18 | pub struct ReleaseEntry { 19 | pub sha256: [u8; 32], 20 | pub filename_or_url: String, 21 | pub version: Version, 22 | pub length: i64, 23 | pub is_delta: bool, 24 | pub percentage: i32, 25 | } 26 | 27 | impl Default for ReleaseEntry { 28 | fn default() -> ReleaseEntry { 29 | ReleaseEntry { 30 | filename_or_url: "Foobdar".to_owned(), 31 | version: Version::parse("1.0.0").unwrap(), 32 | is_delta: true, 33 | length: 42, 34 | sha256: [0; 32], 35 | percentage: 100, 36 | } 37 | } 38 | } 39 | 40 | lazy_static! { 41 | static ref SCHEME: Regex = Regex::new(r"^https:").unwrap(); 42 | } 43 | 44 | lazy_static! { 45 | static ref COMMENT: Regex = Regex::new(r"#.*$").unwrap(); 46 | } 47 | 48 | impl ReleaseEntry { 49 | fn parse_sha256(sha256: &str, to_fill: &mut ReleaseEntry) -> Result> { 50 | let ret = try!(Vec::from_hex(sha256)); 51 | if ret.len() != 32 { 52 | return Err(From::from("SHA256 is malformed")); 53 | } 54 | 55 | for i in 0..32 { to_fill.sha256[i] = ret[i]; } 56 | return Ok(true); 57 | } 58 | 59 | fn parse_delta_full(delta_or_full: &str) -> Result> { 60 | match delta_or_full { 61 | "delta" => Ok(true), 62 | "full" => Ok(false), 63 | _ => Err(From::from("Package type must be either 'delta' or 'full'")) 64 | } 65 | } 66 | 67 | fn parse_name(filename_or_url: &str) -> Result> { 68 | if SCHEME.is_match(filename_or_url) { 69 | try!(Url::parse(filename_or_url)); 70 | return Ok(filename_or_url.to_owned()) 71 | } else { 72 | let u = format!("file:///{}", filename_or_url); 73 | let url = try!(Url::parse(&u)); 74 | 75 | let decoded = try!(percent_decode(url.path().as_bytes()).decode_utf8()); 76 | return Ok(decoded.trim_left_matches("/").to_owned()); 77 | } 78 | } 79 | 80 | fn parse_percentage(percent: &str) -> Result> { 81 | let n = try!(percent.trim_right_matches("%").parse::()); 82 | if n > 100 || n < 0 { 83 | return Err(From::from("Percentage must be between 0 and 100 inclusive")); 84 | } 85 | 86 | return Ok(n); 87 | } 88 | 89 | pub fn parse(entry: &str) -> Result> { 90 | let e = entry.split_whitespace().collect::>(); 91 | 92 | return match e.len() { 93 | 5 => { 94 | let (sha256, name, version, size, delta_or_full) = (e[0], e[1], e[2], e[3], e[4]); 95 | let mut ret = ReleaseEntry { 96 | sha256: [0; 32], 97 | is_delta: try!(ReleaseEntry::parse_delta_full(delta_or_full)), 98 | filename_or_url: try!(ReleaseEntry::parse_name(name)), 99 | version: try!(Version::parse(version)), 100 | length: try!(size.parse::()), 101 | percentage: 100, 102 | }; 103 | 104 | try!(ReleaseEntry::parse_sha256(sha256, &mut ret)); 105 | return Ok(ret); 106 | }, 107 | 6 => { 108 | let (sha256, name, version, size, delta_or_full, percent) = (e[0], e[1], e[2], e[3], e[4], e[5]); 109 | let mut ret = ReleaseEntry { 110 | sha256: [0; 32], 111 | is_delta: try!(ReleaseEntry::parse_delta_full(delta_or_full)), 112 | filename_or_url: try!(ReleaseEntry::parse_name(name)).to_owned(), 113 | version: try!(Version::parse(version)), 114 | length: try!(size.parse::()), 115 | percentage: try!(ReleaseEntry::parse_percentage(percent)) 116 | }; 117 | 118 | try!(ReleaseEntry::parse_sha256(sha256, &mut ret)); 119 | return Ok(ret); 120 | }, 121 | _ => Err(From::from("Invalid Release Entry string")) 122 | } 123 | } 124 | 125 | pub fn parse_entries(content: &str) -> Result, Box> { 126 | let mut was_error: Option> = None; 127 | 128 | let r: Vec = content.split("\n").filter_map(|x| { 129 | let r = COMMENT.replace_all(x, ""); 130 | if r.len() == 0 { 131 | return None; 132 | } 133 | 134 | match ReleaseEntry::parse(&r) { 135 | Err(err) => { 136 | was_error = Some(err); 137 | return None; 138 | }, 139 | Ok(val) => Some(val) 140 | } 141 | }).collect(); 142 | 143 | return match was_error { 144 | Some(err) => Err(err), 145 | None => Ok(r) 146 | }; 147 | } 148 | } 149 | 150 | #[cfg(test)] 151 | mod tests { 152 | use sha2::Sha256; 153 | use sha2::Digest; 154 | use super::ReleaseEntry; 155 | 156 | fn print_result(sum: &[u8], name: &str) { 157 | for byte in sum { 158 | print!("{:02x}", byte); 159 | } 160 | println!("\t{}", name); 161 | } 162 | 163 | #[test] 164 | fn create_a_release_entry() { 165 | let f = ReleaseEntry::default(); 166 | assert_eq!(f.length, 42); 167 | } 168 | 169 | #[test] 170 | fn parse_should_read_valid_sha256() { 171 | let input = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 full"; 172 | let result = ReleaseEntry::parse(input).unwrap(); 173 | 174 | assert_eq!(result.sha256[0], 0xE4); 175 | assert_eq!(result.sha256[1], 0x54); 176 | assert_eq!(result.sha256[31], 0x35); 177 | } 178 | 179 | #[test] 180 | fn parse_should_fail_invalid_sha256() { 181 | let input = "48fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 full"; 182 | ReleaseEntry::parse(input).unwrap_err(); 183 | } 184 | 185 | #[test] 186 | fn parse_should_fail_very_invalid_sha256() { 187 | let input = "48Z myproject.7z 12345 full"; 188 | ReleaseEntry::parse(input).unwrap_err(); 189 | } 190 | 191 | #[test] 192 | fn parse_should_fail_invalid_type() { 193 | let input = "48fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 foobar"; 194 | ReleaseEntry::parse(input).unwrap_err(); 195 | } 196 | 197 | #[test] 198 | fn parse_should_set_delta_package() { 199 | let input = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 delta"; 200 | let result = ReleaseEntry::parse(input).unwrap(); 201 | 202 | assert_eq!(result.is_delta, true); 203 | 204 | let input2 = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 full"; 205 | let result2 = ReleaseEntry::parse(input2).unwrap(); 206 | 207 | assert_eq!(result2.is_delta, false); 208 | } 209 | 210 | #[test] 211 | fn parse_should_accept_percentages() { 212 | let input = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 delta 45%"; 213 | let result = ReleaseEntry::parse(input).unwrap(); 214 | assert_eq!(result.percentage, 45); 215 | } 216 | 217 | #[test] 218 | fn parse_should_fail_giant_percentages() { 219 | let input = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 delta 145%"; 220 | ReleaseEntry::parse(input).unwrap_err(); 221 | } 222 | 223 | #[test] 224 | fn parse_should_fail_negative_percentages() { 225 | let input = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 delta -145%"; 226 | ReleaseEntry::parse(input).unwrap_err(); 227 | } 228 | 229 | #[test] 230 | fn url_encoded_filenames_should_end_up_decoded() { 231 | let input = "e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 my%20project.7z 1.2.3 12345 full"; 232 | let result = ReleaseEntry::parse(input).unwrap(); 233 | 234 | assert_eq!(result.filename_or_url, "my project.7z"); 235 | } 236 | 237 | #[test] 238 | fn parse_all_entries() { 239 | let input = " 240 | # SHA256 of the file Name Version Size [delta/full] release% 241 | e4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject.7z 1.2.3 12345 full 242 | a4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject-delta.7z 1.2.3 555 delta 243 | b4548fba3f902e63e3fff36db7cbbd1837493e21c51f0751e51ee1483ddd0f35 myproject-beta.7z 2.0.0-beta.1 34567 full 5%"; 244 | 245 | let result = ReleaseEntry::parse_entries(input).unwrap(); 246 | assert_eq!(result.len(), 3); 247 | } 248 | 249 | #[test] 250 | fn stringify_a_sha256() { 251 | let mut sha = Sha256::default(); 252 | sha.input("This is a test".as_bytes()); 253 | 254 | let hash = sha.result(); 255 | print_result(&hash, "SHA256"); 256 | println!("Wat."); 257 | } 258 | } 259 | --------------------------------------------------------------------------------