diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_lock.c b/modules/src/test_lock.c deleted file mode 100644 index 0e061e7..0000000 --- a/modules/src/test_lock.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file test_lock.c - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをロックします。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param is_share (true/false) 共有ロック/排他ロック - * @return true/false (生成成功/生成失敗) - */ -bool can_lock(const char* pathname, bool is_share) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // fd 取得 - int fd = open(pathname, O_RDONLY); - if (fd == -1) - { - fd = open(pathname, O_WRONLY); - if (fd == -1) - { - return false; - } - } - - int lock_type = (is_share) ? LOCK_SH : LOCK_EX; - int ret = flock(fd, lock_type); - bool is_success = (ret == 0); - if (is_success) - { - flock(fd, LOCK_UN); - } - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_lock.c b/modules/src/test_lock.c deleted file mode 100644 index 0e061e7..0000000 --- a/modules/src/test_lock.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file test_lock.c - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをロックします。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param is_share (true/false) 共有ロック/排他ロック - * @return true/false (生成成功/生成失敗) - */ -bool can_lock(const char* pathname, bool is_share) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // fd 取得 - int fd = open(pathname, O_RDONLY); - if (fd == -1) - { - fd = open(pathname, O_WRONLY); - if (fd == -1) - { - return false; - } - } - - int lock_type = (is_share) ? LOCK_SH : LOCK_EX; - int ret = flock(fd, lock_type); - bool is_success = (ret == 0); - if (is_success) - { - flock(fd, LOCK_UN); - } - return is_success; -} - diff --git a/modules/src/test_read.c b/modules/src/test_read.c deleted file mode 100644 index 44245e1..0000000 --- a/modules/src/test_read.c +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file test_read.c - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが読込可能か確認します。 - * - * @param pathname パス - * @return true/false (読込可能/読込不可) - */ -bool can_read(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "r"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_lock.c b/modules/src/test_lock.c deleted file mode 100644 index 0e061e7..0000000 --- a/modules/src/test_lock.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file test_lock.c - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをロックします。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param is_share (true/false) 共有ロック/排他ロック - * @return true/false (生成成功/生成失敗) - */ -bool can_lock(const char* pathname, bool is_share) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // fd 取得 - int fd = open(pathname, O_RDONLY); - if (fd == -1) - { - fd = open(pathname, O_WRONLY); - if (fd == -1) - { - return false; - } - } - - int lock_type = (is_share) ? LOCK_SH : LOCK_EX; - int ret = flock(fd, lock_type); - bool is_success = (ret == 0); - if (is_success) - { - flock(fd, LOCK_UN); - } - return is_success; -} - diff --git a/modules/src/test_read.c b/modules/src/test_read.c deleted file mode 100644 index 44245e1..0000000 --- a/modules/src/test_read.c +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file test_read.c - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが読込可能か確認します。 - * - * @param pathname パス - * @return true/false (読込可能/読込不可) - */ -bool can_read(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "r"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/modules/src/test_rename.c b/modules/src/test_rename.c deleted file mode 100644 index a1b6219..0000000 --- a/modules/src/test_rename.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_rename.c - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをリネームします。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param oldpath リネーム前のパス - * @param newpath リネーム後のパス - * @return true/false (リネーム成功/リネーム失敗) - */ -bool can_rename(const char* oldpath, const char* newpath) -{ - // リネーム前のパス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(oldpath); - if (is_error) - { - errno = ENOENT; - return false; - } - - // リネーム - int ret = rename(oldpath, newpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_lock.c b/modules/src/test_lock.c deleted file mode 100644 index 0e061e7..0000000 --- a/modules/src/test_lock.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file test_lock.c - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをロックします。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param is_share (true/false) 共有ロック/排他ロック - * @return true/false (生成成功/生成失敗) - */ -bool can_lock(const char* pathname, bool is_share) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // fd 取得 - int fd = open(pathname, O_RDONLY); - if (fd == -1) - { - fd = open(pathname, O_WRONLY); - if (fd == -1) - { - return false; - } - } - - int lock_type = (is_share) ? LOCK_SH : LOCK_EX; - int ret = flock(fd, lock_type); - bool is_success = (ret == 0); - if (is_success) - { - flock(fd, LOCK_UN); - } - return is_success; -} - diff --git a/modules/src/test_read.c b/modules/src/test_read.c deleted file mode 100644 index 44245e1..0000000 --- a/modules/src/test_read.c +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file test_read.c - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが読込可能か確認します。 - * - * @param pathname パス - * @return true/false (読込可能/読込不可) - */ -bool can_read(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "r"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/modules/src/test_rename.c b/modules/src/test_rename.c deleted file mode 100644 index a1b6219..0000000 --- a/modules/src/test_rename.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_rename.c - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをリネームします。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param oldpath リネーム前のパス - * @param newpath リネーム後のパス - * @return true/false (リネーム成功/リネーム失敗) - */ -bool can_rename(const char* oldpath, const char* newpath) -{ - // リネーム前のパス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(oldpath); - if (is_error) - { - errno = ENOENT; - return false; - } - - // リネーム - int ret = rename(oldpath, newpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_write.c b/modules/src/test_write.c deleted file mode 100644 index e56d63c..0000000 --- a/modules/src/test_write.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが書込可能か確認します。 - * - * @param pathname パス - * @return true/false (書込可能/書込不可) - */ -bool can_write(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスが追記可能か確認します。 - * - * @param pathname パス - * @return true/false (追記可能/追記不可) - */ -bool can_append(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "a"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_lock.c b/modules/src/test_lock.c deleted file mode 100644 index 0e061e7..0000000 --- a/modules/src/test_lock.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file test_lock.c - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをロックします。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param is_share (true/false) 共有ロック/排他ロック - * @return true/false (生成成功/生成失敗) - */ -bool can_lock(const char* pathname, bool is_share) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // fd 取得 - int fd = open(pathname, O_RDONLY); - if (fd == -1) - { - fd = open(pathname, O_WRONLY); - if (fd == -1) - { - return false; - } - } - - int lock_type = (is_share) ? LOCK_SH : LOCK_EX; - int ret = flock(fd, lock_type); - bool is_success = (ret == 0); - if (is_success) - { - flock(fd, LOCK_UN); - } - return is_success; -} - diff --git a/modules/src/test_read.c b/modules/src/test_read.c deleted file mode 100644 index 44245e1..0000000 --- a/modules/src/test_read.c +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file test_read.c - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが読込可能か確認します。 - * - * @param pathname パス - * @return true/false (読込可能/読込不可) - */ -bool can_read(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "r"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/modules/src/test_rename.c b/modules/src/test_rename.c deleted file mode 100644 index a1b6219..0000000 --- a/modules/src/test_rename.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_rename.c - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをリネームします。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param oldpath リネーム前のパス - * @param newpath リネーム後のパス - * @return true/false (リネーム成功/リネーム失敗) - */ -bool can_rename(const char* oldpath, const char* newpath) -{ - // リネーム前のパス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(oldpath); - if (is_error) - { - errno = ENOENT; - return false; - } - - // リネーム - int ret = rename(oldpath, newpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_write.c b/modules/src/test_write.c deleted file mode 100644 index e56d63c..0000000 --- a/modules/src/test_write.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが書込可能か確認します。 - * - * @param pathname パス - * @return true/false (書込可能/書込不可) - */ -bool can_write(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスが追記可能か確認します。 - * - * @param pathname パス - * @return true/false (追記可能/追記不可) - */ -bool can_append(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "a"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/modules/test/Makefile b/modules/test/Makefile deleted file mode 100644 index 9dcb20d..0000000 --- a/modules/test/Makefile +++ /dev/null @@ -1,48 +0,0 @@ -# ============================================================================== -# Makefile -# ============================================================================== -# -# TOPDIR : トップディレクトリ -# RULEDIR : Meke のルール一式が格納されているディレクトリ -# NAME : モジュール名 (拡張子を含めないこと) -# TARGET : モジュールファイル名 (拡張子を含めること) -# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) -# -TOPDIR ?= ../.. -RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe -TARGET = $(NAME) -SUBDIRS = -USE_SO_VERSION = - -# ------------------------------------------------------------------------------ -# *-cmd.mk : コマンド -# *-conf.mk : 設定 -# *-auto.mk : 自動設定 -# ------------------------------------------------------------------------------ -include $(TOPDIR)/config.mk -include $(RULEDIR)/*-cmd.mk -include $(RULEDIR)/*-conf.mk -include $(RULEDIR)/*-auto.mk -# ------------------------------------------------------------------------------ -# -# 以下、オプションを適宜変更してください。 -# - -INCLUDES += -I$(TOPDIR)/include -CFLAGS += -CXXFLAGS += -LDFLAGS += -LIBS += -L$(TOPDIR)/lib - -CLEAN_FILES += -CLEAN_DIRS += - -.DEFAULT_GOAL := all - -# ------------------------------------------------------------------------------ -# *-rule : ルール -# ------------------------------------------------------------------------------ -include $(RULEDIR)/*-rule.mk -# ------------------------------------------------------------------------------ - diff --git a/mk/release-rule.mk b/mk/release-rule.mk index 8862c62..c133521 100644 --- a/mk/release-rule.mk +++ b/mk/release-rule.mk @@ -1,14 +1,18 @@ # ------------------------------------------------------------------------------ # release ルール # ------------------------------------------------------------------------------ -.PHONY: release -release: -ifeq ($(strip $(TOPDIR)),.) - $(MAKE) clean - $(MAKE) all +#ifeq ($(strip $(TOPDIR)),.) +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin +# $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib +#endif + +.PHONY: release-dir +release-dir: $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/bin $(MKDIR) -p $(TOPDIR)/$(RELEASEDIR)/lib -endif + +.PHONY: release +release: clean all release-dir @for subdir in $(SUBDIRS); do \ $(MAKE) release -C $$subdir; \ done @@ -22,4 +26,8 @@ $(CP) $(TARGET) $(TOPDIR)/$(RELEASEDIR)/bin/ endif endif +ifneq ($(strip $(SCRIPTS)),) + $(CP) $(SCRIPTS) $(TOPDIR)/$(RELEASEDIR)/bin/ +endif + diff --git a/modules/Makefile b/modules/Makefile index de3dcff..ad6ba91 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -10,9 +10,10 @@ # TOPDIR ?= .. RULEDIR ?= $(TOPDIR)/mk -NAME = access-test -TARGET = $(NAME) -SUBDIRS = +NAME = +TARGET = +SCRIPTS = +SUBDIRS = main USE_SO_VERSION = # ------------------------------------------------------------------------------ diff --git a/modules/include/filetype.h b/modules/include/filetype.h deleted file mode 100644 index 18c833e..0000000 --- a/modules/include/filetype.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file filetype.h - * @brief ファイル種別を扱います。 - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef FILETYPE_H -#define FILETYPE_H - -#include - -char filetype_get(const char* pathname); -bool is_exists(const char* pathname); - - -#endif // FILETYPE_H - diff --git a/modules/include/test_create.h b/modules/include/test_create.h deleted file mode 100644 index 889f8ef..0000000 --- a/modules/include/test_create.h +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @file test_create.h - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_CREATE_H -#define TEST_CREATE_H - -#include - - -bool can_create_file(const char* pathname); -bool can_create_directory(const char* pathname); -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); -bool can_create_socket(const char* pathname); -bool can_create_fifo(const char* pathname); -bool can_create_symbolic_link(const char* pathname, const char* target); - - -#endif // TEST_CREATE_H - diff --git a/modules/include/test_delete.h b/modules/include/test_delete.h deleted file mode 100644 index d77c77e..0000000 --- a/modules/include/test_delete.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_delete.h - * @brief delete テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_DELETE_H -#define TEST_DELETE_H - -#include - - -bool can_delete(const char* pathname); - - -#endif // TEST_DELETE_H - diff --git a/modules/include/test_exec.h b/modules/include/test_exec.h deleted file mode 100644 index d999287..0000000 --- a/modules/include/test_exec.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_exec.h - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_EXEC_H -#define TEST_EXEC_H - -#include - - -bool can_exec(const char* pathname); - - -#endif // TEST_EXEC_H - diff --git a/modules/include/test_link.h b/modules/include/test_link.h deleted file mode 100644 index e2f4e3a..0000000 --- a/modules/include/test_link.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_link.h - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LINK_H -#define TEST_LINK_H - -#include - - -bool can_link(const char* pathname, const char* linkpath); - - -#endif // TEST_LINK_H - diff --git a/modules/include/test_lock.h b/modules/include/test_lock.h deleted file mode 100644 index 939fcba..0000000 --- a/modules/include/test_lock.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_lock.h - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_LOCK_H -#define TEST_LOCK_H - -#include - - -bool can_lock(const char* pathname, bool is_share); - - -#endif // TEST_LOCK_H - diff --git a/modules/include/test_read.h b/modules/include/test_read.h deleted file mode 100644 index a1a6787..0000000 --- a/modules/include/test_read.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_read.h - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_READ_H -#define TEST_READ_H - -#include - - -bool can_read(const char* pathname); - - -#endif // TEST_READ_H - diff --git a/modules/include/test_rename.h b/modules/include/test_rename.h deleted file mode 100644 index f6eb30b..0000000 --- a/modules/include/test_rename.h +++ /dev/null @@ -1,17 +0,0 @@ -/** - * @file test_rename.h - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_RENAME_H -#define TEST_RENAME_H - -#include - - -bool can_rename(const char* oldpath, const char* newpath); - - -#endif // TEST_RENAME_H - diff --git a/modules/include/test_write.h b/modules/include/test_write.h deleted file mode 100644 index 6fce889..0000000 --- a/modules/include/test_write.h +++ /dev/null @@ -1,18 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#ifndef TEST_WRITE_H -#define TEST_WRITE_H - -#include - - -bool can_write(const char* pathname); -bool can_append(const char* pathname); - - -#endif // TEST_WRITE_H - diff --git a/modules/main/Makefile b/modules/main/Makefile new file mode 100644 index 0000000..8eef71a --- /dev/null +++ b/modules/main/Makefile @@ -0,0 +1,49 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = access-test +TARGET = $(NAME) +SCRIPTS = scripts/test.sh +SUBDIRS = test +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/include/filetype.h b/modules/main/include/filetype.h new file mode 100644 index 0000000..18c833e --- /dev/null +++ b/modules/main/include/filetype.h @@ -0,0 +1,17 @@ +/** + * @file filetype.h + * @brief ファイル種別を扱います。 + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef FILETYPE_H +#define FILETYPE_H + +#include + +char filetype_get(const char* pathname); +bool is_exists(const char* pathname); + + +#endif // FILETYPE_H + diff --git a/modules/main/include/test_create.h b/modules/main/include/test_create.h new file mode 100644 index 0000000..889f8ef --- /dev/null +++ b/modules/main/include/test_create.h @@ -0,0 +1,23 @@ +/** + * @file test_create.h + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_CREATE_H +#define TEST_CREATE_H + +#include + + +bool can_create_file(const char* pathname); +bool can_create_directory(const char* pathname); +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min); +bool can_create_socket(const char* pathname); +bool can_create_fifo(const char* pathname); +bool can_create_symbolic_link(const char* pathname, const char* target); + + +#endif // TEST_CREATE_H + diff --git a/modules/main/include/test_delete.h b/modules/main/include/test_delete.h new file mode 100644 index 0000000..d77c77e --- /dev/null +++ b/modules/main/include/test_delete.h @@ -0,0 +1,17 @@ +/** + * @file test_delete.h + * @brief delete テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_DELETE_H +#define TEST_DELETE_H + +#include + + +bool can_delete(const char* pathname); + + +#endif // TEST_DELETE_H + diff --git a/modules/main/include/test_exec.h b/modules/main/include/test_exec.h new file mode 100644 index 0000000..d999287 --- /dev/null +++ b/modules/main/include/test_exec.h @@ -0,0 +1,17 @@ +/** + * @file test_exec.h + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_EXEC_H +#define TEST_EXEC_H + +#include + + +bool can_exec(const char* pathname); + + +#endif // TEST_EXEC_H + diff --git a/modules/main/include/test_link.h b/modules/main/include/test_link.h new file mode 100644 index 0000000..e2f4e3a --- /dev/null +++ b/modules/main/include/test_link.h @@ -0,0 +1,17 @@ +/** + * @file test_link.h + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LINK_H +#define TEST_LINK_H + +#include + + +bool can_link(const char* pathname, const char* linkpath); + + +#endif // TEST_LINK_H + diff --git a/modules/main/include/test_lock.h b/modules/main/include/test_lock.h new file mode 100644 index 0000000..939fcba --- /dev/null +++ b/modules/main/include/test_lock.h @@ -0,0 +1,17 @@ +/** + * @file test_lock.h + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_LOCK_H +#define TEST_LOCK_H + +#include + + +bool can_lock(const char* pathname, bool is_share); + + +#endif // TEST_LOCK_H + diff --git a/modules/main/include/test_read.h b/modules/main/include/test_read.h new file mode 100644 index 0000000..a1a6787 --- /dev/null +++ b/modules/main/include/test_read.h @@ -0,0 +1,17 @@ +/** + * @file test_read.h + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_READ_H +#define TEST_READ_H + +#include + + +bool can_read(const char* pathname); + + +#endif // TEST_READ_H + diff --git a/modules/main/include/test_rename.h b/modules/main/include/test_rename.h new file mode 100644 index 0000000..f6eb30b --- /dev/null +++ b/modules/main/include/test_rename.h @@ -0,0 +1,17 @@ +/** + * @file test_rename.h + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_RENAME_H +#define TEST_RENAME_H + +#include + + +bool can_rename(const char* oldpath, const char* newpath); + + +#endif // TEST_RENAME_H + diff --git a/modules/main/include/test_write.h b/modules/main/include/test_write.h new file mode 100644 index 0000000..6fce889 --- /dev/null +++ b/modules/main/include/test_write.h @@ -0,0 +1,18 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#ifndef TEST_WRITE_H +#define TEST_WRITE_H + +#include + + +bool can_write(const char* pathname); +bool can_append(const char* pathname); + + +#endif // TEST_WRITE_H + diff --git a/modules/main/scripts/test.sh b/modules/main/scripts/test.sh new file mode 100755 index 0000000..13f4793 --- /dev/null +++ b/modules/main/scripts/test.sh @@ -0,0 +1,2 @@ +#!/bin/sh + diff --git a/modules/main/src/filetype.c b/modules/main/src/filetype.c new file mode 100644 index 0000000..f51d104 --- /dev/null +++ b/modules/main/src/filetype.c @@ -0,0 +1,59 @@ +#include +#include +#include + +#include + + +/** + * 指定されたパスの次の対応する種別シンボルを返します。 + * pathname の情報を取得できない場合、'-' を返します。 + * - r : 通常ファイル + * - d : ディレクトリ + * - c : キャラクタデバイス + * - b : ブロックデバイス + * - s : ソケット + * - p : 名前付きパイプ + * - l : シンボリックリンク + * + * @param pathname パス + * @return 種別シンボル + */ +char filetype_get(const char* pathname) +{ + struct stat st; + char type_symbol = '-'; + + int ret = stat(pathname, &st); + if (ret == 0) + { // ファイル情報取得成功 + int type = (st.st_mode & S_IFMT); + switch (type) + { + case S_IFSOCK: type_symbol = 's'; break; + case S_IFLNK: type_symbol = 'l'; break; + case S_IFREG: type_symbol = 'r'; break; + case S_IFBLK: type_symbol = 'b'; break; + case S_IFDIR: type_symbol = 'd'; break; + case S_IFCHR: type_symbol = 'c'; break; + case S_IFIFO: type_symbol = 'p'; break; + default: type_symbol = '-'; break; + } + } + return type_symbol; +} + + +/** + * 指定されたパスが存在するか否かを返します。 + * + * @param pathname パス名 + * @return true/false (存在する/存在しない) + */ +bool is_exists(const char* pathname) +{ + struct stat st; + int ret = stat(pathname, &st); + return (ret == 0); +} + diff --git a/modules/main/src/main.c b/modules/main/src/main.c new file mode 100644 index 0000000..7b551f8 --- /dev/null +++ b/modules/main/src/main.c @@ -0,0 +1,356 @@ +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" +#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" + +#ifdef UNITTEST +#define main test_main +#endif + +typedef struct { + const char* command; + void (*cmd_func)(int argc, char* argv[]); +} CommandFunction; + +void exec_command(int argc, char* argv[]); +void print_usage(int argc, char* argv[]); +void cmd_read(int argc, char* argv[]); +void cmd_write(int argc, char* argv[]); +void cmd_append(int argc, char* argv[]); +void cmd_create(int argc, char* argv[]); +void cmd_delete(int argc, char* argv[]); +void cmd_rename(int argc, char* argv[]); +void cmd_link(int argc, char* argv[]); +void cmd_lock(int argc, char* argv[]); +void cmd_exec(int argc, char* argv[]); + +CommandFunction FUNC_TABLE[] = { + { "read" , cmd_read }, + { "write" , cmd_write }, + { "append" , cmd_append }, + { "create" , cmd_create }, + { "delete" , cmd_delete }, + { "rename" , cmd_rename }, + { "link" , cmd_link }, + { "lock" , cmd_lock }, + { "exec" , cmd_exec }, + { "--help" , print_usage }, + { NULL , NULL } +}; + + + + + + + +int main(int argc, char* argv[]) +{ + exec_command(argc, argv); + /* + if (argc < 2) + { + print_usage(); + return 1; + } + + const char* pathname = argv[1]; + */ + /* + bool ret = is_exists(pathname); + printf("isexists = %d\n", ret); + */ + + /* + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_create_file(buf); + printf("file = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); + ret = can_create_directory(buf); + printf("directory = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[r] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); + ret = can_create_character_device_file(buf, 1, 8); + printf("cdev[w] = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); + ret = can_create_block_device_file(buf, 7, 15); + printf("bdev = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); + ret = can_create_socket(buf); + printf("socket = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); + ret = can_create_fifo(buf); + printf("pipe = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); + ret = can_create_symbolic_link(buf, "access-test"); + printf("link = %d\n", ret); + + snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); + ret = can_delete(buf); + printf("ret = %d\n", ret); + */ + + + + return 0; +} + + + + + +void exec_command(int argc, char* argv[]) +{ + if (argc >= 2) + { + CommandFunction* cf = &FUNC_TABLE[0]; + while (cf->command != NULL) + { + if (strcmp(cf->command, argv[1]) == 0) + { + cf->cmd_func(argc, argv); + return; + } + cf++; + } + } + // ここに来たらエラー + print_usage(argc, argv); +} + + +// read [pathname] +void cmd_read(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_read(pathname); + printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_write(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + +void cmd_append(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_write(pathname); + printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_create(int argc, char* argv[]) +{ + if (argc >= 4) + { + int maj, min; + const char* cmdname; + const char* pathname = argv[2]; + char type = argv[3][0]; + bool ret = false; + switch (type) + { + case 'r': + cmdname = "create[r]"; + ret = can_create_file(pathname); + break; + case 'd': + cmdname = "create[d]"; + ret = can_create_directory(pathname); + break; + case 'c': + cmdname = "create[c]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_character_device_file(pathname, maj, min); + break; + case 'b': + cmdname = "create[b]"; + maj = (argc >= 5) ? atoi(argv[4]) : 1; + min = (argc >= 6) ? atoi(argv[5]) : 3; + ret = can_create_block_device_file(pathname, maj, min); + break; + case 's': + cmdname = "create[s]"; + ret = can_create_socket(pathname); + break; + case 'p': + cmdname = "create[p]"; + ret = can_create_fifo(pathname); + break; + case 'l': + cmdname = "create[l]"; + if (argc >= 5) + { + const char* target = argv[4]; + ret = can_create_symbolic_link(pathname, target); + } + else + { + print_usage(argc, argv); + return; + } + break; + default: + cmdname = "create[-]"; + ret = false; + break; + } + printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); + + } + else + { + print_usage(argc, argv); + } +} +void cmd_delete(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_delete(pathname); + printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_rename(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* newpath = argv[3]; + bool ret = can_rename(pathname, newpath); + printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_link(int argc, char* argv[]) +{ + if (argc >= 4) + { + const char* pathname = argv[2]; + const char* linkpath = argv[3]; + bool ret = can_link(pathname, linkpath); + printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_lock(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_lock(pathname, false); + printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} +void cmd_exec(int argc, char* argv[]) +{ + if (argc >= 3) + { + const char* pathname = argv[2]; + bool ret = can_exec(pathname); + printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); + } + else + { + print_usage(argc, argv); + } +} + + +/** + * Usage 出力します。 + */ +void print_usage(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("Usage\n"); + printf("./test-access [command] [command args...]\n"); + printf("\n"); + printf("command:\n"); + printf(" read : read [pathname]\n"); + printf(" write : write [pathname]\n"); + printf(" append : append [pathname]\n"); + printf(" create : create [pathname] [args]\n"); + printf(" args:\n"); + printf(" r : regular file\n"); + printf(" d : directory\n"); + printf(" c [major] [minor] : character device\n"); + printf(" Ex. create [pathname] c 1 3 --> null\n"); + printf(" Ex. create [pathname] c 1 8 --> random\n"); + printf(" b [major] [minor] : block device\n"); + printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); + printf(" s : socket\n"); + printf(" p : pipe\n"); + printf(" l : link [frompath]\n"); + printf(" delete : delete [pathname]\n"); + printf(" rename : rename [pathname] [newpathname]\n"); + printf(" link : link [pathname] [linkpath]\n"); + printf(" lock : lock [pathname]\n"); + printf(" exec : exec [pathname]\n"); + printf("\n"); +} + diff --git a/modules/main/src/test_create.c b/modules/main/src/test_create.c new file mode 100644 index 0000000..aefd674 --- /dev/null +++ b/modules/main/src/test_create.c @@ -0,0 +1,218 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include + + +/** + * 指定されたパスのファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_file(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ファイル生成 + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスのディレクトリを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_directory(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ディレクトリ生成 + // mode = 0755 + mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + int ret = mkdir(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのキャラクタデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // キャラクタデバイスファイル生成 + // mode = 0020755 + mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのブロックデバイスファイルを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @param major major + * @param minor minor + * @return true/false (生成成功/生成失敗) + */ +bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ブロックデバイスファイル生成 + // mode = 0060755 + mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; + dev_t dev = makedev(maj, min); + int ret = mknod(pathname, mode, dev); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのソケットを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_socket(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // ソケット生成 + int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); + bool is_success = false; + if (sockfd != -1) + { + struct sockaddr_un name; + memset(&name, 0, sizeof(name)); + name.sun_family = AF_UNIX; + strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); + int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); + is_success = (ret != -1); + close(sockfd); + } + return is_success; +} + + +/** + * 指定されたパスのFIFO(名前付きパイプ)を生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_fifo(const char* pathname) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // FIFO(名前付きパイプ)生成 + // mode = 0644 + mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; + int ret = mkfifo(pathname, mode); + bool is_success = (ret == 0); + return is_success; +} + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 + * + * @param pathname 作成するシンボリックリンク + * @param frompath 作成元のパス + * @return true/false (生成成功/生成失敗) + */ +bool can_create_symbolic_link(const char* pathname, const char* frompath) +{ + // パス存在有無確認 (存在する場合 NG) + bool is_error = is_exists(pathname); + if (is_error) + { + errno = EEXIST; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(frompath, pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_delete.c b/modules/main/src/test_delete.c new file mode 100644 index 0000000..6380594 --- /dev/null +++ b/modules/main/src/test_delete.c @@ -0,0 +1,38 @@ +/** + * @file test_create.c + * @brief create テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスを削除します。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @return true/false (生成成功/生成失敗) + */ +bool can_delete(const char* pathname) +{ + // パス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 削除 + int ret = remove(pathname); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_exec.c b/modules/main/src/test_exec.c new file mode 100644 index 0000000..d58e912 --- /dev/null +++ b/modules/main/src/test_exec.c @@ -0,0 +1,44 @@ +/** + * @file test_exec.c + * @brief exec テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include +#include + +#include +#include + +#include +#include + + +/** + * 指定されたパスを実行します。 + * + * @param pathname パス + * @return true/false (実行成功/実行失敗) + */ +bool can_exec(const char* pathname) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // 実行 + int ret = system(pathname); + if (ret == -1) + { // 実行エラー + return false; + } + + bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); + return is_success; +} + diff --git a/modules/main/src/test_link.c b/modules/main/src/test_link.c new file mode 100644 index 0000000..c8cee12 --- /dev/null +++ b/modules/main/src/test_link.c @@ -0,0 +1,39 @@ +/** + * @file test_link.c + * @brief link テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスのシンボリックリンクを生成します。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param linkpath シンボリックリンク先 + * @return true/false (生成成功/生成失敗) + */ +bool can_link(const char* pathname, const char* linkpath) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // シンボリックリンク生成 + int ret = symlink(pathname, linkpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_lock.c b/modules/main/src/test_lock.c new file mode 100644 index 0000000..0e061e7 --- /dev/null +++ b/modules/main/src/test_lock.c @@ -0,0 +1,54 @@ +/** + * @file test_lock.c + * @brief lock テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをロックします。 + * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param pathname パス + * @param is_share (true/false) 共有ロック/排他ロック + * @return true/false (生成成功/生成失敗) + */ +bool can_lock(const char* pathname, bool is_share) +{ + // パス存在有無確認 (しない場合 NG) + bool is_error = !is_exists(pathname); + if (is_error) + { + errno = ENOENT; + return false; + } + + // fd 取得 + int fd = open(pathname, O_RDONLY); + if (fd == -1) + { + fd = open(pathname, O_WRONLY); + if (fd == -1) + { + return false; + } + } + + int lock_type = (is_share) ? LOCK_SH : LOCK_EX; + int ret = flock(fd, lock_type); + bool is_success = (ret == 0); + if (is_success) + { + flock(fd, LOCK_UN); + } + return is_success; +} + diff --git a/modules/main/src/test_read.c b/modules/main/src/test_read.c new file mode 100644 index 0000000..44245e1 --- /dev/null +++ b/modules/main/src/test_read.c @@ -0,0 +1,28 @@ +/** + * @file test_read.c + * @brief read テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが読込可能か確認します。 + * + * @param pathname パス + * @return true/false (読込可能/読込不可) + */ +bool can_read(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "r"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/src/test_rename.c b/modules/main/src/test_rename.c new file mode 100644 index 0000000..a1b6219 --- /dev/null +++ b/modules/main/src/test_rename.c @@ -0,0 +1,39 @@ +/** + * @file test_rename.c + * @brief rename テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + +#include + +#include +#include + + +/** + * 指定されたパスをリネームします。 + * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 + * + * @param oldpath リネーム前のパス + * @param newpath リネーム後のパス + * @return true/false (リネーム成功/リネーム失敗) + */ +bool can_rename(const char* oldpath, const char* newpath) +{ + // リネーム前のパス存在有無確認 (存在しない場合 NG) + bool is_error = !is_exists(oldpath); + if (is_error) + { + errno = ENOENT; + return false; + } + + // リネーム + int ret = rename(oldpath, newpath); + bool is_success = (ret == 0); + return is_success; +} + diff --git a/modules/main/src/test_write.c b/modules/main/src/test_write.c new file mode 100644 index 0000000..e56d63c --- /dev/null +++ b/modules/main/src/test_write.c @@ -0,0 +1,47 @@ +/** + * @file test_write.h + * @brief write テスト + * @author Nomura Kei + * @copyright 2008 Nomura Kei + */ +#include +#include + + +/** + * 指定されたパスが書込可能か確認します。 + * + * @param pathname パス + * @return true/false (書込可能/書込不可) + */ +bool can_write(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "w"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + + +/** + * 指定されたパスが追記可能か確認します。 + * + * @param pathname パス + * @return true/false (追記可能/追記不可) + */ +bool can_append(const char* pathname) +{ + bool is_success = false; + FILE* fp = fopen(pathname, "a"); + if (fp != NULL) + { + is_success = true; + fclose(fp); + } + return is_success; +} + diff --git a/modules/main/test/Makefile b/modules/main/test/Makefile new file mode 100644 index 0000000..0d89e48 --- /dev/null +++ b/modules/main/test/Makefile @@ -0,0 +1,48 @@ +# ============================================================================== +# Makefile +# ============================================================================== +# +# TOPDIR : トップディレクトリ +# RULEDIR : Meke のルール一式が格納されているディレクトリ +# NAME : モジュール名 (拡張子を含めないこと) +# TARGET : モジュールファイル名 (拡張子を含めること) +# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) +# +TOPDIR ?= ../../.. +RULEDIR ?= $(TOPDIR)/mk +NAME = ut.exe +TARGET = $(NAME) +SUBDIRS = +USE_SO_VERSION = + +# ------------------------------------------------------------------------------ +# *-cmd.mk : コマンド +# *-conf.mk : 設定 +# *-auto.mk : 自動設定 +# ------------------------------------------------------------------------------ +include $(TOPDIR)/config.mk +include $(RULEDIR)/*-cmd.mk +include $(RULEDIR)/*-conf.mk +include $(RULEDIR)/*-auto.mk +# ------------------------------------------------------------------------------ +# +# 以下、オプションを適宜変更してください。 +# + +INCLUDES += -I$(TOPDIR)/include +CFLAGS += +CXXFLAGS += +LDFLAGS += +LIBS += -L$(TOPDIR)/lib + +CLEAN_FILES += +CLEAN_DIRS += + +.DEFAULT_GOAL := all + +# ------------------------------------------------------------------------------ +# *-rule : ルール +# ------------------------------------------------------------------------------ +include $(RULEDIR)/*-rule.mk +# ------------------------------------------------------------------------------ + diff --git a/modules/main/test/src/ut_main.c b/modules/main/test/src/ut_main.c new file mode 100644 index 0000000..c57695f --- /dev/null +++ b/modules/main/test/src/ut_main.c @@ -0,0 +1,11 @@ +#include + +int main(int argc, char* argv[]) +{ + (void) argc; + (void) argv; + printf("ut.exe run\n"); + return 0; +} + + diff --git a/modules/src/filetype.c b/modules/src/filetype.c deleted file mode 100644 index f51d104..0000000 --- a/modules/src/filetype.c +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include -#include - -#include - - -/** - * 指定されたパスの次の対応する種別シンボルを返します。 - * pathname の情報を取得できない場合、'-' を返します。 - * - r : 通常ファイル - * - d : ディレクトリ - * - c : キャラクタデバイス - * - b : ブロックデバイス - * - s : ソケット - * - p : 名前付きパイプ - * - l : シンボリックリンク - * - * @param pathname パス - * @return 種別シンボル - */ -char filetype_get(const char* pathname) -{ - struct stat st; - char type_symbol = '-'; - - int ret = stat(pathname, &st); - if (ret == 0) - { // ファイル情報取得成功 - int type = (st.st_mode & S_IFMT); - switch (type) - { - case S_IFSOCK: type_symbol = 's'; break; - case S_IFLNK: type_symbol = 'l'; break; - case S_IFREG: type_symbol = 'r'; break; - case S_IFBLK: type_symbol = 'b'; break; - case S_IFDIR: type_symbol = 'd'; break; - case S_IFCHR: type_symbol = 'c'; break; - case S_IFIFO: type_symbol = 'p'; break; - default: type_symbol = '-'; break; - } - } - return type_symbol; -} - - -/** - * 指定されたパスが存在するか否かを返します。 - * - * @param pathname パス名 - * @return true/false (存在する/存在しない) - */ -bool is_exists(const char* pathname) -{ - struct stat st; - int ret = stat(pathname, &st); - return (ret == 0); -} - diff --git a/modules/src/main.c b/modules/src/main.c deleted file mode 100644 index 7b551f8..0000000 --- a/modules/src/main.c +++ /dev/null @@ -1,356 +0,0 @@ -#include -#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -#define RESULT_OK "[ \x1b[1m\x1b[32mOK\x1b[39m\x1b[0m ]" -#define RESULT_NG "[ \x1b[1m\x1b[31mNG\x1b[39m\x1b[0m ]" - -#ifdef UNITTEST -#define main test_main -#endif - -typedef struct { - const char* command; - void (*cmd_func)(int argc, char* argv[]); -} CommandFunction; - -void exec_command(int argc, char* argv[]); -void print_usage(int argc, char* argv[]); -void cmd_read(int argc, char* argv[]); -void cmd_write(int argc, char* argv[]); -void cmd_append(int argc, char* argv[]); -void cmd_create(int argc, char* argv[]); -void cmd_delete(int argc, char* argv[]); -void cmd_rename(int argc, char* argv[]); -void cmd_link(int argc, char* argv[]); -void cmd_lock(int argc, char* argv[]); -void cmd_exec(int argc, char* argv[]); - -CommandFunction FUNC_TABLE[] = { - { "read" , cmd_read }, - { "write" , cmd_write }, - { "append" , cmd_append }, - { "create" , cmd_create }, - { "delete" , cmd_delete }, - { "rename" , cmd_rename }, - { "link" , cmd_link }, - { "lock" , cmd_lock }, - { "exec" , cmd_exec }, - { "--help" , print_usage }, - { NULL , NULL } -}; - - - - - - - -int main(int argc, char* argv[]) -{ - exec_command(argc, argv); - /* - if (argc < 2) - { - print_usage(); - return 1; - } - - const char* pathname = argv[1]; - */ - /* - bool ret = is_exists(pathname); - printf("isexists = %d\n", ret); - */ - - /* - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_create_file(buf); - printf("file = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "directory"); - ret = can_create_directory(buf); - printf("directory = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-random"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[r] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "cdev-null"); - ret = can_create_character_device_file(buf, 1, 8); - printf("cdev[w] = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "loopback"); - ret = can_create_block_device_file(buf, 7, 15); - printf("bdev = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "socket"); - ret = can_create_socket(buf); - printf("socket = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "pipe"); - ret = can_create_fifo(buf); - printf("pipe = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "lnk"); - ret = can_create_symbolic_link(buf, "access-test"); - printf("link = %d\n", ret); - - snprintf(buf, sizeof(buf), "%s.%s", pathname, "regular-file"); - ret = can_delete(buf); - printf("ret = %d\n", ret); - */ - - - - return 0; -} - - - - - -void exec_command(int argc, char* argv[]) -{ - if (argc >= 2) - { - CommandFunction* cf = &FUNC_TABLE[0]; - while (cf->command != NULL) - { - if (strcmp(cf->command, argv[1]) == 0) - { - cf->cmd_func(argc, argv); - return; - } - cf++; - } - } - // ここに来たらエラー - print_usage(argc, argv); -} - - -// read [pathname] -void cmd_read(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_read(pathname); - printf(" %-10s : %-50s %s\n", "read", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_write(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "write", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - -void cmd_append(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_write(pathname); - printf(" %-10s : %-50s %s\n", "append", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_create(int argc, char* argv[]) -{ - if (argc >= 4) - { - int maj, min; - const char* cmdname; - const char* pathname = argv[2]; - char type = argv[3][0]; - bool ret = false; - switch (type) - { - case 'r': - cmdname = "create[r]"; - ret = can_create_file(pathname); - break; - case 'd': - cmdname = "create[d]"; - ret = can_create_directory(pathname); - break; - case 'c': - cmdname = "create[c]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_character_device_file(pathname, maj, min); - break; - case 'b': - cmdname = "create[b]"; - maj = (argc >= 5) ? atoi(argv[4]) : 1; - min = (argc >= 6) ? atoi(argv[5]) : 3; - ret = can_create_block_device_file(pathname, maj, min); - break; - case 's': - cmdname = "create[s]"; - ret = can_create_socket(pathname); - break; - case 'p': - cmdname = "create[p]"; - ret = can_create_fifo(pathname); - break; - case 'l': - cmdname = "create[l]"; - if (argc >= 5) - { - const char* target = argv[4]; - ret = can_create_symbolic_link(pathname, target); - } - else - { - print_usage(argc, argv); - return; - } - break; - default: - cmdname = "create[-]"; - ret = false; - break; - } - printf(" %-10s : %-50s %s\n", cmdname, pathname, (ret) ? RESULT_OK : RESULT_NG); - - } - else - { - print_usage(argc, argv); - } -} -void cmd_delete(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_delete(pathname); - printf(" %-10s : %-50s %s\n", "delete", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_rename(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* newpath = argv[3]; - bool ret = can_rename(pathname, newpath); - printf(" %-10s : %-50s %s\n", "rename", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_link(int argc, char* argv[]) -{ - if (argc >= 4) - { - const char* pathname = argv[2]; - const char* linkpath = argv[3]; - bool ret = can_link(pathname, linkpath); - printf(" %-10s : %-50s %s\n", "link", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_lock(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_lock(pathname, false); - printf(" %-10s : %-50s %s\n", "lock", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} -void cmd_exec(int argc, char* argv[]) -{ - if (argc >= 3) - { - const char* pathname = argv[2]; - bool ret = can_exec(pathname); - printf(" %-10s : %-50s %s\n", "exec", pathname, (ret) ? RESULT_OK : RESULT_NG); - } - else - { - print_usage(argc, argv); - } -} - - -/** - * Usage 出力します。 - */ -void print_usage(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("Usage\n"); - printf("./test-access [command] [command args...]\n"); - printf("\n"); - printf("command:\n"); - printf(" read : read [pathname]\n"); - printf(" write : write [pathname]\n"); - printf(" append : append [pathname]\n"); - printf(" create : create [pathname] [args]\n"); - printf(" args:\n"); - printf(" r : regular file\n"); - printf(" d : directory\n"); - printf(" c [major] [minor] : character device\n"); - printf(" Ex. create [pathname] c 1 3 --> null\n"); - printf(" Ex. create [pathname] c 1 8 --> random\n"); - printf(" b [major] [minor] : block device\n"); - printf(" Ex. create [pathname] b 7 15 --> loopback15\n"); - printf(" s : socket\n"); - printf(" p : pipe\n"); - printf(" l : link [frompath]\n"); - printf(" delete : delete [pathname]\n"); - printf(" rename : rename [pathname] [newpathname]\n"); - printf(" link : link [pathname] [linkpath]\n"); - printf(" lock : lock [pathname]\n"); - printf(" exec : exec [pathname]\n"); - printf("\n"); -} - diff --git a/modules/src/test_create.c b/modules/src/test_create.c deleted file mode 100644 index aefd674..0000000 --- a/modules/src/test_create.c +++ /dev/null @@ -1,218 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - - -/** - * 指定されたパスのファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_file(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ファイル生成 - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスのディレクトリを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_directory(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ディレクトリ生成 - // mode = 0755 - mode_t mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - int ret = mkdir(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのキャラクタデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_character_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // キャラクタデバイスファイル生成 - // mode = 0020755 - mode_t mode = S_IFCHR|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのブロックデバイスファイルを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @param major major - * @param minor minor - * @return true/false (生成成功/生成失敗) - */ -bool can_create_block_device_file(const char* pathname, unsigned int maj, unsigned int min) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ブロックデバイスファイル生成 - // mode = 0060755 - mode_t mode = S_IFBLK|S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH; - dev_t dev = makedev(maj, min); - int ret = mknod(pathname, mode, dev); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのソケットを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_socket(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // ソケット生成 - int sockfd = socket(AF_LOCAL, SOCK_STREAM, 0); - bool is_success = false; - if (sockfd != -1) - { - struct sockaddr_un name; - memset(&name, 0, sizeof(name)); - name.sun_family = AF_UNIX; - strncpy(name.sun_path, pathname, sizeof(name.sun_path) - 1); - int ret = bind(sockfd, (const struct sockaddr*) &name, sizeof(name)); - is_success = (ret != -1); - close(sockfd); - } - return is_success; -} - - -/** - * 指定されたパスのFIFO(名前付きパイプ)を生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_fifo(const char* pathname) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // FIFO(名前付きパイプ)生成 - // mode = 0644 - mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH; - int ret = mkfifo(pathname, mode); - bool is_success = (ret == 0); - return is_success; -} - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 既にパスが存在する場合は、errno に EEXIST が設定され、false を返します。 - * - * @param pathname 作成するシンボリックリンク - * @param frompath 作成元のパス - * @return true/false (生成成功/生成失敗) - */ -bool can_create_symbolic_link(const char* pathname, const char* frompath) -{ - // パス存在有無確認 (存在する場合 NG) - bool is_error = is_exists(pathname); - if (is_error) - { - errno = EEXIST; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(frompath, pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_delete.c b/modules/src/test_delete.c deleted file mode 100644 index 6380594..0000000 --- a/modules/src/test_delete.c +++ /dev/null @@ -1,38 +0,0 @@ -/** - * @file test_create.c - * @brief create テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスを削除します。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @return true/false (生成成功/生成失敗) - */ -bool can_delete(const char* pathname) -{ - // パス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 削除 - int ret = remove(pathname); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_exec.c b/modules/src/test_exec.c deleted file mode 100644 index d58e912..0000000 --- a/modules/src/test_exec.c +++ /dev/null @@ -1,44 +0,0 @@ -/** - * @file test_exec.c - * @brief exec テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include -#include - -#include -#include - -#include -#include - - -/** - * 指定されたパスを実行します。 - * - * @param pathname パス - * @return true/false (実行成功/実行失敗) - */ -bool can_exec(const char* pathname) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // 実行 - int ret = system(pathname); - if (ret == -1) - { // 実行エラー - return false; - } - - bool is_success = (WIFEXITED(ret) && (WEXITSTATUS(ret) == 0)); - return is_success; -} - diff --git a/modules/src/test_link.c b/modules/src/test_link.c deleted file mode 100644 index c8cee12..0000000 --- a/modules/src/test_link.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_link.c - * @brief link テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスのシンボリックリンクを生成します。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param linkpath シンボリックリンク先 - * @return true/false (生成成功/生成失敗) - */ -bool can_link(const char* pathname, const char* linkpath) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // シンボリックリンク生成 - int ret = symlink(pathname, linkpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_lock.c b/modules/src/test_lock.c deleted file mode 100644 index 0e061e7..0000000 --- a/modules/src/test_lock.c +++ /dev/null @@ -1,54 +0,0 @@ -/** - * @file test_lock.c - * @brief lock テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをロックします。 - * 指定されたパスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param pathname パス - * @param is_share (true/false) 共有ロック/排他ロック - * @return true/false (生成成功/生成失敗) - */ -bool can_lock(const char* pathname, bool is_share) -{ - // パス存在有無確認 (しない場合 NG) - bool is_error = !is_exists(pathname); - if (is_error) - { - errno = ENOENT; - return false; - } - - // fd 取得 - int fd = open(pathname, O_RDONLY); - if (fd == -1) - { - fd = open(pathname, O_WRONLY); - if (fd == -1) - { - return false; - } - } - - int lock_type = (is_share) ? LOCK_SH : LOCK_EX; - int ret = flock(fd, lock_type); - bool is_success = (ret == 0); - if (is_success) - { - flock(fd, LOCK_UN); - } - return is_success; -} - diff --git a/modules/src/test_read.c b/modules/src/test_read.c deleted file mode 100644 index 44245e1..0000000 --- a/modules/src/test_read.c +++ /dev/null @@ -1,28 +0,0 @@ -/** - * @file test_read.c - * @brief read テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが読込可能か確認します。 - * - * @param pathname パス - * @return true/false (読込可能/読込不可) - */ -bool can_read(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "r"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/modules/src/test_rename.c b/modules/src/test_rename.c deleted file mode 100644 index a1b6219..0000000 --- a/modules/src/test_rename.c +++ /dev/null @@ -1,39 +0,0 @@ -/** - * @file test_rename.c - * @brief rename テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - -#include - -#include -#include - - -/** - * 指定されたパスをリネームします。 - * パスが存在しない場合は、errno に ENOENT が設定され、false を返します。 - * - * @param oldpath リネーム前のパス - * @param newpath リネーム後のパス - * @return true/false (リネーム成功/リネーム失敗) - */ -bool can_rename(const char* oldpath, const char* newpath) -{ - // リネーム前のパス存在有無確認 (存在しない場合 NG) - bool is_error = !is_exists(oldpath); - if (is_error) - { - errno = ENOENT; - return false; - } - - // リネーム - int ret = rename(oldpath, newpath); - bool is_success = (ret == 0); - return is_success; -} - diff --git a/modules/src/test_write.c b/modules/src/test_write.c deleted file mode 100644 index e56d63c..0000000 --- a/modules/src/test_write.c +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @file test_write.h - * @brief write テスト - * @author Nomura Kei - * @copyright 2008 Nomura Kei - */ -#include -#include - - -/** - * 指定されたパスが書込可能か確認します。 - * - * @param pathname パス - * @return true/false (書込可能/書込不可) - */ -bool can_write(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "w"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - - -/** - * 指定されたパスが追記可能か確認します。 - * - * @param pathname パス - * @return true/false (追記可能/追記不可) - */ -bool can_append(const char* pathname) -{ - bool is_success = false; - FILE* fp = fopen(pathname, "a"); - if (fp != NULL) - { - is_success = true; - fclose(fp); - } - return is_success; -} - diff --git a/modules/test/Makefile b/modules/test/Makefile deleted file mode 100644 index 9dcb20d..0000000 --- a/modules/test/Makefile +++ /dev/null @@ -1,48 +0,0 @@ -# ============================================================================== -# Makefile -# ============================================================================== -# -# TOPDIR : トップディレクトリ -# RULEDIR : Meke のルール一式が格納されているディレクトリ -# NAME : モジュール名 (拡張子を含めないこと) -# TARGET : モジュールファイル名 (拡張子を含めること) -# SUBDIR : サブディレクトリ (処理したい順に空白区切りで記述すること) -# -TOPDIR ?= ../.. -RULEDIR ?= $(TOPDIR)/mk -NAME = ut.exe -TARGET = $(NAME) -SUBDIRS = -USE_SO_VERSION = - -# ------------------------------------------------------------------------------ -# *-cmd.mk : コマンド -# *-conf.mk : 設定 -# *-auto.mk : 自動設定 -# ------------------------------------------------------------------------------ -include $(TOPDIR)/config.mk -include $(RULEDIR)/*-cmd.mk -include $(RULEDIR)/*-conf.mk -include $(RULEDIR)/*-auto.mk -# ------------------------------------------------------------------------------ -# -# 以下、オプションを適宜変更してください。 -# - -INCLUDES += -I$(TOPDIR)/include -CFLAGS += -CXXFLAGS += -LDFLAGS += -LIBS += -L$(TOPDIR)/lib - -CLEAN_FILES += -CLEAN_DIRS += - -.DEFAULT_GOAL := all - -# ------------------------------------------------------------------------------ -# *-rule : ルール -# ------------------------------------------------------------------------------ -include $(RULEDIR)/*-rule.mk -# ------------------------------------------------------------------------------ - diff --git a/modules/test/src/ut_main.c b/modules/test/src/ut_main.c deleted file mode 100644 index c57695f..0000000 --- a/modules/test/src/ut_main.c +++ /dev/null @@ -1,11 +0,0 @@ -#include - -int main(int argc, char* argv[]) -{ - (void) argc; - (void) argv; - printf("ut.exe run\n"); - return 0; -} - -