Author: breed
Date: Thu Aug  6 18:03:25 2009
New Revision: 801747

URL: http://svn.apache.org/viewvc?rev=801747&view=rev
Log:
ZOOKEEPER-311. handle small path lengths in zoo_create()

Modified:
    hadoop/zookeeper/trunk/CHANGES.txt
    hadoop/zookeeper/trunk/src/c/include/zookeeper.h
    hadoop/zookeeper/trunk/src/c/src/zookeeper.c
    hadoop/zookeeper/trunk/src/c/tests/TestClient.cc

Modified: hadoop/zookeeper/trunk/CHANGES.txt
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/CHANGES.txt?rev=801747&r1=801746&r2=801747&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/CHANGES.txt (original)
+++ hadoop/zookeeper/trunk/CHANGES.txt Thu Aug  6 18:03:25 2009
@@ -46,6 +46,8 @@
 
   ZOOKEEPER-493. patch for command line setquota (steve bendiola via phunt)
 
+  ZOOKEEPER-311. handle small path lengths in zoo_create() (chris barroch via 
breed)
+
 IMPROVEMENTS:
   ZOOKEEPER-473. cleanup junit tests to eliminate false positives due to
   "socket reuse" and failure to close client (phunt via mahadev)

Modified: hadoop/zookeeper/trunk/src/c/include/zookeeper.h
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/include/zookeeper.h?rev=801747&r1=801746&r2=801747&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/c/include/zookeeper.h (original)
+++ hadoop/zookeeper/trunk/src/c/include/zookeeper.h Thu Aug  6 18:03:25 2009
@@ -958,9 +958,15 @@
  *    used.
  * \param flags this parameter can be set to 0 for normal create or an OR
  *    of the Create Flags
- * \param realpath the real path that is created (this might be different than 
the
- *    path to create because of the ZOO_SEQUENCE flag.
- * \param the maximum length of real path you would want.
+ * \param path_buffer Buffer which will be filled with the path of the
+ *    new node (this might be different than the supplied path
+ *    because of the ZOO_SEQUENCE flag).  The path string will always be
+ *    null-terminated.
+ * \param path_buffer_len Size of path buffer; if the path of the new
+ *    node (including space for the null terminator) exceeds the buffer size,
+ *    the path string will be truncated to fit.  The actual path of the
+ *    new node in the server will not be affected by the truncation.
+ *    The path string will always be null-terminated.
  * \return  one of the following codes are returned:
  * ZOK operation completed succesfully
  * ZNONODE the parent node does not exist.
@@ -972,8 +978,8 @@
  * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
  */
 ZOOAPI int zoo_create(zhandle_t *zh, const char *path, const char *value,
-        int valuelen, const struct ACL_vector *acl, int flags, char *realpath, 
 
-        int max_realpath_len);
+        int valuelen, const struct ACL_vector *acl, int flags,
+        char *path_buffer, int path_buffer_len);
 
 /**
  * \brief delete a node in zookeeper synchronously.

Modified: hadoop/zookeeper/trunk/src/c/src/zookeeper.c
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/src/zookeeper.c?rev=801747&r1=801746&r2=801747&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/c/src/zookeeper.c (original)
+++ hadoop/zookeeper/trunk/src/c/src/zookeeper.c Thu Aug  6 18:03:25 2009
@@ -1878,14 +1878,13 @@
                         struct CreateResponse res;
                         int len;
                         deserialize_CreateResponse(ia, "reply", &res);
-                        if (sc->u.str.str_len > strlen(res.path)) {
-                            len = strlen(res.path);
-                        } else {
-                            len = sc->u.str.str_len-1;
+                        len = strlen(res.path) + 1;
+                        if (len > sc->u.str.str_len) {
+                            len = sc->u.str.str_len;
                         }
                         if (len > 0) {
-                            memcpy(sc->u.str.str, res.path, len);
-                            sc->u.str.str[len] = '\0';
+                            memcpy(sc->u.str.str, res.path, len - 1);
+                            sc->u.str.str[len - 1] = '\0';
                         }
                         deallocate_CreateResponse(&res);
                     }
@@ -2724,16 +2723,16 @@
  * sync API
  */
 int zoo_create(zhandle_t *zh, const char *path, const char *value,
-        int valuelen, const struct ACL_vector *acl, int flags, char *realpath,
-        int max_realpath_len)
+        int valuelen, const struct ACL_vector *acl, int flags,
+        char *path_buffer, int path_buffer_len)
 {
     struct sync_completion *sc = alloc_sync_completion();
     int rc;
     if (!sc) {
         return ZSYSTEMERROR;
     }
-    sc->u.str.str = realpath;
-    sc->u.str.str_len = max_realpath_len;
+    sc->u.str.str = path_buffer;
+    sc->u.str.str_len = path_buffer_len;
     rc=zoo_acreate(zh, path, value, valuelen, acl, flags, SYNCHRONOUS_MARKER, 
sc);
     if(rc==ZOK){
         wait_sync_completion(sc);

Modified: hadoop/zookeeper/trunk/src/c/tests/TestClient.cc
URL: 
http://svn.apache.org/viewvc/hadoop/zookeeper/trunk/src/c/tests/TestClient.cc?rev=801747&r1=801746&r2=801747&view=diff
==============================================================================
--- hadoop/zookeeper/trunk/src/c/tests/TestClient.cc (original)
+++ hadoop/zookeeper/trunk/src/c/tests/TestClient.cc Thu Aug  6 18:03:25 2009
@@ -163,6 +163,7 @@
     CPPUNIT_TEST(testAsyncWatcherAutoReset);
 #ifdef THREADED
     CPPUNIT_TEST(testNullData);
+    CPPUNIT_TEST(testPath);
     CPPUNIT_TEST(testPathValidation);
     CPPUNIT_TEST(testPing);
     CPPUNIT_TEST(testAcl);
@@ -534,6 +535,45 @@
         CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
     }
 
+    void testPath() {
+        watchctx_t ctx;
+        char pathbuf[10];
+        zhandle_t *zk = createClient(&ctx);
+        CPPUNIT_ASSERT(zk);
+        int rc = 0;
+
+        memset(pathbuf, 'X', 10);
+        rc = zoo_create(zk, "/path0", "", 0, 
+                        &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 0);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+        CPPUNIT_ASSERT_EQUAL('X', pathbuf[0]);
+
+        rc = zoo_create(zk, "/path1", "", 0, 
+                        &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 1);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+        CPPUNIT_ASSERT(strlen(pathbuf) == 0);
+
+        rc = zoo_create(zk, "/path2", "", 0, 
+                        &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 2);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+        CPPUNIT_ASSERT(strcmp(pathbuf, "/") == 0);
+
+        rc = zoo_create(zk, "/path3", "", 0, 
+                        &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 3);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+        CPPUNIT_ASSERT(strcmp(pathbuf, "/p") == 0);
+
+        rc = zoo_create(zk, "/path7", "", 0, 
+                        &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 7);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+        CPPUNIT_ASSERT(strcmp(pathbuf, "/path7") == 0);
+
+        rc = zoo_create(zk, "/path8", "", 0, 
+                        &ZOO_OPEN_ACL_UNSAFE, 0, pathbuf, 8);
+        CPPUNIT_ASSERT_EQUAL((int) ZOK, rc);
+        CPPUNIT_ASSERT(strcmp(pathbuf, "/path8") == 0);
+    }
+
     void testPathValidation() {
         watchctx_t ctx;
         zhandle_t *zk = createClient(&ctx);


Reply via email to