@@ -44,6 +44,7 @@ class ExtensionArtifactManager {
4444 static const String _extractedDirName = 'extracted' ;
4545 static const String _sha256StampSuffix = '.sha256' ;
4646 static const String _lockFileSuffix = '.lock' ;
47+ static const String _executableChmodMode = 'a+x' ;
4748
4849 final ExtensionManager _extensionManager;
4950 final FeatureFlags _featureFlags;
@@ -136,25 +137,33 @@ class ExtensionArtifactManager {
136137 return artifactsCacheDirectory.childDirectory (extensionName).childDirectory (normalizedSha256);
137138 }
138139
139- /// Queries all active extensions implementing [ArtifactService] and ensures
140- /// required artifacts for the current engine version, host platform, and
141- /// optional [targetPlatforms] are resolved on disk or downloaded and verified
142- /// in the shared cache.
140+ /// Queries active extensions implementing [ArtifactService] (or only the
141+ /// extension matching [extensionName] when specified) and ensures required
142+ /// artifacts for the current engine version, host platform, and optional
143+ /// [targetPlatforms] are resolved on disk or downloaded and verified in the
144+ /// shared cache.
143145 ///
144146 /// When [targetPlatforms] is `null` , artifacts for all target platforms
145- /// supported by each active extension are requested.
147+ /// supported by each queried extension are requested.
146148 ///
147149 /// Returns a map from each resolved artifact's [ArtifactDependency.name] to
148150 /// its local filesystem path, or an empty map when tool extensions are
149- /// disabled, extension initialization fails, or no active extension implements
150- /// [ArtifactService] .
151+ /// disabled, extension initialization fails, or no matching active extension
152+ /// implements [ArtifactService] . When [extensionName] is provided (for
153+ /// example, during `flutter build` or `ExtensionBackedDevice.startApp` ), the
154+ /// returned map is scoped strictly to that owning extension; when
155+ /// [extensionName] is `null` (for example, during `flutter precache` ), all
156+ /// extensions' artifacts are downloaded into their isolated
157+ /// `<cacheDir>/<extensionName>/<sha256>/<name>` directories.
151158 ///
152159 /// RPC failures when querying an extension's artifact list log a warning and
153160 /// skip that extension, whereas missing local artifacts or download,
154- /// extraction, or SHA-256 verification failures throw a [ToolExit] .
161+ /// extraction, executable permission validation, or SHA-256 verification
162+ /// failures throw a [ToolExit] .
155163 ///
156164 /// When [force] is `true` , existing cached remote artifacts are re-downloaded.
157165 Future <Map <String , String >> ensureArtifactsDownloaded ({
166+ String ? extensionName,
158167 bool force = false ,
159168 Set <String >? targetPlatforms,
160169 }) async {
@@ -172,7 +181,9 @@ class ExtensionArtifactManager {
172181
173182 final artifactExtensions = < ExtensionConnection > [
174183 for (final ext in _extensionManager.connections)
175- if (ext.capabilities.services.contains (ArtifactService .serviceNamespace)) ext,
184+ if (ext.capabilities.services.contains (ArtifactService .serviceNamespace) &&
185+ (extensionName == null || ext.extensionName == extensionName))
186+ ext,
176187 ];
177188
178189 if (artifactExtensions.isEmpty) {
@@ -181,12 +192,6 @@ class ExtensionArtifactManager {
181192
182193 final HostPlatform currentHostPlatform = os.hostPlatform;
183194 final String engineVersion = _engineVersion;
184- if (! ArtifactDependency .isValidSinglePathSegment (engineVersion)) {
185- logger.printWarning (
186- 'Skipping extension artifacts due to invalid engine version "$engineVersion ".' ,
187- );
188- return const < String , String > {};
189- }
190195
191196 final resolvedArtifacts = < String , String > {};
192197 for (final ext in artifactExtensions) {
@@ -213,15 +218,16 @@ class ExtensionArtifactManager {
213218 dep,
214219 ];
215220
221+ final seenForExtension = < String > {};
216222 for (final dep in filteredDeps) {
217- if (resolvedArtifacts. containsKey (dep.name)) {
223+ if (! seenForExtension. add (dep.name)) {
218224 logger.printWarning (
219- 'Artifact "${dep . name }" from extension "${ext . extensionName }" conflicts with an '
220- 'artifact of the same name from another extension and will be ignored .' ,
225+ 'Extension "${ext . extensionName }" declared duplicate artifact "${dep . name }"; '
226+ 'keeping the first declaration .' ,
221227 );
222228 continue ;
223229 }
224- resolvedArtifacts[dep.name] = switch (dep) {
230+ final String resolvedPath = switch (dep) {
225231 final LocalArtifactDependency localDep => _resolveLocalArtifact (
226232 ext.extensionName,
227233 localDep,
@@ -232,6 +238,7 @@ class ExtensionArtifactManager {
232238 force: force,
233239 ),
234240 };
241+ resolvedArtifacts.putIfAbsent (dep.name, () => resolvedPath);
235242 }
236243 }
237244 return resolvedArtifacts;
@@ -390,6 +397,8 @@ class ExtensionArtifactManager {
390397 artifactFile: artifactFile,
391398 dep: dep,
392399 expectedSha: expectedSha,
400+ extensionName: extensionName,
401+ fs: fs,
393402 isZip: isZip,
394403 os: os,
395404 stagingDir: stagingDir,
@@ -507,13 +516,16 @@ Actual: $actualDigest''');
507516 }
508517
509518 /// Installs a verified [stagingFile] into its final cache destination
510- /// ([artifactDir] when [isZip] is `true` , or [artifactFile] otherwise) and
511- /// writes [expectedSha] to [stampFile] last via renames from [stagingDir] .
519+ /// ([artifactDir] when [isZip] is `true` , or [artifactFile] otherwise),
520+ /// applies declared POSIX executable permissions, and writes [expectedSha] to
521+ /// [stampFile] last via renames from [stagingDir] .
512522 void _installStagedArtifact ({
513523 required Directory artifactDir,
514524 required File artifactFile,
515525 required RemoteArtifactDependency dep,
516526 required String expectedSha,
527+ required String extensionName,
528+ required FileSystem fs,
517529 required bool isZip,
518530 required OperatingSystemUtils os,
519531 required Directory stagingDir,
@@ -531,11 +543,21 @@ Actual: $actualDigest''');
531543 } on Object catch (e) {
532544 throwToolExit ('Failed to extract artifact "${dep .name }" from ${dep .url }: $e ' );
533545 }
546+ _applyArchiveExecutablePermissions (
547+ dep: dep,
548+ extensionName: extensionName,
549+ extractedDir: extractedDir,
550+ fs: fs,
551+ os: os,
552+ );
534553 if (artifactDir.existsSync ()) {
535554 artifactDir.deleteSync (recursive: true );
536555 }
537556 extractedDir.renameSync (artifactDir.path);
538557 } else {
558+ if (dep.isExecutable) {
559+ os.chmod (stagingFile, _executableChmodMode);
560+ }
539561 if (artifactFile.existsSync ()) {
540562 artifactFile.deleteSync ();
541563 }
@@ -545,4 +567,51 @@ Actual: $actualDigest''');
545567 ..writeAsStringSync (expectedSha);
546568 stagingStamp.renameSync (stampFile.path);
547569 }
570+
571+ /// Validates every entry in [RemoteArtifactDependency.executables] within
572+ /// [extractedDir] (rejecting out-of-tree paths, missing files, and symbolic
573+ /// links along any directory or file segment) and applies `chmod a+x` .
574+ void _applyArchiveExecutablePermissions ({
575+ required RemoteArtifactDependency dep,
576+ required String extensionName,
577+ required Directory extractedDir,
578+ required FileSystem fs,
579+ required OperatingSystemUtils os,
580+ }) {
581+ for (final String relativePath in dep.executables) {
582+ assert (ArtifactDependency .isValidExecutableSubpath (relativePath));
583+ final List <String > segments = relativePath.split ('/' );
584+ String currentPath = extractedDir.path;
585+ for (var i = 0 ; i < segments.length - 1 ; i++ ) {
586+ currentPath = fs.path.join (currentPath, segments[i]);
587+ if (fs.isLinkSync (currentPath)) {
588+ throwToolExit (
589+ 'Executable "$relativePath " in artifact "${dep .name }" for extension '
590+ '"$extensionName " traverses a symbolic link.' ,
591+ );
592+ }
593+ if (! fs.directory (currentPath).existsSync ()) {
594+ throwToolExit (
595+ 'Executable "$relativePath " declared by artifact "${dep .name }" for extension '
596+ '"$extensionName " was not found in the extracted archive.' ,
597+ );
598+ }
599+ }
600+ final String execPath = fs.path.join (currentPath, segments.last);
601+ if (fs.isLinkSync (execPath)) {
602+ throwToolExit (
603+ 'Executable "$relativePath " in artifact "${dep .name }" for extension '
604+ '"$extensionName " must not be a symbolic link.' ,
605+ );
606+ }
607+ final File execFile = fs.file (execPath);
608+ if (! execFile.existsSync ()) {
609+ throwToolExit (
610+ 'Executable "$relativePath " declared by artifact "${dep .name }" for extension '
611+ '"$extensionName " was not found in the extracted archive.' ,
612+ );
613+ }
614+ os.chmod (execFile, _executableChmodMode);
615+ }
616+ }
548617}
0 commit comments