# Contents

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 Daytona
2
3def basic_file_operations():
4 # Initialize Daytona
5 daytona = Daytona()
6 workspace = daytona.create()
7
8 try:
9 # Get workspace root directory
10 root_dir = workspace.get_workspace_root_dir()
11
12 # Create a new file
13 file_path = f"{root_dir}/hello.txt"
14 content = b"Hello from Daytona!"
15 workspace.fs.upload_file(file_path, content)
16
17 # Read the file contents
18 file_content = workspace.fs.download_file(file_path)
19 print(f"File contents: {file_content.decode('utf-8')}")
20
21 finally:
22 # Clean up
23 daytona.remove(workspace)

Example 2: Working with Project Files

This example demonstrates how to manage multiple files in a project structure:

1import os
2from daytona_sdk import Daytona
3
4def manage_project_files():
5 daytona = Daytona()
6 workspace = daytona.create()
7
8 try:
9 root_dir = workspace.get_workspace_root_dir()
10
11 # Create project structure
12 project_dir = os.path.join(root_dir, "my_project")
13 workspace.fs.create_folder(project_dir, "755")
14
15 # Create multiple files
16 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 }
21
22 for filename, content in files.items():
23 file_path = os.path.join(project_dir, filename)
24 workspace.fs.upload_file(file_path, content)
25
26 # List all files in the project
27 project_files = workspace.fs.list_files(project_dir)
28 print("Project files:", project_files)
29
30 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()
4
5 try:
6 root_dir = workspace.get_workspace_root_dir()
7
8 # Create test files
9 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 }
14
15 for filename, content in files.items():
16 file_path = os.path.join(root_dir, filename)
17 workspace.fs.upload_file(file_path, content)
18
19 # Search for files containing "placeholder"
20 matches = workspace.fs.find_files(root_dir, "placeholder")
21 print(f"Found {len(matches)} files containing 'placeholder'")
22
23 # Replace content in matching files
24 for file_path in matches:
25 workspace.fs.replace_in_files(
26 [file_path],
27 "placeholder",
28 "updated content"
29 )
30
31 # Verify changes
32 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'))
36
37 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()
4
5 root_dir = workspace.get_workspace_root_dir()
6
7 # Create a directory with specific permissions
8 scripts_dir = os.path.join(root_dir, "scripts")
9 workspace.fs.create_folder(scripts_dir, "755") # rwxr-xr-x
10
11 # Create an executable script
12 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)
15
16 # Make the script executable
17 workspace.fs.set_file_permissions(script_path, "755") # rwxr-xr-x
18
19 # Create a config file with restricted permissions
20 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-------
24
25 # Get and display file permissions
26 script_info = workspace.fs.get_file_details(script_path)
27 config_info = workspace.fs.get_file_details(config_path)
28
29 print(f"Script permissions: {script_info['mode']}")
30 print(f"Config permissions: {config_info['mode']}")
31
32 daytona.remove(workspace)

Best Practices for File Management

When working with Daytona's filesystem API, keep these best practices in mind:

  1. Handle file paths carefully using os.path.join() for cross-platform compatibility

  2. Set appropriate file permissions when creating directories and files

  3. Use binary mode (bytes) when uploading files to avoid encoding issues

  4. 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.

Tags::
  • ai
  • sdk
  • langchain