factor out the size check for both readdir methods (raven at themaw dot net)
authorzoulasc <christos@zoulas.com>
Fri, 5 Sep 2014 09:56:16 +0000 (05:56 -0400)
committerzoulasc <christos@zoulas.com>
Fri, 5 Sep 2014 09:56:16 +0000 (05:56 -0400)
ChangeLog
amd/readdir.c

index b1bc0972c7b0d58fa1b485b52d9aa00323c625fe..06bdce585e9e5877273b3d63b1eff9df1ab9988a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2014-09-05  Christos Zoulas <christos@zoulas.com>
+
+       * factor out the size check for both readdir methods
+         (raven at themaw.net)
+
 2014-09-03  Christos Zoulas <christos@zoulas.com>
 
        * configure test for 64 bit xdr function (raven at themaw.net)
index 511bdc047fbe32a0742114bbd1f8d9a1346416a4..4b5e61b3d56d95841258f19209f72c3fccbdb9bb 100644 (file)
@@ -619,6 +619,20 @@ make_entry_chain3(am_node *mp, const am_entry3 *current_chain, int fully_browsab
   return retval;
 }
 
+static size_t needroom3(void)
+{
+  /*
+   * Check for enough room.  This is extremely approximate but should
+   * be enough space.  Really need 2 times:
+   *      (8byte fileid
+   *      8byte cookie
+   *      8byte name pointer
+   *      8byte next entry addres) = sizeof(am_entry3)
+   *      2byte name + 1byte terminator
+   * plus the size of the am_dirlist3 structure */
+  return ((2 * ((sizeof(am_entry3) + sizeof("..") + 1))) + sizeof(am_dirlist3));
+}
+
 /* This one is called only if map is browsable */
 static int
 amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie,
@@ -636,6 +650,7 @@ amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie,
     plog(XLOG_DEBUG, "amfs_readdir3_browsable gen=%lu, count=%d", gen, count);
 
   if (gen == 0) {
+    size_t needed = needroom3();
     /*
      * In the default instance (which is used to start a search) we return
      * "." and "..".
@@ -645,17 +660,11 @@ amfs_readdir3_browsable(am_node *mp, am_cookie3 cookie,
      * fairly unbelievable) then tough.
      */
     dlog("%s: default search", __func__);
-    /*
-     * Check for enough room.  This is extremely approximate but should
-     * be enough space.  Really need 2 times:
-     *      (8byte fileid
-     *      8byte cookie
-     *      8byte name pointer
-     *      8byte next entry addres) = sizeof(*ep)
-     *      2byte name + 1byte terminator
-     * plus the dirlist structure */
-    if (count < ((2 * ((sizeof(*ep) + sizeof("..") + 1))) + sizeof(*dp)));
+
+    if (count < needed) {
+      dlog("%s: not enough room %u < %zu", __func__, count, needed);
       return EINVAL;
+    }
 
     /*
      * compute # of entries to send in this chain.
@@ -797,6 +806,7 @@ amfs_readdir3(am_node *mp, am_cookie3 cookie,
 
   /* when gen is 0, we start reading from the beginning of the directory */
   if (gen == 0) {
+    size_t needed = needroom3();
     /*
      * In the default instance (which is used to start a search) we return
      * "." and "..".
@@ -806,18 +816,9 @@ amfs_readdir3(am_node *mp, am_cookie3 cookie,
      * fairly unbelievable) then tough.
      */
     dlog("%s: default search", __func__);
-    /*
-     * Check for enough room.  This is extremely approximate but should
-     * be enough space.  Really need 2 times:
-     *      (8byte fileid
-     *      8byte cookie
-     *      8byte name pointer
-     *      8byte next entry addres) = sizeof(*ep)
-     *      2byte name + 1byte terminator
-     * plus the dirlist structure */
-#define NEEDROOM3 ((2 * ((sizeof(*ep) + sizeof("..") + 1))) + sizeof(*dp))
-    if (count < NEEDROOM3) {
-      dlog("%s: not enough room %u < %zu", __func__, count, NEEDROOM3);
+
+    if (count < needed) {
+      dlog("%s: not enough room %u < %zu", __func__, count, needed);
       return EINVAL;
     }