filesys: introduce a routine to check if a directory is empty of files
authorAlan Cox <alan@linux.intel.com>
Wed, 21 Feb 2018 20:23:42 +0000 (20:23 +0000)
committerAlan Cox <alan@linux.intel.com>
Wed, 21 Feb 2018 20:23:42 +0000 (20:23 +0000)
Kernel/filesys.c
Kernel/include/kernel.h

index cf64a9c..1957406 100644 (file)
@@ -281,6 +281,28 @@ badino:
     return NULLINODE;
 }
 
+bool emptydir(inoptr wd)
+{
+    struct direct curentry;
+
+    i_islocked(wd);
+
+    udata.u_offset =  2 * DIR_LEN;     /* . .. ignored */
+
+    do
+    {
+        udata.u_count = DIR_LEN;
+        udata.u_base  =(unsigned char *)&curentry;
+        udata.u_sysio = true;
+        readi(wd, 0);
+
+        /* Read until EOF or name is found.  readi() advances udata.u_offset */
+        if (*curentry.d_name)
+            return false;
+    } while(udata.u_done == DIR_LEN);
+
+    return true;
+}
 
 
 /* Ch_link modifies or makes a new entry in the directory for the name
index 7fb7629..3eef225 100644 (file)
@@ -848,6 +848,7 @@ extern inoptr n_open(char *uname, inoptr *parent);
 extern inoptr i_open(uint16_t dev, uint16_t ino);
 extern inoptr srch_dir(inoptr wd, char *compname);
 extern inoptr srch_mt(inoptr ino);
+extern bool emptydir(inoptr ino);
 extern bool ch_link(inoptr wd, char *oldname, char *newname, inoptr nindex);
 /* return true if n1 == n2 */
 extern bool namecomp(char *n1, char *n2);