# HG changeset patch
# User jfp <jf.pieronne@laposte.net>
# Date 1716358342 -7200
#      Wed May 22 08:12:22 2024 +0200
# Node ID 80ab28cec3d6c77d6f24cc22a5448f5f753db11e
# Parent  cb0095d1a630e3392c95a28126411e08c0ec6e9f
Add optionnal boolean parameter fid to rms.search. If set to True FID for the file name specified
is added to the returned tuple.

diff --git a/python/local/ovms_module/ovms/rms/_rms.pyi b/python/local/ovms_module/ovms/rms/_rms.pyi
--- a/python/local/ovms_module/ovms/rms/_rms.pyi
+++ b/python/local/ovms_module/ovms/rms/_rms.pyi
@@ -31,9 +31,9 @@
     ...
 
 def search(
-    path: str | bytes, noconceal: bool = False
+    path: str | bytes, noconceal: bool = False, fid: bool = False
 ) -> Tuple[bytes, bytes, bytes, bytes, bytes, bytes, Tuple[int, int, int]]:
-    """parse(path, noconceal=False) -> (node, dev, dir, name, ext, ver, did)"""
+    """parse(path, noconceal=False, fid=False) -> (node, dev, dir, name, ext, ver, did[,fid])"""
     ...
 
 class error(IOError): ...
@@ -42,9 +42,7 @@
     def close(self) -> int: ...
     def delete(self, key: bytes | None = None) -> int: ...
     def fetch(self, key: bytes | None = None) -> Tuple[int, Any]: ...
-    def find(
-        self, key: bytes | None = None, rop: int | None = None
-    ) -> int: ...
+    def find(self, key: bytes | None = None, rop: int | None = None) -> int: ...
     def flush(self) -> int: ...
     def free(self) -> int: ...
     def put(self, record: Any) -> int: ...
diff --git a/python/local/ovms_module/ovms/rms/_rmsmodule.c b/python/local/ovms_module/ovms/rms/_rmsmodule.c
--- a/python/local/ovms_module/ovms/rms/_rmsmodule.c
+++ b/python/local/ovms_module/ovms/rms/_rmsmodule.c
@@ -1242,7 +1242,7 @@
 
 static int parse_search(char *path, char *node, char *dev, char *dir,
                         char *name, char *type, char *ver, int noconceal,
-                        int search, unsigned int *did) {
+                        int search, unsigned int *did, unsigned int *fid) {
     int stat;
     unsigned int fop = NAM$M_SYNCHK;
     int fac = 0;
@@ -1310,6 +1310,12 @@
         *did++ = fo->naml.naml$b_did_rvn;
     }
 
+    if (fid) {
+        *fid++ = fo->naml.naml$w_fid_num + (fo->naml.naml$b_fid_nmx << 16);
+        *fid++ = fo->naml.naml$w_fid_seq;
+        *fid++ = fo->naml.naml$b_fid_rvn;
+    }
+
     PyMem_Del(fo);
     return stat;
 }
@@ -1336,7 +1342,7 @@
 
     int stat;
     stat =
-        parse_search(path, node, dev, dir, name, type, ver, noconceal, 0, NULL);
+        parse_search(path, node, dev, dir, name, type, ver, noconceal, 0, NULL, NULL);
     if (!$VMS_STATUS_SUCCESS(stat)) {
         char msg[256];
         PyObject *v;
@@ -1359,7 +1365,7 @@
 }
 
 static PyObject *rms_search(PyObject *dummy, PyObject *args, PyObject *kwargs) {
-    char *kwnames[] = {"path", "noconceal", NULL};
+    char *kwnames[] = {"path", "noconceal", "fid", NULL};
     char *path;
     char node[NAML$C_MAXRSS + 1];
     char dev[NAML$C_MAXRSS + 1];
@@ -1369,20 +1375,23 @@
     char ver[NAML$C_MAXRSS + 1];
     PyObject *res;
     PyObject *didres;
+    PyObject *fidres;
     int noconceal = 0;
     unsigned int did[3];
+    int retfid = 0;
+    unsigned int fid[3];
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y|p:search", kwnames, &path,
-                                     &noconceal)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y|pp:search", kwnames, &path,
+                                     &noconceal, &retfid)) {
         PyErr_Clear();
-        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|p:search", kwnames,
-                                         &path, &noconceal))
+        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|pp:search", kwnames,
+                                         &path, &noconceal, &retfid))
             return NULL;
     }
 
     int stat;
     stat =
-        parse_search(path, node, dev, dir, name, type, ver, noconceal, 1, did);
+        parse_search(path, node, dev, dir, name, type, ver, noconceal, 1, did, retfid ? fid : NULL);
     if (!$VMS_STATUS_SUCCESS(stat)) {
         char msg[256];
         PyObject *v;
@@ -1398,7 +1407,13 @@
     PyTuple_SetItem(didres, 0, PyLong_FromLong(did[0]));
     PyTuple_SetItem(didres, 1, PyLong_FromLong(did[1]));
     PyTuple_SetItem(didres, 2, PyLong_FromLong(did[2]));
-    res = PyTuple_New(7);
+    if (retfid) {
+        fidres = PyTuple_New(3);
+        PyTuple_SetItem(fidres, 0, PyLong_FromLong(fid[0]));
+        PyTuple_SetItem(fidres, 1, PyLong_FromLong(fid[1]));
+        PyTuple_SetItem(fidres, 2, PyLong_FromLong(fid[2]));
+    }
+    res = PyTuple_New(retfid ? 8 : 7);
     PyTuple_SetItem(res, 0, PyBytes_FromString(node));
     PyTuple_SetItem(res, 1, PyBytes_FromString(dev));
     PyTuple_SetItem(res, 2, PyBytes_FromString(dir));
@@ -1406,6 +1421,9 @@
     PyTuple_SetItem(res, 4, PyBytes_FromString(type));
     PyTuple_SetItem(res, 5, PyBytes_FromString(ver));
     PyTuple_SetItem(res, 6, didres);
+    if (retfid) {
+        PyTuple_SetItem(res, 7, fidres);
+    }
     return res;
 }
 
@@ -1571,11 +1589,11 @@
     {"getfdl", (PyCFunction)rms_getfdl, METH_VARARGS,
      PyDoc_STR("getfdl(path) -> tuple")},
     {"parse", (PyCFunction)rms_parse, METH_VARARGS | METH_KEYWORDS,
-     PyDoc_STR("parse(path, noconceal=False) -> (node, dev, dir, name, "
+     PyDoc_STR("parse(path, noconceal=False, fid=False) -> (node, dev, dir, name, "
                "ext, ver)")},
     {"search", (PyCFunction)rms_search, METH_VARARGS | METH_KEYWORDS,
-     PyDoc_STR("parse(path, noconceal=False) -> (node, dev, dir, name, "
-               "ext, ver)")},
+     PyDoc_STR("search(path, noconceal=False, fid=False) -> (node, dev, dir, name, "
+               "ext, ver, did[,fid])")},
     {"erase", (PyCFunction)rms_erase, METH_VARARGS,
      PyDoc_STR("erase(path) -> None")},
     /*  {"setmtime", (PyCFunction)rms_setmtime, METH_VARARGS,