Skip to content
This repository has been archived by the owner on May 19, 2024. It is now read-only.

Commit

Permalink
imp - GetMagicPaths() should not return garbage strings
Browse files Browse the repository at this point in the history
---

GetMagicPaths() now returns just meaningful strings that contain paths to the magic database.

---

Type: imp
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 1, 2024
1 parent 0933ef5 commit 74f6ea0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
1 change: 1 addition & 0 deletions FileMagic.Console/FileMagic.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<OutputPath>../FileMagic.Console.Bin/</OutputPath>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
48 changes: 34 additions & 14 deletions FileMagic.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using FileMagic.Native.Interop;
using System;
using System.IO;
using System.Linq;
using Terminaux.Colors.Data;
using Terminaux.Writer.ConsoleWriters;

Expand All @@ -29,6 +30,10 @@ internal class Program
{
static int Main(string[] args)
{
bool systemwide = args.Contains("-s");
bool verbose = args.Contains("-v");
args = args.Except(["-s", "-v"]).ToArray();

// Check the arguments
if (args.Length == 0)
{
Expand Down Expand Up @@ -64,23 +69,38 @@ static int Main(string[] args)
// Now, analyze the file!
try
{
TextWriterColor.WriteColor($"libmagic version {MagicHandler.MagicVersionId}", ConsoleColors.Green);
string[] magicPaths = MagicHandler.GetMagicPaths(customMagic);
TextWriterColor.WriteColor("Magic paths:", ConsoleColors.White);
ListWriterColor.WriteList(magicPaths, false);
string finalPath = File.Exists(magicPaths[0]) ? magicPaths[0] : customMagic;
ListEntryWriterColor.WriteListEntry("Final path", finalPath);
TextWriterColor.WriteColor("File info:", ConsoleColors.White);
string[] magicPathsSystemwide = MagicHandler.GetMagicPaths(customMagic, true);
string[] magicPathsProbed = MagicHandler.GetMagicPaths(null);
string[] magicPathsProbedSystemwide = MagicHandler.GetMagicPaths(null, true);
string finalPath = systemwide ? magicPathsProbedSystemwide[0] : File.Exists(magicPaths[0]) ? magicPaths[0] : File.Exists(customMagic) ? customMagic : "";
if (verbose)
{
TextWriterColor.WriteColor($"libmagic version {MagicHandler.MagicVersionId}", ConsoleColors.Green);
TextWriterColor.WriteColor("Magic paths:", ConsoleColors.White);
ListWriterColor.WriteList(magicPaths, false);
TextWriterColor.WriteColor("Magic paths (systemwide):", ConsoleColors.White);
ListWriterColor.WriteList(magicPathsSystemwide, false);
TextWriterColor.WriteColor("Magic paths (probed):", ConsoleColors.White);
ListWriterColor.WriteList(magicPathsProbed, false);
TextWriterColor.WriteColor("Magic paths (probed, systemwide):", ConsoleColors.White);
ListWriterColor.WriteList(magicPathsProbedSystemwide, false);
ListEntryWriterColor.WriteListEntry("Final path", string.IsNullOrEmpty(finalPath) ? "default path" : finalPath);
TextWriterColor.WriteColor("File info:", ConsoleColors.White);
}
string normalMagic = MagicHandler.GetMagicInfo(path, finalPath);
string normalMimeInfo = MagicHandler.GetMagicMimeInfo(path, finalPath);
string normalMimeType = MagicHandler.GetMagicMimeType(path, finalPath);
string normalExtensions = MagicHandler.GetMagicCustomType(path, finalPath, MagicFlags.Extension);
string normalMimeEncoding = MagicHandler.GetMagicCustomType(path, finalPath, MagicFlags.MimeEncoding);
ListEntryWriterColor.WriteListEntry("File description", normalMagic);
ListEntryWriterColor.WriteListEntry("File MIME info", normalMimeInfo);
ListEntryWriterColor.WriteListEntry("File MIME type", normalMimeType);
ListEntryWriterColor.WriteListEntry("File MIME encoding", normalMimeEncoding);
ListEntryWriterColor.WriteListEntry("Alternative extensions", normalExtensions);
if (verbose)
{
string normalMimeInfo = MagicHandler.GetMagicMimeInfo(path, finalPath);
string normalMimeType = MagicHandler.GetMagicMimeType(path, finalPath);
string normalExtensions = MagicHandler.GetMagicCustomType(path, finalPath, MagicFlags.Extension);
string normalMimeEncoding = MagicHandler.GetMagicCustomType(path, finalPath, MagicFlags.MimeEncoding);
ListEntryWriterColor.WriteListEntry("File MIME info", normalMimeInfo);
ListEntryWriterColor.WriteListEntry("File MIME type", normalMimeType);
ListEntryWriterColor.WriteListEntry("File MIME encoding", normalMimeEncoding);
ListEntryWriterColor.WriteListEntry("Alternative extensions", normalExtensions);
}
}
catch (Exception ex)
{
Expand Down
14 changes: 13 additions & 1 deletion FileMagic.Console/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@
"FileMagic.Console": {
"commandName": "Project",
"commandLineArgs": "FileMagic.dll"
},
"FileMagic.Console - systemwide": {
"commandName": "Project",
"commandLineArgs": "FileMagic.dll -s"
},
"FileMagic.Console - verbose": {
"commandName": "Project",
"commandLineArgs": "FileMagic.dll -v"
},
"FileMagic.Console - verbose, systemwide": {
"commandName": "Project",
"commandLineArgs": "FileMagic.dll -s -v"
}
}
}
}
2 changes: 1 addition & 1 deletion FileMagic.Native/Interop/MagicHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal static unsafe class MagicHelper
/// file_public const char *magic_getpath(const char *magicfile, int action)
/// </summary>
[DllImport("libmagic")]
public static extern IntPtr magic_getpath([In][MarshalAs(UnmanagedType.LPStr)] string magicfile, int action);
public static extern IntPtr magic_getpath(IntPtr magicfile, int action);

/// <summary>
/// file_public const char *magic_file(struct magic_set *ms, const char *inname)
Expand Down
14 changes: 12 additions & 2 deletions FileMagic/MagicHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ public static class MagicHandler
/// Gets the file magic paths
/// </summary>
/// <param name="magicPath">Magic path. If null, the libmagic library tries to find the magic database files.</param>
/// <param name="systemWide">Uses system-wide magic path if <paramref name="magicPath"/> is null. This has no effect if that path is not null</param>
/// <returns>A colon separated list of magic locations</returns>
public static string[] GetMagicPaths(string magicPath = null)
public static string[] GetMagicPaths(string magicPath = null, bool systemWide = false)
{
var pathsStringHandle = MagicHelper.magic_getpath(magicPath, 0);
// We need to make another magicPath handle, because if we directly passed the magicPath string, we'll get corrupt
// string. We need to make a native handle to our string.
var magicPathHandle = !string.IsNullOrEmpty(magicPath) ? Marshal.StringToHGlobalAnsi(magicPath) : IntPtr.Zero;
var pathsStringHandle = MagicHelper.magic_getpath(magicPathHandle, systemWide ? 1 : 0);
string pathsString = Marshal.PtrToStringAnsi(pathsStringHandle);

// Dispose of our magic path handle
if (magicPathHandle != IntPtr.Zero)
Marshal.FreeHGlobal(magicPathHandle);

// Now, separate the paths by colon. If on Windows, it'll never return more than one path.
string[] paths = PlatformHelper.IsOnWindows() ? [pathsString] : pathsString.Split(':');
return paths;
}
Expand Down

0 comments on commit 74f6ea0

Please sign in to comment.