kopi 0.2.3

Kopi is a JDK version management tool
Documentation
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// Copyright 2025 dentsusoken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::config::KopiConfig;
use crate::error::{KopiError, Result};
use crate::platform::{ProcessInfo, processes_using_path};
use crate::storage::{InstalledJdk, JdkRepository};
use crate::version::VersionRequest;
use log::{debug, trace, warn};
use std::env;
use std::fmt::{self, Write};
use std::fs;
use std::path::{Path, PathBuf};
use std::str::FromStr;

const GLOBAL_VERSION_FILENAME: &str = "version";
const KOPI_VERSION_FILE: &str = ".kopi-version";
const JAVA_VERSION_FILE: &str = ".java-version";

/// Perform safety checks before uninstalling a JDK.
///
/// The new active-use detection deliberately ignores the `KOPI_JAVA_VERSION`
/// environment variable for now (see T-s2g7h Phase 1 decision); only global and
/// project version files participate in uninstall blocking.
pub fn perform_safety_checks(
    config: &KopiConfig,
    _repository: &JdkRepository,
    jdk: &InstalledJdk,
    force: bool,
) -> Result<ActiveUseSummary> {
    debug!(
        "Performing safety checks for {}@{}",
        jdk.distribution, jdk.version
    );

    let global_active = detect_global_active_jdk(config, jdk)?;
    let project_active = detect_project_active_jdk(jdk)?;

    if !force {
        if let Some(active) = &global_active {
            return Err(KopiError::ValidationError(format!(
                "Cannot uninstall {dist}@{ver} - it is currently active globally via {} \
                 (configured as {}). Use --force to override this check or run \
                 'kopi global unset' before uninstalling.",
                active.version_file.display(),
                active.request,
                dist = jdk.distribution,
                ver = jdk.version
            )));
        }

        if let Some(active) = &project_active {
            return Err(KopiError::ValidationError(format!(
                "Cannot uninstall {dist}@{ver} - it is configured for this project via {} \
                 (configured as {}). Use --force to override this check or update the \
                 project version file.",
                active.version_file.display(),
                active.request,
                dist = jdk.distribution,
                ver = jdk.version
            )));
        }
    }

    let processes = detect_running_processes(jdk)?;

    if !force && !processes.is_empty() {
        return Err(build_running_process_error(jdk, &processes));
    }

    let summary = ActiveUseSummary {
        global: global_active,
        project: project_active,
        processes,
    };

    Ok(summary)
}

fn detect_global_active_jdk(config: &KopiConfig, jdk: &InstalledJdk) -> Result<Option<ActiveUse>> {
    let version_file = config.kopi_home().join(GLOBAL_VERSION_FILENAME);
    if !version_file.exists() {
        return Ok(None);
    }

    match read_kopi_version_request(&version_file)? {
        Some(request) => {
            if request_matches_jdk(&request, jdk) {
                debug!(
                    "Global version file {} matches target {}@{} (request: {})",
                    version_file.display(),
                    jdk.distribution,
                    jdk.version,
                    request
                );
                Ok(Some(ActiveUse::new(version_file, request)))
            } else {
                Ok(None)
            }
        }
        None => Ok(None),
    }
}

fn detect_project_active_jdk(jdk: &InstalledJdk) -> Result<Option<ActiveUse>> {
    let start_dir = env::current_dir().map_err(|e| {
        KopiError::SystemError(format!("Failed to determine current directory: {e}"))
    })?;

    let mut current = start_dir.as_path();
    loop {
        let kopi_version_file = current.join(KOPI_VERSION_FILE);
        if let Some(request) = read_kopi_version_request(&kopi_version_file)?
            && request_matches_jdk(&request, jdk)
        {
            debug!(
                "Project version file {} matches target {}@{} (request: {})",
                kopi_version_file.display(),
                jdk.distribution,
                jdk.version,
                request
            );
            return Ok(Some(ActiveUse::new(kopi_version_file, request)));
        }

        let java_version_file = current.join(JAVA_VERSION_FILE);
        if let Some(request) = read_java_version_request(&java_version_file)?
            && request_matches_jdk(&request, jdk)
        {
            debug!(
                "Java version file {} matches target {}@{} (request: {})",
                java_version_file.display(),
                jdk.distribution,
                jdk.version,
                request
            );
            return Ok(Some(ActiveUse::new(java_version_file, request)));
        }

        match current.parent() {
            Some(parent) => current = parent,
            None => break,
        }
    }

    Ok(None)
}

