From f334bbc65202bced1aac7f9ae857f366a9792821 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Wed, 21 Feb 2018 20:23:42 +0000 Subject: [PATCH] filesys: introduce a routine to check if a directory is empty of files --- Kernel/filesys.c | 22 ++++++++++++++++++++++ Kernel/include/kernel.h | 1 + 2 files changed, 23 insertions(+) diff --git a/Kernel/filesys.c b/Kernel/filesys.c index cf64a9c0..19574067 100644 --- a/Kernel/filesys.c +++ b/Kernel/filesys.c @@ -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 diff --git a/Kernel/include/kernel.h b/Kernel/include/kernel.h index 7fb7629f..3eef2256 100644 --- a/Kernel/include/kernel.h +++ b/Kernel/include/kernel.h @@ -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); -- 2.34.1