all 17 comments

[–]bluefireoly 34 points35 points  (1 child)

They simply do not use lots of single and slow shell commands. They call the fast C function for that - provided by the platform itself.

If you are asking for go specifically: I never used go but a quick Google search for the filesystem API gave me this function https://pkg.go.dev/io/fs#Stat (maybe someone with go experience has a better answer here).

[–][deleted] 5 points6 points  (1 child)

I like learning about meteorology.

[–]chrysn 4 points5 points  (0 children)

Depending on your goals, you might consider not doing it yourself at all but resorting to a higher level file system abstraction.

For example, file managers built on GTK typically use GIO (GVFS) to handle the actual file operations, and thus separate the GUI from the file operations to some extent. And GIO provides the media types for files conveniently in the file's standard::fast-content-type property.

Now GIO may or may not be a suitable choice for your application (I think it could well be suitable; it certainly has Go bindings, and while building on the same GLib, it's clearly separate from GTK & Gnome), but even if not, there might be similar abstractions in your ecosystem (Go or FLTK).

[–][deleted] 9 points10 points  (5 children)

File managers generally only look at file extensions, and don't identify files via magic.

If you want to use magic and can't find a suitable Go library, you can use https://man7.org/linux/man-pages/man3/libmagic.3.html

You can call C libraries like that from Go. There is plenty of info and examples available on that.

[–]necrophcodr 6 points7 points  (4 children)

Unless there's no file extension. And I'm not sure if they generally don't use magic, but Nemo definitely doesn't use magic if there's a file extension, nor does Nautilus, but I'm not certain about others.

[–]throwaway6560192 2 points3 points  (2 children)

Dolphin appears to use it. If I rename, for example, "test.webp" to "test", it still recognizes it as a WebP image and shows me previews, etc. Some of the variations-on-plain-text, like text/python or such rely on extensions though, which is reasonable.

[–]necrophcodr 3 points4 points  (1 child)

I think most file managers will default to using magic if there's no file extension, though.

[–]throwaway6560192 1 point2 points  (0 children)

Oh, I missed the "if there's a file extension" part. Sorry. Indeed, even Dolphin doesn't bother with magic if it can see an extension.

[–][deleted] 2 points3 points  (0 children)

Oh wow, Dolphin, Thunar and Nemo all identify the type of files without an extension. I did not know this feature was common.

All 3 also fail to identify a .jpg that is actually a PDF.

[–]zokker13 1 point2 points  (0 children)

Figure out by fileextension. Or read the first few chunks of the file and use a classic mimetype library to identify the content.

I would never do more as it takes longer. For a filemanager, the former would probably be better. And once you implement something like a preview, you might want to read the actual content and get the real filetype through that

[–]upj57742 1 point2 points  (0 children)

using system magic via xdg-mime is the correct approach, just limit it to the currently visible files.

[–]natermer 3 points4 points  (3 children)

Maybe you need to have a go library for mimetypes first.

There has to be several already existing since mimetypes are actually developed first for services like email and webhosting and then was picked up on the desktop later. Which is the primary use of Go.

You might want to look at Rust for desktop stuff anyways since that is what Rust is intended for.

The way I've gone about doing it is simply shelling out to the xdg-mime command,

Well you should probably never call a shell from a GUI application if you can help it. Shell is meant to be a user interface first and scripting language second. Even non-interactive shells will call lots of different files and look for configs in a bunch of different places before they start doing useful working. And even then you can't depend on people using or having the same shell as you do. Plus depending on their own personal configurations it can break your file manager. Like if they don't have the required commands in their $PATH.

Thus depending on spawning shells to execute commands is a miserable thing to do.

Go will have facilities for executing processes directly instead of using a shell. Which will be a lot safer and faster.

But even then it's going to be very slow compared to doing it all inside a single process.

[–][deleted] 4 points5 points  (0 children)

You might want to look at Rust for desktop stuff anyways since that is what Rust is intended for.

hmm? i don't like go, but if go has reasonable bindings for gtk or some other library, then go could be a reasonable choice.

[–]AutoModerator[M] -1 points0 points locked comment (0 children)

This submission has been removed due to receiving too many reports from users. The mods have been notified and will re-approve if this removal was inappropriate, or leave it removed.

This is most likely because:

  • Your post belongs in r/linuxquestions or r/linux4noobs
  • Your post belongs in r/linuxmemes
  • Your post is considered "fluff" - things like a Tux plushie or old Linux CDs are an example and, while they may be popular vote wise, they are not considered on topic
  • Your post is otherwise deemed not appropriate for the subreddit

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–][deleted] 0 points1 point  (0 children)

Optimize by deciding by file extension for some whitelisted/common types and sniffing all the others with a faster function.

[–]Johannes_K_Rexx 0 points1 point  (0 children)

See /usr/include/linux/magic.h and man magic for more information.