Safely managing files when working with AI-generated code is a critical challenge for modern development workflows. Building on our previous exploration of secure AI code execution in Part 1, we'll now focus on file management within sandbox environments.
We've already seen how LangChain and Daytona work together to generate and execute code safely - now it's time to dive deeper into controlling the files these systems create and modify. By leveraging Daytona's filesystem API, developers can create, modify, and execute files without compromising system security, while maintaining full control over their development environment.
TL;DR
Daytona's filesystem API provides secure file management in sandbox environments
Create, read, modify files and control permissions within isolated workspaces
Best practices focus on cross-platform compatibility and error handling
Introduction to Daytona's Filesystem API
Managing files is crucial when working with AI-generated code in sandbox environments. Daytona's filesystem API provides programmatic access to workspace files, allowing you to create, modify, and manage files within the secure sandbox. Let's explore some practical examples of how to use these capabilities.
Basic File Operations
Example 1: Creating and Reading Files
Here's a simple example of creating a file and reading its contents:
1from daytona_sdk import Daytona23def basic_file_operations():4 # Initialize Daytona5 daytona = Daytona()6 workspace = daytona.create()78 try:9 # Get workspace root directory10 root_dir = workspace.get_workspace_root_dir()1112 # Create a new file13 file_path = f"{root_dir}/hello.txt"14 content = b"Hello from Daytona!"15 workspace.fs.upload_file(file_path, content)1617 # Read the file contents18 file_content = workspace.fs.download_file(file_path)19 print(f"File contents: {file_content.decode('utf-8')}")2021 finally:22 # Clean up23 daytona.remove(workspace)
Example 2: Working with Project Files
This example demonstrates how to manage multiple files in a project structure:
1import os2from daytona_sdk import Daytona34def manage_project_files():5 daytona = Daytona()6 workspace = daytona.create()78 try:9 root_dir = workspace.get_workspace_root_dir()1011 # Create project structure12 project_dir = os.path.join(root_dir, "my_project")13 workspace.fs.create_folder(project_dir, "755")1415 # Create multiple files16 files = {17 "main.py": b"print('Hello, World!')",18 "config.json": b'{"setting": "value"}',19 "README.md": b"# My Project\nThis is a test project."20 }2122 for filename, content in files.items():23 file_path = os.path.join(project_dir, filename)24 workspace.fs.upload_file(file_path, content)2526 # List all files in the project27 project_files = workspace.fs.list_files(project_dir)28 print("Project files:", project_files)2930 finally:31 daytona.remove(workspace)
Example 3: File Search and Replace
Here's how to search for specific content and replace it across files:
1def search_and_replace():2 daytona = Daytona()3 workspace = daytona.create()45 try:6 root_dir = workspace.get_workspace_root_dir()78 # Create test files9 files = {10 "file1.txt": b"This is a test file with placeholder.",11 "file2.txt": b"Another file with placeholder text.",12 "file3.txt": b"No matching content here."13 }1415 for filename, content in files.items():16 file_path = os.path.join(root_dir, filename)17 workspace.fs.upload_file(file_path, content)1819 # Search for files containing "placeholder"20 matches = workspace.fs.find_files(root_dir, "placeholder")21 print(f"Found {len(matches)} files containing 'placeholder'")2223 # Replace content in matching files24 for file_path in matches:25 workspace.fs.replace_in_files(26 [file_path],27 "placeholder",28 "updated content"29 )3031 # Verify changes32 for file_path in matches:33 content = workspace.fs.download_file(file_path)34 print(f"Updated content in {file_path}:")35 print(content.decode('utf-8'))3637 finally:38 daytona.remove(workspace)
Example 4: Managing File Permissions
Here's how to work with file permissions in your workspace:
1def manage_file_permissions():2 daytona = Daytona()3 workspace = daytona.create()45 root_dir = workspace.get_workspace_root_dir()67 # Create a directory with specific permissions8 scripts_dir = os.path.join(root_dir, "scripts")9 workspace.fs.create_folder(scripts_dir, "755") # rwxr-xr-x1011 # Create an executable script12 script_path = os.path.join(scripts_dir, "run.sh")13 script_content = b"#!/bin/bash\necho 'Hello from script!'"14 workspace.fs.upload_file(script_path, script_content)1516 # Make the script executable17 workspace.fs.set_file_permissions(script_path, "755") # rwxr-xr-x1819 # Create a config file with restricted permissions20 config_path = os.path.join(scripts_dir, "config.json")21 config_content = b'{"api_key": "secret123"}'22 workspace.fs.upload_file(config_path, config_content)23 workspace.fs.set_file_permissions(config_path, "600") # rw-------2425 # Get and display file permissions26 script_info = workspace.fs.get_file_details(script_path)27 config_info = workspace.fs.get_file_details(config_path)2829 print(f"Script permissions: {script_info['mode']}")30 print(f"Config permissions: {config_info['mode']}")3132 daytona.remove(workspace)
Best Practices for File Management
When working with Daytona's filesystem API, keep these best practices in mind:
Handle file paths carefully using os.path.join() for cross-platform compatibility
Set appropriate file permissions when creating directories and files
Use binary mode (bytes) when uploading files to avoid encoding issues
Implement error handling for file operations
Conclusion
Daytona's filesystem API provides powerful tools for managing files within sandbox environments. Combined with the secure execution capabilities we explored in Part 1, you can build sophisticated systems for working with AI-generated code while maintaining full control over the file system.
Our examples demonstrate common use cases, from basic file operations to more complex scenarios like managing permissions and handling large files. By following the best practices and leveraging these capabilities, you can build robust systems that safely manage files within AI sandbox environments.
What's Next?
In the next part of this series, we'll dive into Daytona's integration with Git and code analysis tools, exploring how to manage source control operations and leverage various code analysis capabilities to enhance your development workflow.