Directories in Ada

Here’s the translation of the Go code to Ada, with explanations in Markdown format suitable for Hugo:

with Ada.Text_IO;
with Ada.Directories;
with Ada.Command_Line;

procedure Directories is
   use Ada.Text_IO;
   use Ada.Directories;

   procedure Check (Condition : Boolean) is
   begin
      if not Condition then
         raise Program_Error with "An error occurred";
      end if;
   end Check;

   procedure Create_Empty_File (Name : String) is
      File : File_Type;
   begin
      Create (File, Out_File, Name);
      Close (File);
   end Create_Empty_File;

   procedure Visit (Directory_Entry : Directory_Entry_Type;
                    Continue        : in out Boolean) is
   begin
      Put_Line (" " & Full_Name (Directory_Entry) & " " &
                Boolean'Image (Is_Directory (Directory_Entry)));
   end Visit;

begin
   -- Create a new sub-directory in the current working directory
   Create_Directory ("subdir");

   -- When creating temporary directories, it's good practice to ensure
   -- their removal. Ada doesn't have a built-in defer mechanism, so we'll
   -- use a block statement to ensure cleanup.
   declare
   begin
      -- Helper procedure to create a new empty file
      Create_Empty_File ("subdir/file1");

      -- We can create a hierarchy of directories, including parents
      Create_Path ("subdir/parent/child");

      Create_Empty_File ("subdir/parent/file2");
      Create_Empty_File ("subdir/parent/file3");
      Create_Empty_File ("subdir/parent/child/file4");

      -- Search lists directory contents
      Put_Line ("Listing subdir/parent");
      declare
         Search : Search_Type;
         Dir_Ent : Directory_Entry_Type;
      begin
         Start_Search (Search, "subdir/parent", "");
         while More_Entries (Search) loop
            Get_Next_Entry (Search, Dir_Ent);
            Put_Line (" " & Simple_Name (Dir_Ent) & " " &
                      Boolean'Image (Is_Directory (Dir_Ent)));
         end loop;
         End_Search (Search);
      end;

      -- Set_Directory lets us change the current working directory
      Set_Directory ("subdir/parent/child");

      -- Now we'll see the contents of subdir/parent/child when listing
      -- the current directory
      Put_Line ("Listing subdir/parent/child");
      declare
         Search : Search_Type;
         Dir_Ent : Directory_Entry_Type;
      begin
         Start_Search (Search, ".", "");
         while More_Entries (Search) loop
            Get_Next_Entry (Search, Dir_Ent);
            Put_Line (" " & Simple_Name (Dir_Ent) & " " &
                      Boolean'Image (Is_Directory (Dir_Ent)));
         end loop;
         End_Search (Search);
      end;

      -- Change back to where we started
      Set_Directory ("../../..");

      -- We can also visit a directory recursively, including all its
      -- sub-directories. Walk_Tree accepts a callback procedure to handle
      -- every file or directory visited.
      Put_Line ("Visiting subdir");
      Walk_Tree ("subdir", "", Visit'Access);

   exception
      when others =>
         -- Ensure cleanup even if an exception occurs
         Delete_Tree ("subdir");
         raise;
   end;

   -- Clean up
   Delete_Tree ("subdir");
end Directories;

This Ada program demonstrates various operations with directories, mirroring the functionality of the original Go example. Here’s a breakdown of the key points:

  1. We use the Ada.Directories package for directory operations.

  2. The Check procedure is used for error handling, similar to the Go version.

  3. Create_Empty_File is a helper procedure to create empty files.

  4. Ada doesn’t have a built-in deferred execution mechanism like Go’s defer, so we use a block statement to ensure cleanup.

  5. The Search_Type and associated operations are used to list directory contents, similar to Go’s ReadDir.

  6. Set_Directory is used to change the current working directory, equivalent to Go’s os.Chdir.

  7. The Walk_Tree procedure is used for recursive directory traversal, similar to Go’s filepath.WalkDir.

  8. Exception handling is used to ensure cleanup even if an error occurs.

To run this program, save it as directories.adb and compile it with an Ada compiler like GNAT:

$ gnatmake directories.adb
$ ./directories

The output will be similar to the Go version, showing the created directory structure and the results of directory operations.