fn read_kopi_version_request(path: &Path) -> Result<Option<VersionRequest>> {
    read_version_request(path, VersionFileKind::Kopi)
}

fn read_java_version_request(path: &Path) -> Result<Option<VersionRequest>> {
    read_version_request(path, VersionFileKind::Java)
}

fn read_version_request(path: &Path, kind: VersionFileKind) -> Result<Option<VersionRequest>> {
    if !path.exists() {
        return Ok(None);
    }

    let content = match fs::read_to_string(path) {
        Ok(raw) => raw,
        Err(e) => {
            warn!(
                "Failed to read version file {}: {e}. Ignoring for active-use detection.",
                path.display()
            );
            return Ok(None);
        }
    };

    let trimmed = content.trim();
    if trimmed.is_empty() {
        trace!(
            "Version file {} is empty; ignoring for active-use detection",
            path.display()
        );
        return Ok(None);
    }

    let request_result = match kind {
        VersionFileKind::Kopi => VersionRequest::from_str(trimmed),
        VersionFileKind::Java => VersionRequest::new(trimmed.to_string()),
    };

    match request_result {
        Ok(request) => Ok(Some(request)),
        Err(e) => {
            warn!(
                "Ignoring version file {} due to parse error: {e}",
                path.display()
            );
            Ok(None)
        }
    }
}

fn request_matches_jdk(request: &VersionRequest, jdk: &InstalledJdk) -> bool {
    if request
        .distribution
        .as_ref()
        .is_some_and(|distribution| !distribution.eq_ignore_ascii_case(&jdk.distribution))
    {
        return false;
    }

    if request
        .javafx_bundled
        .is_some_and(|javafx| javafx != jdk.javafx_bundled)
    {
        return false;
    }

    jdk.version.matches_pattern(&request.version_pattern)
}

fn detect_running_processes(jdk: &InstalledJdk) -> Result<Vec<ProcessInfo>> {
    processes_using_path(&jdk.path)
}

fn build_running_process_error(jdk: &InstalledJdk, processes: &[ProcessInfo]) -> KopiError {
    let canonical_root = jdk.path.canonicalize().unwrap_or_else(|_| jdk.path.clone());

    let mut message = format!(
        "Cannot uninstall {dist}@{ver} - running processes are using {jdk_path}:",
        dist = jdk.distribution,
        ver = jdk.version,
        jdk_path = jdk.path.display()
    );

    for process in processes {
        let exe_display = process.exe_path.display();
        let _ = writeln!(message, "\n  PID {} ({exe_display})", process.pid);

        if process.handles.is_empty() {
            let _ = writeln!(message, "    - <no open handles reported>");
            continue;
        }

        for handle in &process.handles {
            let handle_display = handle
                .strip_prefix(&canonical_root)
                .ok()
                .map(|relative| {
                    let rendered = relative.display().to_string();
                    if rendered.is_empty() {
                        ".".to_string()
                    } else {
                        rendered
                    }
                })
                .unwrap_or_else(|| handle.display().to_string());
            let _ = writeln!(message, "    - {handle_display}");
        }
    }

    message
        .push_str("\nClose these processes or re-run with --force to proceed with the uninstall.");

    KopiError::ValidationError(message)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ActiveUse {
    version_file: PathBuf,
    request: VersionRequest,
}

impl ActiveUse {
    fn new(version_file: PathBuf, request: VersionRequest) -> Self {
        Self {
            version_file,
            request,
        }
    }
}

impl fmt::Display for ActiveUse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} (configured as {})",
            self.version_file.display(),
            self.request
        )
    }
}

#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct ActiveUseSummary {
    pub global: Option<ActiveUse>,
    pub project: Option<ActiveUse>,
    pub processes: Vec<ProcessInfo>,
}

impl ActiveUseSummary {
    pub fn has_active_use(&self) -> bool {
        self.global.is_some() || self.project.is_some() || !self.processes.is_empty()
    }
}

enum VersionFileKind {
    Kopi,
    Java,
}

/// Verify user has permission to remove the directory
pub fn verify_removal_permission(path: &Path) -> Result<()> {
    debug!("Verifying removal permission for {}", path.display());

    // Check if path exists
    if !path.exists() {
        return Err(KopiError::DirectoryNotFound(path.display().to_string()));
    }

    // Try to check if we can write to parent directory (proxy for removal permission)
    if let Some(parent) = path.parent() {
        match fs::metadata(parent) {
            Ok(metadata) => {
                if metadata.permissions().readonly() {
                    return Err(KopiError::PermissionDenied(format!(
                        "Parent directory is read-only: {}",
                        parent.display()
                    )));
                }
            }
            Err(e) => {
                return Err(KopiError::PermissionDenied(format!(
                    "Cannot access parent directory: {e}"
                )));
            }
        }
    }

    Ok(())
}

