shithub: lwext4

Download patch

ref: 1fcd1b0075c24a5627233fab0fc468040afde8f2
parent: dfa54d1e23c32059b292bbbb99d4251bf15e7517
parent: 299dbe030a4dc518910e6a8cd2e37e640053506e
author: gkostka <kostka.grzegorz@gmail.com>
date: Sat Sep 26 13:49:21 EDT 2015

Merge branch 'master' of https://github.com/ngkaho1234/lwext4

--- a/lwext4/ext4.c
+++ b/lwext4/ext4.c
@@ -874,10 +874,6 @@
 		goto Finish;
 
 Finish:
-	if (r != EOK)
-		ext4_fs_put_inode_ref(child_ref);
-
-	ext4_fs_put_inode_ref(parent_ref);
 	return r;
 }
 
@@ -1085,12 +1081,15 @@
 	return r;
 }
 
-int ext4_fill_raw_inode(const char *mount_point, uint32_t ino,
+int ext4_fill_raw_inode(const char *path,
+			uint32_t *ret_ino,
 			struct ext4_inode *inode)
 {
 	int r;
+	ext4_file f;
 	struct ext4_inode_ref inode_ref;
-	struct ext4_mountpoint *mp = ext4_get_mount(mount_point);
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
+	uint32_t ino;
 
 	if (!mp)
 		return ENOENT;
@@ -1097,6 +1096,17 @@
 
 	EXT4_MP_LOCK(mp);
 
+	r = ext4_generic_open2(&f, path, O_RDONLY,
+			       EXT4_DIRECTORY_FILETYPE_UNKNOWN,
+			       NULL, NULL);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
+
+	ino = f.inode;
+	ext4_fclose(&f);
+
 	/*Load parent*/
 	r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
 	if (r != EOK) {
@@ -1108,6 +1118,10 @@
 
 	ext4_fs_put_inode_ref(&inode_ref);
 	EXT4_MP_UNLOCK(mp);
+
+	if (ret_ino)
+		*ret_ino = ino;
+
 	return r;
 }
 
@@ -1127,7 +1141,7 @@
 	return r;
 }
 
-int ext4_fopen2(ext4_file *f, const char *path, int flags, bool file_expect)
+int ext4_fopen2(ext4_file *f, const char *path, int flags)
 {
 	struct ext4_mountpoint *mp = ext4_get_mount(path);
 	int r;
@@ -1136,10 +1150,7 @@
 	if (!mp)
 		return ENOENT;
 
-	if (file_expect == true)
-		filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
-	else
-		filetype = EXT4_DIRECTORY_FILETYPE_DIR;
+        filetype = EXT4_DIRECTORY_FILETYPE_REG_FILE;
 
 	EXT4_MP_LOCK(mp);
 	ext4_block_cache_write_back(mp->fs.bdev, 1);
@@ -1149,28 +1160,6 @@
 	return r;
 }
 
-int ext4_fopen_all(ext4_file *f, const char *path, int flags)
-{
-	struct ext4_mountpoint *mp = ext4_get_mount(path);
-	int r;
-	int filetype;
-
-	if (!mp)
-		return ENOENT;
-
-	if (flags & O_CREAT)
-		return EINVAL;
-
-	filetype = EXT4_DIRECTORY_FILETYPE_UNKNOWN;
-
-	EXT4_MP_LOCK(mp);
-	ext4_block_cache_write_back(mp->fs.bdev, 1);
-	r = ext4_generic_open2(f, path, flags, filetype, 0, 0);
-	ext4_block_cache_write_back(mp->fs.bdev, 0);
-	EXT4_MP_UNLOCK(mp);
-	return r;
-}
-
 int ext4_fclose(ext4_file *f)
 {
 	ext4_assert(f && f->mp);
@@ -1607,13 +1596,14 @@
 
 uint64_t ext4_fsize(ext4_file *f) { return f->fsize; }
 
-int ext4_fchmod(ext4_file *f, uint32_t mode)
+int ext4_chmod(const char *path, uint32_t mode)
 {
 	int r;
 	uint32_t ino;
+	ext4_file f;
 	struct ext4_sblock *sb;
 	struct ext4_inode_ref inode_ref;
-	struct ext4_mountpoint *mp = f->mp;
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
 
 	if (!mp)
 		return ENOENT;
@@ -1620,7 +1610,14 @@
 
 	EXT4_MP_LOCK(mp);
 
-	ino = f->inode;
+	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
+	ino = f.inode;
+	sb = &mp->fs.sb;
+	ext4_fclose(&f);
 	r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
 	if (r != EOK) {
 		EXT4_MP_UNLOCK(mp);
@@ -1627,7 +1624,6 @@
 		return r;
 	}
 
-	sb = &f->mp->fs.sb;
 	ext4_inode_set_mode(sb, inode_ref.inode, mode);
 	inode_ref.dirty = true;
 
@@ -1636,12 +1632,13 @@
 	return r;
 }
 
-int ext4_fchown(ext4_file *f, uint32_t uid, uint32_t gid)
+int ext4_chown(const char *path, uint32_t uid, uint32_t gid)
 {
 	int r;
+	ext4_file f;
 	uint32_t ino;
 	struct ext4_inode_ref inode_ref;
-	struct ext4_mountpoint *mp = f->mp;
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
 
 	if (!mp)
 		return ENOENT;
@@ -1648,7 +1645,13 @@
 
 	EXT4_MP_LOCK(mp);
 
-	ino = f->inode;
+	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
+	ino = f.inode;
+	ext4_fclose(&f);
 	r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
 	if (r != EOK) {
 		EXT4_MP_UNLOCK(mp);
@@ -1664,12 +1667,13 @@
 	return r;
 }
 
-int ext4_file_set_atime(ext4_file *f, uint32_t atime)
+int ext4_file_set_atime(const char *path, uint32_t atime)
 {
 	int r;
+	ext4_file f;
 	uint32_t ino;
 	struct ext4_inode_ref inode_ref;
-	struct ext4_mountpoint *mp = f->mp;
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
 
 	if (!mp)
 		return ENOENT;
@@ -1676,7 +1680,13 @@
 
 	EXT4_MP_LOCK(mp);
 
-	ino = f->inode;
+	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
+	ino = f.inode;
+	ext4_fclose(&f);
 	r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
 	if (r != EOK) {
 		EXT4_MP_UNLOCK(mp);
@@ -1691,12 +1701,13 @@
 	return r;
 }
 
-int ext4_file_set_mtime(ext4_file *f, uint32_t mtime)
+int ext4_file_set_mtime(const char *path, uint32_t mtime)
 {
 	int r;
+	ext4_file f;
 	uint32_t ino;
 	struct ext4_inode_ref inode_ref;
-	struct ext4_mountpoint *mp = f->mp;
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
 
 	if (!mp)
 		return ENOENT;
@@ -1703,7 +1714,13 @@
 
 	EXT4_MP_LOCK(mp);
 
-	ino = f->inode;
+	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
+	ino = f.inode;
+	ext4_fclose(&f);
 	r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
 	if (r != EOK) {
 		EXT4_MP_UNLOCK(mp);
@@ -1718,12 +1735,13 @@
 	return r;
 }
 
-int ext4_file_set_ctime(ext4_file *f, uint32_t ctime)
+int ext4_file_set_ctime(const char *path, uint32_t ctime)
 {
 	int r;
+	ext4_file f;
 	uint32_t ino;
 	struct ext4_inode_ref inode_ref;
-	struct ext4_mountpoint *mp = f->mp;
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
 
 	if (!mp)
 		return ENOENT;
@@ -1730,7 +1748,13 @@
 
 	EXT4_MP_LOCK(mp);
 
-	ino = f->inode;
+	r = ext4_generic_open2(&f, path, O_RDWR, EXT4_DIRECTORY_FILETYPE_UNKNOWN, 0, 0);
+	if (r != EOK) {
+		EXT4_MP_UNLOCK(mp);
+		return r;
+	}
+	ino = f.inode;
+	ext4_fclose(&f);
 	r = ext4_fs_get_inode_ref(&mp->fs, ino, &inode_ref);
 	if (r != EOK) {
 		EXT4_MP_UNLOCK(mp);
@@ -1837,6 +1861,39 @@
 	r = ext4_generic_open2(&f, path, O_RDWR|O_CREAT, filetype, 0, 0);
 	if (r == EOK)
 		r = ext4_fsymlink_set(&f, target, strlen(target));
+	else
+		goto Finish;
+
+	ext4_fclose(&f);
+
+Finish:
+	ext4_block_cache_write_back(mp->fs.bdev, 0);
+	EXT4_MP_UNLOCK(mp);
+	return r;
+}
+
+int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt)
+{
+	struct ext4_mountpoint *mp = ext4_get_mount(path);
+	int r;
+	ext4_file f;
+	int filetype;
+
+	if (!mp)
+		return ENOENT;
+
+	if (!buf)
+		return EINVAL;
+
+	memset(buf, 0, sizeof(bufsize));
+
+	filetype = EXT4_DIRECTORY_FILETYPE_SYMLINK;
+
+	EXT4_MP_LOCK(mp);
+	ext4_block_cache_write_back(mp->fs.bdev, 1);
+	r = ext4_generic_open2(&f, path, O_RDONLY, filetype, 0, 0);
+	if (r == EOK)
+		r = ext4_fread(&f, buf, bufsize, rcnt);
 	else
 		goto Finish;
 
--- a/lwext4/ext4.h
+++ b/lwext4/ext4.h
@@ -297,7 +297,7 @@
  *          /my_partition/my_file
  * @param   flags open file flags
  * @return  standard error code*/
-int ext4_fopen2(ext4_file *f, const char *path, int flags, bool file_expect);
+int ext4_fopen2(ext4_file *f, const char *path, int flags);
 
 /**@brief   File close function.
  * @param   f file handle
@@ -305,11 +305,12 @@
 int ext4_fclose(ext4_file *f);
 
 /**@brief   Fill in the ext4_inode buffer.
- * @param   mount_point
- * @param   inode no.
+ * @param   path fetch inode data of the path
+ * @param   ret_ino the inode id of the path
  * @param   ext4_inode buffer
  * @return  standard error code*/
-int ext4_fill_raw_inode(const char *mount_point, uint32_t ino,
+int ext4_fill_raw_inode(const char *path,
+			uint32_t *ret_ino,
 			struct ext4_inode *inode);
 
 /**@brief   File truncate function.
@@ -354,13 +355,15 @@
  * @return  file size */
 uint64_t ext4_fsize(ext4_file *f);
 
-int ext4_fchmod(ext4_file *f, uint32_t mode);
-int ext4_fchown(ext4_file *f, uint32_t uid, uint32_t gid);
-int ext4_file_set_atime(ext4_file *f, uint32_t atime);
-int ext4_file_set_mtime(ext4_file *f, uint32_t mtime);
-int ext4_file_set_ctime(ext4_file *f, uint32_t ctime);
+int ext4_chmod(const char *path, uint32_t mode);
+int ext4_chown(const char *path, uint32_t uid, uint32_t gid);
+int ext4_file_set_atime(const char *path, uint32_t atime);
+int ext4_file_set_mtime(const char *path, uint32_t mtime);
+int ext4_file_set_ctime(const char *path, uint32_t ctime);
 
 int ext4_fsymlink(const char *target, const char *path);
+
+int ext4_readlink(const char *path, char *buf, size_t bufsize, size_t *rcnt);
 
 /*********************************DIRECTORY OPERATION***********************/
 
--- a/lwext4/ext4_config.h
+++ b/lwext4/ext4_config.h
@@ -112,6 +112,11 @@
 #define CONFIG_DEBUG_ASSERT 1
 #endif
 
+/**@brief   Include assert codes from ext4_debug or standard library.*/
+#ifndef CONFIG_HAVE_OWN_ASSERT
+#define CONFIG_HAVE_OWN_ASSERT 1
+#endif
+
 /**@brief   Statistics of block device*/
 #ifndef CONFIG_BLOCK_DEV_ENABLE_STATS
 #define CONFIG_BLOCK_DEV_ENABLE_STATS 1
--- a/lwext4/ext4_debug.h
+++ b/lwext4/ext4_debug.h
@@ -40,6 +40,10 @@
 #include "ext4_config.h"
 #include "ext4_errno.h"
 
+#if !CONFIG_HAVE_OWN_ASSERT
+#include <assert.h>
+#endif
+
 #include <stdint.h>
 #include <stdio.h>
 
@@ -101,13 +105,17 @@
 
 #if CONFIG_DEBUG_ASSERT
 /**@brief   Debug assertion.*/
-#define ext4_assert(_v)                                                        \
-	do {                                                                   \
-		if (!(_v)) {                                                   \
-			printf("Assertion failed:\nmodule: %s\nline: %d\n",    \
-			       __FILE__, __LINE__);                            \
-		}                                                              \
-	} while (0)
+ #if CONFIG_HAVE_OWN_ASSERT
+ #define ext4_assert(_v)                                                        \
+ 	do {                                                                   \
+ 		if (!(_v)) {                                                   \
+ 			printf("Assertion failed:\nmodule: %s\nline: %d\n",    \
+ 			       __FILE__, __LINE__);                            \
+ 		}                                                              \
+ 	} while (0)
+ #else
+ #define ext4_assert(_v) assert(_v)
+ #endif
 #else
 #define ext4_assert(_v)
 #endif
--- a/toolchain/generic.cmake
+++ b/toolchain/generic.cmake
@@ -13,9 +13,12 @@
 set(CMAKE_C_FLAGS   "-std=gnu99 -fdata-sections -ffunction-sections" CACHE INTERNAL "c compiler flags")
 set(CMAKE_CXX_FLAGS "-fdata-sections -ffunction-sections" CACHE INTERNAL "cxx compiler flags")
 set(CMAKE_ASM_FLAGS "" CACHE INTERNAL "asm compiler flags")
-set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections" CACHE INTERNAL "exe link flags")
+if (APPLE)
+    set(CMAKE_EXE_LINKER_FLAGS "-dead_strip" CACHE INTERNAL "exe link flags")
+else (APPLE)
+    set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections" CACHE INTERNAL "exe link flags")
+endif (APPLE)
 
-
 SET(CMAKE_C_FLAGS_DEBUG "-O0 -g -ggdb3" CACHE INTERNAL "c debug compiler flags")
 SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g -ggdb3" CACHE INTERNAL "cxx debug compiler flags")
 SET(CMAKE_ASM_FLAGS_DEBUG "-g -ggdb3" CACHE INTERNAL "asm debug compiler flags")
@@ -22,4 +25,4 @@
 
 SET(CMAKE_C_FLAGS_RELEASE "-O2" CACHE INTERNAL "c release compiler flags")
 SET(CMAKE_CXX_FLAGS_RELEASE "-O2" CACHE INTERNAL "cxx release compiler flags")
-SET(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm release compiler flags")
\ No newline at end of file
+SET(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "asm release compiler flags")