@@ -21,10 +21,12 @@ var (
2121 hasNamespacePrivileges = false
2222 // Set to true if we have cgroup access
2323 hasCgroupAccess = false
24- imagesDir = "/tmp/basic-docker/images"
25- layersDir = "/tmp/basic-docker/layers"
2624)
2725
26+ var baseDir = filepath .Join (os .TempDir (), "basic-docker" )
27+ var imagesDir = filepath .Join (baseDir , "images" )
28+ var layersDir = filepath .Join (baseDir , "layers" )
29+
2830// Define the ImageLayer type
2931type ImageLayer struct {
3032 ID string
@@ -37,14 +39,13 @@ type ImageLayer struct {
3739// To initialize the directories
3840func initDirectories () error {
3941 dirs := []string {
40- "/tmp/basic-docker/ containers" ,
42+ filepath . Join ( baseDir , " containers") ,
4143 imagesDir ,
4244 layersDir ,
4345 }
44-
4546 for _ , dir := range dirs {
4647 if err := os .MkdirAll (dir , 0755 ); err != nil {
47- return err
48+ return fmt . Errorf ( "failed to create directory %s: %w" , dir , err )
4849 }
4950 }
5051 return nil
@@ -172,12 +173,12 @@ func run() {
172173 }
173174
174175 // Create rootfs for this container
175- rootfs := filepath .Join ("/tmp/basic-docker/ containers" , containerID , "rootfs" )
176+ rootfs := filepath .Join (baseDir , " containers" , containerID , "rootfs" )
176177
177178 // Instead of calling createMinimalRootfs directly:
178179 // 1. Create a base layer if it doesn't exist
179180 baseLayerID := "base-layer"
180- baseLayerPath := filepath .Join ("/tmp/basic-docker/ layers" , baseLayerID )
181+ baseLayerPath := filepath .Join (baseDir , " layers" , baseLayerID )
181182
182183 if _ , err := os .Stat (baseLayerPath ); os .IsNotExist (err ) {
183184 // Create the base layer
@@ -209,7 +210,7 @@ func run() {
209210
210211 // 2. Create an app layer for this specific container (optional)
211212 appLayerID := "app-layer-" + containerID
212- appLayerPath := filepath .Join ("/tmp/basic-docker/ layers" , appLayerID )
213+ appLayerPath := filepath .Join (baseDir , " layers" , appLayerID )
213214
214215 // Use the appLayerID variable to log its creation
215216 fmt .Printf ("App layer created with ID: %s\n " , appLayerID )
@@ -234,7 +235,7 @@ func run() {
234235 must (mountLayeredFilesystem (layers , rootfs ))
235236
236237 // Write the PID of the current process to a file
237- pidFile := filepath .Join ("/tmp/basic-docker/ containers" , containerID , "pid" )
238+ pidFile := filepath .Join (baseDir , " containers" , containerID , "pid" )
238239 fmt .Printf ("Debug: Writing PID file for container %s at %s\n " , containerID , pidFile )
239240 fmt .Printf ("Debug: Current process PID is %d\n " , os .Getpid ())
240241 if err := os .WriteFile (pidFile , []byte (fmt .Sprintf ("%d" , os .Getpid ())), 0644 ); err != nil {
@@ -594,7 +595,7 @@ func setupCgroups(containerID string, memoryLimit int) error {
594595}
595596
596597func getContainerStatus (containerID string ) string {
597- pidFile := filepath .Join ("/tmp/basic-docker/ containers" , containerID , "pid" )
598+ pidFile := filepath .Join (baseDir , " containers" , containerID , "pid" )
598599 pidData , err := os .ReadFile (pidFile )
599600 if err != nil {
600601 return "Stopped"
@@ -617,7 +618,7 @@ func getContainerStatus(containerID string) string {
617618}
618619
619620func listContainers () {
620- containerDir := "/tmp/basic-docker/ containers"
621+ containerDir := filepath . Join ( baseDir , " containers")
621622 fmt .Println ("CONTAINER ID\t STATUS\t COMMAND" )
622623
623624 if _ , err := os .Stat (containerDir ); os .IsNotExist (err ) {
@@ -683,7 +684,7 @@ func must(err error) {
683684func testMultiLayerMount () {
684685 // Create a base layer
685686 baseLayerID := "base-layer-" + fmt .Sprintf ("%d" , time .Now ().Unix ())
686- baseLayerPath := filepath .Join ("/tmp/basic-docker/ layers" , baseLayerID )
687+ baseLayerPath := filepath .Join (baseDir , " layers" , baseLayerID )
687688 if err := os .MkdirAll (baseLayerPath , 0755 ); err != nil {
688689 fmt .Printf ("Error creating base layer: %v\n " , err )
689690 return
@@ -697,7 +698,7 @@ func testMultiLayerMount() {
697698
698699 // Create a second layer
699700 appLayerID := "app-layer-" + fmt .Sprintf ("%d" , time .Now ().Unix ())
700- appLayerPath := filepath .Join ("/tmp/basic-docker/ layers" , appLayerID )
701+ appLayerPath := filepath .Join (baseDir , " layers" , appLayerID )
701702 if err := os .MkdirAll (appLayerPath , 0755 ); err != nil {
702703 fmt .Printf ("Error creating app layer: %v\n " , err )
703704 return
@@ -713,7 +714,7 @@ func testMultiLayerMount() {
713714 }
714715
715716 // Create a target directory
716- targetPath := filepath .Join ("/tmp/basic-docker/ test-mount" )
717+ targetPath := filepath .Join (baseDir , " test-mount" )
717718
718719 // Mount the layers
719720 layers := []string {baseLayerID , appLayerID }
@@ -748,14 +749,14 @@ func execCommand() {
748749 args := os .Args [4 :]
749750
750751 // Check if the container directory exists
751- containerDir := filepath .Join ("/tmp/basic-docker/ containers" , containerID )
752+ containerDir := filepath .Join (baseDir , " containers" , containerID )
752753 if _ , err := os .Stat (containerDir ); os .IsNotExist (err ) {
753754 fmt .Printf ("Error: Container %s does not exist. Please ensure the container is running.\n " , containerID )
754755 os .Exit (1 )
755756 }
756757
757758 // Locate the PID of the container
758- pidFile := fmt . Sprintf ( "/tmp/basic-docker/ containers/%s/pid " , containerID )
759+ pidFile := filepath . Join ( baseDir , " containers" , containerID , "pid" )
759760 pidData , err := os .ReadFile (pidFile )
760761 if err != nil {
761762 fmt .Printf ("Error: Failed to read PID file for container %s: %v\n " , containerID , err )
0 commit comments