/// Check if other tools depend on this JDK
pub fn check_tool_dependencies(path: &Path) -> Result<()> {
    debug!("Checking tool dependencies for {}", path.display());

    // TODO: Future enhancement - check if other tools have hardcoded paths to this JDK
    // For now, just warn about potential issues
    if path.join("bin/java").exists() {
        warn!("Note: Other tools may have references to this JDK installation");
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::KopiConfig;
    use crate::paths::install;
    use crate::storage::JdkRepository;
    use crate::version::Version;
    use serial_test::serial;
    use std::env;
    use tempfile::TempDir;

    struct TestFixture {
        temp_dir: TempDir,
        config: KopiConfig,
    }

    impl TestFixture {
        fn new() -> Self {
            let temp_dir = TempDir::new().unwrap();
            let config = KopiConfig::new(temp_dir.path().to_path_buf()).unwrap();
            fs::create_dir_all(config.jdks_dir().unwrap()).unwrap();
            Self { temp_dir, config }
        }

        fn repository(&self) -> JdkRepository<'_> {
            JdkRepository::new(&self.config)
        }

        fn create_installed_jdk(&self, distribution: &str, version: &str) -> InstalledJdk {
            let jdk_path = self
                .config
                .jdks_dir()
                .unwrap()
                .join(format!("{distribution}-{version}"));
            fs::create_dir_all(&jdk_path).unwrap();

            let bin_dir = install::bin_directory(&jdk_path);
            fs::create_dir_all(&bin_dir).unwrap();
            fs::write(bin_dir.join("java"), "#!/bin/sh\necho mock java").unwrap();
            fs::write(
                jdk_path.join("release"),
                format!("JAVA_VERSION=\"{version}\""),
            )
            .unwrap();

            InstalledJdk::new(
                distribution.to_string(),
                Version::from_str(version).unwrap(),
                jdk_path,
                false,
            )
        }
    }

    #[test]
    fn safety_checks_allow_when_not_active() {
        let fixture = TestFixture::new();
        let repository = fixture.repository();
        let jdk = fixture.create_installed_jdk("temurin", "21.0.5+11");

        let summary = perform_safety_checks(&fixture.config, &repository, &jdk, false).unwrap();
        assert!(!summary.has_active_use());
        assert!(summary.processes.is_empty());
    }

    #[test]
    fn safety_checks_block_global_default() {
        let fixture = TestFixture::new();
        let repository = fixture.repository();
        let jdk = fixture.create_installed_jdk("temurin", "21.0.5+11");

        let global_path = fixture.config.kopi_home().join(GLOBAL_VERSION_FILENAME);
        jdk.write_to(&global_path).unwrap();

        let result = perform_safety_checks(&fixture.config, &repository, &jdk, false);
        assert!(matches!(result, Err(KopiError::ValidationError(_))));

        // Force override should bypass the guard
        let summary = perform_safety_checks(&fixture.config, &repository, &jdk, true).unwrap();
        assert!(summary.global.is_some());
        assert!(summary.processes.is_empty());
    }

    #[test]
    #[serial]
    fn safety_checks_block_project_kopi_version() {
        let fixture = TestFixture::new();
        let repository = fixture.repository();
        let jdk = fixture.create_installed_jdk("temurin", "21.0.5+11");

        let project_dir = fixture.temp_dir.path().join("workspace/project");
        fs::create_dir_all(&project_dir).unwrap();
        jdk.write_to(&project_dir.join(KOPI_VERSION_FILE)).unwrap();

        let original_dir = env::current_dir().unwrap();
        env::set_current_dir(&project_dir).unwrap();
        let result = perform_safety_checks(&fixture.config, &repository, &jdk, false);
        env::set_current_dir(original_dir).unwrap();

        assert!(matches!(result, Err(KopiError::ValidationError(_))));
    }

    #[test]
    #[serial]
    fn safety_checks_block_project_java_version() {
        let fixture = TestFixture::new();
        let repository = fixture.repository();
        let jdk = fixture.create_installed_jdk("zulu", "17.0.9");

        let project_dir = fixture.temp_dir.path().join("workspace/java");
        fs::create_dir_all(&project_dir).unwrap();
        fs::write(project_dir.join(JAVA_VERSION_FILE), "17.0.9\n").unwrap();

        let original_dir = env::current_dir().unwrap();
        env::set_current_dir(&project_dir).unwrap();
        let result = perform_safety_checks(&fixture.config, &repository, &jdk, false);
        env::set_current_dir(original_dir).unwrap();

        assert!(matches!(result, Err(KopiError::ValidationError(_))));
    }

    #[test]
    #[serial]
    fn safety_checks_ignore_invalid_version_files() {
        let fixture = TestFixture::new();
        let repository = fixture.repository();
        let jdk = fixture.create_installed_jdk("temurin", "21.0.5+11");

        let project_dir = fixture.temp_dir.path().join("workspace/invalid");
        fs::create_dir_all(&project_dir).unwrap();
        fs::write(project_dir.join(KOPI_VERSION_FILE), "not-a-version").unwrap();

        let original_dir = env::current_dir().unwrap();
        env::set_current_dir(&project_dir).unwrap();
        let result = perform_safety_checks(&fixture.config, &repository, &jdk, false);
        env::set_current_dir(original_dir).unwrap();

        let summary = result.unwrap();
        assert!(!summary.has_active_use());
        assert!(summary.processes.is_empty());
    }

    #[test]
    #[cfg(target_os = "linux")]
    fn safety_checks_detect_running_processes() {
        use std::process;

        let fixture = TestFixture::new();
        let repository = fixture.repository();
        let jdk = fixture.create_installed_jdk("temurin", "21.0.5+11");

        let bin_java = install::bin_directory(&jdk.path).join("java");

        // Hold an open handle inside the JDK to simulate active use.
        let file_handle = fs::OpenOptions::new()
            .read(true)
            .open(&bin_java)
            .expect("open java binary for reading");

        let result = perform_safety_checks(&fixture.config, &repository, &jdk, false);
        drop(file_handle);

        match result {
            Err(KopiError::ValidationError(message)) => {
                assert!(
                    message.contains("running processes are using"),
                    "expected message to mention running processes: {message}"
                );
            }
            other => panic!("expected validation error, got {other:?}"),
        }

        // Re-open to verify force path collects diagnostics.
        let file_handle = fs::OpenOptions::new()
            .read(true)
            .open(&bin_java)
            .expect("reopen java binary for reading");
        let summary = perform_safety_checks(&fixture.config, &repository, &jdk, true).unwrap();
        drop(file_handle);

        assert!(
            summary
                .processes
                .iter()
                .any(|info| info.pid == process::id()),
            "expected current process to be reported"
        );
    }

    #[test]
    fn test_verify_removal_permission() {
        let temp_dir = TempDir::new().unwrap();
        let test_path = temp_dir.path().join("test_jdk");
        fs::create_dir(&test_path).unwrap();

        // Should succeed for existing directory
        assert!(verify_removal_permission(&test_path).is_ok());

        // Should fail for non-existent directory
        let non_existent = temp_dir.path().join("non_existent");
        assert!(verify_removal_permission(&non_existent).is_err());
    }

    #[test]
    fn test_check_tool_dependencies() {
        let temp_dir = TempDir::new().unwrap();
        let jdk_path = temp_dir.path().join("jdk");

        // No warnings for empty directory
        fs::create_dir(&jdk_path).unwrap();
        assert!(check_tool_dependencies(&jdk_path).is_ok());

        // Should succeed even with java binary (just warns)
        let bin_dir = install::bin_directory(&jdk_path);
        fs::create_dir(&bin_dir).unwrap();
        fs::write(bin_dir.join("java"), "mock").unwrap();
        assert!(check_tool_dependencies(&jdk_path).is_ok());
    }

    #[test]
    #[cfg(unix)]
    fn test_permission_check_readonly_parent() {
        use std::os::unix::fs::PermissionsExt;

        let temp_dir = TempDir::new().unwrap();
        let parent = temp_dir.path().join("readonly_parent");
        fs::create_dir(&parent).unwrap();

        let test_path = parent.join("jdk");
        fs::create_dir(&test_path).unwrap();

        // Make parent read-only
        let mut perms = fs::metadata(&parent).unwrap().permissions();
        perms.set_mode(0o444);
        fs::set_permissions(&parent, perms).unwrap();

        // Should detect read-only parent
        let result = verify_removal_permission(&test_path);

        // Restore permissions before asserting (so cleanup works)
        let mut perms = fs::metadata(&parent).unwrap().permissions();
        perms.set_mode(0o755);
        fs::set_permissions(&parent, perms).unwrap();

        assert!(result.is_err());
    }
}