Whamcloud - gitweb
lib/{ext2fs,support}: fix 32-bit Windows build
[tools/e2fsprogs.git] / lib / ext2fs / symlink.c
index 13bca6e..a66fb7e 100644 (file)
 #include "ext2_fs.h"
 #include "ext2fs.h"
 
+#ifndef HAVE_STRNLEN
+/*
+ * Incredibly, libc5 doesn't appear to have strnlen.  So we have to
+ * provide our own.
+ */
+static int my_strnlen(const char * s, int count)
+{
+       const char *cp = s;
+
+       while (count-- && *cp)
+               cp++;
+       return cp - s;
+}
+#define strnlen(str, x) my_strnlen((str),(x))
+#endif
+
 errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
                         const char *name, const char *target)
 {
@@ -38,6 +54,7 @@ errcode_t ext2fs_symlink(ext2_filsys fs, ext2_ino_t parent, ext2_ino_t ino,
        int                     fastlink, inlinelink;
        unsigned int            target_len;
        char                    *block_buf = 0;
+       int                     drop_refcount = 0;
 
        EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS);
 
@@ -147,6 +164,14 @@ need_block:
        }
 
        /*
+        * Update accounting....
+        */
+       if (!fastlink && !inlinelink)
+               ext2fs_block_alloc_stats2(fs, blk, +1);
+       ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
+       drop_refcount = 1;
+
+       /*
         * Link the symlink into the filesystem hierarchy
         */
        if (name) {
@@ -162,17 +187,16 @@ need_block:
                if (retval)
                        goto cleanup;
        }
-
-       /*
-        * Update accounting....
-        */
-       if (!fastlink && !inlinelink)
-               ext2fs_block_alloc_stats2(fs, blk, +1);
-       ext2fs_inode_alloc_stats2(fs, ino, +1, 0);
+       drop_refcount = 0;
 
 cleanup:
        if (block_buf)
                ext2fs_free_mem(&block_buf);
+       if (drop_refcount) {
+               if (!fastlink && !inlinelink)
+                       ext2fs_block_alloc_stats2(fs, blk, -1);
+               ext2fs_inode_alloc_stats2(fs, ino, -1, 0);
+       }
        return retval;
 }