diff --git a/docs/changelog.rst b/docs/changelog.rst index fa08cbc4e..cabf88435 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -219,6 +219,14 @@ Detailed list of changes with momentum, and a finger held still before it moves selects text (:iss:`5432`, :pull:`10551`) +- Linux/BSD: Detect MIME types for :doc:`open actions ` and + kittens using the fast binary shared-mime-info cache + (:file:`mime/mime.cache`), matching the MIME types used by the rest of the + desktop. Lookups are faster and also match file names without an extension, + such as :file:`Makefile`, and case-sensitive patterns. MIME types in open + actions and the choose-files kitten also match the aliases of the detected + MIME type, for example, ``text/x-sh`` matches shell scripts (:iss:`3056`) + - Fix a space immediately after a tab stop being dropped when the screen contents are copied or sent to the scrollback pager (:iss:`10609`) diff --git a/docs/open_actions.rst b/docs/open_actions.rst index 2fa544608..90c045efd 100644 --- a/docs/open_actions.rst +++ b/docs/open_actions.rst @@ -113,8 +113,9 @@ lines. The various available criteria are: Useful if your system MIME database does not have definitions you need. This file is in the standard format of one definition per line, like: ``text/plain rst md``. Note that the MIME type for directories is - ``inode/directory``. MIME types are detected based on file extension, not - file contents. + ``inode/directory``. MIME types are detected based on file name, not + file contents. On Linux and BSD the system shared-mime-info database + (:file:`mime/mime.cache` in the XDG data directories) is used. ``ext`` A comma separated list of file extensions, for example: ``jpeg, tar.gz`` diff --git a/kittens/choose_files/filters.go b/kittens/choose_files/filters.go index f08cb3812..d2e0fe0fa 100644 --- a/kittens/choose_files/filters.go +++ b/kittens/choose_files/filters.go @@ -45,8 +45,12 @@ func NewFilter(spec string) (*Filter, error) { if mime == "" { return false } - m, _ := filepath.Match(ans.Pattern, mime) - return m + for _, q := range utils.MimeTypeAliases(mime) { + if m, _ := filepath.Match(ans.Pattern, q); m { + return true + } + } + return false } default: return nil, fmt.Errorf("%#v is not a valid filter type", ans.Type) diff --git a/kitty/data-types.c b/kitty/data-types.c index a7e2a1afa..5b416e264 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -915,6 +915,7 @@ extern bool init_png_reader(PyObject *module); extern bool init_utmp(PyObject *module); extern bool init_loop_utils(PyObject *module); extern bool init_systemd_module(PyObject *module); +extern bool init_mime_cache(PyObject *module); #ifdef __APPLE__ extern int init_CoreText(PyObject *); extern bool init_cocoa(PyObject *module); @@ -980,6 +981,7 @@ PyInit_fast_data_types(void) { if (!init_loop_utils(m)) return NULL; if (!init_crypto_library(m)) return NULL; if (!init_systemd_module(m)) return NULL; + if (!init_mime_cache(m)) return NULL; if (!init_animations(m)) return NULL; CellAttrs a; diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 0831e6b5f..122bd5c1e 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -1585,6 +1585,9 @@ def monotonic() -> float: ... def timed_debug_print(x: str) -> None: ... def gpu_driver_version_string() -> str: ... def systemd_move_pid_into_new_scope(pid: int, scope_name: str, description: str) -> str: ... +def mime_type_for_filename(path: str) -> str | None: ... +def mime_type_aliases(mime: str) -> tuple[str, ...]: ... +def clear_mime_cache_data() -> None: ... def play_desktop_sound_async(name: str, event_id: str = 'test sound', is_path: bool = False, theme_name: str = '') -> str: ... def cocoa_play_system_sound_by_id_async(sound_id: int) -> None: ... def glfw_get_system_color_theme(query_if_unintialized: bool = True) -> Literal['light', 'dark', 'no_preference']: ... diff --git a/kitty/guess_mime_type.py b/kitty/guess_mime_type.py index a10717114..51f582e65 100644 --- a/kitty/guess_mime_type.py +++ b/kitty/guess_mime_type.py @@ -66,16 +66,46 @@ def initialize_mime_database() -> None: from mimetypes import init init(None) - from kitty.constants import config_dir - local_defs = os.path.join(config_dir, 'mime.types') - if os.path.exists(local_defs): - init((local_defs,)) + +def user_mime_map() -> dict[str, str]: + ans: dict[str, str] | None = getattr(user_mime_map, 'ans', None) + if ans is None: + from mimetypes import read_mime_types + + from kitty.constants import config_dir + + ans = read_mime_types(os.path.join(config_dir, 'mime.types')) or {} + setattr(user_mime_map, 'ans', ans) + return ans def clear_mime_cache() -> None: if hasattr(initialize_mime_database, 'inited'): delattr(initialize_mime_database, 'inited') + if hasattr(user_mime_map, 'ans'): + delattr(user_mime_map, 'ans') + from kitty.fast_data_types import clear_mime_cache_data + + clear_mime_cache_data() + + +def guess_type_from_user_definitions(path: str) -> str | None: + umap = user_mime_map() + if not umap: + return None + ext = os.path.splitext(path)[1] + return umap.get(ext) or umap.get(ext.lower()) + + +def guess_type_from_stdlib(path: str) -> str | None: + from mimetypes import guess_type as stdlib_guess_type + + initialize_mime_database() + try: + return stdlib_guess_type(path)[0] + except Exception: + return None def guess_type(path: str, allow_filesystem_access: bool = False) -> str | None: @@ -89,17 +119,16 @@ def guess_type(path: str, allow_filesystem_access: bool = False) -> str | None: if is_dir: return 'inode/directory' - from mimetypes import guess_type as stdlib_guess_type + from kitty.fast_data_types import mime_type_for_filename - initialize_mime_database() - mt = None - with suppress(Exception): - mt = stdlib_guess_type(path)[0] + # The stdlib database is only loaded if the faster shared-mime-info cache + # lookup fails, as loading it requires parsing various files + mt = guess_type_from_user_definitions(path) or mime_type_for_filename(path) or guess_type_from_stdlib(path) if not mt: ext = path.rpartition('.')[-1].lower() mt = known_extensions.get(ext) - if mt in text_mimes: - mt = f'text/{mt.split("/", 1)[-1]}' + if mt: + mt = textual_mime_type(mt) mt = mt or is_special_file(path) if not mt: if is_dir: @@ -107,3 +136,33 @@ def guess_type(path: str, allow_filesystem_access: bool = False) -> str | None: elif is_exe: mt = 'inode/executable' return mt + + +def textual_mime_type(mt: str) -> str: + return f'text/{mt.split("/", 1)[-1]}' if mt in text_mimes else mt + + +def mime_types_for_matching(path: str, mt: str) -> set[str]: + """ + Return all names that the MIME type mt, as returned by guess_type() for path, + is known by. This includes the aliases from the shared-mime-info database + as well as the names that were returned by older versions of kitty, so + that matching against user specified MIME types keeps working. + """ + from kitty.fast_data_types import mime_type_aliases + + queries = {mt} + if mt.startswith('text/'): + # undo the conversion of textual application/* types done by guess_type() + orig = f'application/{mt[5:]}' + if orig in text_mimes: + queries.add(orig) + ans: set[str] = set() + for q in queries: + for x in mime_type_aliases(q): + ans.add(x) + ans.add(textual_mime_type(x)) + legacy = known_extensions.get(path.rpartition('.')[-1].lower()) or is_special_file(path) + if legacy: + ans.add(legacy) + return ans diff --git a/kitty/mime-cache.c b/kitty/mime-cache.c new file mode 100644 index 000000000..d89914417 --- /dev/null +++ b/kitty/mime-cache.c @@ -0,0 +1,543 @@ +/* + * mime-cache.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Fast lookup of MIME types for file names using the binary mime.cache files +// generated by update-mime-database from shared-mime-info. The lookup +// algorithm mirrors the one used by xdgmime (GLib/GIO). See +// https://specifications.freedesktop.org/shared-mime-info-spec/latest/ar01s02.html + +#include "data-types.h" +#include "monotonic.h" + +#ifdef __APPLE__ + +static PyObject * +mime_type_for_filename(PyObject *self UNUSED, PyObject *name UNUSED) { + Py_RETURN_NONE; +} + +static PyObject * +mime_type_aliases(PyObject *self UNUSED, PyObject *mime) { + if (!PyUnicode_Check(mime)) { + PyErr_SetString(PyExc_TypeError, "mime must be a string"); + return NULL; + } + return PyTuple_Pack(1, mime); +} + +static PyObject * +clear_mime_cache_data(PyObject *self UNUSED, PyObject *args UNUSED) { + Py_RETURN_NONE; +} + +#else + +#include +#include +#include +#include +#include +#include "safe-wrappers.h" + +#define CASE_SENSITIVE_FLAG 0x100 +#define WEIGHT_MASK 0xff +#define MAX_MATCHES 16 +#define MAX_SUFFIX_TREE_DEPTH 256 +#define HEADER_SIZE 40 +#define ALIAS_LIST_OFFSET_POS 4 +#define LITERAL_LIST_OFFSET_POS 12 +#define REVERSE_SUFFIX_TREE_OFFSET_POS 16 +#define GLOB_LIST_OFFSET_POS 20 + +typedef struct { + char *path; + const uint8_t *data; + size_t sz; + bool exists; + dev_t dev; + ino_t ino; + off_t size; + struct timespec mtime; +} MimeCache; + +typedef struct { + const char *mime; + uint32_t weight; +} Match; + +typedef struct { + Match items[MAX_MATCHES]; + unsigned count; +} Matches; + +static struct { + MimeCache *items; + size_t count; + bool loaded; + monotonic_t last_check_at; +} caches = {0}; + +// Reading data {{{ +static inline bool +read_u32(const MimeCache *c, size_t offset, uint32_t *ans) { + if (offset > c->sz || c->sz - offset < 4) return false; + const uint8_t *p = c->data + offset; + *ans = ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | (uint32_t)p[3]; + return true; +} + +static inline const char * +read_string(const MimeCache *c, uint32_t offset) { + if (offset >= c->sz || !memchr(c->data + offset, 0, c->sz - offset)) return NULL; + return (const char *)c->data + offset; +} + +static inline bool +read_entry(const MimeCache *c, size_t offset, uint32_t *a, uint32_t *b, uint32_t *weight) { + return read_u32(c, offset, a) && read_u32(c, offset + 4, b) && read_u32(c, offset + 8, weight); +} + +static void +add_match(const MimeCache *c, Matches *m, uint32_t mime_offset, uint32_t weight) { + if (m->count >= MAX_MATCHES) return; + const char *mime = read_string(c, mime_offset); + if (!mime) return; + m->items[m->count].mime = mime; + m->items[m->count++].weight = weight & WEIGHT_MASK; +} +// }}} + +// Lookups {{{ +static bool +lookup_literal(const MimeCache *c, const char *name, bool case_sensitive_check, Matches *ans) { + uint32_t list_offset, n; + if (!read_u32(c, LITERAL_LIST_OFFSET_POS, &list_offset) || !read_u32(c, list_offset, &n)) return false; + size_t lo = 0, hi = n; + while (lo < hi) { + size_t mid = lo + (hi - lo) / 2; + uint32_t literal_offset, mime_offset, weight; + if (!read_entry(c, (size_t)list_offset + 4 + 12 * mid, &literal_offset, &mime_offset, &weight)) return false; + const char *literal = read_string(c, literal_offset); + if (!literal) return false; + int cmp = strcmp(literal, name); + if (cmp < 0) lo = mid + 1; + else if (cmp > 0) hi = mid; + else { + if (!case_sensitive_check && (weight & CASE_SENSITIVE_FLAG)) return false; + unsigned before = ans->count; + add_match(c, ans, mime_offset, weight); + return ans->count > before; + } + } + return false; +} + +static void +lookup_suffix_node( + const MimeCache *c, uint32_t num_nodes, uint32_t offset, const char_type *name, size_t len, bool case_sensitive_check, Matches *ans, unsigned depth) { + if (!len || depth > MAX_SUFFIX_TREE_DEPTH) return; + const char_type ch = name[len - 1]; + size_t lo = 0, hi = num_nodes; + while (lo < hi) { + size_t mid = lo + (hi - lo) / 2; + uint32_t node_char, num_children, first_child_offset; + if (!read_entry(c, (size_t)offset + 12 * mid, &node_char, &num_children, &first_child_offset)) return; + if (ch < node_char) hi = mid; + else if (ch > node_char) lo = mid + 1; + else { + const unsigned before = ans->count; + lookup_suffix_node(c, num_children, first_child_offset, name, len - 1, case_sensitive_check, ans, depth + 1); + if (ans->count == before) { + // No longer suffix matched, use the leaf nodes of this node, + // which are sorted before all other children. + for (uint32_t i = 0; i < num_children; i++) { + uint32_t leaf_char, mime_offset, weight; + if (!read_entry(c, (size_t)first_child_offset + 12 * (size_t)i, &leaf_char, &mime_offset, &weight) || leaf_char) break; + if (case_sensitive_check || !(weight & CASE_SENSITIVE_FLAG)) add_match(c, ans, mime_offset, weight); + } + } + return; + } + } +} + +static void +lookup_suffix(const MimeCache *c, const char_type *name, size_t len, bool case_sensitive_check, Matches *ans) { + uint32_t tree_offset, num_roots, first_root_offset; + if (!read_u32(c, REVERSE_SUFFIX_TREE_OFFSET_POS, &tree_offset) || !read_u32(c, tree_offset, &num_roots) || + !read_u32(c, (size_t)tree_offset + 4, &first_root_offset)) + return; + lookup_suffix_node(c, num_roots, first_root_offset, name, len, case_sensitive_check, ans, 0); +} + +static void +lookup_glob(const MimeCache *c, const char *name, bool case_sensitive_check, Matches *ans) { + uint32_t list_offset, n; + if (!read_u32(c, GLOB_LIST_OFFSET_POS, &list_offset) || !read_u32(c, list_offset, &n)) return; + for (size_t i = 0; i < n && ans->count < MAX_MATCHES; i++) { + uint32_t glob_offset, mime_offset, weight; + if (!read_entry(c, (size_t)list_offset + 4 + 12 * i, &glob_offset, &mime_offset, &weight)) return; + if (!case_sensitive_check && (weight & CASE_SENSITIVE_FLAG)) continue; + const char *glob = read_string(c, glob_offset); + if (glob && fnmatch(glob, name, 0) == 0) add_match(c, ans, mime_offset, weight); + } +} + +typedef struct { + const char *utf8; + const char_type *ucs4; + size_t ucs4_len; +} FileName; + +static const char * +lookup(const FileName *name, const FileName *lowercased) { + Matches m = {0}; + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data && lookup_literal(caches.items + i, name->utf8, true, &m)) return m.items[0].mime; + } + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data && lookup_literal(caches.items + i, lowercased->utf8, false, &m)) return m.items[0].mime; + } + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data) lookup_suffix(caches.items + i, lowercased->ucs4, lowercased->ucs4_len, false, &m); + } + if (m.count < 2) { + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data) lookup_suffix(caches.items + i, name->ucs4, name->ucs4_len, true, &m); + } + } + if (!m.count) { + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data) lookup_glob(caches.items + i, name->utf8, true, &m); + } + } + if (!m.count) { + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data) lookup_glob(caches.items + i, lowercased->utf8, false, &m); + } + } + if (!m.count) return NULL; + // Highest weight wins, ties go to the earliest match, which means the + // longest suffix and the highest priority cache file. + const Match *best = m.items; + for (unsigned i = 1; i < m.count; i++) { + if (m.items[i].weight > best->weight) best = m.items + i; + } + return best->mime; +} + +static bool +read_alias_list(const MimeCache *c, uint32_t *list_offset, uint32_t *n) { + return read_u32(c, ALIAS_LIST_OFFSET_POS, list_offset) && read_u32(c, *list_offset, n); +} + +static bool +read_alias_entry(const MimeCache *c, uint32_t list_offset, size_t idx, const char **alias, const char **mime) { + uint32_t alias_offset, mime_offset; + const size_t offset = (size_t)list_offset + 4 + 8 * idx; + if (!read_u32(c, offset, &alias_offset) || !read_u32(c, offset + 4, &mime_offset)) return false; + *alias = read_string(c, alias_offset); + *mime = read_string(c, mime_offset); + return *alias && *mime; +} + +// Returns the canonical MIME type for the specified alias or NULL if mime is +// not an alias. The alias list is sorted by alias. +static const char * +canonical_mime_type(const MimeCache *c, const char *mime) { + uint32_t list_offset, n; + if (!read_alias_list(c, &list_offset, &n)) return NULL; + size_t lo = 0, hi = n; + while (lo < hi) { + size_t mid = lo + (hi - lo) / 2; + const char *alias, *canonical; + if (!read_alias_entry(c, list_offset, mid, &alias, &canonical)) return NULL; + int cmp = strcmp(alias, mime); + if (cmp < 0) lo = mid + 1; + else if (cmp > 0) hi = mid; + else return canonical; + } + return NULL; +} + +static bool +add_unique_string(PyObject *list, const char *val) { + RAII_PyObject(s, PyUnicode_FromString(val)); + if (!s) return false; + int ret = PySequence_Contains(list, s); + if (ret < 0) return false; + return ret == 1 || PyList_Append(list, s) == 0; +} + +static bool +add_aliases(const MimeCache *c, const char *canonical, PyObject *ans) { + uint32_t list_offset, n; + if (!read_alias_list(c, &list_offset, &n)) return true; + for (size_t i = 0; i < n; i++) { + const char *alias, *mime; + if (!read_alias_entry(c, list_offset, i, &alias, &mime)) break; + if (strcmp(mime, canonical) == 0 && !add_unique_string(ans, alias)) return false; + } + return true; +} +// }}} + +// Loading {{{ +static void +unmap_cache(MimeCache *c) { + if (c->data) munmap((void *)c->data, c->sz); + c->data = NULL; + c->sz = 0; +} + +static bool +stat_cache(const MimeCache *c, struct stat *st) { + return stat(c->path, st) == 0 && S_ISREG(st->st_mode); +} + +static bool +cache_changed(const MimeCache *c) { + struct stat st; + if (!stat_cache(c, &st)) return c->exists; + return !c->exists || st.st_dev != c->dev || st.st_ino != c->ino || st.st_size != c->size || st.st_mtim.tv_sec != c->mtime.tv_sec || + st.st_mtim.tv_nsec != c->mtime.tv_nsec; +} + +static void +map_cache(MimeCache *c) { + unmap_cache(c); + c->exists = false; + int fd = safe_open(c->path, O_RDONLY | O_CLOEXEC, 0); + if (fd < 0) { + if (errno != ENOENT && errno != ENOTDIR) log_error("Failed to open MIME cache file %s with error: %s", c->path, strerror(errno)); + return; + } + struct stat st; + if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { + safe_close(fd, __FILE__, __LINE__); + return; + } + c->exists = true; + c->dev = st.st_dev; + c->ino = st.st_ino; + c->size = st.st_size; + c->mtime = st.st_mtim; + if (st.st_size < HEADER_SIZE) { + safe_close(fd, __FILE__, __LINE__); + log_error("The MIME cache file %s is too small, ignoring it", c->path); + return; + } + void *addr = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); + safe_close(fd, __FILE__, __LINE__); + if (addr == MAP_FAILED) { + log_error("Failed to mmap MIME cache file %s with error: %s", c->path, strerror(errno)); + return; + } + const uint8_t *p = addr; + const unsigned major = (p[0] << 8) | p[1], minor = (p[2] << 8) | p[3]; + if (major != 1 || minor < 1) { + log_error("The MIME cache file %s has unsupported version %u.%u, ignoring it", c->path, major, minor); + munmap(addr, (size_t)st.st_size); + return; + } + c->data = p; + c->sz = (size_t)st.st_size; +} + +static void +free_caches(void) { + for (size_t i = 0; i < caches.count; i++) { + unmap_cache(caches.items + i); + free(caches.items[i].path); + } + free(caches.items); + zero_at_ptr(&caches); +} + +static bool +add_cache_path(const char *data_dir, size_t len) { + if (!len || data_dir[0] != '/') return true; // relative paths are ignored as per the XDG spec + static const char suffix[] = "/mime/mime.cache"; + char *path = malloc(len + sizeof(suffix)); + if (!path) return false; + memcpy(path, data_dir, len); + memcpy(path + len, suffix, sizeof(suffix)); + for (size_t i = 0; i < caches.count; i++) { + if (strcmp(caches.items[i].path, path) == 0) { + free(path); + return true; + } + } + MimeCache *items = realloc(caches.items, sizeof(caches.items[0]) * (caches.count + 1)); + if (!items) { + free(path); + return false; + } + caches.items = items; + zero_at_ptr(caches.items + caches.count); + caches.items[caches.count++].path = path; + return true; +} + +static bool +find_cache_paths(void) { + // Cache files are in order of decreasing priority + const char *data_home = getenv("XDG_DATA_HOME"); + if (data_home && data_home[0]) { + if (!add_cache_path(data_home, strlen(data_home))) return false; + } else { + const char *home = getenv("HOME"); + if (home && home[0]) { + static const char rel[] = "/.local/share"; + const size_t hl = strlen(home); + RAII_ALLOC(char, buf, malloc(hl + sizeof(rel))); + if (!buf) return false; + memcpy(buf, home, hl); + memcpy(buf + hl, rel, sizeof(rel)); + if (!add_cache_path(buf, hl + sizeof(rel) - 1)) return false; + } + } + const char *data_dirs = getenv("XDG_DATA_DIRS"); + if (!data_dirs || !data_dirs[0]) data_dirs = "/usr/local/share/:/usr/share/"; + for (const char *p = data_dirs; *p;) { + const char *end = strchr(p, ':'); + size_t len = end ? (size_t)(end - p) : strlen(p); + while (len > 1 && p[len - 1] == '/') len--; + if (!add_cache_path(p, len)) return false; + if (!end) break; + p = end + 1; + } + return true; +} + +static bool +ensure_caches_loaded(void) { + const monotonic_t now = monotonic(); + if (caches.loaded) { + // Check for updated cache files at most once every few seconds, like xdgmime + if (now - caches.last_check_at < s_to_monotonic_t(5)) return true; + caches.last_check_at = now; + for (size_t i = 0; i < caches.count; i++) { + if (cache_changed(caches.items + i)) map_cache(caches.items + i); + } + return true; + } + free_caches(); + if (!find_cache_paths()) { + free_caches(); + PyErr_NoMemory(); + return false; + } + for (size_t i = 0; i < caches.count; i++) map_cache(caches.items + i); + caches.loaded = true; + caches.last_check_at = now; + return true; +} +// }}} + +// Python API {{{ +static bool +filename_from_python(PyObject *name, FileName *ans, PyObject **utf8_holder, Py_UCS4 **ucs4_holder) { + // Use surrogateescape so that file names that are not valid UTF-8 round trip to their original bytes + *utf8_holder = PyUnicode_AsEncodedString(name, "utf-8", "surrogateescape"); + if (!*utf8_holder) return false; + *ucs4_holder = PyUnicode_AsUCS4Copy(name); + if (!*ucs4_holder) return false; + ans->utf8 = PyBytes_AS_STRING(*utf8_holder); + ans->ucs4 = *ucs4_holder; + ans->ucs4_len = (size_t)PyUnicode_GET_LENGTH(name); + return true; +} + +static void +cleanup_ucs4(Py_UCS4 **p) { + PyMem_Free(*p); + *p = NULL; +} +#define RAII_UCS4(name) __attribute__((cleanup(cleanup_ucs4))) Py_UCS4 *name = NULL + +static PyObject * +mime_type_for_filename(PyObject *self UNUSED, PyObject *path) { + if (!PyUnicode_Check(path)) { + PyErr_SetString(PyExc_TypeError, "path must be a string"); + return NULL; + } + const Py_ssize_t path_len = PyUnicode_GET_LENGTH(path); + const Py_ssize_t slash = PyUnicode_FindChar(path, '/', 0, path_len, -1); + if (slash == -2) return NULL; + RAII_PyObject(name, PyUnicode_Substring(path, slash + 1, path_len)); + if (!name) return NULL; + const Py_ssize_t name_len = PyUnicode_GET_LENGTH(name); + if (!name_len) Py_RETURN_NONE; + const Py_ssize_t nul = PyUnicode_FindChar(name, 0, 0, name_len, 1); + if (nul == -2) return NULL; + if (nul != -1) Py_RETURN_NONE; // embedded NUL cannot be part of a valid file name + RAII_PyObject(lowercased, PyObject_CallMethod(name, "lower", NULL)); + if (!lowercased) return NULL; + if (!ensure_caches_loaded()) return NULL; + if (!caches.count) Py_RETURN_NONE; + RAII_PyObject(utf8, NULL); + RAII_PyObject(lutf8, NULL); + RAII_UCS4(ucs4); + RAII_UCS4(lucs4); + FileName fname, lname; + if (!filename_from_python(name, &fname, &utf8, &ucs4) || !filename_from_python(lowercased, &lname, &lutf8, &lucs4)) return NULL; + const char *ans = lookup(&fname, &lname); + if (!ans) Py_RETURN_NONE; + return PyUnicode_FromString(ans); +} + +static PyObject * +mime_type_aliases(PyObject *self UNUSED, PyObject *mime) { + if (!PyUnicode_Check(mime)) { + PyErr_SetString(PyExc_TypeError, "mime must be a string"); + return NULL; + } + const char *q = PyUnicode_AsUTF8(mime); + if (!q) return NULL; + if (!ensure_caches_loaded()) return NULL; + const char *canonical = NULL; + for (size_t i = 0; i < caches.count && !canonical; i++) { + if (caches.items[i].data) canonical = canonical_mime_type(caches.items + i, q); + } + if (!canonical) canonical = q; + RAII_PyObject(ans, PyList_New(0)); + if (!ans || !add_unique_string(ans, canonical)) return NULL; + for (size_t i = 0; i < caches.count; i++) { + if (caches.items[i].data && !add_aliases(caches.items + i, canonical, ans)) return NULL; + } + // Ensure the queried name is always present, even if a cache entry is corrupt + if (!add_unique_string(ans, q)) return NULL; + return PyList_AsTuple(ans); +} + +static PyObject * +clear_mime_cache_data(PyObject *self UNUSED, PyObject *args UNUSED) { + free_caches(); + Py_RETURN_NONE; +} +// }}} + +#endif + +static PyMethodDef methods[] = { + {"mime_type_for_filename", + mime_type_for_filename, + METH_O, + "Return the MIME type for the specified file name based on the shared-mime-info mime.cache files or None if no match is found"}, + {"mime_type_aliases", + mime_type_aliases, + METH_O, + "Return all names for the specified MIME type, starting with its canonical name, followed by all its aliases"}, + {"clear_mime_cache_data", clear_mime_cache_data, METH_NOARGS, "Release all loaded MIME cache data, it will be re-loaded on next use"}, + {NULL, NULL, 0, NULL}, +}; + +bool +init_mime_cache(PyObject *module) { + return PyModule_AddFunctions(module, methods) == 0; +} diff --git a/kitty/open_actions.py b/kitty/open_actions.py index ef0fc22d0..ff6c1b3c9 100644 --- a/kitty/open_actions.py +++ b/kitty/open_actions.py @@ -13,7 +13,7 @@ from urllib.parse import ParseResult, unquote, urlparse from .conf.utils import KeyAction, to_cmdline_implementation from .constants import config_dir from .fast_data_types import get_options -from .guess_mime_type import guess_type +from .guess_mime_type import guess_type, mime_types_for_matching from .options.utils import ActionAlias, MapType, resolve_aliases_and_parse_actions from .types import run_once from .typing_compat import MatchType @@ -95,11 +95,11 @@ def url_matches_criterion(purl: 'ParseResult', url: str, unquoted_path: str, mc: mt = guess_type(unquoted_path, allow_filesystem_access=purl.scheme in ('', 'file')) if not mt: return False - mt = mt.lower() + mts = {x.lower() for x in mime_types_for_matching(unquoted_path, mt)} for mpat in mc.value.split(','): mpat = mpat.strip() with suppress(Exception): - if fnmatch.fnmatchcase(mt, mpat): + if any(fnmatch.fnmatchcase(x, mpat) for x in mts): return True return False diff --git a/kitty_tests/open_actions.py b/kitty_tests/open_actions.py index 0fb792d81..9259edcd2 100644 --- a/kitty_tests/open_actions.py +++ b/kitty_tests/open_actions.py @@ -57,3 +57,127 @@ action two single('file://hostname/tmp/moo.txt#23', 'launch', *get_editor(), '/tmp/moo.txt', '23') single('some thing.txt', 'ignored') self.ae(actions('x:///a.txt'), (KeyAction('one', ()), KeyAction('two', ()))) + + def test_mime_cache(self): + import shutil + import subprocess + import tempfile + + from kitty.fast_data_types import clear_mime_cache_data, mime_type_aliases, mime_type_for_filename + from kitty.guess_mime_type import guess_type, mime_types_for_matching + from kitty.open_actions import KeyAction, actions_for_url + + if not shutil.which('update-mime-database'): + self.skipTest('update-mime-database not available') + + def build_cache(data_dir: str, xml: str) -> None: + pkgs = os.path.join(data_dir, 'mime', 'packages') + os.makedirs(pkgs) + with open(os.path.join(pkgs, 'kitty-test.xml'), 'w') as f: + f.write(MIME_CACHE_TEST_PACKAGE.replace('MIME_TYPES', xml)) + with patch_env(PKGSYSTEM_ENABLE_FSYNC='0'): + subprocess.run(['update-mime-database', os.path.join(data_dir, 'mime')], check=True, capture_output=True) + + with tempfile.TemporaryDirectory() as tdir: + user, system = os.path.join(tdir, 'user'), os.path.join(tdir, 'system') + build_cache(system, MIME_CACHE_TEST_TYPES) + build_cache( + user, + ( + '' + '' + ), + ) + build_cache(os.path.join(tdir, 'ignored'), '') + try: + with patch_env(XDG_DATA_HOME=user, XDG_DATA_DIRS=f'relative/dir:{system}/:{tdir}/missing:{tdir}/ignored'): + clear_mime_cache_data() + for name, expected in MIME_CACHE_TEST_EXPECTATIONS.items(): + self.ae(mime_type_for_filename(name), expected, name) + self.ae(guess_type('/a/b.kext'), 'text/x-kt-ext') + self.ae(guess_type('/a/b.kjson'), 'text/json') + self.assertRaises(TypeError, mime_type_for_filename, b'a.kext') + + # aliases + ext_names = ('text/x-kt-ext', 'text/x-kt-user-alias', 'application/x-kt-older', 'text/x-kt-old') + self.ae(mime_type_aliases('text/x-kt-ext'), ext_names) + self.ae(mime_type_aliases('text/x-kt-old'), ext_names) + self.ae(mime_type_aliases('unknown/x-kt'), ('unknown/x-kt',)) + self.assertRaises(TypeError, mime_type_aliases, b'text/x-kt-ext') + self.ae(mime_types_for_matching('b.kjson', 'text/json'), {'text/json', 'application/json', 'application/x-kt-json'}) + self.ae(guess_type('/a/Makefile'), 'text/x-kt-make') + self.ae(mime_types_for_matching('/a/Makefile', 'text/x-kt-make'), {'text/x-kt-make', 'text/makefile'}) + self.set_options() + spec = """ +mime text/x-kt-old +action old + +mime text/MakeFile +action make + +mime image/*, application/x-kt-j* +action json +""" + for url, action in {'/tmp/b.kext': 'old', 'file:///tmp/Makefile': 'make', 'b.kjson': 'json', 'b.kuser': ''}.items(): + self.ae(tuple(actions_for_url(url, spec)), (KeyAction(action, ()),) if action else (), url) + truncated = os.path.join(tdir, 'truncated') + os.makedirs(os.path.join(truncated, 'mime')) + with open(os.path.join(system, 'mime', 'mime.cache'), 'rb') as src, open(os.path.join(truncated, 'mime', 'mime.cache'), 'wb') as dest: + dest.write(src.read()[:48]) + with patch_env(XDG_DATA_HOME=truncated, XDG_DATA_DIRS=f'{tdir}/missing'): + clear_mime_cache_data() + self.assertIsNone(mime_type_for_filename('a.kext')) + finally: + clear_mime_cache_data() + + +MIME_CACHE_TEST_PACKAGE = """ + +MIME_TYPES + +""" + +# Keep in sync with tools/utils/mime_cache_test.go +MIME_CACHE_TEST_TYPES = """ + + + + + + + + + + + + +""" + +MIME_CACHE_TEST_EXPECTATIONS = { + 'KittyLiteral': 'text/x-kt-literal', + 'kittyliteral': 'text/x-kt-literal', + 'KITTYLITERAL': 'text/x-kt-literal', + 'xKittyLiteral': None, + 'a.KCS': 'text/x-kt-cs', + 'a.kcs': None, + 'a.kext': 'text/x-kt-ext', + 'A.KEXT': 'text/x-kt-ext', + '/some/dir/a.kext': 'text/x-kt-ext', + 'dir.kext/noext': None, + 'x.long.kext': 'text/x-kt-long', + 'x.LONG.KEXT': 'text/x-kt-long', + 'xlong.kext': 'text/x-kt-ext', + 'a.kw': 'text/x-kt-heavy', + 'kglob1.txt': 'text/x-kt-glob', + 'KGLOB1.TXT': 'text/x-kt-glob', + 'kglobx.txt': None, + 'knegx.txt': 'text/x-kt-negated-glob', + 'kneg1.txt': None, + 'a.kü': 'text/x-kt-unicode', + 'a.KÜ': 'text/x-kt-unicode', + 'a.kuser': 'text/x-kt-user', + 'a.kext.override': 'text/x-kt-user', + '': None, + 'a\0.kext': None, + 'notmatched': None, +} diff --git a/tools/utils/mime_cache.go b/tools/utils/mime_cache.go new file mode 100644 index 000000000..58f44a47c --- /dev/null +++ b/tools/utils/mime_cache.go @@ -0,0 +1,486 @@ +// License: GPLv3 Copyright: 2026, Kovid Goyal, + +package utils + +import ( + "bytes" + "encoding/binary" + "errors" + "fmt" + "io/fs" + "os" + "path" + "path/filepath" + "runtime" + "strings" + "sync" + + "golang.org/x/sys/unix" +) + +// Fast lookup of MIME types for file names using the binary mime.cache files +// generated by update-mime-database from shared-mime-info. The lookup +// algorithm mirrors the one used by xdgmime (GLib/GIO). See +// https://specifications.freedesktop.org/shared-mime-info-spec/latest/ar01s02.html + +const ( + mime_cache_case_sensitive_flag = 0x100 + mime_cache_weight_mask = 0xff + mime_cache_max_matches = 16 + mime_cache_max_suffix_tree_depth = 256 + mime_cache_header_size = 40 + mime_cache_alias_list_offset_pos = 4 + mime_cache_literal_list_offset_pos = 12 + mime_cache_suffix_tree_offset_pos = 16 + mime_cache_glob_list_offset_pos = 20 + mime_cache_default_data_dirs = "/usr/local/share/:/usr/share/" + mime_cache_relative_path_in_datadir = "mime/mime.cache" +) + +type mime_cache struct { + data []byte +} + +type mime_cache_match struct { + mime string + weight uint32 +} + +type mime_cache_matches struct { + items []mime_cache_match +} + +func (m *mime_cache_matches) full() bool { return len(m.items) >= mime_cache_max_matches } + +func (c *mime_cache) u32(offset uint64) (uint32, bool) { + if offset > uint64(len(c.data)) || uint64(len(c.data))-offset < 4 { + return 0, false + } + return binary.BigEndian.Uint32(c.data[offset:]), true +} + +func (c *mime_cache) entry(offset uint64) (a, b, weight uint32, ok bool) { + if a, ok = c.u32(offset); !ok { + return + } + if b, ok = c.u32(offset + 4); !ok { + return + } + weight, ok = c.u32(offset + 8) + return +} + +// cstring returns the NUL terminated string at offset, without copying +func (c *mime_cache) cstring(offset uint32) ([]byte, bool) { + if uint64(offset) >= uint64(len(c.data)) { + return nil, false + } + data := c.data[offset:] + idx := bytes.IndexByte(data, 0) + if idx < 0 { + return nil, false + } + return data[:idx], true +} + +func (c *mime_cache) add_match(ans *mime_cache_matches, mime_offset, weight uint32) bool { + if ans.full() { + return false + } + if mime, ok := c.cstring(mime_offset); ok { + ans.items = append(ans.items, mime_cache_match{string(mime), weight & mime_cache_weight_mask}) + return true + } + return false +} + +func (c *mime_cache) lookup_literal(name []byte, case_sensitive_check bool, ans *mime_cache_matches) bool { + list_offset, ok := c.u32(mime_cache_literal_list_offset_pos) + if !ok { + return false + } + n, ok := c.u32(uint64(list_offset)) + if !ok { + return false + } + lo, hi := uint64(0), uint64(n) + for lo < hi { + mid := lo + (hi-lo)/2 + literal_offset, mime_offset, weight, ok := c.entry(uint64(list_offset) + 4 + 12*mid) + if !ok { + return false + } + literal, ok := c.cstring(literal_offset) + if !ok { + return false + } + switch cmp := bytes.Compare(literal, name); { + case cmp < 0: + lo = mid + 1 + case cmp > 0: + hi = mid + default: + if !case_sensitive_check && weight&mime_cache_case_sensitive_flag != 0 { + return false + } + return c.add_match(ans, mime_offset, weight) + } + } + return false +} + +func (c *mime_cache) lookup_suffix_node(num_nodes, offset uint32, name []rune, case_sensitive_check bool, ans *mime_cache_matches, depth int) { + if len(name) == 0 || depth > mime_cache_max_suffix_tree_depth { + return + } + ch := uint32(name[len(name)-1]) + lo, hi := uint64(0), uint64(num_nodes) + for lo < hi { + mid := lo + (hi-lo)/2 + node_char, num_children, first_child_offset, ok := c.entry(uint64(offset) + 12*mid) + if !ok { + return + } + switch { + case ch < node_char: + hi = mid + case ch > node_char: + lo = mid + 1 + default: + before := len(ans.items) + c.lookup_suffix_node(num_children, first_child_offset, name[:len(name)-1], case_sensitive_check, ans, depth+1) + if len(ans.items) == before { + // No longer suffix matched, use the leaf nodes of this node, + // which are sorted before all other children. + for i := range uint64(num_children) { + leaf_char, mime_offset, weight, ok := c.entry(uint64(first_child_offset) + 12*i) + if !ok || leaf_char != 0 { + break + } + if case_sensitive_check || weight&mime_cache_case_sensitive_flag == 0 { + c.add_match(ans, mime_offset, weight) + } + } + } + return + } + } +} + +func (c *mime_cache) lookup_suffix(name []rune, case_sensitive_check bool, ans *mime_cache_matches) { + tree_offset, ok := c.u32(mime_cache_suffix_tree_offset_pos) + if !ok { + return + } + num_roots, ok := c.u32(uint64(tree_offset)) + if !ok { + return + } + first_root_offset, ok := c.u32(uint64(tree_offset) + 4) + if !ok { + return + } + c.lookup_suffix_node(num_roots, first_root_offset, name, case_sensitive_check, ans, 0) +} + +// fnmatch_to_go_pattern converts the fnmatch() negated bracket expression +// syntax [!...] to the [^...] syntax used by path.Match +func fnmatch_to_go_pattern(glob string) string { + if !strings.Contains(glob, "[!") { + return glob + } + return strings.ReplaceAll(glob, "[!", "[^") +} + +func (c *mime_cache) lookup_glob(name string, case_sensitive_check bool, ans *mime_cache_matches) { + list_offset, ok := c.u32(mime_cache_glob_list_offset_pos) + if !ok { + return + } + n, ok := c.u32(uint64(list_offset)) + if !ok { + return + } + for i := uint64(0); i < uint64(n) && !ans.full(); i++ { + glob_offset, mime_offset, weight, ok := c.entry(uint64(list_offset) + 4 + 12*i) + if !ok { + return + } + if !case_sensitive_check && weight&mime_cache_case_sensitive_flag != 0 { + continue + } + if glob, ok := c.cstring(glob_offset); ok { + if matched, err := path.Match(fnmatch_to_go_pattern(string(glob)), name); err == nil && matched { + c.add_match(ans, mime_offset, weight) + } + } + } +} + +type mime_caches []*mime_cache + +func (caches mime_caches) lookup(name string) string { + if name == "" || strings.IndexByte(name, 0) > -1 { + return "" + } + ans := mime_cache_matches{items: make([]mime_cache_match, 0, 4)} + bname := []byte(name) + for _, c := range caches { + if c.lookup_literal(bname, true, &ans) { + return ans.items[0].mime + } + } + lname := strings.ToLower(name) + for _, c := range caches { + if c.lookup_literal([]byte(lname), false, &ans) { + return ans.items[0].mime + } + } + lrunes := []rune(lname) + for _, c := range caches { + c.lookup_suffix(lrunes, false, &ans) + } + if len(ans.items) < 2 { + runes := []rune(name) + for _, c := range caches { + c.lookup_suffix(runes, true, &ans) + } + } + if len(ans.items) == 0 { + for _, c := range caches { + c.lookup_glob(name, true, &ans) + } + } + if len(ans.items) == 0 { + for _, c := range caches { + c.lookup_glob(lname, false, &ans) + } + } + if len(ans.items) == 0 { + return "" + } + // Highest weight wins, ties go to the earliest match, which means the + // longest suffix and the highest priority cache file. + best := ans.items[0] + for _, m := range ans.items[1:] { + if m.weight > best.weight { + best = m + } + } + return best.mime +} + +func (c *mime_cache) alias_list() (list_offset, n uint32, ok bool) { + if list_offset, ok = c.u32(mime_cache_alias_list_offset_pos); ok { + n, ok = c.u32(uint64(list_offset)) + } + return +} + +func (c *mime_cache) alias_entry(list_offset uint32, idx uint64) (alias, mime []byte, ok bool) { + offset := uint64(list_offset) + 4 + 8*idx + alias_offset, ok := c.u32(offset) + if !ok { + return + } + mime_offset, ok := c.u32(offset + 4) + if !ok { + return + } + if alias, ok = c.cstring(alias_offset); ok { + mime, ok = c.cstring(mime_offset) + } + return +} + +// canonical_mime_type returns the canonical MIME type for the specified alias +// or the empty string if mime is not an alias. The alias list is sorted by alias. +func (c *mime_cache) canonical_mime_type(mime []byte) string { + list_offset, n, ok := c.alias_list() + if !ok { + return "" + } + lo, hi := uint64(0), uint64(n) + for lo < hi { + mid := lo + (hi-lo)/2 + alias, canonical, ok := c.alias_entry(list_offset, mid) + if !ok { + return "" + } + switch cmp := bytes.Compare(alias, mime); { + case cmp < 0: + lo = mid + 1 + case cmp > 0: + hi = mid + default: + return string(canonical) + } + } + return "" +} + +func (c *mime_cache) aliases_of(canonical []byte, add func(string)) { + list_offset, n, ok := c.alias_list() + if !ok { + return + } + for i := range uint64(n) { + alias, mime, ok := c.alias_entry(list_offset, i) + if !ok { + return + } + if bytes.Equal(mime, canonical) { + add(string(alias)) + } + } +} + +func (caches mime_caches) aliases(mime string) (ans []string) { + canonical := "" + for _, c := range caches { + if canonical = c.canonical_mime_type([]byte(mime)); canonical != "" { + break + } + } + if canonical == "" { + canonical = mime + } + seen := NewSet[string](8) + add := func(x string) { + if !seen.Has(x) { + seen.Add(x) + ans = append(ans, x) + } + } + add(canonical) + for _, c := range caches { + c.aliases_of([]byte(canonical), add) + } + add(mime) + return +} + +func load_mime_cache(path string) (*mime_cache, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + st, err := f.Stat() + if err != nil { + return nil, err + } + if !st.Mode().IsRegular() { + return nil, fmt.Errorf("%s is not a regular file", path) + } + if st.Size() < mime_cache_header_size { + return nil, fmt.Errorf("the MIME cache file %s is too small", path) + } + // The mapping is never unmapped as it is used for the lifetime of the + // process. update-mime-database replaces the cache file atomically so the + // mapped data remains valid. + data, err := unix.Mmap(int(f.Fd()), 0, int(st.Size()), unix.PROT_READ, unix.MAP_PRIVATE) + is_mmapped := err == nil + if !is_mmapped { + if data, err = os.ReadFile(path); err != nil { + return nil, err + } + if len(data) < mime_cache_header_size { + return nil, fmt.Errorf("the MIME cache file %s is too small", path) + } + } + major, minor := binary.BigEndian.Uint16(data), binary.BigEndian.Uint16(data[2:]) + if major != 1 || minor < 1 { + if is_mmapped { + _ = unix.Munmap(data) + } + return nil, fmt.Errorf("the MIME cache file %s has unsupported version %d.%d", path, major, minor) + } + return &mime_cache{data: data}, nil +} + +// mime_cache_paths returns the paths to mime.cache files in order of decreasing priority +func mime_cache_paths() (ans []string) { + seen := NewSet[string](4) + add := func(data_dir string) { + // relative paths are ignored as per the XDG spec + if filepath.IsAbs(data_dir) { + if p := filepath.Join(data_dir, mime_cache_relative_path_in_datadir); !seen.Has(p) { + seen.Add(p) + ans = append(ans, p) + } + } + } + if data_home := os.Getenv("XDG_DATA_HOME"); data_home != "" { + add(data_home) + } else if home := os.Getenv("HOME"); home != "" { + add(filepath.Join(home, ".local", "share")) + } + data_dirs := os.Getenv("XDG_DATA_DIRS") + if data_dirs == "" { + data_dirs = mime_cache_default_data_dirs + } + for _, x := range strings.Split(data_dirs, ":") { + add(x) + } + return +} + +func load_mime_caches(paths []string) (ans mime_caches) { + for _, p := range paths { + c, err := load_mime_cache(p) + if err != nil { + if !errors.Is(err, fs.ErrNotExist) && !errors.Is(err, unix.ENOTDIR) { + fmt.Fprintln(os.Stderr, "Failed to load MIME cache file with error:", err) + } + continue + } + ans = append(ans, c) + } + return +} + +var system_mime_caches = sync.OnceValue(func() mime_caches { + if runtime.GOOS == "darwin" { + return nil + } + return load_mime_caches(mime_cache_paths()) +}) + +// HaveMimeCache returns true if at least one shared-mime-info mime.cache file +// was successfully loaded +func HaveMimeCache() bool { + return len(system_mime_caches()) > 0 +} + +// MimeTypeFromCache returns the MIME type for the specified file name, as +// determined by the shared-mime-info mime.cache files. Returns the empty +// string if no match is found. Only the base name of the file is used. +func MimeTypeFromCache(filename string) string { + if caches := system_mime_caches(); len(caches) > 0 { + return caches.lookup(filepath.Base(filename)) + } + return "" +} + +// MimeTypeAliases returns all names for the specified MIME type as known to +// the shared-mime-info mime.cache files, starting with the canonical name, +// followed by all aliases. Always contains at least the specified MIME type. +// The returned slice must not be modified. +func MimeTypeAliases(mime string) []string { + mime_type_aliases_cache.RLock() + ans, found := mime_type_aliases_cache.m[mime] + mime_type_aliases_cache.RUnlock() + if !found { + ans = system_mime_caches().aliases(mime) + mime_type_aliases_cache.Lock() + mime_type_aliases_cache.m[mime] = ans + mime_type_aliases_cache.Unlock() + } + return ans +} + +var mime_type_aliases_cache = struct { + sync.RWMutex + m map[string][]string +}{m: make(map[string][]string)} diff --git a/tools/utils/mime_cache_test.go b/tools/utils/mime_cache_test.go new file mode 100644 index 000000000..ce1a3a159 --- /dev/null +++ b/tools/utils/mime_cache_test.go @@ -0,0 +1,133 @@ +// License: GPLv3 Copyright: 2026, Kovid Goyal, + +package utils + +import ( + "os" + "os/exec" + "path/filepath" + "strings" + "testing" +) + +// Keep in sync with kitty_tests/open_actions.py +const mime_cache_test_types = ` + + + + + + + + + + + + +` + +func TestMimeCache(t *testing.T) { + if _, err := exec.LookPath("update-mime-database"); err != nil { + t.Skip("update-mime-database not available") + } + tdir := t.TempDir() + build_cache := func(data_dir, types string) { + pkgs := filepath.Join(data_dir, "mime", "packages") + if err := os.MkdirAll(pkgs, 0o755); err != nil { + t.Fatal(err) + } + xml := ` +` + types + `` + if err := os.WriteFile(filepath.Join(pkgs, "kitty-test.xml"), []byte(xml), 0o644); err != nil { + t.Fatal(err) + } + cmd := exec.Command("update-mime-database", filepath.Join(data_dir, "mime")) + cmd.Env = append(os.Environ(), "PKGSYSTEM_ENABLE_FSYNC=0") + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("update-mime-database failed with error: %s and output:\n%s", err, out) + } + } + user, system := filepath.Join(tdir, "user"), filepath.Join(tdir, "system") + build_cache(system, mime_cache_test_types) + build_cache(user, ``) + build_cache(filepath.Join(tdir, "ignored"), ``) + + t.Setenv("XDG_DATA_HOME", user) + t.Setenv("XDG_DATA_DIRS", strings.Join([]string{"relative/dir", system + "/", tdir + "/missing", tdir + "/ignored"}, ":")) + paths := mime_cache_paths() + expected_paths := []string{ + filepath.Join(user, "mime", "mime.cache"), filepath.Join(system, "mime", "mime.cache"), + filepath.Join(tdir, "missing", "mime", "mime.cache"), filepath.Join(tdir, "ignored", "mime", "mime.cache")} + if strings.Join(paths, "\n") != strings.Join(expected_paths, "\n") { + t.Fatalf("Incorrect mime.cache paths: %#v != %#v", expected_paths, paths) + } + caches := load_mime_caches(paths) + if len(caches) != 3 { + t.Fatalf("Expected to load 3 caches, loaded: %d", len(caches)) + } + for name, expected := range map[string]string{ + "KittyLiteral": "text/x-kt-literal", + "kittyliteral": "text/x-kt-literal", + "KITTYLITERAL": "text/x-kt-literal", + "xKittyLiteral": "", + "a.KCS": "text/x-kt-cs", + "a.kcs": "", + "a.kext": "text/x-kt-ext", + "A.KEXT": "text/x-kt-ext", + "x.long.kext": "text/x-kt-long", + "x.LONG.KEXT": "text/x-kt-long", + "xlong.kext": "text/x-kt-ext", + "a.kw": "text/x-kt-heavy", + "kglob1.txt": "text/x-kt-glob", + "KGLOB1.TXT": "text/x-kt-glob", + "kglobx.txt": "", + "knegx.txt": "text/x-kt-negated-glob", + "kneg1.txt": "", + "a.kü": "text/x-kt-unicode", + "a.KÜ": "text/x-kt-unicode", + "a.kuser": "text/x-kt-user", + "a.kext.override": "text/x-kt-user", + "": "", + "a\x00.kext": "", + "notmatched": "", + } { + if actual := caches.lookup(name); actual != expected { + t.Errorf("Incorrect MIME type for %#v: %#v != %#v", name, expected, actual) + } + } + ext_names := []string{"text/x-kt-ext", "text/x-kt-user-alias", "application/x-kt-older", "text/x-kt-old"} + for q, expected := range map[string][]string{ + "text/x-kt-ext": ext_names, + "text/x-kt-old": ext_names, + "application/json": {"application/json", "application/x-kt-json"}, + "unknown/x-kt": {"unknown/x-kt"}, + } { + if actual := caches.aliases(q); strings.Join(actual, " ") != strings.Join(expected, " ") { + t.Errorf("Incorrect aliases for %#v: %#v != %#v", q, expected, actual) + } + } + if actual := (mime_caches{}).aliases("text/x-kt-ext"); len(actual) != 1 || actual[0] != "text/x-kt-ext" { + t.Errorf("Incorrect aliases with no caches: %#v", actual) + } + if _, err := load_mime_cache(filepath.Join(tdir, "missing", "mime", "mime.cache")); err == nil { + t.Fatalf("Loading a non-existent cache did not fail") + } + bad := filepath.Join(tdir, "bad.cache") + if err := os.WriteFile(bad, []byte(strings.Repeat("\x00", mime_cache_header_size)), 0o644); err != nil { + t.Fatal(err) + } + if _, err := load_mime_cache(bad); err == nil { + t.Fatalf("Loading a cache with an invalid version did not fail") + } + truncated := filepath.Join(tdir, "truncated.cache") + if data, err := os.ReadFile(filepath.Join(system, "mime", "mime.cache")); err != nil { + t.Fatal(err) + } else if err := os.WriteFile(truncated, data[:mime_cache_header_size+8], 0o644); err != nil { + t.Fatal(err) + } + if c, err := load_mime_cache(truncated); err != nil { + t.Fatal(err) + } else if actual := (mime_caches{c}).lookup("a.kext"); actual != "" { + t.Fatalf("Lookup in truncated cache returned: %#v", actual) + } +} diff --git a/tools/utils/mimetypes.go b/tools/utils/mimetypes.go index c63d85c7e..32296e38b 100644 --- a/tools/utils/mimetypes.go +++ b/tools/utils/mimetypes.go @@ -71,6 +71,12 @@ func GuessMimeType(filename string) string { ext := filepath.Ext(filename) mime_with_parameters := UserMimeMap()[ext] if mime_with_parameters == "" { + mime_with_parameters = MimeTypeFromCache(filename) + } + // The stdlib database is loaded from the same shared-mime-info data as + // the cache, but slowly, by parsing text files, so avoid it if the cache + // is available. + if mime_with_parameters == "" && !HaveMimeCache() { mime_with_parameters = mime.TypeByExtension(ext) } if mime_with_parameters == "" {