-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathipa.rs
More file actions
135 lines (118 loc) · 4.55 KB
/
ipa.rs
File metadata and controls
135 lines (118 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#![cfg(feature = "unstable-mobile-app")]
use anyhow::{anyhow, Result};
use log::debug;
use std::io::Cursor;
use std::path::{Path, PathBuf};
use zip::ZipArchive;
use crate::utils::fs::TempDir;
/// Converts an IPA file to an XCArchive directory structure. The provided IPA must be a valid IPA file.
///
/// # Format Overview
///
/// ## IPA (iOS App Store Package)
/// An IPA file is a compressed archive containing an iOS app ready for distribution.
/// It has the following structure:
/// ```
/// MyApp.ipa
/// └── Payload/
/// └── MyApp.app/
/// ├── Info.plist
/// ├── MyApp (executable)
/// ├── Assets.car
/// └── ... (other app resources)
/// ```
///
/// ## XCArchive (Xcode Archive)
/// An XCArchive is a directory structure created by Xcode when archiving an app for distribution.
/// It has the following structure:
/// ```
/// MyApp.xcarchive/
/// ├── Info.plist
/// ├── Products/
/// │ └── Applications/
/// │ └── MyApp.app/
/// │ ├── Info.plist
/// │ ├── MyApp (executable)
/// │ ├── Assets.car
/// │ └── ... (other app resources)
/// └── ... (other archive metadata)
/// ```
///
/// # Transformation Process
///
/// This function performs the following steps:
/// 1. Creates the XCArchive directory structure (`archive.xcarchive/Products/Applications/`)
/// 2. Extracts the app name from the IPA by finding the shortest path ending with `.app/Info.plist`
/// 3. Extracts all files from the IPA's `Payload/` directory into the XCArchive structure
/// 4. Creates an `Info.plist` file for the XCArchive with the app path reference
/// 5. Returns the path to the XCArchive directory structure
pub fn ipa_to_xcarchive(ipa_path: &Path, ipa_bytes: &[u8], temp_dir: &TempDir) -> Result<PathBuf> {
debug!(
"Converting IPA to XCArchive structure: {}",
ipa_path.display()
);
let xcarchive_dir = temp_dir.path().join("archive.xcarchive");
let products_dir = xcarchive_dir.join("Products");
let applications_dir = products_dir.join("Applications");
debug!("Creating XCArchive directory structure");
std::fs::create_dir_all(&applications_dir)?;
// Extract IPA file
let cursor = Cursor::new(ipa_bytes);
let mut ipa_archive = ZipArchive::new(cursor)?;
let app_name = extract_app_name_from_ipa(&ipa_archive)?;
log::debug!("Extracted app name: {}", app_name);
// Extract all files from the archive
for i in 0..ipa_archive.len() {
let mut file = ipa_archive.by_index(i)?;
if let Some(name) = file.enclosed_name() {
if let Ok(stripped) = name.strip_prefix("Payload/") {
if !file.is_dir() {
// Create the file path in the XCArchive structure
let target_path = applications_dir.join(stripped);
// Create parent directories if necessary
if let Some(parent) = target_path.parent() {
std::fs::create_dir_all(parent)?;
}
// Extract file
let mut target_file = std::fs::File::create(&target_path)?;
std::io::copy(&mut file, &mut target_file)?;
}
}
}
}
// Create Info.plist for XCArchive
let info_plist_path = xcarchive_dir.join("Info.plist");
let info_plist_content = format!(
r#"<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>ApplicationProperties</key>
<dict>
<key>ApplicationPath</key>
<string>Applications/{app_name}.app</string>
</dict>
<key>ArchiveVersion</key>
<integer>1</integer>
</dict>
</plist>"#
);
std::fs::write(&info_plist_path, info_plist_content)?;
debug!(
"Created XCArchive Info.plist at: {}",
info_plist_path.display()
);
Ok(xcarchive_dir)
}
fn extract_app_name_from_ipa(archive: &ZipArchive<Cursor<&[u8]>>) -> Result<String> {
log::debug!("app names: {:?}", archive.file_names().collect::<Vec<_>>());
archive
.file_names()
.filter(|name| name.starts_with("Payload/") && name.ends_with(".app/Info.plist"))
.min_by_key(|name| name.len())
.and_then(|name| name.strip_prefix("Payload/"))
.and_then(|name| name.split('/').next())
.and_then(|name| name.strip_suffix(".app"))
.map(|name| name.to_owned())
.ok_or_else(|| anyhow!("No .app found in IPA"))
}