File size: 4,465 Bytes
684cc60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
App Interface Module - Provides high-level file operations for applications
"""
import threading
from typing import Dict, List, Optional

class AppInterface:
    """
    Provides a high-level interface for applications to interact with the virtual SSD.
    Simulates the interface that applications would use to save and read files.
    """
    
    def __init__(self, virtual_os):
        self.virtual_os = virtual_os
        self.lock = threading.RLock()
        
        print("AppInterface initialized")
    
    def save(self, filename: str, data: bytes) -> bool:
        """
        Save data to a file. This is the main entry point for applications.
        """
        with self.lock:
            if not filename:
                print("Error: Filename cannot be empty")
                return False
            
            if not isinstance(data, bytes):
                print("Error: Data must be bytes")
                return False
            
            print(f"App saving file '{filename}' ({len(data)} bytes)")
            return self.virtual_os.write_file(filename, data)
    
    def load(self, filename: str) -> Optional[bytes]:
        """
        Load data from a file.
        """
        with self.lock:
            if not filename:
                print("Error: Filename cannot be empty")
                return None
            
            print(f"App loading file '{filename}'")
            return self.virtual_os.read_file(filename)
    
    def delete(self, filename: str) -> bool:
        """
        Delete a file.
        """
        with self.lock:
            if not filename:
                print("Error: Filename cannot be empty")
                return False
            
            print(f"App deleting file '{filename}'")
            return self.virtual_os.delete_file(filename)
    
    def list_files(self) -> Dict[str, Dict]:
        """
        List all files in the system.
        """
        with self.lock:
            return self.virtual_os.list_files()
    
    def get_file_info(self, filename: str) -> Optional[Dict]:
        """
        Get detailed information about a file.
        """
        with self.lock:
            return self.virtual_os.get_file_info(filename)
    
    def get_system_info(self) -> Dict:
        """
        Get system information including capacity and usage.
        """
        with self.lock:
            return self.virtual_os.get_os_stats()
    
    def sync(self):
        """
        Force synchronization of all buffered data to storage.
        """
        with self.lock:
            print("App requesting sync...")
            self.virtual_os.flush_buffers()
    
    def save_text_file(self, filename: str, text: str, encoding: str = 'utf-8') -> bool:
        """
        Convenience method to save text data.
        """
        try:
            data = text.encode(encoding)
            return self.save(filename, data)
        except Exception as e:
            print(f"Error encoding text: {e}")
            return False
    
    def load_text_file(self, filename: str, encoding: str = 'utf-8') -> Optional[str]:
        """
        Convenience method to load text data.
        """
        try:
            data = self.load(filename)
            if data is None:
                return None
            return data.decode(encoding)
        except Exception as e:
            print(f"Error decoding text: {e}")
            return None
    
    def get_capacity_info(self) -> Dict:
        """
        Get capacity information in a user-friendly format.
        """
        with self.lock:
            stats = self.get_system_info()
            fs_stats = stats.get("file_system", {})
            
            total_gb = fs_stats.get("total_bytes", 0) / (1024**3)
            used_gb = fs_stats.get("used_bytes", 0) / (1024**3)
            free_gb = fs_stats.get("free_bytes", 0) / (1024**3)
            
            return {
                "total_gb": round(total_gb, 2),
                "used_gb": round(used_gb, 2),
                "free_gb": round(free_gb, 2),
                "usage_percent": round(fs_stats.get("usage_percent", 0), 2),
                "num_files": fs_stats.get("num_files", 0)
            }
    
    def shutdown(self):
        """
        Shutdown the application interface.
        """
        print("AppInterface shutdown complete")
    
    def __del__(self):
        try:
            self.shutdown()
        except:
            pass