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

Commit

Permalink
add - Added getting a list of paths
Browse files Browse the repository at this point in the history
---

We've added getting list of paths for magic database file.

---

Type: add
Breaking: False
Doc Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Mar 25, 2024
1 parent 88ae1b1 commit 3dd8d6c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 10 additions & 6 deletions FileMagic.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static int Main(string[] args)
}

// Check for custom magic
string customMagic = "";
string customMagic = null;
if (args.Length > 1)
{
customMagic = args[1];
Expand All @@ -64,11 +64,15 @@ static int Main(string[] args)
// Now, analyze the file!
try
{
string normalMagic = MagicHandler.GetMagicInfo(path, customMagic);
string normalMimeInfo = MagicHandler.GetMagicMimeInfo(path, customMagic);
string normalMimeType = MagicHandler.GetMagicMimeType(path, customMagic);
string normalExtensions = MagicHandler.GetMagicCustomType(path, customMagic, MagicFlags.Extension);
string normalMimeEncoding = MagicHandler.GetMagicCustomType(path, customMagic, MagicFlags.MimeEncoding);
string[] magicPaths = MagicHandler.GetMagicPaths(customMagic);
TextWriterColor.WriteColor("Magic paths:", ConsoleColors.White);
ListWriterColor.WriteList(magicPaths, false);
TextWriterColor.WriteColor("File info:", ConsoleColors.White);
string normalMagic = MagicHandler.GetMagicInfo(path, magicPaths[0]);
string normalMimeInfo = MagicHandler.GetMagicMimeInfo(path, magicPaths[0]);
string normalMimeType = MagicHandler.GetMagicMimeType(path, magicPaths[0]);
string normalExtensions = MagicHandler.GetMagicCustomType(path, magicPaths[0], MagicFlags.Extension);
string normalMimeEncoding = MagicHandler.GetMagicCustomType(path, magicPaths[0], MagicFlags.MimeEncoding);
ListEntryWriterColor.WriteListEntry("File description", normalMagic);
ListEntryWriterColor.WriteListEntry("File MIME info", normalMimeInfo);
ListEntryWriterColor.WriteListEntry("File MIME type", normalMimeType);
Expand Down
14 changes: 14 additions & 0 deletions FileMagic/MagicHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

using FileMagic.Native;
using FileMagic.Native.Interop;
using SpecProbe.Platform;
using System;
using System.IO;
using System.Runtime.InteropServices;
Expand All @@ -33,6 +34,19 @@ public static class MagicHandler
private const string magicFileName = "magic.mgc";
private static readonly string magicPathDefault = Path.GetFullPath(magicFileName);

/// <summary>
/// Gets the file magic paths
/// </summary>
/// <param name="magicPath">Magic path. If null, the libmagic library tries to find the magic database files.</param>
/// <returns>A colon separated list of magic locations</returns>
public static string[] GetMagicPaths(string magicPath = null)
{
var pathsStringHandle = MagicHelper.magic_getpath(magicPath, 0);
string pathsString = Marshal.PtrToStringAnsi(pathsStringHandle);
string[] paths = PlatformHelper.IsOnWindows() ? [pathsString] : pathsString.Split(':');
return paths;
}

/// <summary>
/// Gets the file magic information
/// </summary>
Expand Down

0 comments on commit 3dd8d6c

Please sign in to comment.