get_cache_info Subroutine

public subroutine get_cache_info(custom_cache_dir, info)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: custom_cache_dir
character(len=*), intent(out) :: info

Source Code

    subroutine get_cache_info(custom_cache_dir, info)
        character(len=*), intent(in) :: custom_cache_dir
        character(len=*), intent(out) :: info
        character(len=256) :: cache_dir
        character(len=:), allocatable :: command, size_output
        integer :: unit, ios, exitstat, cmdstat
        integer :: num_files, num_dirs
        logical :: exists

        ! Get cache directory
        if (len_trim(custom_cache_dir) > 0) then
            cache_dir = trim(custom_cache_dir)
        else
            cache_dir = get_cache_dir()
        end if

        ! Check if cache directory exists
        inquire (file=trim(cache_dir), exist=exists)
        if (.not. exists) then
            info = "Cache directory does not exist: "//trim(cache_dir)
            return
        end if

        ! Get cache size and file count
        if (get_os_type() == OS_WINDOWS) then
            ! Windows: Use dir command
     command = 'dir /s "'//trim(escape_shell_arg(cache_dir))//'" 2>nul | find "File(s)"'
        else
            ! Unix-like: Use du and find commands
      command = 'du -sh "'//trim(escape_shell_arg(cache_dir))//'" 2>/dev/null | cut -f1'
        end if

        ! Execute command and capture output
        block
            character(len=256) :: temp_file
            temp_file = create_temp_file('fortran_cache_cache_size', '.tmp')
          call execute_command_line(command//' > '//trim(escape_shell_arg(temp_file)), &
                                      exitstat=exitstat, cmdstat=cmdstat)

            size_output = "unknown"
            if (cmdstat == 0 .and. exitstat == 0) then
            open (newunit=unit, file=temp_file, status='old', action='read', iostat=ios)
                if (ios == 0) then
                    read (unit, '(A)', iostat=ios) size_output
                    close (unit)
                end if
                ! Clean up temp file is handled by temp_utils
            end if
        end block

        ! Count files and directories
        if (get_os_type() == OS_WINDOWS) then
   command = 'dir /b /s "'//trim(escape_shell_arg(cache_dir))//'" 2>nul | find /c /v ""'
        else
  command = 'find "'//trim(escape_shell_arg(cache_dir))//'" -type f 2>/dev/null | wc -l'
        end if

        block
            character(len=256) :: temp_file
            temp_file = create_temp_file('fortran_cache_cache_count', '.tmp')
          call execute_command_line(command//' > '//trim(escape_shell_arg(temp_file)), &
                                      exitstat=exitstat, cmdstat=cmdstat)

            num_files = 0
            if (cmdstat == 0 .and. exitstat == 0) then
            open (newunit=unit, file=temp_file, status='old', action='read', iostat=ios)
                if (ios == 0) then
                    read (unit, *, iostat=ios) num_files
                    close (unit)
                end if
                ! Clean up temp file is handled by temp_utils
            end if
        end block

        ! Build info string
        write (info, '(A)') "Fortran Cache Information:"
    write(info, '(A,A,A)') trim(info), new_line('a'), "  Cache directory: " // trim(cache_dir)
    write(info, '(A,A,A,I0,A)') trim(info), new_line('a'), "  Number of files: ", num_files, " files"
    write(info, '(A,A,A,A)') trim(info), new_line('a'), "  Total size: ", trim(adjustl(size_output))

    end subroutine get_cache_info