From 9c754053b07a724bdd98d039f34899d6a49115b7 Mon Sep 17 00:00:00 2001 From: bigbiff bigbiff Date: Wed, 9 Jan 2013 09:09:08 -0500 Subject: Add libtar to TWRP instead of using busybox tar Add proper mkdosfs tool Add fuse to TWRP Add experimental exfat-fuse to TWRP Convert all system() functions to use new Exec_Cmd function --- .gitignore | 1 + Android.mk | 19 +- README.md | 6 +- data.cpp | 4 +- dosfstools/Android.mk | 59 + dosfstools/COPYING | 674 ++++++ dosfstools/ChangeLog | 760 ++++++ dosfstools/Makefile | 112 + dosfstools/bin/Nindent | 18 + dosfstools/doc/ANNOUNCE.mkdosfs | 41 + dosfstools/doc/ChangeLog.dosfsck | 10 + dosfstools/doc/ChangeLog.dosfstools-2.x | 161 ++ dosfstools/doc/ChangeLog.mkdosfs | 18 + dosfstools/doc/README.dosfsck | 60 + dosfstools/doc/README.dosfstools-2.x | 60 + dosfstools/doc/README.mkdosfs | 50 + dosfstools/doc/TODO.dosfstools-2.x | 14 + dosfstools/man/dosfsck.8 | 112 + dosfstools/man/dosfslabel.8 | 29 + dosfstools/man/mkdosfs.8 | 224 ++ dosfstools/src/boot.c | 518 ++++ dosfstools/src/boot.h | 30 + dosfstools/src/check.c | 1051 ++++++++ dosfstools/src/check.h | 39 + dosfstools/src/common.c | 118 + dosfstools/src/common.h | 57 + dosfstools/src/dosfsck.c | 224 ++ dosfstools/src/dosfsck.h | 218 ++ dosfstools/src/dosfslabel.c | 128 + dosfstools/src/fat.c | 547 +++++ dosfstools/src/fat.h | 84 + dosfstools/src/file.c | 282 +++ dosfstools/src/file.h | 69 + dosfstools/src/io.c | 230 ++ dosfstools/src/io.h | 70 + dosfstools/src/lfn.c | 502 ++++ dosfstools/src/lfn.h | 38 + dosfstools/src/mkdosfs.c | 1733 ++++++++++++++ dosfstools/src/version.h | 28 + exfat/COPYING | 674 ++++++ exfat/ChangeLog | 70 + exfat/dump/dumpexfat.8 | 46 + exfat/dump/main.c | 180 ++ exfat/exfat-fuse/Android.mk | 22 + exfat/exfat-fuse/main.c | 543 +++++ exfat/exfat-fuse/mount.exfat-fuse.8 | 76 + exfat/fsck/exfatfsck | Bin 0 -> 162472 bytes exfat/fsck/exfatfsck.8 | 32 + exfat/fsck/main.c | 172 ++ exfat/label/exfatlabel.8 | 48 + exfat/label/main.c | 61 + exfat/libexfat/Android.mk | 13 + exfat/libexfat/byteorder.h | 109 + exfat/libexfat/cluster.c | 445 ++++ exfat/libexfat/exfat.h | 215 ++ exfat/libexfat/exfatfs.h | 163 ++ exfat/libexfat/io.c | 385 +++ exfat/libexfat/log.c | 108 + exfat/libexfat/lookup.c | 223 ++ exfat/libexfat/mount.c | 314 +++ exfat/libexfat/node.c | 1041 ++++++++ exfat/libexfat/time.c | 156 ++ exfat/libexfat/utf.c | 229 ++ exfat/libexfat/utils.c | 176 ++ exfat/libexfat/version.h | 28 + exfat/mkfs/Android.mk | 21 + exfat/mkfs/cbm.c | 74 + exfat/mkfs/cbm.h | 28 + exfat/mkfs/fat.c | 86 + exfat/mkfs/fat.h | 28 + exfat/mkfs/main.c | 269 +++ exfat/mkfs/mkexfat.c | 160 ++ exfat/mkfs/mkexfat.h | 47 + exfat/mkfs/mkexfatfs.8 | 68 + exfat/mkfs/rootdir.c | 100 + exfat/mkfs/rootdir.h | 28 + exfat/mkfs/uct.c | 50 + exfat/mkfs/uct.h | 28 + exfat/mkfs/uctc.c | 755 ++++++ exfat/mkfs/uctc.h | 28 + exfat/mkfs/vbr.c | 146 ++ exfat/mkfs/vbr.h | 28 + fixPermissions.cpp | 42 +- fixPermissions.hpp | 1 - fuse/Android.mk | 65 + fuse/README | 12 + fuse/cuse_lowlevel.c | 371 +++ fuse/fuse.c | 3968 +++++++++++++++++++++++++++++++ fuse/fuse_i.h | 101 + fuse/fuse_kern_chan.c | 95 + fuse/fuse_loop.c | 39 + fuse/fuse_loop_mt.c | 246 ++ fuse/fuse_lowlevel.c | 1862 +++++++++++++++ fuse/fuse_misc.h | 53 + fuse/fuse_mt.c | 120 + fuse/fuse_opt.c | 409 ++++ fuse/fuse_session.c | 205 ++ fuse/fuse_signals.c | 72 + fuse/fusexmp.c | 385 +++ fuse/helper.c | 455 ++++ fuse/include/Makefile | 515 ++++ fuse/include/Makefile.am | 17 + fuse/include/Makefile.in | 515 ++++ fuse/include/config.h | 87 + fuse/include/config.h.in | 86 + fuse/include/cuse_lowlevel.h | 87 + fuse/include/fuse.h | 928 ++++++++ fuse/include/fuse_common.h | 298 +++ fuse/include/fuse_common_compat.h | 26 + fuse/include/fuse_compat.h | 201 ++ fuse/include/fuse_kernel.h | 593 +++++ fuse/include/fuse_lowlevel.h | 1577 ++++++++++++ fuse/include/fuse_lowlevel_compat.h | 155 ++ fuse/include/fuse_opt.h | 270 +++ fuse/include/old/fuse.h | 9 + fuse/include/stamp-h1 | 1 + fuse/include/sys/statvfs.h | 18 + fuse/include/ulockmgr.h | 24 + fuse/mount.c | 596 +++++ fuse/mount_bsd.c | 388 +++ fuse/mount_util.c | 363 +++ fuse/mount_util.h | 18 + fuse/ulockmgr.c | 440 ++++ gui/action.cpp | 57 +- gui/devices/720x1280/res/ui.xml | 4 +- libtar/Android.mk | 15 + libtar/COPYRIGHT | 35 + libtar/ChangeLog | 15 + libtar/ChangeLog-1.0.x | 141 ++ libtar/INSTALL | 183 ++ libtar/README | 121 + libtar/TODO | 21 + libtar/append.c | 256 ++ libtar/basename.c | 74 + libtar/block.c | 383 +++ libtar/compat.h | 256 ++ libtar/config.h | 188 ++ libtar/decode.c | 122 + libtar/dirname.c | 77 + libtar/encode.c | 213 ++ libtar/extract.c | 551 +++++ libtar/fnmatch.c | 237 ++ libtar/gethostbyname_r.c | 41 + libtar/gethostname.c | 36 + libtar/getservbyname_r.c | 41 + libtar/glob.c | 873 +++++++ libtar/handle.c | 129 + libtar/inet_aton.c | 27 + libtar/internal.h | 17 + libtar/libtar.h | 300 +++ libtar/libtar_hash.c | 344 +++ libtar/libtar_list.c | 458 ++++ libtar/libtar_listhash.h | 196 ++ libtar/output.c | 134 ++ libtar/snprintf.c | 761 ++++++ libtar/strdup.c | 62 + libtar/strlcat.c | 72 + libtar/strlcpy.c | 68 + libtar/strmode.c | 152 ++ libtar/strrstr.c | 40 + libtar/strsep.c | 87 + libtar/tar.h | 108 + libtar/util.c | 165 ++ libtar/wrapper.c | 155 ++ makelist.cpp | 133 -- makelist.hpp | 40 - openrecoveryscript.cpp | 20 +- partition.cpp | 261 +- partitionmanager.cpp | 73 +- partitions.hpp | 3 +- prebuilt/Android.mk | 9 + recovery.cpp | 10 +- resources.cpp | 280 --- twrp-functions.cpp | 206 +- twrp-functions.hpp | 3 + twrpTar.cpp | 409 ++++ twrpTar.hpp | 61 + variables.h | 8 +- 178 files changed, 39527 insertions(+), 788 deletions(-) create mode 100644 .gitignore create mode 100644 dosfstools/Android.mk create mode 100644 dosfstools/COPYING create mode 100644 dosfstools/ChangeLog create mode 100644 dosfstools/Makefile create mode 100755 dosfstools/bin/Nindent create mode 100644 dosfstools/doc/ANNOUNCE.mkdosfs create mode 100644 dosfstools/doc/ChangeLog.dosfsck create mode 100644 dosfstools/doc/ChangeLog.dosfstools-2.x create mode 100644 dosfstools/doc/ChangeLog.mkdosfs create mode 100644 dosfstools/doc/README.dosfsck create mode 100644 dosfstools/doc/README.dosfstools-2.x create mode 100644 dosfstools/doc/README.mkdosfs create mode 100644 dosfstools/doc/TODO.dosfstools-2.x create mode 100644 dosfstools/man/dosfsck.8 create mode 100644 dosfstools/man/dosfslabel.8 create mode 100644 dosfstools/man/mkdosfs.8 create mode 100644 dosfstools/src/boot.c create mode 100644 dosfstools/src/boot.h create mode 100644 dosfstools/src/check.c create mode 100644 dosfstools/src/check.h create mode 100644 dosfstools/src/common.c create mode 100644 dosfstools/src/common.h create mode 100644 dosfstools/src/dosfsck.c create mode 100644 dosfstools/src/dosfsck.h create mode 100644 dosfstools/src/dosfslabel.c create mode 100644 dosfstools/src/fat.c create mode 100644 dosfstools/src/fat.h create mode 100644 dosfstools/src/file.c create mode 100644 dosfstools/src/file.h create mode 100644 dosfstools/src/io.c create mode 100644 dosfstools/src/io.h create mode 100644 dosfstools/src/lfn.c create mode 100644 dosfstools/src/lfn.h create mode 100644 dosfstools/src/mkdosfs.c create mode 100644 dosfstools/src/version.h create mode 100644 exfat/COPYING create mode 100644 exfat/ChangeLog create mode 100644 exfat/dump/dumpexfat.8 create mode 100644 exfat/dump/main.c create mode 100644 exfat/exfat-fuse/Android.mk create mode 100644 exfat/exfat-fuse/main.c create mode 100644 exfat/exfat-fuse/mount.exfat-fuse.8 create mode 100755 exfat/fsck/exfatfsck create mode 100644 exfat/fsck/exfatfsck.8 create mode 100644 exfat/fsck/main.c create mode 100644 exfat/label/exfatlabel.8 create mode 100644 exfat/label/main.c create mode 100644 exfat/libexfat/Android.mk create mode 100644 exfat/libexfat/byteorder.h create mode 100644 exfat/libexfat/cluster.c create mode 100644 exfat/libexfat/exfat.h create mode 100644 exfat/libexfat/exfatfs.h create mode 100644 exfat/libexfat/io.c create mode 100644 exfat/libexfat/log.c create mode 100644 exfat/libexfat/lookup.c create mode 100644 exfat/libexfat/mount.c create mode 100644 exfat/libexfat/node.c create mode 100644 exfat/libexfat/time.c create mode 100644 exfat/libexfat/utf.c create mode 100644 exfat/libexfat/utils.c create mode 100644 exfat/libexfat/version.h create mode 100644 exfat/mkfs/Android.mk create mode 100644 exfat/mkfs/cbm.c create mode 100644 exfat/mkfs/cbm.h create mode 100644 exfat/mkfs/fat.c create mode 100644 exfat/mkfs/fat.h create mode 100644 exfat/mkfs/main.c create mode 100644 exfat/mkfs/mkexfat.c create mode 100644 exfat/mkfs/mkexfat.h create mode 100644 exfat/mkfs/mkexfatfs.8 create mode 100644 exfat/mkfs/rootdir.c create mode 100644 exfat/mkfs/rootdir.h create mode 100644 exfat/mkfs/uct.c create mode 100644 exfat/mkfs/uct.h create mode 100644 exfat/mkfs/uctc.c create mode 100644 exfat/mkfs/uctc.h create mode 100644 exfat/mkfs/vbr.c create mode 100644 exfat/mkfs/vbr.h create mode 100644 fuse/Android.mk create mode 100644 fuse/README create mode 100644 fuse/cuse_lowlevel.c create mode 100644 fuse/fuse.c create mode 100644 fuse/fuse_i.h create mode 100644 fuse/fuse_kern_chan.c create mode 100644 fuse/fuse_loop.c create mode 100644 fuse/fuse_loop_mt.c create mode 100644 fuse/fuse_lowlevel.c create mode 100644 fuse/fuse_misc.h create mode 100644 fuse/fuse_mt.c create mode 100644 fuse/fuse_opt.c create mode 100644 fuse/fuse_session.c create mode 100644 fuse/fuse_signals.c create mode 100644 fuse/fusexmp.c create mode 100644 fuse/helper.c create mode 100644 fuse/include/Makefile create mode 100644 fuse/include/Makefile.am create mode 100644 fuse/include/Makefile.in create mode 100644 fuse/include/config.h create mode 100644 fuse/include/config.h.in create mode 100644 fuse/include/cuse_lowlevel.h create mode 100644 fuse/include/fuse.h create mode 100644 fuse/include/fuse_common.h create mode 100644 fuse/include/fuse_common_compat.h create mode 100644 fuse/include/fuse_compat.h create mode 100644 fuse/include/fuse_kernel.h create mode 100644 fuse/include/fuse_lowlevel.h create mode 100644 fuse/include/fuse_lowlevel_compat.h create mode 100644 fuse/include/fuse_opt.h create mode 100644 fuse/include/old/fuse.h create mode 100644 fuse/include/stamp-h1 create mode 100644 fuse/include/sys/statvfs.h create mode 100644 fuse/include/ulockmgr.h create mode 100644 fuse/mount.c create mode 100644 fuse/mount_bsd.c create mode 100644 fuse/mount_util.c create mode 100644 fuse/mount_util.h create mode 100644 fuse/ulockmgr.c create mode 100644 libtar/Android.mk create mode 100644 libtar/COPYRIGHT create mode 100644 libtar/ChangeLog create mode 100644 libtar/ChangeLog-1.0.x create mode 100644 libtar/INSTALL create mode 100644 libtar/README create mode 100644 libtar/TODO create mode 100644 libtar/append.c create mode 100644 libtar/basename.c create mode 100644 libtar/block.c create mode 100644 libtar/compat.h create mode 100644 libtar/config.h create mode 100644 libtar/decode.c create mode 100644 libtar/dirname.c create mode 100644 libtar/encode.c create mode 100644 libtar/extract.c create mode 100644 libtar/fnmatch.c create mode 100644 libtar/gethostbyname_r.c create mode 100644 libtar/gethostname.c create mode 100644 libtar/getservbyname_r.c create mode 100644 libtar/glob.c create mode 100644 libtar/handle.c create mode 100644 libtar/inet_aton.c create mode 100644 libtar/internal.h create mode 100644 libtar/libtar.h create mode 100644 libtar/libtar_hash.c create mode 100644 libtar/libtar_list.c create mode 100644 libtar/libtar_listhash.h create mode 100644 libtar/output.c create mode 100644 libtar/snprintf.c create mode 100644 libtar/strdup.c create mode 100644 libtar/strlcat.c create mode 100644 libtar/strlcpy.c create mode 100644 libtar/strmode.c create mode 100644 libtar/strrstr.c create mode 100644 libtar/strsep.c create mode 100644 libtar/tar.h create mode 100644 libtar/util.c create mode 100644 libtar/wrapper.c delete mode 100644 makelist.cpp delete mode 100644 makelist.hpp delete mode 100644 resources.cpp create mode 100644 twrpTar.cpp create mode 100644 twrpTar.hpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..a01ee289f --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp diff --git a/Android.mk b/Android.mk index 14ff6acbd..6600e15a2 100644 --- a/Android.mk +++ b/Android.mk @@ -26,11 +26,11 @@ LOCAL_SRC_FILES := \ screen_ui.cpp \ verifier.cpp \ fixPermissions.cpp \ + twrpTar.cpp \ adb_install.cpp LOCAL_SRC_FILES += \ data.cpp \ - makelist.cpp \ partition.cpp \ partitionmanager.cpp \ mtdutils/mtdutils.c \ @@ -65,7 +65,7 @@ LOCAL_SHARED_LIBRARIES := LOCAL_STATIC_LIBRARIES += libmtdutils LOCAL_STATIC_LIBRARIES += libminadbd libminzip libunz LOCAL_STATIC_LIBRARIES += libminuitwrp libpixelflinger_static libpng libjpegtwrp libgui -LOCAL_SHARED_LIBRARIES += libz libc libstlport libcutils libstdc++ libmincrypt libext4_utils +LOCAL_SHARED_LIBRARIES += libz libc libstlport libcutils libstdc++ libmincrypt libext4_utils libtar ifeq ($(TARGET_USERIMAGES_USE_EXT4), true) LOCAL_CFLAGS += -DUSE_EXT4 @@ -76,7 +76,7 @@ endif ifeq ($(HAVE_SELINUX), true) LOCAL_C_INCLUDES += external/libselinux/include LOCAL_STATIC_LIBRARIES += libselinux - LOCAL_CFLAGS += -DHAVE_SELINUX + LOCAL_CFLAGS += -DHAVE_SELINUX -g endif # HAVE_SELINUX # This binary is in the recovery ramdisk, which is otherwise a copy of root. @@ -269,14 +269,16 @@ include $(commands_recovery_local_path)/libjpegtwrp/Android.mk \ $(commands_recovery_local_path)/gui/Android.mk \ $(commands_recovery_local_path)/mmcutils/Android.mk \ $(commands_recovery_local_path)/bmlutils/Android.mk \ - $(commands_recovery_local_path)/flashutils/Android.mk \ $(commands_recovery_local_path)/prebuilt/Android.mk \ $(commands_recovery_local_path)/mtdutils/Android.mk \ + $(commands_recovery_local_path)/flashutils/Android.mk \ $(commands_recovery_local_path)/pigz/Android.mk \ + $(commands_recovery_local_path)/dosfstools/Android.mk \ + $(commands_recovery_local_path)/libtar/Android.mk \ $(commands_recovery_local_path)/crypto/cryptsettings/Android.mk \ $(commands_recovery_local_path)/crypto/cryptfs/Android.mk \ $(commands_recovery_local_path)/libcrecovery/Android.mk \ - $(commands_recovery_local_path)/twmincrypt/Android.mk + $(commands_recovery_local_path)/twmincrypt/Android.mk \ ifeq ($(TW_INCLUDE_CRYPTO_SAMSUNG), true) include $(commands_recovery_local_path)/crypto/libcrypt_samsung/Android.mk @@ -286,4 +288,11 @@ ifeq ($(TW_INCLUDE_JB_CRYPTO), true) include $(commands_recovery_local_path)/crypto/fs_mgr/Android.mk endif +ifeq ($(TW_INCLUDE_EXFAT), true) + include $(commands_recovery_local_path)/fuse/Android.mk + include $(commands_recovery_local_path)/exfat/libexfat/Android.mk + include $(commands_recovery_local_path)/exfat/exfat-fuse/Android.mk + include $(commands_recovery_local_path)/exfat/mkfs/Android.mk +endif + commands_recovery_local_path := diff --git a/README.md b/README.md index 9031f6b14..b3c45295c 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,11 @@ The goal of this branch is to rebase TWRP onto AOSP while maintaining as much of the original AOSP code as possible. This goal should allow us to apply updates to the AOSP code going forward with little to no extra work. With this goal in mind, we will carefully consider any changes needed to the AOSP code before allowing them. In most cases, instead of changing the AOSP code, we'll create our own functions instead. The only changes that should be made to AOSP code should be those affecting startup of the recovery and some of the make files. -This branch is currently a work in progress, however, most features have been implemented and it is now ready for testing. +If there are changes that need to be merged from AOSP, we will pull the change directly from AOSP instead of creating a new patch in order to prevent merge conflicts with AOSP. -You can find a compiling guide [here](http://rootzwiki.com/topic/23903-how-to-compile-twrp-from-source/ "Guide"). +This branch is under final testing and will be used shortly for public builds, but has not officially been released. + +You can find a compiling guide [here](http://forum.xda-developers.com/showthread.php?t=1943625 "Guide"). [More information about the project.](http://www.teamw.in/project/twrp2 "More Information") diff --git a/data.cpp b/data.cpp index e7dafa1fe..75d689f0c 100644 --- a/data.cpp +++ b/data.cpp @@ -390,7 +390,6 @@ int DataManager::SetValue(const string varName, string value, int persist /* = 0 if (pos->second.second != 0) SaveValues(); - gui_notifyVarChange(varName.c_str(), value.c_str()); return 0; } @@ -842,8 +841,7 @@ void DataManager::Output_Version(void) { } Path += "/TWRP/.version"; if (TWFunc::Path_Exists(Path)) { - Command = "rm -f " + Path; - system(Command.c_str()); + unlink(Path.c_str()); } FILE *fp = fopen(Path.c_str(), "w"); if (fp == NULL) { diff --git a/dosfstools/Android.mk b/dosfstools/Android.mk new file mode 100644 index 000000000..62297f333 --- /dev/null +++ b/dosfstools/Android.mk @@ -0,0 +1,59 @@ +ifneq ($(TARGET_SIMULATOR),true) +LOCAL_PATH:= $(call my-dir) + +include $(CLEAR_VARS) +LOCAL_SRC_FILES := src/boot.c src/check.c src/common.c \ + src/fat.c src/file.c src/io.c src/lfn.c src/dosfsck.c +LOCAL_C_INCLUDES := $(KERNEL_HEADERS) +LOCAL_SHARED_LIBRARIES := libc +LOCAL_CFLAGS += -D_USING_BIONIC_ +LOCAL_CFLAGS += -DUSE_ANDROID_RETVALS +LOCAL_MODULE = dosfsck +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +include $(BUILD_EXECUTABLE) + +# build symlink +SYMLINKS := $(addprefix $(TARGET_OUT)/bin/,fsck_msdos) +$(SYMLINKS): DOSFSCK_BINARY := $(LOCAL_MODULE) +$(SYMLINKS): $(LOCAL_INSTALLED_MODULE) $(LOCAL_PATH)/Android.mk + @echo "Symlink: $@ -> $(DOSFSCK_BINARY)" + @mkdir -p $(dir $@) + @rm -rf $@ + $(hide) ln -sf $(DOSFSCK_BINARY) $@ + +ALL_DEFAULT_INSTALLED_MODULES += $(SYMLINKS) + +# We need this so that the installed files could be picked up based on the +# local module name +ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \ + $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS) + + + +include $(CLEAR_VARS) +LOCAL_SRC_FILES := src/boot.c src/check.c src/common.c src/fat.c \ + src/file.c src/io.c src/lfn.c src/dosfslabel.c +LOCAL_C_INCLUDES := $(KERNEL_HEADERS) \ + bionic/libc/kernel/common +LOCAL_SHARED_LIBRARIES := libc +LOCAL_CFLAGS += -D_USING_BIONIC_ +LOCAL_MODULE = dosfslabel +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +include $(BUILD_EXECUTABLE) + +include $(CLEAR_VARS) +LOCAL_SRC_FILES := src/mkdosfs.c +LOCAL_C_INCLUDES := $(KERNEL_HEADERS) +LOCAL_SHARED_LIBRARIES := libc +LOCAL_CFLAGS += -D_USING_BIONIC_ +LOCAL_MODULE = mkdosfs +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +include $(BUILD_EXECUTABLE) + +endif diff --git a/dosfstools/COPYING b/dosfstools/COPYING new file mode 100644 index 000000000..94a9ed024 --- /dev/null +++ b/dosfstools/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/dosfstools/ChangeLog b/dosfstools/ChangeLog new file mode 100644 index 000000000..b8dc2e010 --- /dev/null +++ b/dosfstools/ChangeLog @@ -0,0 +1,760 @@ +commit 2e20319843ea85d77c51a9ce9a9b278662426d98 +Author: Michael Casadevall +Date: Tue Jun 7 19:19:30 2011 +0200 + + Correcting miscalculation of sector number in some cases. + + mkdosfs will incorrectly calculate the number of sectors of a + given FAT partition if the number sectors are odd due to + count_blocks incorrectly handling the remainder of a division + operation. This miscalculation causes the OMAP4 bootloader to + fail to boot. + + This bug can be observed by comparing the total sector size in + fdisk expert more to fsck.msdos; this discrepancy only shows up + when the number of sectors are odd. + + See https://bugs.launchpad.net/ubuntu/+source/dosfstools/+bug/794043 + for more information. + +commit 45c3a5d8229ef998962e495f1efe7d2a6cd8a825 +Author: Daniel Baumann +Date: Sat Jan 8 23:38:59 2011 +0100 + + Re-running Nindent. + +commit 37115695884422e6f58ec490d11d460539715f8a +Author: Sergey Gusarov +Date: Sat Jan 8 23:36:11 2011 +0100 + + Fixing compiler warnings related to the mismatch of types "char *" / "unsigned + char *". + + These warnings appear when you compile the project with the option "-Wall", what + is done with the current default Makefile. + +commit a9055613f0d826021db65c79c2df87ac91e89215 +Author: Jaroslav Skarvada +Date: Thu Jan 6 22:35:00 2011 +0100 + + Fixing overflow bug in reclaim_file function, see + https://bugzilla.redhat.com/show_bug.cgi?id=660154 for more information. + + The problem is that alloc_rootdir_entry counts with 10000 files at max, but the + filename buffer is only 8 chars long. Due to pattern mask used it results to + only 10 files at max (FSCK0-9REC). If there is more than 10 files, it overflows + and hangs. + +commit 5a2b37f3ef664dbc7850f3d800890d7bb919b3cd +Author: Sergey Gusarov +Date: Thu Jan 6 22:31:39 2011 +0100 + + Fixing conversion specifiers in accordance with the type of expressions. + +commit 258e5ebbb24fd6293a86fe22d6bcda8ce1794dcd +Author: Daniel Baumann +Date: Sun Jan 2 15:41:44 2011 +0100 + + Indenting source files. + +commit 9c22278dda0f8fc05aa537eb0bcb07e51f0dec6a +Author: Daniel Baumann +Date: Sun Jan 2 15:39:03 2011 +0100 + + Adding Nindent script from syslinux. + +commit e6008ff5c15dc2e6d5b33f88a447d1159165c95d +Author: Daniel Baumann +Date: Fri Dec 24 17:58:29 2010 +0100 + + Releasing upstream version 3.0.11. + +commit bce60d1b0b739612b63852722d8504986096b40d +Author: Michael Stapelberg +Date: Fri Nov 19 14:09:36 2010 +0100 + + Add better error message when the device cannot be opened. + + This is helpful for SD cards or other removable media which have an enabled + write lock -- without the "Permission denied" message, the user has to strace + mkdosfs to find out what's going on. + +commit ff45bd967e5d8ff7edc496adbad57257d4d5432b +Author: Jaroslav Skarvada +Date: Fri Oct 8 13:38:16 2010 +0200 + + Unalign on s390x, see http://bugzilla.redhat.com/show_bug.cgi?id=624596 for + more information. + +commit 22874327372f914d2919490326c95f4607f8de74 +Author: Daniel Baumann +Date: Sun Sep 12 09:35:47 2010 +0200 + + Releasing upstream version 3.0.10. + +commit 8b7c9d94a4571142a77a587138bc26b39f8e2863 +Author: Alexander Korolkov +Date: Sun Sep 12 09:29:12 2010 +0200 + + Modify LFN direntries when file is renamed or deleted, see + Debian bug #596329. + +commit 761b798f3bf2b4a87f2d454f555e18758791c864 +Author: Alexander Korolkov +Date: Sun Sep 12 09:27:07 2010 +0200 + + If the test of short filename fails, dosfsck could complain about + bad long filename, see Debian bug #596327. + +commit 8fa3587a946614cd43d813052e0e31e595e6d63d +Author: Alexander Korolkov +Date: Sun Sep 12 09:24:47 2010 +0200 + + dosfsck: don't complain about bad filenames when short filename + contains 7 or more characters with codes 128-255, see Debian + bug #596327. + +commit 3893857b841096de6a422ef5bed1b2618a7037d5 +Author: Mitch Rybczynski +Date: Mon Jul 5 14:45:54 2010 +0200 + + Adding __arm__ define check for some crosscompile toolchains. + +commit 7d03b3cc96b83b67638b463610a29abfd6f51f77 +Author: Daniel Baumann +Date: Sun Mar 14 16:42:32 2010 +0100 + + Modernizing dosfslabel manpage. + +commit 258049911c5df476fb434e0d87e0ece01b9ba137 +Author: Daniel Baumann +Date: Sun Mar 14 16:33:47 2010 +0100 + + Modernizing dosfsck manpage. + +commit 50d1d96b9666913a90e16904a63e29925675859c +Author: Daniel Baumann +Date: Sun Mar 14 16:05:32 2010 +0100 + + Fixing spelling error in boot.c. + +commit 0e87c7890b598d78c6aa3d2a06b2306980e75a3d +Author: Daniel Baumann +Date: Sun Jan 31 08:31:32 2010 +0100 + + Releasing upstream version 3.0.9. + +commit 9415707c2c9ad22b48660593915667dd228722fa +Author: Daniel Kahn Gillmor +Date: Sun Jan 31 00:11:41 2010 -0500 + + Be sure to store the updated reserved_sector count in the boot sector, + see Debian bug #567337. + +commit 68b3f00471f60a692fe021d65289bbaf2dc990d5 +Author: Daniel Baumann +Date: Sat Jan 23 10:16:18 2010 +0100 + + Releasing upstream version 3.0.8. + +commit 69dbf2e002f0cb3f0781256dec7258b66ffae3b6 +Author: Daniel Baumann +Date: Sat Jan 23 10:15:01 2010 +0100 + + Removing some cruft in end-comments. + +commit eef306657f3152bbf913a8a45c514f11b2dc2494 +Author: Steven J. Magnani +Date: Thu Jan 21 16:58:11 2010 +0100 + + When compiling a 32-bit version of dosfstools on an x86_64 machine, + the resulting applications report strange errors on "large" (> 2 GiB) + partitions: + + Seek to -2118967808:Invalid argument + + Warning: Filesystem is FAT32 according to fat_length and fat32_length fields, + but has only 8613 clusters, less than the required minimum of 65525. + This may lead to problems on some systems. + + This appears to be due to compilation with a 32-bit off_t and lseek() library + function. + + Use lseek64 for positioning, and change some suspect uses of off_t to loff_t. + +commit e69f49dd1fe52780071cb3f024d1a8246125915a +Author: Steven J. Magnani +Date: Thu Jan 21 16:56:26 2010 +0100 + + If dosfsck is run in read-only mode (-n), exit with code 0 + if the only issue found is an uninitialized free cluster summary. + +commit e52a16d488cf680117e4d476400bdd7915ef2f7a +Author: Steven J. Magnani +Date: Thu Jan 21 16:55:30 2010 +0100 + + On x86_64, dosfsck incorrectly claims that a free_cluster summary of + 0xFFFFFFFF, defined by Microsoft to be "uninitialized," is wrong. + +commit 32db02998ed7882df355fa4077009e8d363df3ab +Author: H. Peter Anvin +Date: Fri Jan 8 09:16:38 2010 +0100 + + mkdosfs: correct alignment of the root directory. + + Correct the code to align the root directory; it was broken before + since bs.dir_entries had already been set at the point of alignment. + This patch removes the dual use of bs.dir_entries and root_dir_entries + to carry the same information: the information is carried in + root_dir_entires exclusively, and then bs.dir_entries is set inside + setup_tables() at a late point. + + The code to align the root directory is also wrapped in + if (align_structures); this avoids rounding the number of root + directory entries up to a whole sector when used with -a + (i.e. preserves the previous behavior.) + +commit e462ac31a1d5d235b8a31a9e392e44e2dbc3783c +Author: H. Peter Anvin +Date: Wed Jan 6 20:55:36 2010 +0100 + + mkdosfs: improve wording in the man page for the -a option. + + Improve the English language used in the man page for the -a (no + align) option to mkdosfs. + +commit 680d71d167f30a823f88dd66473fc664cd887ab0 +Author: Daniel Baumann +Date: Wed Jan 6 11:27:25 2010 +0100 + + Adding reference to dosfslable in mkdosfs manpage. + +commit 60fc9f853c1045e615b34a193738f88021678d30 +Author: H. Peter Anvin +Date: Wed Jan 6 11:18:55 2010 +0100 + + mkdosfs: by default align all structures to cluster boundaries + + Align all data structures (reserved sectors, FATs, root directory for + FAT12/16) to an even multiple of the cluster size. This means that if + the partition is aligned, so will all clusters be. This adds + significant performance for anything where the physical sector size is + larger than the logical sector size, e.g. flash media or large-sector + hard disks. + +commit 312b05fc47107f695483994375a1f6f429069708 +Author: Daniel Baumann +Date: Thu Dec 24 10:53:36 2009 +0100 + + Releasing upstream version 3.0.7. + +commit 844307669208608a3464157ddb5e789bd9556f34 +Author: Ben Hutchings +Date: Thu Dec 24 09:55:52 2009 +0100 + + Fixing dosfslabel to set volume label in the right place, + see Debian bug #559985. + +commit 1bae0e2037717d65b3283db9da51ae7686a7a9be +Author: Lubomir Rintel +Date: Thu Dec 24 09:39:39 2009 +0100 + + Fixing out-of bound writes. + + Firstly, packed attribute is added to the structure so that extension + is guarranteed to immediately follow name for the cross-name-extension + reads to succeed. + + Secondly, writes into dir_entry->name that span through the extension as + well are split into two, so that FORTIFY_SOURCE's bound checking does + not abort dosfsck. There also was an off-by-one error in auto_rename()'s + sprintf(). + +commit eb297071adfca1ed7af85ca111f20ab41db8ac59 +Author: San Mehat +Date: Thu Dec 24 09:31:41 2009 +0100 + + Adding custom exit code in dosfsck for the case where the FS is read only. + +commit b3864d0939c960d0e0f15e4e3b1d626639b64681 +Author: Daniel Baumann +Date: Sun Oct 4 10:59:33 2009 +0200 + + Releasing upstream version 3.0.6. + +commit 144f8fcfc3f7982e8f460f8379a753b7a5941783 +Author: Steven J. Magnani +Date: Sun Oct 4 10:58:43 2009 +0200 + + Attempt to improve clarity of the orphan cluster reclaim code. + Minor optimization - remove some unnecessary checking. + +commit 343fe6d5e7135efadc498fd91e19ba8da499d0c9 +Author: Steven J. Magnani +Date: Sun Oct 4 08:37:19 2009 +0200 + + Close hole that permitted clusters to link to (invalid) cluster 1. + + If an orphan chain that linked to cluster 1 was reclaimed to a file, + deletion of the file would result in a filesystem panic. + +commit db079a02059d7f7296fbe9f87624623a43816c5f +Author: Steven J. Magnani +Date: Sun Oct 4 08:32:30 2009 +0200 + + Fix erroneous report of huge number of clusters in use on big-endian + systems when the FSINFO free cluster count is reset. + +commit 7d5320b8a374b8da1a16b09b3b9b0713828d6755 +Author: Daniel Baumann +Date: Mon Jul 27 14:26:11 2009 +0200 + + Releasing upstream version 3.0.5. + +commit e80ede4dd3c2058fe32e29ff82244ecb1c7c5514 +Author: Piotr Kaczuba +Date: Sun Jul 26 22:21:25 2009 +0200 + + Signed/unsigned char mismatch in check.c causes false positives + in bad_name() and can result in data loss, see Debian bug #538758. + +commit 9e15ddf6d52dd166efcb59f91f16fb9d695c86c5 +Author: Andrew Tridgell +Date: Sun Jul 26 22:12:06 2009 +0200 + + Update to new kernel patches that add FAT_NO_83NAME flag. + + See http://lkml.org/lkml/2009/7/20/425 and + http://lkml.org/lkml/2009/7/20/424 for more information. + +commit 6c68b94008157c444954d2f90a7f9ec8ffc2ec87 +Author: Daniel Baumann +Date: Tue Jul 21 08:10:52 2009 +0200 + + Releasing upstream version 3.0.4. + +commit 3ce6422e93f3de746be092e324253a8722917a86 +Author: Andrew Tridgell +Date: Tue Jul 21 07:59:22 2009 +0200 + + Modify dosfstools to support the dummy 8.3 short filename values + used by Linux systems with the VFAT_FS_DUALNAMES option disabled. + + See http://lkml.org/lkml/2009/6/26/313 and + http://lkml.org/lkml/2009/6/26/314 for more information. + +commit 94f8769aeddf0d0d3f0be54361514a464907a4a1 +Author: Paul Rupe +Date: Tue May 19 10:37:52 2009 +0200 + + Fixing "Too many files need repair" error during fsck. + +commit 89566399e407e54eb14d275770106ad42b3ac87c +Author: Daniel Baumann +Date: Mon May 18 15:12:04 2009 +0200 + + Releasing upstream version 3.0.3. + +commit 8147c98a542b714ccab34b57c84ed842bb6b50f2 +Author: Daniel Baumann +Date: Mon May 18 15:10:55 2009 +0200 + + Also declaring arm as an unaligned architecture, see Debian bug #502961. + +commit 18e27fa5c0f811e7edc10bca771acc7c04b19146 +Author: Steven J. Magnani +Date: Mon May 18 15:01:49 2009 +0200 + + Adding support for limited-memory embedded systems. + + This patch reorganizes heap memory usage by dosfsck and mkdosfs + to support limited-memory embedded systems - in particular, those + based on Xilinx's Microblaze processor. It also adds a few comments. + +commit 180b68e652df7bfeb7f336e0247aee8873edea7f +Author: Mike Frysinger +Date: Thu Mar 5 07:03:36 2009 +0100 + + Declaring Blackfin as an unaligned architecture. + +commit 71ac75353d9158aed663f0a3a9d1a1a67ee4ff4f +Author: Daniel Baumann +Date: Sat Feb 28 09:48:04 2009 +0100 + + Releasing upstream version 3.0.2. + +commit a75924b8ff629fe7ca5ba9c58ac75f66290242ee +Author: Hiroaki Ishizawa +Date: Fri Feb 13 10:00:46 2009 +0100 + + dosfsck corrupts root directory when fs->nfats is 1. + +commit 161a5e1fdd019732e0a304beceaeeb606eb128d6 +Author: Stepan Kasal +Date: Fri Jan 30 14:56:33 2009 +0100 + + src/dosfslabel.c (main): After writing the label, exit code should be 0. + +commit 26ffa1fb565c2b3284b846ca2733118808c85cb5 +Author: Daniel Baumann +Date: Fri Jan 30 14:06:01 2009 +0100 + + Also installing ChangeLog in install-doc target of Makefile. + +commit abad38ee561b02ec47be7e861780bf5fa2a05d0b +Author: Stepan Kasal +Date: Fri Jan 30 14:05:12 2009 +0100 + + Makefile: Do not clobber time stamps of doc files. + +commit 81882d20ec6bd4bf4914d39636cecc8c8e57dd67 +Author: Daniel Baumann +Date: Sun Nov 23 22:45:45 2008 +0100 + + Releasing upstream version 3.0.1. + +commit 41574812a2586f0b6aa1d4f6e2276e557e9cbbcf +Author: Daniel Baumann +Date: Sun Nov 23 18:41:01 2008 +0100 + + Applying Fedoras dosfstools-vfat-timingfix.diff from Bill Nottingham + to fix vfat timing issue. See + https://bugzilla.redhat.com/show_bug.cgi?id=448247 for more information. + +commit b80656109cc5cffdefd626c2ec9d45e3cf63a03e +Author: Ulrich Mueller +Date: Tue Oct 7 07:55:37 2008 +0200 + + Patch to check for bad number of clusters in dosfsck: + + * FAT16 filesystems with 65525 clusters or more will be rejected + (Before, this was not tested for. Up to 65535 clusters were accepted + as good). + + * For FAT32 filesystems with less than 65525 a warning message will be + output. + + Macro MSDOS_FAT12 is now replaced by FAT12_THRESHOLD to make it + consistent with the definition in mkdosfs and to remove the dependency + on the kernel version. + +commit b9c13d143c420a3ec6e1dcb652cafa407621e9c7 +Author: Dann Frazier +Date: Tue Sep 30 07:25:19 2008 +0200 + + Changing some wording to make the indended meaning of "full-disk device" + more obvious. + +commit 4df18ad463f41ae368c3c51bfb5a033072605663 +Author: Daniel Baumann +Date: Sun Sep 28 11:43:19 2008 +0200 + + Releasing upstream version 3.0.0. + +commit 17fbf2a6dc9255a6bb832472ae7cda674b55e961 +Author: Daniel Baumann +Date: Sun Sep 28 11:29:01 2008 +0200 + + Adding GPL headers to all files. + +commit d2039e12c8d53472411c91eb8e7a7c0544e13d6d +Author: Daniel Baumann +Date: Sun Sep 28 10:51:55 2008 +0200 + + Adding new GPL license file. + +commit e4e457f4b57090ecf0539f48dc682ab9afd14ab8 +Author: Daniel Baumann +Date: Fri Sep 26 23:31:12 2008 +0200 + + Redoing Makefile from scratch. + +commit 216568ca3a01ed38962b22c7bf838d15d5a4d98d +Author: Daniel Baumann +Date: Sat Sep 27 00:17:38 2008 +0200 + + Removing whitespaces in all files at EOL and EOF. + +commit f59157e06561c525605279145057361afa721042 +Author: Daniel Baumann +Date: Fri Sep 26 23:48:56 2008 +0200 + + Adding Debians dosfslabel.8 manpage from Francois Wendling + . + +commit c1bacab212d2b7f6ea93914976cb60056ff8276d +Author: Daniel Baumann +Date: Fri Sep 26 18:36:04 2008 +0200 + + Updating version.h includes to new location of version.h file. + +commit 1dae9f522062037d3539cadf344e0359c644171f +Author: Daniel Baumann +Date: Fri Sep 26 18:19:36 2008 +0200 + + Removing old lsm file. + +commit d843bec0b987f5582fe048f70e42df18c34d3ae4 +Author: Daniel Baumann +Date: Fri Sep 26 18:07:47 2008 +0200 + + Removing old cvsignore files. + +commit 77fddbc03016752286b26913c62b98f86ee63211 +Author: Daniel Baumann +Date: Fri Sep 26 18:18:39 2008 +0200 + + Removing old build file. + +commit f3e7168fc9eb6f32a6c85021186d84944cefeba8 +Author: Daniel Baumann +Date: Fri Sep 26 18:19:16 2008 +0200 + + Removing old GPL license files. + +commit 68089477036e97911791516ee2167286f26ff819 +Author: Daniel Baumann +Date: Fri Sep 26 18:21:57 2008 +0200 + + Unifying dosfsck and mkdosfs Makefiles in common src/Makefile. + +commit 9432a02d6e7c38872d7b1042f1b8be1b24a57427 +Author: Daniel Baumann +Date: Fri Sep 26 18:04:02 2008 +0200 + + Unifying dosfsck and mkdosfs sources in common src directory. + +commit 0c179b9ee47174d0f34d9fc796d540012740ac01 +Author: Daniel Baumann +Date: Fri Sep 26 18:05:27 2008 +0200 + + Unifying dosfsck and mkdosfs manpages in common man directory. + +commit 2d246c28ba6cfd43be2e44b11283891d922f352b +Author: Daniel Baumann +Date: Fri Sep 26 18:12:29 2008 +0200 + + Unifying dosfsck and mkdosfs documents in common doc directory. + +commit e5b16990515d0214fd103dd8aa22ff6a3cda4b64 +Author: Daniel Baumann +Date: Fri Sep 26 15:39:51 2008 +0200 + + Applying Gentoos dosfstools-2.11-preen.patch from Roy Marples + to alias dosfsck -p to -a: + + * Map -p to -a for baselayout-2, #177514. + +commit 6a1d974251a9f9a142775ace3a8048149abfa90c +Author: Daniel Baumann +Date: Fri Sep 26 15:49:43 2008 +0200 + + Applying Gentoos dosfstools-2.11-build.patch from Mike Frysinger + to improve Makefile: + + * Respect user settings #157785/#157786 by Diego Petteno. + +commit 1ea49f00e61b554dc833d44e1a3617e923be667e +Author: Daniel Baumann +Date: Fri Sep 26 15:37:34 2008 +0200 + + Applying Gentoos dosfstools-2.11-verify-double-count-fix.patch from + Robin H. Johnson to fix double count of files + during verification: + + * Don't double-count n_files during a verification pass. + Bugzilla: http://bugs.gentoo.org/show_bug.cgi?id=99845 + +commit 2d2f20b2c495fa620c7bb3ec5a0838b08f65ab05 +Author: Daniel Baumann +Date: Fri Sep 26 15:33:36 2008 +0200 + + Applying Gentoos dosfstools-2.11-fat32size.patch from Mike Frysinger + to fix generation of filesystems on 256meg devices: + + * Fix generation of FAT filesystems on devices that are 256meg in size + Patch by Ulrich Mueller and accepted upstream + http://bugs.gentoo.org/112504 + +commit a86564a9317b2bf9f7734feacdce794f20af74a7 +Author: Daniel Baumann +Date: Fri Sep 26 15:22:06 2008 +0200 + + Applying Suses dosfstools-2.11-unsupported-sector-size.patch from Petr + Gajdos to add sector size warning: + + * added warning for creation msdos on filesystem with sector size + greater than 4096 [fate#303325] + +commit 8171e51f4e02bd9f92bb515ca7896d3cb1b564b5 +Author: Daniel Baumann +Date: Fri Sep 26 15:18:35 2008 +0200 + + Applying Suses dosfstools-2.11-mkdosfs-geo0.diff from Ludwig Nussel + to fix handling of zero heads and sectors: + + * the HDIO_GETGEO ioctl works on device mapper devices but returns + zero heads and sectors. Therefore let's a) assume dummy values in + that case in mkdosfs and b) don't consider such fat file systems as + invalid in dosfsck. The Linux kernel accepts them anyways. + +commit db887df619f4e995db0ab112334f31464a03fa0e +Author: Daniel Baumann +Date: Fri Sep 26 15:15:40 2008 +0200 + + Applying Suses dosfstools-2.11-linuxfs.patch from Ruediger Oertel + to not include linux/fs.h. + +commit 7fe3fa643494b26962d542fac38d988ac60f8c09 +Author: Daniel Baumann +Date: Fri Sep 26 15:11:50 2008 +0200 + + Applying Fedoras dosfstools-2.11-assumeKernel26.patch from Peter Vrabec + to remove linux 2.6 conditionals: + + * LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) does not work with + glibc-kernheaders-2.4-9.1.94 + +commit 90c1c93c100722a03e48be51b1312fe65c1cb156 +Author: Daniel Baumann +Date: Fri Sep 26 15:05:00 2008 +0200 + + Applying Debians 99-conglomeration.dpatch (no other information + available). + +commit bb6541bf4735e3a7f1c71b4722c6d03bb4549eea +Author: Daniel Baumann +Date: Fri Sep 26 14:26:41 2008 +0200 + + Applying Debians 15-manpage-files.dpatch from Daniel Baumann + to improve dosfsck manpage: + + * Lists fsckNNNN.rec files in FILES section (Closes: #444596). + +commit 49282165866be19d3bc54a3f4bdee2cf3a63ba6c +Author: Daniel Baumann +Date: Fri Sep 26 14:34:42 2008 +0200 + + Applying Debians 13-getopt.dpatch from Adonikam Virgo + to fix mkdosfs getopt: + + * Fixes backup sector getopt (Closes: #232387, #479794). + +commit c32e44b85f4eac6f6a94bd620eea4abba257042a +Author: Daniel Baumann +Date: Fri Sep 26 14:34:17 2008 +0200 + + Applying Debians 12-zero-slot.dpatch by Karl Tomlinson + to fix dosfsck zero slot crashes: + + * Fixes crashes due to zero slot numbers causing a negative offset in + the call to copy_lfn_part in lfn_add_slot. On amd64 this results in + a SIGSEGV in copy_lfn_part. On x86 the result is heap corruption + and thus sometimes a SIGSEGV or double free abort later. (Closes: + #152550, #353198, #356377, #401798). + +commit 370847af533e628aa9e38710e6d50af09f2b71ba +Author: Daniel Baumann +Date: Fri Sep 26 14:33:54 2008 +0200 + + Applying Debians 11-memory-efficiency.dpatch from Eero Tamminen + to improve dosfsck memory efficiency: + + * Improves memory efficiency when checking filesystems. + +commit 28da9f286a52acec7df7ad06cb0679e5f828e7f3 +Author: Daniel Baumann +Date: Fri Sep 26 14:33:28 2008 +0200 + + Applying Debians 10-manpage-synopsis.dpatch from Daniel Baumann + to fix manpage synopsis: + + * List alternative binary names in manpage synopsis (Closes: #284983). + +commit f253073021551c9b58d0f2ac262deb3c1b950b06 +Author: Daniel Baumann +Date: Fri Sep 26 14:32:46 2008 +0200 + + Applying Debians 09-manpage-fat32.dpatch from Daniel Baumann + to improve mkdosfs manpage: + + * Don't claim that FAT32 is not choosed automatically (Closes: + #414183). + +commit f37c07aec3972cfc0db374d544ee3b27c0b4b20b +Author: Daniel Baumann +Date: Fri Sep 26 14:32:23 2008 +0200 + + Applying Debians 08-manpage-drop.dpatch from Daniel Baumann + to improve dosfsck manpage: + + * Don't use confusing word 'drop' when 'delete' is meant (Closes: + #134100). + +commit 3f970c65586da2f44d2a49b639e89341bbaf1fba +Author: Daniel Baumann +Date: Fri Sep 26 14:31:50 2008 +0200 + + Applying Debians 07-manpage-spelling.dpatch from Justin Pryzby + to fix mkdosfs manpage typos. + +commit 18678ba5f3a10c2a54ffee735651d7a265ae7d54 +Author: Daniel Baumann +Date: Fri Sep 26 14:30:31 2008 +0200 + + Applying Suses dosfstools-2.11_determine-sector-size.patch from Petr + Gajdos to determine mkdosfs sector size automatically: + + * determine sector size of device automatically or if -S parameter + present, verify, that it's not under physical sector size + +commit 29753931b078856d78f473cfb6273e111a26891e +Author: Daniel Baumann +Date: Fri Sep 26 14:30:03 2008 +0200 + + Applying Suses dosfstools-2.11-o_excl.patch from Pavol Rusnak + to use O_EXCL in mkdosfs: + + * mkdosfs now opens device with O_EXCL [#238687] + +commit 16bb7b09ad9eaf0fe37a732cabcdbdf975b77d3e +Author: Daniel Baumann +Date: Fri Sep 26 14:29:36 2008 +0200 + + Applying Debians 04-unaligned-memory.dpatch from Khalid Aziz + to fix dosfsck unaligned memory accesses: + + * Fix unaligned memory accesses which cause warnings to appear + everytime the elilo bootloader script runs. This has led a number + of users to believe their install has failed (Closes: #258839). + +commit 1298cc8f37eaa27ca542b8b7186ea5a47a63cd7e +Author: Daniel Baumann +Date: Fri Sep 26 13:47:40 2008 +0200 + + Applying Fedoras dosfstools-2.11-label.patch from Jeremy Katz + to add dosfslabel (originally by Peter Jones). + +commit d23890e1d89770d6d2ba58362c2fc4ebafbde15c +Author: Daniel Baumann +Date: Fri Sep 26 13:41:14 2008 +0200 + + Applying Fedoras dosfstools-2.11-fortify.patch from Jakub Jelinek + to make it build with -D_FORTIFY_SOURCE=2: + + * This violates -D_FORTIFY_SOURCE=2 (which is stricter than C + standard), but isn't actually any buffer overflow. But using memcpy + is more efficient anyway. + +commit 7dbd82000e59246c1c2f8c280c4491259e10a767 +Author: Daniel Baumann +Date: Fri Sep 26 13:40:47 2008 +0200 + + Applying Fedoras dosfstools-2.7-argfix.patch (no other information + available). + +commit 88f3b3139c72ac11cb3dd3f5afa8dbb2198a8de5 +Author: Daniel Baumann +Date: Thu Jun 26 12:45:36 2008 +0200 + + Adding upstream version 2.11. diff --git a/dosfstools/Makefile b/dosfstools/Makefile new file mode 100644 index 000000000..050b750a5 --- /dev/null +++ b/dosfstools/Makefile @@ -0,0 +1,112 @@ +# Makefile +# +# Copyright (C) 2008 Daniel Baumann +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# On Debian systems, the complete text of the GNU General Public License +# can be found in /usr/share/common-licenses/GPL-3 file. + +DESTDIR = +PREFIX = /usr/local +SBINDIR = $(PREFIX)/sbin +DOCDIR = $(PREFIX)/share/doc +MANDIR = $(PREFIX)/share/man + +#OPTFLAGS = -O2 -fomit-frame-pointer -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 +OPTFLAGS = -O2 -fomit-frame-pointer $(shell getconf LFS_CFLAGS) +#WARNFLAGS = -Wall -pedantic -std=c99 +WARNFLAGS = -Wall +DEBUGFLAGS = -g +CFLAGS += $(OPTFLAGS) $(WARNFLAGS) $(DEBUGFLAGS) + +VPATH = src + +all: build + +build: dosfsck dosfslabel mkdosfs + +dosfsck: boot.o check.o common.o fat.o file.o io.o lfn.o dosfsck.o + +dosfslabel: boot.o check.o common.o fat.o file.o io.o lfn.o dosfslabel.o + +mkdosfs: mkdosfs.o + +rebuild: distclean build + +install: install-bin install-doc install-man + +install-bin: build + install -d -m 0755 $(DESTDIR)/$(SBINDIR) + install -m 0755 dosfsck dosfslabel mkdosfs $(DESTDIR)/$(SBINDIR) + + ln -sf dosfsck $(DESTDIR)/$(SBINDIR)/fsck.msdos + ln -sf dosfsck $(DESTDIR)/$(SBINDIR)/fsck.vfat + ln -sf mkdosfs $(DESTDIR)/$(SBINDIR)/mkfs.msdos + ln -sf mkdosfs $(DESTDIR)/$(SBINDIR)/mkfs.vfat + +install-doc: + install -d -m 0755 $(DESTDIR)/$(DOCDIR)/dosfstools + install -p -m 0644 ChangeLog doc/* $(DESTDIR)/$(DOCDIR)/dosfstools + +install-man: + install -d -m 0755 $(DESTDIR)/$(MANDIR)/man8 + install -p -m 0644 man/*.8 $(DESTDIR)/$(MANDIR)/man8 + + ln -sf dosfsck.8 $(DESTDIR)/$(MANDIR)/man8/fsck.msdos.8 + ln -sf dosfsck.8 $(DESTDIR)/$(MANDIR)/man8/fsck.vfat.8 + ln -sf mkdosfs.8 $(DESTDIR)/$(MANDIR)/man8/mkfs.msdos.8 + ln -sf mkdosfs.8 $(DESTDIR)/$(MANDIR)/man8/mkfs.vfat.8 + +uninstall: uninstall-bin uninstall-doc uninstall-man + +uninstall-bin: + rm -f $(DESTDIR)/$(SBINDIR)/dosfsck + rm -f $(DESTDIR)/$(SBINDIR)/dosfslabel + rm -f $(DESTDIR)/$(SBINDIR)/mkdosfs + + rm -f $(DESTDIR)/$(SBINDIR)/fsck.msdos + rm -f $(DESTDIR)/$(SBINDIR)/fsck.vfat + rm -f $(DESTDIR)/$(SBINDIR)/mkfs.msdos + rm -f $(DESTDIR)/$(SBINDIR)/mkfs.vfat + + rmdir --ignore-fail-on-non-empty $(DESTDIR)/$(SBINDIR) + +uninstall-doc: + rm -rf $(DESTDIR)/$(DOCDIR)/dosfstools + + rmdir --ignore-fail-on-non-empty $(DESTDIR)/$(DOCDIR) + +uninstall-man: + rm -f $(DESTDIR)/$(MANDIR)/man8/dosfsck.8 + rm -f $(DESTDIR)/$(MANDIR)/man8/dosfslabel.8 + rm -f $(DESTDIR)/$(MANDIR)/man8/mkdosfs.8 + + rm -f $(DESTDIR)/$(MANDIR)/man8/fsck.msdos.8 + rm -f $(DESTDIR)/$(MANDIR)/man8/fsck.vfat.8 + rm -f $(DESTDIR)/$(MANDIR)/man8/mkfs.msdos.8 + rm -f $(DESTDIR)/$(MANDIR)/man8/mkfs.vfat.8 + + rmdir --ignore-fail-on-non-empty $(DESTDIR)/$(MANDIR)/man8 + rmdir --ignore-fail-on-non-empty $(DESTDIR)/$(MANDIR) + +reinstall: distclean install + +clean: + rm -f *.o + +distclean: clean + rm -f dosfsck dosfslabel mkdosfs + +.PHONY: build rebuild install install-bin install-doc install-man uninstall uninstall-bin uninstall-doc uninstall-man reinstall clean distclean diff --git a/dosfstools/bin/Nindent b/dosfstools/bin/Nindent new file mode 100755 index 000000000..cf8ecfd50 --- /dev/null +++ b/dosfstools/bin/Nindent @@ -0,0 +1,18 @@ +#!/bin/sh +PARAM="-npro -kr -i4 -ts8 -sob -l80 -ss -ncs -cp1" +RES=`indent --version` +V1=`echo $RES | cut -d' ' -f3 | cut -d'.' -f1` +V2=`echo $RES | cut -d' ' -f3 | cut -d'.' -f2` +V3=`echo $RES | cut -d' ' -f3 | cut -d'.' -f3` +if [ $V1 -gt 2 ]; then + PARAM="$PARAM -il0" +elif [ $V1 -eq 2 ]; then + if [ $V2 -gt 2 ]; then + PARAM="$PARAM -il0"; + elif [ $V2 -eq 2 ]; then + if [ $V3 -ge 10 ]; then + PARAM="$PARAM -il0" + fi + fi +fi +exec indent $PARAM "$@" diff --git a/dosfstools/doc/ANNOUNCE.mkdosfs b/dosfstools/doc/ANNOUNCE.mkdosfs new file mode 100644 index 000000000..1f02ea281 --- /dev/null +++ b/dosfstools/doc/ANNOUNCE.mkdosfs @@ -0,0 +1,41 @@ +Announcing the release of mkdosfs version 0.3b (Yggdrasil) + +It seems I didn't get the bug completely fixed in 0.3a. Some +borderline cases would still allocate too many sectors for the FAT. +Again, nothing to worry about, just a nitpick -- this one would only +in certain cases add one sector per FAT. + +Announcing the release of mkdosfs version 0.3a (Yggdrasil) + +Fixed a bug which would cause too many sectors to be reserved for the +FAT (filesystem will still work fine, but have slightly less space +available). + +Announcing the release of mkdosfs version 0.3 (Yggdrasil) + +This version correctly handles even very large filesystems, and +properly supports the modern (3.3+) DOS bootsector format, including a +message printed on boot attempts. + +Peter Anvin +Yggdrasil Computing, Inc. +hpa@yggdrasil.com + + -------------- + +Announcing the release of mkdosfs version 0.2 + + +I've just uploaded mkdosfs to sunsite.unc.edu. It works in a similar way +to Remy Card's mke2fs, but creates an MS-DOS file system. + +The filename is mkdosfs-0.2.tar.gz. + +This second release should fix a small bug that could lead to FAT sizes that +Linux's dosfs would accept but MS-DOS wouldn't. + +The archive contains a manual page, binary and source versions. + + +Dave Hudson +dave@humbug.demon.co.uk diff --git a/dosfstools/doc/ChangeLog.dosfsck b/dosfstools/doc/ChangeLog.dosfsck new file mode 100644 index 000000000..f628ac289 --- /dev/null +++ b/dosfstools/doc/ChangeLog.dosfsck @@ -0,0 +1,10 @@ +Changes from version 0 to 1 +=========================== + + - fixed an off-by-two error in check.c:check_file + - fixed marking clusters bad in fat.c:set_fat + - fat.c:reclaim_free was also reclaiming bad clusters. + - fixed many incorrect byte sex conversions in check.c and fat.c + - -t and -w now require -a or -r + - added option -d to drop files. + - added option -u to try to "undelete" non-directory files. diff --git a/dosfstools/doc/ChangeLog.dosfstools-2.x b/dosfstools/doc/ChangeLog.dosfstools-2.x new file mode 100644 index 000000000..7ed9efe94 --- /dev/null +++ b/dosfstools/doc/ChangeLog.dosfstools-2.x @@ -0,0 +1,161 @@ +version 2.11 +============ + + - all: don't use own llseek() anymore, glibc lseek() does everything we need + - dosfsck: lfn.c: avoid segfault + - dosfsck: check.c, lfn.c: check for orphaned LFN slots + - dosfsck: check.c alloc_rootdir_entry(): set owner of newly alloced clusters + - dosfsck: dosfsck.h: better use for byte swapping + - dosfsck: io.c: added code for real DOS + - mkdosfs: raised FAT12_THRESHOLD from 4078 to 4085, introduced MIN_CLUST_32 + - mkdosfs: fix loop device size + - mkdosfs: by default, use FAT32 on devices >= 512MB + - mkdosfs: fix a memory leak (blank_sector) + - mkdosfs: fix parsing of number of blocks on command line, so that numbers + >2G can be used + - mkdosfs: add 'b' to getopt() string so this option can be used :) + - mkdosfs: fix parsing of -i arg (should be unsigned) + - mkdosfs: change default permissions of created images (-C) to 0666 & ~umask + - mkdosfs: relax geometry check: if HDIO_GETGEO fails, print a warning and + default to H=255,S=63 + - dosfsck: new option -n (no-op): just check non-interactively, but + don't write anything to filesystem + - A few #include changes to support compilation with linux 2.6 + headers (thanks to Jim Gifford ) + - dosfsck: remove directory entries pointing to start cluster 0, if they're + not "." or ".." entries that should actually point to the root dir + (pointed out by Thomas Winkler ) + - mkdosfs: new option -h to set number of hidden sectors + (thanks to Godwin Stewart ) + - all: updated my mail address everywhere... + +version 2.10 +============ + + - dosfsck: various 64-bit fixes and removed some warnings by Michal + Cihar + - mkdosfs: better error message if called without parameters (also + suggested by Michal) + +version 2.9 +=========== + + - dosfsck: if EOF from stdin, exit with error code + - dosfsck: Fix potential for "Internal error: next_cluster on bad cluster". + - dosfsck: When clearing long file names, don't overwrite the dir + entries with all zeros, but put 0xe5 into the first byte. + Otherwise, some OSes stop reading the directory at that point... + - dosfsck: in statistics printed by -v, fix 32bit overflow in number + of data bytes. + - dosfsck: fix an potential overflow in "too many clusters" check + - dosfsck: fix 64bit problem in fat.c (Debian bug #152769) + - dosfsck: allow FAT size > 32MB. + - dosfsck: allow for only one FAT + - dosfsck: with -v, also check that last sector of the filesystem can + be read (in case a partition is smaller than the fs thinks) + - mkdosfs: add note in manpage that creating bootable filesystems is + not supported. + - mkdosfs: better error message with pointer to -I if target is a + full-disk device. + +version 2.8 +=========== + + - dosfsck: Fixed endless loop whenever a volume label was present. + +version 2.7 +=========== + + - dosfsck: Don't check volume label for bad characters, everything + seems to be allowed there... Also ignore duplicate names where one of + them is a volume label. + +version 2.6 +=========== + + - mkdosfs: Added correct heads definition for 2.88M floppies if not + created via loopback. + - dosfsck: If boot sector and its backup are different (FAT32), offer + to write the backup to sector 0. (tnx to Pavel Roskin for this) + - For 64 bit alpha, struct bootsector in dosfsck.h must be defined + with __attribute__((packed)). + - mkdosfs now actually accepts -R option. (tnx to David Kerrawn) + - Fixed typo in dosfsck boot.c (recognition of boot signature in FSINFO) + - Various compilation fixes for 2.4 kernel headers and for ia64. + +version 2.5 +=========== + + - The llseek() implementation for alpha didn't really work; fixed it. + +version 2.4 +=========== + + - Fix compiling problem on alpha (made a silly typo...) + +version 2.3 +=========== + + - mkdosfs: Fixed usage message (printed only "bad address"). + - both: made man pages and usage statements more consistent. + - both: fix llseek function for alpha. + - dosfsck: fix reading of unaligned fields in boot sector for alpha. + - dosfsck: fixed renaming of files (extension wasn't really written). + +version 2.2 +=========== + + - Added dosfsck/COPYING, putting dosfsck officially under GPL (Werner + and I agree that it should be GPL). + - mkdosfs: Allow creation of a 16 bit FAT on filesystems that are too + small for it if the user explicitly selected FAT16 (but a warning + is printed). Formerly, you got the misleading error message "make + the fs a bit smaller". + - dosfsck: new option -y as synonym for -y; for compability with + other fs checkers, which also accept this option. + - dosfsck: Now prints messages similar to e2fsck: at start version + and feature list; at end number of files (and directories) and + number of used/total clusters. This makes the printouts of *fsck at + boot time nicer. + - dosfsck: -a (auto repair) now turns on -f (salvage files), too. -a + should act as non-destructive as possible, so lost clusters should + be assigned to files. Otherwise the data in them might be + overwritten later. + - dosfsck: Don't drop a directory with lots of bad entries in + auto-repair mode for the same reason as above. + - dosfsck: avoid deleting the whole FAT32 root dir if something is + wrong with it (bad start cluster or the like). + - general: also create symlinks {mkfs,fsck}.vfat.8 to the respective + real man pages. + +version 2.1 +=========== + + - Fix some forgotten loff_t's for filesystems > 4GB. (Thanks to + ). + - Fix typo in mkdosfs manpage. + - Removed inclusion of from mkdosfs.c; it's unnecessary and + caused problems in some environments. + - Fix condition when to expect . and .. entries in a directory. (Was + wrong for non-FAT32 if first entry in root dir was a directory also.) + - Also create mkfs.vfat and fsck.vfat symlinks, so that also + filesystems listed with type "vfat" in /etc/fstab can be + automatically checked. + +version 2.0 +=========== + + - merge of mkdosfs and dosfstools in one package + - new maintainer: Roman Hodek + - FAT32 support in both mkdosfs and dosfsck + - VFAT (long filename) support in dosfsck + - Support for Atari variant of MS-DOS filesystem in both tools + - Working support for big-endian systems in both tools + - Better support for loop devices in mkdosfs: usual floppy sizes are + detected and media byte etc. set accordingly; if loop fs has no + standard floppy size, use hd params + (mainly by Giuliano Procida ) + - Removed lots of gcc warnings + - Fixed some minor calculation bugs in mkdosfs. + +For change logs previous to 2.0, see the CHANGES files in the subdirectories. diff --git a/dosfstools/doc/ChangeLog.mkdosfs b/dosfstools/doc/ChangeLog.mkdosfs new file mode 100644 index 000000000..e39d9d6a4 --- /dev/null +++ b/dosfstools/doc/ChangeLog.mkdosfs @@ -0,0 +1,18 @@ +28th January 1995 H. Peter Anvin (hpa@yggdrasil.com) + + Better algorithm to select cluster sizes on large filesystems. + Added bogus boot sector code that on attempts to boot prints a + message (which can be chosen at mkdosfs time) and lets the user + press any key and try again. Corrected support for 1.2 Mb + floppies. mkdosfs now generates the extended bootsector + (superblock) format of DOS 3.3+, with support for volume ID's and + volume labels (volume labels are also written to the root + directory, as they should). + +18th February 1994 Dave Hudson (dave@humbug.demon.co.uk) + + Released version 0.2 - clears a bug in the FAT sizing code. + +1st September 1993 Dave Hudson (dave@humbug.demon.co.uk) + + Released version 0.1 - ALPHA release of mkdosfs diff --git a/dosfstools/doc/README.dosfsck b/dosfstools/doc/README.dosfsck new file mode 100644 index 000000000..7b351aa62 --- /dev/null +++ b/dosfstools/doc/README.dosfsck @@ -0,0 +1,60 @@ +dosfsck, version 1 +================== + +WARNING: This is ALPHA test software. Use at your own risk. + +dosfsck is the Linux equivalent of PC/MS-DOS' CHKDSK. It checks the +consistency of PC/MS-DOS file systems and optionally tries to repair +them. The tests dosfsck performs are described in the man page. + +dosfsck needs header files from dosfs.9 (or later) to compile. + +Before using dosfsck to repair a file system that contains data of any +value, you should verify that dosfsck is able to correct all reported +errors. (Except fatal errors and those reported as unfixable, of +course.) In order to do this, run it with the -V option, e.g. + + dosfsck -V /dev/sda1 (automatic check) +or dosfsck -V -r /dev/sda1 (interactive check and repair) + +dosfsck will perform two passes: in the first pass, inconsistencies are +detected and a list of changes to correct the problems is generated. In +the second pass, those changes are applied whenever dosfsck reads data +from disk. Hence no fixable errors should be reported in the second +pass if the first pass was successful. + +Please notify the author if fixable errors are reported in the second +pass. + +After verifying that dosfsck appears to be able to perform the desired +operations, either confirm that you want the changes to be performed +(if dosfsck was started with -r) or re-run dosfsck with the -a option +(if it was started without -r). + +Please send bug reports, comments, flames, etc. to +almesber@nessie.cs.id.ethz.ch or almesber@bernina.ethz.ch + +- Werner + +FAT32 and LFN support +===================== + +I've finally implemented some of the new features of MS-DOS +filesystems: FAT32 and long filenames. + +FAT32 is automatically detected and of course the different FAT +structure is handled. (Internally many changes were needed, so 32 bit +variables for all cluster numbers and 64 bit vars for offsets inside +the filesystem.) New checks for FAT32 are most notably on the backup +boot sector and the new info sector. Also the possibility that the +root directory resides in a cluster chain (instead of in a static +area) on FAT32 is handled. + +dosfscheck also knows about VFAT long filenames now. It parses those +names and uses them in listings etc. when available. There are also +some checks on the (cruel) structure of how LFNs are stored and some +attempts to fix problems. + +- Roman + +BTW, version 2 isn't ALPHA anymore :-) diff --git a/dosfstools/doc/README.dosfstools-2.x b/dosfstools/doc/README.dosfstools-2.x new file mode 100644 index 000000000..5fb00ed07 --- /dev/null +++ b/dosfstools/doc/README.dosfstools-2.x @@ -0,0 +1,60 @@ + +Atari format support +==================== + +Both mkdosfs and dosfsck now can also handle the Atari variation of +the MS-DOS filesystem format. The Atari format has some minor +differences, some caused by the different machine architecture (m68k), +some being "historic" (Atari didn't change some things that M$ +changed). + +Both tools automatically select Atari format if they run on an Atari. +Additionally the -A switch toggles between Atari and MS-DOS format. +I.e., on an Atari it selects plain DOS format, on any other machine it +switches to Atari format. + +The differences are in detail: + + - Atari TOS doesn't like cluster sizes != 2, so the usual solution + for bigger partitions was to increase the logical sector size. So + mkdosfs can handle sector sizes != 512 now, you can also manually + select it with the -S option. On filesystems larger than approx. 32 + MB, the sector size is automatically increased (stead of the + cluster size) to make the filesystem fit. mkdosfs will always use 2 + sectors per cluster (also with the floppy standard configurations), + except when directed otherwise on the command line. + + - From the docs, all values between 0xfff8 and 0xffff in the FAT mark + an end-of-file. However, DOS usually uses 0xfff8 and Atari 0xffff. + This seems to be only an consmetic difference. At least TOS doesn't + complain about 0xffff EOF marks. Don't know what DOS thinks of + 0xfff8 :-) Anyway, both tools use the EOF mark common to the + system (DOS/Atari). + + - Something similar of the bad cluster marks: On Atari the FAT values + 0xfff0 to 0xfff7 are used for this, under DOS only 0xfff7 (the + others can be normal cluster numbers, allowing 7 more clusters :-) + However, both systems usually mark with 0xfff7. Just dosfsck has to + interpret 0xfff0...0xfff7 differently. + + - Some fields in the boot sector are interpreted differently. For + example, Atari has a disk serial number (used to aid disk change + detection) where DOS stores the system name; the 'hidden' field is + 32 bit for DOS, but 16 bit for Atari, and there's no 'total_sect' + field; the 12/16 bit FAT decision is different: it's not based on + the number of clusters, but always FAT12 on floppies and FAT16 on + hard disks. mkdosfs nows about these differences and constructs the + boot sector accordingly. + + - In dosfsck, the boot sector differences also have to known, to not + warn about things that are no error on Atari. In addition, most + Atari formatting tools fill the 'tracks' and 'heads' fields with 0 + for hard disks, because they're meaningless on SCSI disks (Atari + has/had no IDE). Due to this, the check that they should be + non-zero is switched off. + + - Under Atari TOS, some other characters are illegal in filenames: + '<', '>', '|', '"', and ':' are allowed, but all non-ASCII chars + (codes >= 128) are forbidden. + +- Roman diff --git a/dosfstools/doc/README.mkdosfs b/dosfstools/doc/README.mkdosfs new file mode 100644 index 000000000..ae93ada4e --- /dev/null +++ b/dosfstools/doc/README.mkdosfs @@ -0,0 +1,50 @@ +mkdosfs - Make DOS file system utilty. + + +I wrote this, partially to complement the dosfsck utility written by Werner +Almesberger (who graciously gave me some pointers when I asked for some +advice about writing this code), and also to avoid me having to boot DOS +just to create data partitions (I use Linux to back up DOS :-) ). + +The code is really derived from Remy Card's mke2fs utility - I used this as a +framework, although all of the file system specific stuff was removed and the +DOS stuff inserted. I believe originally mke2fs was based on Linus' mkfs +code, hence the acknowledgements in the source code. + +Neither Remy nor Linus have had any involvement with mkdosfs, so if there are +any bugs they're almost certainly "all my own work". + +The code has been available for ftp since 1st September 1993, and I have yet +to receive any bug reports from users. I don't know of any bugs, but if you +do find a bug or have any constructive comments, please mail me! + +The only bug I found with version 0.1 was an obscure fault that could lead +to an invalid (for MS-DOS, not Linux's dos fs) number of sectors used in the +file allocation table(s). + + +Dave Hudson +dave@humbug.demon.co.uk + + +FAT32 support +============= + +mkdosfs now can also create filesystems in the new FAT32 format. To do +this, give mkdosfs a "-F 32" option. FAT32 isn't selected +automatically (yet), even if very large clusters are needed with +FAT16. With FAT32 you have two additional options, -R to select the +number of reserved sectors (usually 32), and -b to select the location +of the backup boot sector (default 6). Of course such a backup is +created, as well as the new info sector. On FAT32, the root directory +is always created as a cluster chain. Sorry, there's no switch to +generate an old static root dir. + +One bigger bug fix besides FAT32 was to reject filesystems that need a +16 bit FAT to fit all possible clusters, but the bigger FAT needs some +more sectors, so the total number of clusters drop below the border +where MS-DOS expects a 12 bit FAT. So such filesystems would be FAT16, +but interpreted as FAT32 by DOS. The fix is to reduce filesystem size +a bit. + +- Roman diff --git a/dosfstools/doc/TODO.dosfstools-2.x b/dosfstools/doc/TODO.dosfstools-2.x new file mode 100644 index 000000000..dbc2de074 --- /dev/null +++ b/dosfstools/doc/TODO.dosfstools-2.x @@ -0,0 +1,14 @@ + -*- mode: indented-text -*- + + - dosfsck: Better checking of file times: ctime <= mtime <= atime + + - mkdosfs: If /etc/bootsect.dos (or similar) exists, use it as a + template for generating boot sectors. This way, you can, e.g., make + bootable DOS disks. + + Addendum: Don't know if that's so wise... There are really many + variants of DOS/Windows bootcode out in the wild, and the code is + proprietary, too. + + - dosfsck: read-only sector test (-t without -a or -r); just print + out errors. diff --git a/dosfstools/man/dosfsck.8 b/dosfstools/man/dosfsck.8 new file mode 100644 index 000000000..d363c68ce --- /dev/null +++ b/dosfstools/man/dosfsck.8 @@ -0,0 +1,112 @@ +.TH DOSFSCK 8 "2010\-01\-31" "3.0.9" "check and repair MS\-DOS filesystems" + +.SH NAME +\fBdosfsck\fR \- check and repair MS\-DOS filesystems + +.SH SYNOPSIS +\fBdosfsck\fR|\fBfsck.msdos\fR|\fBfsck.vfat\fR [\-aAflnrtvVwy] [\-d \fIPATH\fR \-d\ \fI...\fR] [\-u\ \fIPATH\fR \-u \fI...\fR] \fIDEVICE\fR + +.SH DESCRIPTION +\fBdosfsck\fR verifies the consistency of MS\-DOS filesystems and optionally tries to repair them. +.PP +The following filesystem problems can be corrected (in this order): +.IP "*" 4 +FAT contains invalid cluster numbers. Cluster is changed to EOF. +.IP "*" 4 +File's cluster chain contains a loop. The loop is broken. +.IP "*" 4 +Bad clusters (read errors). The clusters are marked bad and they are removed from files owning them. This check is optional. +.IP "*" 4 +Directories with a large number of bad entries (probably corrupt). The directory can be deleted. +.IP "*" 4 +Files . and .. are non\-directories. They can be deleted or renamed. +.IP "*" 4 +Directories . and .. in root directory. They are deleted. +.IP "*" 4 +Bad filenames. They can be renamed. +.IP "*" 4 +Duplicate directory entries. They can be deleted or renamed. +.IP "*" 4 +Directories with non\-zero size field. Size is set to zero. +.IP "*" 4 +Directory . does not point to parent directory. The start pointer is adjusted. +.IP "*" 4 +Directory .. does not point to parent of parent directory. The start pointer is adjusted. +.IP "*" 4 +Start cluster number of a file is invalid. The file is truncated. +.IP "*" 4 +File contains bad or free clusters. The file is truncated. +.IP "*" 4 +File's cluster chain is longer than indicated by the size fields. The file is truncated. +.IP "*" 4 +Two or more files share the same cluster(s). All but one of the files are truncated. If the file being truncated is a directory file that has already been read, the filesystem check is restarted after truncation. +.IP "*" 4 +File's cluster chain is shorter than indicated by the size fields. The file is truncated. +.IP "*" 4 +Clusters are marked as used but are not owned by a file. They are marked as free. +.PP +Additionally, the following problems are detected, but not repaired: +.IP "*" 4 +Invalid parameters in boot sector. +.IP "*" 4 +Absence of . and .. entries in non\-root directories +.PP +When \fBdosfsck\fR checks a filesystem, it accumulates all changes in memory and performs them only after all checks are complete. This can be disabled with the \fB\-w\fR option. + +.SH OPTIONS +.IP "\fB\-a\fR" 4 +Automatically repair the filesystem. No user intervention is necessary. Whenever there is more than one method to solve a problem, the least destructive approach is used. +.IP "\fB\-A\fR" 4 +Use Atari variation of the MS\-DOS filesystem. This is default if \fBdosfsck\fR is run on an Atari, then this option turns off Atari format. There are some minor differences in Atari format: Some boot sector fields are interpreted slightly different, and the special FAT entries for end\-of\-file and bad cluster can be different. Under MS\-DOS 0xfff8 is used for EOF and Atari employs 0xffff by default, but both systems recognize all values from 0xfff8...0xffff as end\-of\-file. MS\-DOS uses only 0xfff7 for bad clusters, where on Atari values 0xfff0...0xfff7 are for this purpose (but the standard value is still 0xfff7). +.IP "\fB\-d\fR" 4 +Delete the specified file. If more that one file with that name exists, the first one is deleted. +.IP "\fB\-f\fR" 4 +Salvage unused cluster chains to files. By default, unused clusters are added to the free disk space except in auto mode (\fB\-a\fR). +.IP "\fB\-l\fR" 4 +List path names of files being processed. +.IP "\fB\-n\fR" 4 +No\-operation mode: non\-interactively check for errors, but don't write +anything to the filesystem. +.IP "\fB\-r\fR" 4 +Interactively repair the filesystem. The user is asked for advice whenever +there is more than one approach to fix an inconsistency. +.IP "\fB\-t\fR" 4 +Mark unreadable clusters as bad. +.IP "\fB\-u\fR" 4 +Try to undelete the specified file. \fBdosfsck\fR tries to allocate a chain of contiguous unallocated clusters beginning with the start cluster of the undeleted file. +.IP "\fB\-v\fR" 4 +Verbose mode. Generates slightly more output. +.IP "\fB\-V\fR" 4 +Perform a verification pass. The filesystem check is repeated after the first run. The second pass should never report any fixable errors. It may take considerably longer than the first pass, because the first pass may have generated long list of modifications that have to be scanned for each disk read. +.IP "\fB\-w\fR" 4 +Write changes to disk immediately. +.IP "\fB\-y\fR" 4 +Same as \fB\-a\fR (automatically repair filesystem) for compatibility with other fsck tools. +.PP +\fBNote:\fR If \fB\-a\fR and \fB\-r\fR are absent, the filesystem is only checked, but not repaired. + +.SH "EXIT STATUS" +.IP "0" 4 +No recoverable errors have been detected. +.IP "1" 4 +Recoverable errors have been detected or \fBdosfsck\fR has discovered an internal inconsistency. +.IP "2" 4 +Usage error. \fBdosfsck\fR did not access the filesystem. + +.SH FILES +.IP "fsck0000.rec, fsck0001.rec, ..." 4 +When recovering from a corrupted filesystem, \fBdosfsck\fR dumps recovered data into files named 'fsckNNNN.rec' in the top level directory of the filesystem. + +.SH BUGS +Does not create . and .. files where necessary. Does not remove entirely empty directories. Should give more diagnostic messages. Undeleting files should use a more sophisticated algorithm. + +.SH SEE ALSO +\fBdosfslabel\fR(8) +.br +\fBmkdosfs\fR(8) + +.SH HOMEPAGE +More information about \fBdosfsck\fR and \fBdosfstools\fR can be found at <\fIhttp://www.daniel\-baumann.ch/software/dosfstools/\fR>. + +.SH AUTHORS +\fBdosfstools\fR were written by Werner Almesberger <\fIwerner.almesberger@lrc.di.epfl.ch\fR>, Roman Hodek <\fIRoman.Hodek@informatik.uni-erlangen.de\fR>, and others. The current maintainer is Daniel Baumann <\fIdaniel@debian.org\fR>. diff --git a/dosfstools/man/dosfslabel.8 b/dosfstools/man/dosfslabel.8 new file mode 100644 index 000000000..293ff5de9 --- /dev/null +++ b/dosfstools/man/dosfslabel.8 @@ -0,0 +1,29 @@ +.TH DOSFSLABEL 8 "2010\-01\-31" "3.0.9" "set or get MS\-DOS filesystem label" + +.SH NAME +\fBdosfslabel\fR \- set or get MS\-DOS filesystem label + +.SH SYNOPSIS +\fBdosfslabel\fR \fIDEVICE\fR [\fILABEL\fR] + +.SH DESCRIPTION +\fBdosfslabel\fR set or gets a MS\-DOS filesystem label from a given device. +.PP +If the label is omitted, then the label name of the specified device is written on the standard output. A label can't be longer than 11 characters. + +.SH OPTIONS +.IP "\fB\-h\fR, \fB\-\-help\fR" 4 +Displays a help message. +.IP "\fB\-V\fR, \fB\-\-version\fR" 4 +Shows version. + +.SH SEE ALSO +\fBdosfsck\fR(8) +.br +\fBmkdosfs\fR(8) + +.SH HOMEPAGE +More information about \fBdosfsck\fR and \fBdosfstools\fR can be found at <\fIhttp://www.daniel\-baumann.ch/software/dosfstools/\fR>. + +.SH AUTHORS +\fBdosfstools\fR were written by Werner Almesberger <\fIwerner.almesberger@lrc.di.epfl.ch\fR>, Roman Hodek <\fIRoman.Hodek@informatik.uni-erlangen.de\fR>, and others. The current maintainer is Daniel Baumann <\fIdaniel@debian.org\fR>. diff --git a/dosfstools/man/mkdosfs.8 b/dosfstools/man/mkdosfs.8 new file mode 100644 index 000000000..9100c39b4 --- /dev/null +++ b/dosfstools/man/mkdosfs.8 @@ -0,0 +1,224 @@ +.\" -*- nroff -*- +.TH MKDOSFS 8 "5 May 1995" "Version 2.x" +.SH NAME +.B mkdosfs +\- create an MS-DOS file system under Linux +.SH SYNOPSIS +.B mkdosfs|mkfs.msdos|mkfs.vfat +[ +.B \-a +] +[ +.B \-A +] +[ +.B \-b +.I sector-of-backup +] +[ +.B \-c +] +[ +.B \-l +.I filename +] +[ +.B \-C +] +[ +.B \-f +.I number-of-FATs +] +[ +.B \-F +.I FAT-size +] +[ +.B \-h +.I number-of-hidden-sectors +] +[ +.B \-i +.I volume-id +] +.RB [ " \-I " ] +[ +.B \-m +.I message-file +] +[ +.B \-n +.I volume-name +] +[ +.B \-r +.I root-dir-entries +] +[ +.B \-R +.I number-of-reserved-sectors +] +[ +.B \-s +.I sectors-per-cluster +] +[ +.B \-S +.I logical-sector-size +] +[ +.B \-v +] +.I device +[ +.I block-count +] +.SH DESCRIPTION +.B mkdosfs +is used to create an MS-DOS file system under Linux on a device (usually +a disk partition). +.I device +is the special file corresponding to the device (e.g /dev/hdXX). +.I block-count +is the number of blocks on the device. If omitted, +.B mkdosfs +automatically determines the file system size. +.SH OPTIONS +.TP +.B \-a +Normally, for any filesystem except very small ones, \fBmkdosfs\fP +will align all the data structures to cluster size, to make sure that +as long as the partition is properly aligned, so will all the data +structures in the filesystem. This option disables alignment; this +may provide a handful of additional clusters of storage at the expense +of a significant performance degradation on RAIDs, flash media or +large-sector hard disks. +.TP +.B \-A +Use Atari variation of the MS-DOS file system. This is default if +\fBmkdosfs\fP is run on an Atari, then this option turns off Atari +format. There are some differences when using Atari format: If not +directed otherwise by the user, \fBmkdosfs\fP will always use 2 +sectors per cluster, since GEMDOS doesn't like other values very much. +It will also obey the maximum number of sectors GEMDOS can handle. +Larger file systems are managed by raising the logical sector size. +Under Atari format, an Atari-compatible serial number for the +file system is generated, and a 12 bit FAT is used only for file systems +that have one of the usual floppy sizes (720k, 1.2M, 1.44M, 2.88M), a +16 bit FAT otherwise. This can be overridden with the \fB\-F\fP +option. Some PC-specific boot sector fields aren't written, and a boot +message (option \fB\-m\fP) is ignored. +.TP +.BI \-b " sector-of-backup " +Selects the location of the backup boot sector for FAT32. Default +depends on number of reserved sectors, but usually is sector 6. The +backup must be within the range of reserved sectors. +.TP +.B \-c +Check the device for bad blocks before creating the file system. +.TP +.B \-C +Create the file given as \fIdevice\fP on the command line, and write +the to-be-created file system to it. This can be used to create the +new file system in a file instead of on a real device, and to avoid +using \fBdd\fP in advance to create a file of appropriate size. With +this option, the \fIblock-count\fP must be given, because otherwise +the intended size of the file system wouldn't be known. The file +created is a sparse file, which actually only contains the meta-data +areas (boot sector, FATs, and root directory). The data portions won't +be stored on the disk, but the file nevertheless will have the +correct size. The resulting file can be copied later to a floppy disk +or other device, or mounted through a loop device. +.TP +.BI \-f " number-of-FATs" +Specify the number of file allocation tables in the file system. The +default is 2. Currently the Linux MS-DOS file system does not support +more than 2 FATs. +.TP +.BI \-F " FAT-size" +Specifies the type of file allocation tables used (12, 16 or 32 bit). +If nothing is specified, \fBmkdosfs\fR will automatically select +between 12, 16 and 32 bit, whatever fits better for the file system size. +.TP +.BI \-h " number-of-hidden-sectors " +Select the number of hidden sectors in the volume. Apparently some +digital cameras get indigestion if you feed them a CF card without +such hidden sectors, this option allows you to satisfy them. Assumes +\'0\' if no value is given on the command line. +.TP +.I \-i " volume-id" +Sets the volume ID of the newly created file system; +.I volume-id +is a 32-bit hexadecimal number (for example, 2e24ec82). The default +is a number which depends on the file system creation time. +.TP +.B \-I +It is typical for fixed disk devices to be partitioned so, by default, you are +not permitted to create a filesystem across the entire device. +.B mkdosfs +will complain and tell you that it refuses to work. This is different +when using MO disks. One doesn't always need partitions on MO disks. +The file system can go directly to the whole disk. Under other OSes +this is known as the 'superfloppy' format. + +This switch will force +.B mkdosfs +to work properly. +.TP +.BI \-l " filename" +Read the bad blocks list from +.IR filename . +.TP +.BI \-m " message-file" +Sets the message the user receives on attempts to boot this file system +without having properly installed an operating system. The message +file must not exceed 418 bytes once line feeds have been converted to +carriage return-line feed combinations, and tabs have been expanded. +If the filename is a hyphen (-), the text is taken from standard input. +.TP +.BI \-n " volume-name" +Sets the volume name (label) of the file system. The volume name can +be up to 11 characters long. The default is no label. +.TP +.BI \-r " root-dir-entries" +Select the number of entries available in the root directory. The +default is 112 or 224 for floppies and 512 for hard disks. +.TP +.BI \-R " number-of-reserved-sectors " +Select the number of reserved sectors. With FAT32 format at least 2 +reserved sectors are needed, the default is 32. Otherwise the default +is 1 (only the boot sector). +.TP +.BI \-s " sectors-per-cluster" +Specify the number of disk sectors per cluster. Must be a power of 2, +i.e. 1, 2, 4, 8, ... 128. +.TP +.BI \-S " logical-sector-size" +Specify the number of bytes per logical sector. Must be a power of 2 +and greater than or equal to 512, i.e. 512, 1024, 2048, 4096, 8192, +16384, or 32768. +.TP +.B \-v +Verbose execution. +.SH BUGS +.B mkdosfs +can not create boot-able file systems. This isn't as easy as you might +think at first glance for various reasons and has been discussed a lot +already. +.B mkdosfs +simply will not support it ;) +.SH AUTHOR +Dave Hudson - ; modified by Peter Anvin +. Fixes and additions by Roman Hodek + for Debian/GNU Linux. +.SH ACKNOWLEDGMENTS +.B mkdosfs +is based on code from +.BR mke2fs +(written by Remy Card - ) which is itself based on +.BR mkfs +(written by Linus Torvalds - ). +.SH SEE ALSO +.BR dosfsck (8), +.BR dosfslabel (8), +.BR mkfs (8) diff --git a/dosfstools/src/boot.c b/dosfstools/src/boot.c new file mode 100644 index 000000000..bbaee0471 --- /dev/null +++ b/dosfstools/src/boot.c @@ -0,0 +1,518 @@ +/* boot.c - Read and analyze ia PC/MS-DOS boot sector + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#include +#include +#include +#include +#include + +#include "common.h" +#include "dosfsck.h" +#include "fat.h" +#include "io.h" +#include "boot.h" + +#define ROUND_TO_MULTIPLE(n,m) ((n) && (m) ? (n)+(m)-1-((n)-1)%(m) : 0) + /* don't divide by zero */ + +/* cut-over cluster counts for FAT12 and FAT16 */ +#define FAT12_THRESHOLD 4085 +#define FAT16_THRESHOLD 65525 + +static struct { + __u8 media; + char *descr; +} mediabytes[] = { + { + 0xf0, "5.25\" or 3.5\" HD floppy"}, { + 0xf8, "hard disk"}, { + 0xf9, "3,5\" 720k floppy 2s/80tr/9sec or " + "5.25\" 1.2M floppy 2s/80tr/15sec"}, { + 0xfa, "5.25\" 320k floppy 1s/80tr/8sec"}, { + 0xfb, "3.5\" 640k floppy 2s/80tr/8sec"}, { + 0xfc, "5.25\" 180k floppy 1s/40tr/9sec"}, { + 0xfd, "5.25\" 360k floppy 2s/40tr/9sec"}, { + 0xfe, "5.25\" 160k floppy 1s/40tr/8sec"}, { +0xff, "5.25\" 320k floppy 2s/40tr/8sec"},}; + +#if defined __alpha || defined __arm || defined __arm__ || defined __ia64__ || defined __x86_64__ \ + || defined __ppc64__ || defined __bfin__ || defined __MICROBLAZE__ +/* Unaligned fields must first be copied byte-wise */ +#define GET_UNALIGNED_W(f) \ + ({ \ + unsigned short __v; \ + memcpy( &__v, &f, sizeof(__v) ); \ + CF_LE_W( *(unsigned short *)&__v ); \ + }) +#else +#define GET_UNALIGNED_W(f) CF_LE_W( *(unsigned short *)&f ) +#endif + +static char *get_media_descr(unsigned char media) +{ + int i; + + for (i = 0; i < sizeof(mediabytes) / sizeof(*mediabytes); ++i) { + if (mediabytes[i].media == media) + return (mediabytes[i].descr); + } + return ("undefined"); +} + +static void dump_boot(DOS_FS * fs, struct boot_sector *b, unsigned lss) +{ + unsigned short sectors; + + printf("Boot sector contents:\n"); + if (!atari_format) { + char id[9]; + strncpy(id, (const char *)b->system_id, 8); + id[8] = 0; + printf("System ID \"%s\"\n", id); + } else { + /* On Atari, a 24 bit serial number is stored at offset 8 of the boot + * sector */ + printf("Serial number 0x%x\n", + b->system_id[5] | (b-> + system_id[6] << 8) | (b->system_id[7] << 16)); + } + printf("Media byte 0x%02x (%s)\n", b->media, get_media_descr(b->media)); + printf("%10d bytes per logical sector\n", GET_UNALIGNED_W(b->sector_size)); + printf("%10d bytes per cluster\n", fs->cluster_size); + printf("%10d reserved sector%s\n", CF_LE_W(b->reserved), + CF_LE_W(b->reserved) == 1 ? "" : "s"); + printf("First FAT starts at byte %llu (sector %llu)\n", + (unsigned long long)fs->fat_start, + (unsigned long long)fs->fat_start / lss); + printf("%10d FATs, %d bit entries\n", b->fats, fs->fat_bits); + printf("%10d bytes per FAT (= %u sectors)\n", fs->fat_size, + fs->fat_size / lss); + if (!fs->root_cluster) { + printf("Root directory starts at byte %llu (sector %llu)\n", + (unsigned long long)fs->root_start, + (unsigned long long)fs->root_start / lss); + printf("%10d root directory entries\n", fs->root_entries); + } else { + printf("Root directory start at cluster %lu (arbitrary size)\n", + fs->root_cluster); + } + printf("Data area starts at byte %llu (sector %llu)\n", + (unsigned long long)fs->data_start, + (unsigned long long)fs->data_start / lss); + printf("%10lu data clusters (%llu bytes)\n", fs->clusters, + (unsigned long long)fs->clusters * fs->cluster_size); + printf("%u sectors/track, %u heads\n", CF_LE_W(b->secs_track), + CF_LE_W(b->heads)); + printf("%10u hidden sectors\n", atari_format ? + /* On Atari, the hidden field is only 16 bit wide and unused */ + (((unsigned char *)&b->hidden)[0] | + ((unsigned char *)&b->hidden)[1] << 8) : CF_LE_L(b->hidden)); + sectors = GET_UNALIGNED_W(b->sectors); + printf("%10u sectors total\n", sectors ? sectors : CF_LE_L(b->total_sect)); +} + +static void check_backup_boot(DOS_FS * fs, struct boot_sector *b, int lss) +{ + struct boot_sector b2; + + if (!fs->backupboot_start) { + printf("There is no backup boot sector.\n"); + if (CF_LE_W(b->reserved) < 3) { + printf("And there is no space for creating one!\n"); + return; + } + if (interactive) + printf("1) Create one\n2) Do without a backup\n"); + else + printf(" Auto-creating backup boot block.\n"); + if (!interactive || get_key("12", "?") == '1') { + int bbs; + /* The usual place for the backup boot sector is sector 6. Choose + * that or the last reserved sector. */ + if (CF_LE_W(b->reserved) >= 7 && CF_LE_W(b->info_sector) != 6) + bbs = 6; + else { + bbs = CF_LE_W(b->reserved) - 1; + if (bbs == CF_LE_W(b->info_sector)) + --bbs; /* this is never 0, as we checked reserved >= 3! */ + } + fs->backupboot_start = bbs * lss; + b->backup_boot = CT_LE_W(bbs); + fs_write(fs->backupboot_start, sizeof(*b), b); + fs_write((loff_t) offsetof(struct boot_sector, backup_boot), + sizeof(b->backup_boot), &b->backup_boot); + printf("Created backup of boot sector in sector %d\n", bbs); + return; + } else + return; + } + + fs_read(fs->backupboot_start, sizeof(b2), &b2); + if (memcmp(b, &b2, sizeof(b2)) != 0) { + /* there are any differences */ + __u8 *p, *q; + int i, pos, first = 1; + char buf[20]; + + printf("There are differences between boot sector and its backup.\n"); + printf("Differences: (offset:original/backup)\n "); + pos = 2; + for (p = (__u8 *) b, q = (__u8 *) & b2, i = 0; i < sizeof(b2); + ++p, ++q, ++i) { + if (*p != *q) { + sprintf(buf, "%s%u:%02x/%02x", first ? "" : ", ", + (unsigned)(p - (__u8 *) b), *p, *q); + if (pos + strlen(buf) > 78) + printf("\n "), pos = 2; + printf("%s", buf); + pos += strlen(buf); + first = 0; + } + } + printf("\n"); + + if (interactive) + printf("1) Copy original to backup\n" + "2) Copy backup to original\n" "3) No action\n"); + else + printf(" Not automatically fixing this.\n"); + switch (interactive ? get_key("123", "?") : '3') { + case '1': + fs_write(fs->backupboot_start, sizeof(*b), b); + break; + case '2': + fs_write(0, sizeof(b2), &b2); + break; + default: + break; + } + } +} + +static void init_fsinfo(struct info_sector *i) +{ + i->magic = CT_LE_L(0x41615252); + i->signature = CT_LE_L(0x61417272); + i->free_clusters = CT_LE_L(-1); + i->next_cluster = CT_LE_L(2); + i->boot_sign = CT_LE_W(0xaa55); +} + +static void read_fsinfo(DOS_FS * fs, struct boot_sector *b, int lss) +{ + struct info_sector i; + + if (!b->info_sector) { + printf("No FSINFO sector\n"); + if (interactive) + printf("1) Create one\n2) Do without FSINFO\n"); + else + printf(" Not automatically creating it.\n"); + if (interactive && get_key("12", "?") == '1') { + /* search for a free reserved sector (not boot sector and not + * backup boot sector) */ + __u32 s; + for (s = 1; s < CF_LE_W(b->reserved); ++s) + if (s != CF_LE_W(b->backup_boot)) + break; + if (s > 0 && s < CF_LE_W(b->reserved)) { + init_fsinfo(&i); + fs_write((loff_t) s * lss, sizeof(i), &i); + b->info_sector = CT_LE_W(s); + fs_write((loff_t) offsetof(struct boot_sector, info_sector), + sizeof(b->info_sector), &b->info_sector); + if (fs->backupboot_start) + fs_write(fs->backupboot_start + + offsetof(struct boot_sector, info_sector), + sizeof(b->info_sector), &b->info_sector); + } else { + printf("No free reserved sector found -- " + "no space for FSINFO sector!\n"); + return; + } + } else + return; + } + + fs->fsinfo_start = CF_LE_W(b->info_sector) * lss; + fs_read(fs->fsinfo_start, sizeof(i), &i); + + if (i.magic != CT_LE_L(0x41615252) || + i.signature != CT_LE_L(0x61417272) || i.boot_sign != CT_LE_W(0xaa55)) { + printf("FSINFO sector has bad magic number(s):\n"); + if (i.magic != CT_LE_L(0x41615252)) + printf(" Offset %llu: 0x%08x != expected 0x%08x\n", + (unsigned long long)offsetof(struct info_sector, magic), + CF_LE_L(i.magic), 0x41615252); + if (i.signature != CT_LE_L(0x61417272)) + printf(" Offset %llu: 0x%08x != expected 0x%08x\n", + (unsigned long long)offsetof(struct info_sector, signature), + CF_LE_L(i.signature), 0x61417272); + if (i.boot_sign != CT_LE_W(0xaa55)) + printf(" Offset %llu: 0x%04x != expected 0x%04x\n", + (unsigned long long)offsetof(struct info_sector, boot_sign), + CF_LE_W(i.boot_sign), 0xaa55); + if (interactive) + printf("1) Correct\n2) Don't correct (FSINFO invalid then)\n"); + else + printf(" Auto-correcting it.\n"); + if (!interactive || get_key("12", "?") == '1') { + init_fsinfo(&i); + fs_write(fs->fsinfo_start, sizeof(i), &i); + } else + fs->fsinfo_start = 0; + } + + if (fs->fsinfo_start) + fs->free_clusters = CF_LE_L(i.free_clusters); +} + +void read_boot(DOS_FS * fs) +{ + struct boot_sector b; + unsigned total_sectors; + unsigned short logical_sector_size, sectors; + unsigned fat_length; + loff_t data_size; + + fs_read(0, sizeof(b), &b); + logical_sector_size = GET_UNALIGNED_W(b.sector_size); + if (!logical_sector_size) + die("Logical sector size is zero."); + + /* This was moved up because it's the first thing that will fail */ + /* if the platform needs special handling of unaligned multibyte accesses */ + /* but such handling isn't being provided. See GET_UNALIGNED_W() above. */ + if (logical_sector_size & (SECTOR_SIZE - 1)) + die("Logical sector size (%d bytes) is not a multiple of the physical " + "sector size.", logical_sector_size); + + fs->cluster_size = b.cluster_size * logical_sector_size; + if (!fs->cluster_size) + die("Cluster size is zero."); + if (b.fats != 2 && b.fats != 1) + die("Currently, only 1 or 2 FATs are supported, not %d.\n", b.fats); + fs->nfats = b.fats; + sectors = GET_UNALIGNED_W(b.sectors); + total_sectors = sectors ? sectors : CF_LE_L(b.total_sect); + if (verbose) + printf("Checking we can access the last sector of the filesystem\n"); + /* Can't access last odd sector anyway, so round down */ + fs_test((loff_t) ((total_sectors & ~1) - 1) * (loff_t) logical_sector_size, + logical_sector_size); + fat_length = CF_LE_W(b.fat_length) ? + CF_LE_W(b.fat_length) : CF_LE_L(b.fat32_length); + fs->fat_start = (loff_t) CF_LE_W(b.reserved) * logical_sector_size; + fs->root_start = ((loff_t) CF_LE_W(b.reserved) + b.fats * fat_length) * + logical_sector_size; + fs->root_entries = GET_UNALIGNED_W(b.dir_entries); + fs->data_start = fs->root_start + ROUND_TO_MULTIPLE(fs->root_entries << + MSDOS_DIR_BITS, + logical_sector_size); + data_size = (loff_t) total_sectors *logical_sector_size - fs->data_start; + fs->clusters = data_size / fs->cluster_size; + fs->root_cluster = 0; /* indicates standard, pre-FAT32 root dir */ + fs->fsinfo_start = 0; /* no FSINFO structure */ + fs->free_clusters = -1; /* unknown */ + if (!b.fat_length && b.fat32_length) { + fs->fat_bits = 32; + fs->root_cluster = CF_LE_L(b.root_cluster); + if (!fs->root_cluster && fs->root_entries) + /* M$ hasn't specified this, but it looks reasonable: If + * root_cluster is 0 but there is a separate root dir + * (root_entries != 0), we handle the root dir the old way. Give a + * warning, but convertig to a root dir in a cluster chain seems + * to complex for now... */ + printf("Warning: FAT32 root dir not in cluster chain! " + "Compatibility mode...\n"); + else if (!fs->root_cluster && !fs->root_entries) + die("No root directory!"); + else if (fs->root_cluster && fs->root_entries) + printf("Warning: FAT32 root dir is in a cluster chain, but " + "a separate root dir\n" + " area is defined. Cannot fix this easily.\n"); + if (fs->clusters < FAT16_THRESHOLD) + printf("Warning: Filesystem is FAT32 according to fat_length " + "and fat32_length fields,\n" + " but has only %lu clusters, less than the required " + "minimum of %d.\n" + " This may lead to problems on some systems.\n", + fs->clusters, FAT16_THRESHOLD); + + fs->backupboot_start = CF_LE_W(b.backup_boot) * logical_sector_size; + check_backup_boot(fs, &b, logical_sector_size); + + read_fsinfo(fs, &b, logical_sector_size); + } else if (!atari_format) { + /* On real MS-DOS, a 16 bit FAT is used whenever there would be too + * much clusers otherwise. */ + fs->fat_bits = (fs->clusters >= FAT12_THRESHOLD) ? 16 : 12; + if (fs->clusters >= FAT16_THRESHOLD) + die("Too many clusters (%lu) for FAT16 filesystem.", fs->clusters); + } else { + /* On Atari, things are more difficult: GEMDOS always uses 12bit FATs + * on floppies, and always 16 bit on harddisks. */ + fs->fat_bits = 16; /* assume 16 bit FAT for now */ + /* If more clusters than fat entries in 16-bit fat, we assume + * it's a real MSDOS FS with 12-bit fat. */ + if (fs->clusters + 2 > fat_length * logical_sector_size * 8 / 16 || + /* if it's a floppy disk --> 12bit fat */ + device_no == 2 || + /* if it's a ramdisk or loopback device and has one of the usual + * floppy sizes -> 12bit FAT */ + ((device_no == 1 || device_no == 7) && + (total_sectors == 720 || total_sectors == 1440 || + total_sectors == 2880))) + fs->fat_bits = 12; + } + /* On FAT32, the high 4 bits of a FAT entry are reserved */ + fs->eff_fat_bits = (fs->fat_bits == 32) ? 28 : fs->fat_bits; + fs->fat_size = fat_length * logical_sector_size; + + fs->label = calloc(12, sizeof(__u8)); + if (fs->fat_bits == 12 || fs->fat_bits == 16) { + struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b; + if (b16->extended_sig == 0x29) + memmove(fs->label, b16->label, 11); + else + fs->label = NULL; + } else if (fs->fat_bits == 32) { + if (b.extended_sig == 0x29) + memmove(fs->label, &b.label, 11); + else + fs->label = NULL; + } + + if (fs->clusters > + ((unsigned long long)fs->fat_size * 8 / fs->fat_bits) - 2) + die("File system has %d clusters but only space for %d FAT entries.", + fs->clusters, + ((unsigned long long)fs->fat_size * 8 / fs->fat_bits) - 2); + if (!fs->root_entries && !fs->root_cluster) + die("Root directory has zero size."); + if (fs->root_entries & (MSDOS_DPS - 1)) + die("Root directory (%d entries) doesn't span an integral number of " + "sectors.", fs->root_entries); + if (logical_sector_size & (SECTOR_SIZE - 1)) + die("Logical sector size (%d bytes) is not a multiple of the physical " + "sector size.", logical_sector_size); +#if 0 /* linux kernel doesn't check that either */ + /* ++roman: On Atari, these two fields are often left uninitialized */ + if (!atari_format && (!b.secs_track || !b.heads)) + die("Invalid disk format in boot sector."); +#endif + if (verbose) + dump_boot(fs, &b, logical_sector_size); +} + +static void write_boot_label(DOS_FS * fs, char *label) +{ + struct boot_sector b; + struct boot_sector_16 *b16 = (struct boot_sector_16 *)&b; + + fs_read(0, sizeof(b), &b); + if (fs->fat_bits == 12 || fs->fat_bits == 16) { + if (b16->extended_sig != 0x29) { + b16->extended_sig = 0x29; + b16->serial = 0; + memmove(b16->fs_type, fs->fat_bits == 12 ? "FAT12 " : "FAT16 ", + 8); + } + memmove(b16->label, label, 11); + } else if (fs->fat_bits == 32) { + if (b.extended_sig != 0x29) { + b.extended_sig = 0x29; + b.serial = 0; + memmove(b.fs_type, "FAT32 ", 8); + } + memmove(b.label, label, 11); + } + fs_write(0, sizeof(b), &b); + if (fs->fat_bits == 32 && fs->backupboot_start) + fs_write(fs->backupboot_start, sizeof(b), &b); +} + +static loff_t find_volume_de(DOS_FS * fs, DIR_ENT * de) +{ + unsigned long cluster; + loff_t offset; + int i; + + if (fs->root_cluster) { + for (cluster = fs->root_cluster; + cluster != 0 && cluster != -1; + cluster = next_cluster(fs, cluster)) { + offset = cluster_start(fs, cluster); + for (i = 0; i * sizeof(DIR_ENT) < fs->cluster_size; i++) { + fs_read(offset, sizeof(DIR_ENT), de); + if (de->attr & ATTR_VOLUME) + return offset; + offset += sizeof(DIR_ENT); + } + } + } else { + for (i = 0; i < fs->root_entries; i++) { + offset = fs->root_start + i * sizeof(DIR_ENT); + fs_read(offset, sizeof(DIR_ENT), de); + if (de->attr & ATTR_VOLUME) + return offset; + } + } + + return 0; +} + +static void write_volume_label(DOS_FS * fs, char *label) +{ + time_t now = time(NULL); + struct tm *mtime = localtime(&now); + loff_t offset; + DIR_ENT de; + + offset = find_volume_de(fs, &de); + if (offset == 0) + return; + + memcpy(de.name, label, 11); + de.time = CT_LE_W((unsigned short)((mtime->tm_sec >> 1) + + (mtime->tm_min << 5) + + (mtime->tm_hour << 11))); + de.date = CT_LE_W((unsigned short)(mtime->tm_mday + + ((mtime->tm_mon + 1) << 5) + + ((mtime->tm_year - 80) << 9))); + fs_write(offset, sizeof(DIR_ENT), &de); +} + +void write_label(DOS_FS * fs, char *label) +{ + int l = strlen(label); + + while (l < 11) + label[l++] = ' '; + + write_boot_label(fs, label); + write_volume_label(fs, label); +} diff --git a/dosfstools/src/boot.h b/dosfstools/src/boot.h new file mode 100644 index 000000000..c9edfa337 --- /dev/null +++ b/dosfstools/src/boot.h @@ -0,0 +1,30 @@ +/* boot.h - Read and analyze ia PC/MS-DOS boot sector + + Copyright (C) 1993 Werner Almesberger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#ifndef _BOOT_H +#define _BOOT_H + +void read_boot(DOS_FS * fs); +void write_label(DOS_FS * fs, char *label); + +/* Reads the boot sector from the currently open device and initializes *FS */ + +#endif diff --git a/dosfstools/src/check.c b/dosfstools/src/check.c new file mode 100644 index 000000000..3f175b019 --- /dev/null +++ b/dosfstools/src/check.c @@ -0,0 +1,1051 @@ +/* check.c - Check and repair a PC/MS-DOS file system + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#include +#include +#include +#include +#include + +#include "common.h" +#include "dosfsck.h" +#include "io.h" +#include "fat.h" +#include "file.h" +#include "lfn.h" +#include "check.h" + +static DOS_FILE *root; + +/* get start field of a dir entry */ +#define FSTART(p,fs) \ + ((unsigned long)CF_LE_W(p->dir_ent.start) | \ + (fs->fat_bits == 32 ? CF_LE_W(p->dir_ent.starthi) << 16 : 0)) + +#define MODIFY(p,i,v) \ + do { \ + if (p->offset) { \ + p->dir_ent.i = v; \ + fs_write(p->offset+offsetof(DIR_ENT,i), \ + sizeof(p->dir_ent.i),&p->dir_ent.i); \ + } \ + } while(0) + +#define MODIFY_START(p,v,fs) \ + do { \ + unsigned long __v = (v); \ + if (!p->offset) { \ + /* writing to fake entry for FAT32 root dir */ \ + if (!__v) die("Oops, deleting FAT32 root dir!"); \ + fs->root_cluster = __v; \ + p->dir_ent.start = CT_LE_W(__v&0xffff); \ + p->dir_ent.starthi = CT_LE_W(__v>>16); \ + __v = CT_LE_L(__v); \ + fs_write((loff_t)offsetof(struct boot_sector,root_cluster), \ + sizeof(((struct boot_sector *)0)->root_cluster), \ + &__v); \ + } \ + else { \ + MODIFY(p,start,CT_LE_W((__v)&0xffff)); \ + if (fs->fat_bits == 32) \ + MODIFY(p,starthi,CT_LE_W((__v)>>16)); \ + } \ + } while(0) + +loff_t alloc_rootdir_entry(DOS_FS * fs, DIR_ENT * de, const char *pattern) +{ + static int curr_num = 0; + loff_t offset; + + if (fs->root_cluster) { + DIR_ENT d2; + int i = 0, got = 0; + unsigned long clu_num, prev = 0; + loff_t offset2; + + clu_num = fs->root_cluster; + offset = cluster_start(fs, clu_num); + while (clu_num > 0 && clu_num != -1) { + fs_read(offset, sizeof(DIR_ENT), &d2); + if (IS_FREE(d2.name) && d2.attr != VFAT_LN_ATTR) { + got = 1; + break; + } + i += sizeof(DIR_ENT); + offset += sizeof(DIR_ENT); + if ((i % fs->cluster_size) == 0) { + prev = clu_num; + if ((clu_num = next_cluster(fs, clu_num)) == 0 || clu_num == -1) + break; + offset = cluster_start(fs, clu_num); + } + } + if (!got) { + /* no free slot, need to extend root dir: alloc next free cluster + * after previous one */ + if (!prev) + die("Root directory has no cluster allocated!"); + for (clu_num = prev + 1; clu_num != prev; clu_num++) { + FAT_ENTRY entry; + + if (clu_num >= fs->clusters + 2) + clu_num = 2; + get_fat(&entry, fs->fat, clu_num, fs); + if (!entry.value) + break; + } + if (clu_num == prev) + die("Root directory full and no free cluster"); + set_fat(fs, prev, clu_num); + set_fat(fs, clu_num, -1); + set_owner(fs, clu_num, get_owner(fs, fs->root_cluster)); + /* clear new cluster */ + memset(&d2, 0, sizeof(d2)); + offset = cluster_start(fs, clu_num); + for (i = 0; i < fs->cluster_size; i += sizeof(DIR_ENT)) + fs_write(offset + i, sizeof(d2), &d2); + } + memset(de, 0, sizeof(DIR_ENT)); + while (1) { + char expanded[12]; + sprintf(expanded, pattern, curr_num); + memcpy(de->name + 4, expanded, 4); + memcpy(de->ext, expanded + 4, 3); + clu_num = fs->root_cluster; + i = 0; + offset2 = cluster_start(fs, clu_num); + while (clu_num > 0 && clu_num != -1) { + fs_read(offset2, sizeof(DIR_ENT), &d2); + if (offset2 != offset && + !strncmp((const char *)d2.name, (const char *)de->name, + MSDOS_NAME)) + break; + i += sizeof(DIR_ENT); + offset2 += sizeof(DIR_ENT); + if ((i % fs->cluster_size) == 0) { + if ((clu_num = next_cluster(fs, clu_num)) == 0 || + clu_num == -1) + break; + offset2 = cluster_start(fs, clu_num); + } + } + if (clu_num == 0 || clu_num == -1) + break; + if (++curr_num >= 10000) + die("Unable to create unique name"); + } + } else { + DIR_ENT *root; + int next_free = 0, scan; + + root = alloc(fs->root_entries * sizeof(DIR_ENT)); + fs_read(fs->root_start, fs->root_entries * sizeof(DIR_ENT), root); + + while (next_free < fs->root_entries) + if (IS_FREE(root[next_free].name) && + root[next_free].attr != VFAT_LN_ATTR) + break; + else + next_free++; + if (next_free == fs->root_entries) + die("Root directory is full."); + offset = fs->root_start + next_free * sizeof(DIR_ENT); + memset(de, 0, sizeof(DIR_ENT)); + while (1) { + sprintf((char *)de->name, pattern, curr_num); + for (scan = 0; scan < fs->root_entries; scan++) + if (scan != next_free && + !strncmp((const char *)root[scan].name, + (const char *)de->name, MSDOS_NAME)) + break; + if (scan == fs->root_entries) + break; + if (++curr_num >= 10000) + die("Unable to create unique name"); + } + free(root); + } + ++n_files; + return offset; +} + +/** + * Construct a full path (starting with '/') for the specified dentry, + * relative to the partition. All components are "long" names where possible. + * + * @param[in] file Information about dentry (file or directory) of interest + * + * return Pointer to static string containing file's full path + */ +static char *path_name(DOS_FILE * file) +{ + static char path[PATH_MAX * 2]; + + if (!file) + *path = 0; /* Reached the root directory */ + else { + if (strlen(path_name(file->parent)) > PATH_MAX) + die("Path name too long."); + if (strcmp(path, "/") != 0) + strcat(path, "/"); + + /* Append the long name to the path, + * or the short name if there isn't a long one + */ + strcpy(strrchr(path, 0), + file->lfn ? file->lfn : file_name(file->dir_ent.name)); + } + return path; +} + +static int day_n[] = + { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 0, 0, 0 }; + /* JanFebMarApr May Jun Jul Aug Sep Oct Nov Dec */ + +/* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */ + +time_t date_dos2unix(unsigned short time, unsigned short date) +{ + int month, year; + time_t secs; + + month = ((date >> 5) & 15) - 1; + year = date >> 9; + secs = + (time & 31) * 2 + 60 * ((time >> 5) & 63) + (time >> 11) * 3600 + + 86400 * ((date & 31) - 1 + day_n[month] + (year / 4) + year * 365 - + ((year & 3) == 0 && month < 2 ? 1 : 0) + 3653); + /* days since 1.1.70 plus 80's leap day */ + return secs; +} + +static char *file_stat(DOS_FILE * file) +{ + static char temp[100]; + struct tm *tm; + char tmp[100]; + time_t date; + + date = + date_dos2unix(CF_LE_W(file->dir_ent.time), CF_LE_W(file->dir_ent.date)); + tm = localtime(&date); + strftime(tmp, 99, "%H:%M:%S %b %d %Y", tm); + sprintf(temp, " Size %u bytes, date %s", CF_LE_L(file->dir_ent.size), tmp); + return temp; +} + +static int bad_name(DOS_FILE * file) +{ + int i, spc, suspicious = 0; + char *bad_chars = atari_format ? "*?\\/:" : "*?<>|\"\\/:"; + unsigned char *name = file->dir_ent.name; + + /* Do not complain about (and auto-correct) the extended attribute files + * of OS/2. */ + if (strncmp((const char *)name, "EA DATA SF", 11) == 0 || + strncmp((const char *)name, "WP ROOT SF", 11) == 0) + return 0; + + /* don't complain about the dummy 11 bytes used by patched Linux + kernels */ + if (file->dir_ent.lcase & FAT_NO_83NAME) + return 0; + + for (i = 0; i < 8; i++) { + if (name[i] < ' ' || name[i] == 0x7f) + return 1; + if (name[i] > 0x7f) + ++suspicious; + if (strchr(bad_chars, name[i])) + return 1; + } + + for (i = 8; i < 11; i++) { + if (name[i] < ' ' || name[i] == 0x7f) + return 1; + if (name[i] > 0x7f) + ++suspicious; + if (strchr(bad_chars, name[i])) + return 1; + } + + spc = 0; + for (i = 0; i < 8; i++) { + if (name[i] == ' ') + spc = 1; + else if (spc) + /* non-space after a space not allowed, space terminates the name + * part */ + return 1; + } + + spc = 0; + for (i = 8; i < 11; i++) { + if (name[i] == ' ') + spc = 1; + else if (spc) + /* non-space after a space not allowed, space terminates the name + * part */ + return 1; + } + + /* Under GEMDOS, chars >= 128 are never allowed. */ + if (atari_format && suspicious) + return 1; + + /* Under MS-DOS and Windows, chars >= 128 in short names are valid + * (but these characters can be visualised differently depending on + * local codepage: CP437, CP866, etc). The chars are all basically ok, + * so we shouldn't auto-correct such names. */ + return 0; +} + +static void lfn_remove(loff_t from, loff_t to) +{ + DIR_ENT empty; + + /* New dir entry is zeroed except first byte, which is set to 0xe5. + * This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading + * a directory at the first zero entry... + */ + memset(&empty, 0, sizeof(empty)); + empty.name[0] = DELETED_FLAG; + + for (; from < to; from += sizeof(empty)) { + fs_write(from, sizeof(DIR_ENT), &empty); + } +} + +static void drop_file(DOS_FS * fs, DOS_FILE * file) +{ + unsigned long cluster; + + MODIFY(file, name[0], DELETED_FLAG); + if (file->lfn) + lfn_remove(file->lfn_offset, file->offset); + for (cluster = FSTART(file, fs); cluster > 0 && cluster < + fs->clusters + 2; cluster = next_cluster(fs, cluster)) + set_owner(fs, cluster, NULL); + --n_files; +} + +static void truncate_file(DOS_FS * fs, DOS_FILE * file, unsigned long clusters) +{ + int deleting; + unsigned long walk, next, prev; + + walk = FSTART(file, fs); + prev = 0; + if ((deleting = !clusters)) + MODIFY_START(file, 0, fs); + while (walk > 0 && walk != -1) { + next = next_cluster(fs, walk); + if (deleting) + set_fat(fs, walk, 0); + else if ((deleting = !--clusters)) + set_fat(fs, walk, -1); + prev = walk; + walk = next; + } +} + +static void auto_rename(DOS_FILE * file) +{ + DOS_FILE *first, *walk; + unsigned long int number; + + if (!file->offset) + return; /* cannot rename FAT32 root dir */ + first = file->parent ? file->parent->first : root; + number = 0; + while (1) { + char num[8]; + sprintf(num, "%07lu", number); + memcpy(file->dir_ent.name, "FSCK", 4); + memcpy(file->dir_ent.name + 4, num, 4); + memcpy(file->dir_ent.ext, num + 4, 3); + for (walk = first; walk; walk = walk->next) + if (walk != file + && !strncmp((const char *)walk->dir_ent.name, + (const char *)file->dir_ent.name, MSDOS_NAME)) + break; + if (!walk) { + fs_write(file->offset, MSDOS_NAME, file->dir_ent.name); + if (file->lfn) + lfn_fix_checksum(file->lfn_offset, file->offset, + (const char *)file->dir_ent.name); + return; + } + number++; + if (number > 9999999) { + die("Too many files need repair."); + } + } + die("Can't generate a unique name."); +} + +static void rename_file(DOS_FILE * file) +{ + unsigned char name[46]; + unsigned char *walk, *here; + + if (!file->offset) { + printf("Cannot rename FAT32 root dir\n"); + return; /* cannot rename FAT32 root dir */ + } + while (1) { + printf("New name: "); + fflush(stdout); + if (fgets((char *)name, 45, stdin)) { + if ((here = (unsigned char *)strchr((const char *)name, '\n'))) + *here = 0; + for (walk = (unsigned char *)strrchr((const char *)name, 0); + walk >= name && (*walk == ' ' || *walk == '\t'); walk--) ; + walk[1] = 0; + for (walk = name; *walk == ' ' || *walk == '\t'; walk++) ; + if (file_cvt(walk, file->dir_ent.name)) { + fs_write(file->offset, MSDOS_NAME, file->dir_ent.name); + if (file->lfn) + lfn_fix_checksum(file->lfn_offset, file->offset, + (const char *)file->dir_ent.name); + return; + } + } + } +} + +static int handle_dot(DOS_FS * fs, DOS_FILE * file, int dots) +{ + char *name; + + name = + strncmp((const char *)file->dir_ent.name, MSDOS_DOT, + MSDOS_NAME) ? ".." : "."; + if (!(file->dir_ent.attr & ATTR_DIR)) { + printf("%s\n Is a non-directory.\n", path_name(file)); + if (interactive) + printf("1) Drop it\n2) Auto-rename\n3) Rename\n" + "4) Convert to directory\n"); + else + printf(" Auto-renaming it.\n"); + switch (interactive ? get_key("1234", "?") : '2') { + case '1': + drop_file(fs, file); + return 1; + case '2': + auto_rename(file); + printf(" Renamed to %s\n", file_name(file->dir_ent.name)); + return 0; + case '3': + rename_file(file); + return 0; + case '4': + MODIFY(file, size, CT_LE_L(0)); + MODIFY(file, attr, file->dir_ent.attr | ATTR_DIR); + break; + } + } + if (!dots) { + printf("Root contains directory \"%s\". Dropping it.\n", name); + drop_file(fs, file); + return 1; + } + return 0; +} + +static int check_file(DOS_FS * fs, DOS_FILE * file) +{ + DOS_FILE *owner; + int restart; + unsigned long expect, curr, this, clusters, prev, walk, clusters2; + + if (file->dir_ent.attr & ATTR_DIR) { + if (CF_LE_L(file->dir_ent.size)) { + printf("%s\n Directory has non-zero size. Fixing it.\n", + path_name(file)); + MODIFY(file, size, CT_LE_L(0)); + } + if (file->parent + && !strncmp((const char *)file->dir_ent.name, MSDOS_DOT, + MSDOS_NAME)) { + expect = FSTART(file->parent, fs); + if (FSTART(file, fs) != expect) { + printf("%s\n Start (%ld) does not point to parent (%ld)\n", + path_name(file), FSTART(file, fs), expect); + MODIFY_START(file, expect, fs); + } + return 0; + } + if (file->parent + && !strncmp((const char *)file->dir_ent.name, MSDOS_DOTDOT, + MSDOS_NAME)) { + expect = + file->parent->parent ? FSTART(file->parent->parent, fs) : 0; + if (fs->root_cluster && expect == fs->root_cluster) + expect = 0; + if (FSTART(file, fs) != expect) { + printf("%s\n Start (%lu) does not point to .. (%lu)\n", + path_name(file), FSTART(file, fs), expect); + MODIFY_START(file, expect, fs); + } + return 0; + } + if (FSTART(file, fs) == 0) { + printf("%s\n Start does point to root directory. Deleting dir. \n", + path_name(file)); + MODIFY(file, name[0], DELETED_FLAG); + return 0; + } + } + if (FSTART(file, fs) >= fs->clusters + 2) { + printf + ("%s\n Start cluster beyond limit (%lu > %lu). Truncating file.\n", + path_name(file), FSTART(file, fs), fs->clusters + 1); + if (!file->offset) + die("Bad FAT32 root directory! (bad start cluster)\n"); + MODIFY_START(file, 0, fs); + } + clusters = prev = 0; + for (curr = FSTART(file, fs) ? FSTART(file, fs) : + -1; curr != -1; curr = next_cluster(fs, curr)) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, curr, fs); + + if (!curEntry.value || bad_cluster(fs, curr)) { + printf("%s\n Contains a %s cluster (%lu). Assuming EOF.\n", + path_name(file), curEntry.value ? "bad" : "free", curr); + if (prev) + set_fat(fs, prev, -1); + else if (!file->offset) + die("FAT32 root dir starts with a bad cluster!"); + else + MODIFY_START(file, 0, fs); + break; + } + if (!(file->dir_ent.attr & ATTR_DIR) && CF_LE_L(file->dir_ent.size) <= + (unsigned long long)clusters * fs->cluster_size) { + printf + ("%s\n File size is %u bytes, cluster chain length is > %llu " + "bytes.\n Truncating file to %u bytes.\n", path_name(file), + CF_LE_L(file->dir_ent.size), + (unsigned long long)clusters * fs->cluster_size, + CF_LE_L(file->dir_ent.size)); + truncate_file(fs, file, clusters); + break; + } + if ((owner = get_owner(fs, curr))) { + int do_trunc = 0; + printf("%s and\n", path_name(owner)); + printf("%s\n share clusters.\n", path_name(file)); + clusters2 = 0; + for (walk = FSTART(owner, fs); walk > 0 && walk != -1; walk = + next_cluster(fs, walk)) + if (walk == curr) + break; + else + clusters2++; + restart = file->dir_ent.attr & ATTR_DIR; + if (!owner->offset) { + printf(" Truncating second to %llu bytes because first " + "is FAT32 root dir.\n", + (unsigned long long)clusters2 * fs->cluster_size); + do_trunc = 2; + } else if (!file->offset) { + printf(" Truncating first to %llu bytes because second " + "is FAT32 root dir.\n", + (unsigned long long)clusters * fs->cluster_size); + do_trunc = 1; + } else if (interactive) + printf("1) Truncate first to %llu bytes%s\n" + "2) Truncate second to %llu bytes\n", + (unsigned long long)clusters * fs->cluster_size, + restart ? " and restart" : "", + (unsigned long long)clusters2 * fs->cluster_size); + else + printf(" Truncating second to %llu bytes.\n", + (unsigned long long)clusters2 * fs->cluster_size); + if (do_trunc != 2 + && (do_trunc == 1 + || (interactive && get_key("12", "?") == '1'))) { + prev = 0; + clusters = 0; + for (this = FSTART(owner, fs); this > 0 && this != -1; this = + next_cluster(fs, this)) { + if (this == curr) { + if (prev) + set_fat(fs, prev, -1); + else + MODIFY_START(owner, 0, fs); + MODIFY(owner, size, + CT_LE_L((unsigned long long)clusters * + fs->cluster_size)); + if (restart) + return 1; + while (this > 0 && this != -1) { + set_owner(fs, this, NULL); + this = next_cluster(fs, this); + } + this = curr; + break; + } + clusters++; + prev = this; + } + if (this != curr) + die("Internal error: didn't find cluster %d in chain" + " starting at %d", curr, FSTART(owner, fs)); + } else { + if (prev) + set_fat(fs, prev, -1); + else + MODIFY_START(file, 0, fs); + break; + } + } + set_owner(fs, curr, file); + clusters++; + prev = curr; + } + if (!(file->dir_ent.attr & ATTR_DIR) && CF_LE_L(file->dir_ent.size) > + (unsigned long long)clusters * fs->cluster_size) { + printf + ("%s\n File size is %u bytes, cluster chain length is %llu bytes." + "\n Truncating file to %llu bytes.\n", path_name(file), + CF_LE_L(file->dir_ent.size), + (unsigned long long)clusters * fs->cluster_size, + (unsigned long long)clusters * fs->cluster_size); + MODIFY(file, size, + CT_LE_L((unsigned long long)clusters * fs->cluster_size)); + } + return 0; +} + +static int check_files(DOS_FS * fs, DOS_FILE * start) +{ + while (start) { + if (check_file(fs, start)) + return 1; + start = start->next; + } + return 0; +} + +static int check_dir(DOS_FS * fs, DOS_FILE ** root, int dots) +{ + DOS_FILE *parent, **walk, **scan; + int dot, dotdot, skip, redo; + int good, bad; + + if (!*root) + return 0; + parent = (*root)->parent; + good = bad = 0; + for (walk = root; *walk; walk = &(*walk)->next) + if (bad_name(*walk)) + bad++; + else + good++; + if (*root && parent && good + bad > 4 && bad > good / 2) { + printf("%s\n Has a large number of bad entries. (%d/%d)\n", + path_name(parent), bad, good + bad); + if (!dots) + printf(" Not dropping root directory.\n"); + else if (!interactive) + printf(" Not dropping it in auto-mode.\n"); + else if (get_key("yn", "Drop directory ? (y/n)") == 'y') { + truncate_file(fs, parent, 0); + MODIFY(parent, name[0], DELETED_FLAG); + /* buglet: deleted directory stays in the list. */ + return 1; + } + } + dot = dotdot = redo = 0; + walk = root; + while (*walk) { + if (!strncmp + ((const char *)((*walk)->dir_ent.name), MSDOS_DOT, MSDOS_NAME) + || !strncmp((const char *)((*walk)->dir_ent.name), MSDOS_DOTDOT, + MSDOS_NAME)) { + if (handle_dot(fs, *walk, dots)) { + *walk = (*walk)->next; + continue; + } + if (!strncmp + ((const char *)((*walk)->dir_ent.name), MSDOS_DOT, MSDOS_NAME)) + dot++; + else + dotdot++; + } + if (!((*walk)->dir_ent.attr & ATTR_VOLUME) && bad_name(*walk)) { + puts(path_name(*walk)); + printf(" Bad short file name (%s).\n", + file_name((*walk)->dir_ent.name)); + if (interactive) + printf("1) Drop file\n2) Rename file\n3) Auto-rename\n" + "4) Keep it\n"); + else + printf(" Auto-renaming it.\n"); + switch (interactive ? get_key("1234", "?") : '3') { + case '1': + drop_file(fs, *walk); + walk = &(*walk)->next; + continue; + case '2': + rename_file(*walk); + redo = 1; + break; + case '3': + auto_rename(*walk); + printf(" Renamed to %s\n", file_name((*walk)->dir_ent.name)); + break; + case '4': + break; + } + } + /* don't check for duplicates of the volume label */ + if (!((*walk)->dir_ent.attr & ATTR_VOLUME)) { + scan = &(*walk)->next; + skip = 0; + while (*scan && !skip) { + if (!((*scan)->dir_ent.attr & ATTR_VOLUME) && + !memcmp((*walk)->dir_ent.name, (*scan)->dir_ent.name, + MSDOS_NAME)) { + printf("%s\n Duplicate directory entry.\n First %s\n", + path_name(*walk), file_stat(*walk)); + printf(" Second %s\n", file_stat(*scan)); + if (interactive) + printf + ("1) Drop first\n2) Drop second\n3) Rename first\n" + "4) Rename second\n5) Auto-rename first\n" + "6) Auto-rename second\n"); + else + printf(" Auto-renaming second.\n"); + switch (interactive ? get_key("123456", "?") : '6') { + case '1': + drop_file(fs, *walk); + *walk = (*walk)->next; + skip = 1; + break; + case '2': + drop_file(fs, *scan); + *scan = (*scan)->next; + continue; + case '3': + rename_file(*walk); + printf(" Renamed to %s\n", path_name(*walk)); + redo = 1; + break; + case '4': + rename_file(*scan); + printf(" Renamed to %s\n", path_name(*walk)); + redo = 1; + break; + case '5': + auto_rename(*walk); + printf(" Renamed to %s\n", + file_name((*walk)->dir_ent.name)); + break; + case '6': + auto_rename(*scan); + printf(" Renamed to %s\n", + file_name((*scan)->dir_ent.name)); + break; + } + } + scan = &(*scan)->next; + } + if (skip) + continue; + } + if (!redo) + walk = &(*walk)->next; + else { + walk = root; + dot = dotdot = redo = 0; + } + } + if (dots && !dot) + printf("%s\n \".\" is missing. Can't fix this yet.\n", + path_name(parent)); + if (dots && !dotdot) + printf("%s\n \"..\" is missing. Can't fix this yet.\n", + path_name(parent)); + return 0; +} + +/** + * Check a dentry's cluster chain for bad clusters. + * If requested, we verify readability and mark unreadable clusters as bad. + * + * @param[inout] fs Information about the filesystem + * @param[in] file dentry to check + * @param[in] read_test Nonzero == verify that dentry's clusters can + * be read + */ +static void test_file(DOS_FS * fs, DOS_FILE * file, int read_test) +{ + DOS_FILE *owner; + unsigned long walk, prev, clusters, next_clu; + + prev = clusters = 0; + for (walk = FSTART(file, fs); walk > 0 && walk < fs->clusters + 2; + walk = next_clu) { + next_clu = next_cluster(fs, walk); + + /* In this stage we are checking only for a loop within our own + * cluster chain. + * Cross-linking of clusters is handled in check_file() + */ + if ((owner = get_owner(fs, walk))) { + if (owner == file) { + printf("%s\n Circular cluster chain. Truncating to %lu " + "cluster%s.\n", path_name(file), clusters, + clusters == 1 ? "" : "s"); + if (prev) + set_fat(fs, prev, -1); + else if (!file->offset) + die("Bad FAT32 root directory! (bad start cluster)\n"); + else + MODIFY_START(file, 0, fs); + } + break; + } + if (bad_cluster(fs, walk)) + break; + if (read_test) { + if (fs_test(cluster_start(fs, walk), fs->cluster_size)) { + prev = walk; + clusters++; + } else { + printf("%s\n Cluster %lu (%lu) is unreadable. Skipping it.\n", + path_name(file), clusters, walk); + if (prev) + set_fat(fs, prev, next_cluster(fs, walk)); + else + MODIFY_START(file, next_cluster(fs, walk), fs); + set_fat(fs, walk, -2); + } + } + set_owner(fs, walk, file); + } + /* Revert ownership (for now) */ + for (walk = FSTART(file, fs); walk > 0 && walk < fs->clusters + 2; + walk = next_cluster(fs, walk)) + if (bad_cluster(fs, walk)) + break; + else if (get_owner(fs, walk) == file) + set_owner(fs, walk, NULL); + else + break; +} + +static void undelete(DOS_FS * fs, DOS_FILE * file) +{ + unsigned long clusters, left, prev, walk; + + clusters = left = (CF_LE_L(file->dir_ent.size) + fs->cluster_size - 1) / + fs->cluster_size; + prev = 0; + + walk = FSTART(file, fs); + + while (left && (walk >= 2) && (walk < fs->clusters + 2)) { + + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, walk, fs); + + if (!curEntry.value) + break; + + left--; + if (prev) + set_fat(fs, prev, walk); + prev = walk; + walk++; + } + if (prev) + set_fat(fs, prev, -1); + else + MODIFY_START(file, 0, fs); + if (left) + printf("Warning: Did only undelete %lu of %lu cluster%s.\n", + clusters - left, clusters, clusters == 1 ? "" : "s"); + +} + +static void new_dir(void) +{ + lfn_reset(); +} + +/** + * Create a description for a referenced dentry and insert it in our dentry + * tree. Then, go check the dentry's cluster chain for bad clusters and + * cluster loops. + * + * @param[inout] fs Information about the filesystem + * @param[out] chain + * @param[in] parent Information about parent directory of this file + * NULL == no parent ('file' is root directory) + * @param[in] offset Partition-relative byte offset of directory entry of interest + * 0 == Root directory + * @param cp + */ +static void add_file(DOS_FS * fs, DOS_FILE *** chain, DOS_FILE * parent, + loff_t offset, FDSC ** cp) +{ + DOS_FILE *new; + DIR_ENT de; + FD_TYPE type; + + if (offset) + fs_read(offset, sizeof(DIR_ENT), &de); + else { + /* Construct a DIR_ENT for the root directory */ + memcpy(de.name, " ", MSDOS_NAME); + de.attr = ATTR_DIR; + de.size = de.time = de.date = 0; + de.start = CT_LE_W(fs->root_cluster & 0xffff); + de.starthi = CT_LE_W((fs->root_cluster >> 16) & 0xffff); + } + if ((type = file_type(cp, (char *)de.name)) != fdt_none) { + if (type == fdt_undelete && (de.attr & ATTR_DIR)) + die("Can't undelete directories."); + file_modify(cp, (char *)de.name); + fs_write(offset, 1, &de); + } + if (IS_FREE(de.name)) { + lfn_check_orphaned(); + return; + } + if (de.attr == VFAT_LN_ATTR) { + lfn_add_slot(&de, offset); + return; + } + new = qalloc(&mem_queue, sizeof(DOS_FILE)); + new->lfn = lfn_get(&de, &new->lfn_offset); + new->offset = offset; + memcpy(&new->dir_ent, &de, sizeof(de)); + new->next = new->first = NULL; + new->parent = parent; + if (type == fdt_undelete) + undelete(fs, new); + **chain = new; + *chain = &new->next; + if (list) { + printf("Checking file %s", path_name(new)); + if (new->lfn) + printf(" (%s)", file_name(new->dir_ent.name)); /* (8.3) */ + printf("\n"); + } + /* Don't include root directory, '.', or '..' in the total file count */ + if (offset && + strncmp((const char *)de.name, MSDOS_DOT, MSDOS_NAME) != 0 && + strncmp((const char *)de.name, MSDOS_DOTDOT, MSDOS_NAME) != 0) + ++n_files; + test_file(fs, new, test); /* Bad cluster check */ +} + +static int subdirs(DOS_FS * fs, DOS_FILE * parent, FDSC ** cp); + +static int scan_dir(DOS_FS * fs, DOS_FILE * this, FDSC ** cp) +{ + DOS_FILE **chain; + int i; + unsigned long clu_num; + + chain = &this->first; + i = 0; + clu_num = FSTART(this, fs); + new_dir(); + while (clu_num > 0 && clu_num != -1) { + add_file(fs, &chain, this, + cluster_start(fs, clu_num) + (i % fs->cluster_size), cp); + i += sizeof(DIR_ENT); + if (!(i % fs->cluster_size)) + if ((clu_num = next_cluster(fs, clu_num)) == 0 || clu_num == -1) + break; + } + lfn_check_orphaned(); + if (check_dir(fs, &this->first, this->offset)) + return 0; + if (check_files(fs, this->first)) + return 1; + return subdirs(fs, this, cp); +} + +/** + * Recursively scan subdirectories of the specified parent directory. + * + * @param[inout] fs Information about the filesystem + * @param[in] parent Identifies the directory to scan + * @param[in] cp + * + * @return 0 Success + * @return 1 Error + */ +static int subdirs(DOS_FS * fs, DOS_FILE * parent, FDSC ** cp) +{ + DOS_FILE *walk; + + for (walk = parent ? parent->first : root; walk; walk = walk->next) + if (walk->dir_ent.attr & ATTR_DIR) + if (strncmp((const char *)walk->dir_ent.name, MSDOS_DOT, MSDOS_NAME) + && strncmp((const char *)walk->dir_ent.name, MSDOS_DOTDOT, + MSDOS_NAME)) + if (scan_dir(fs, walk, file_cd(cp, (char *)walk->dir_ent.name))) + return 1; + return 0; +} + +/** + * Scan all directory and file information for errors. + * + * @param[inout] fs Information about the filesystem + * + * @return 0 Success + * @return 1 Error + */ +int scan_root(DOS_FS * fs) +{ + DOS_FILE **chain; + int i; + + root = NULL; + chain = &root; + new_dir(); + if (fs->root_cluster) { + add_file(fs, &chain, NULL, 0, &fp_root); + } else { + for (i = 0; i < fs->root_entries; i++) + add_file(fs, &chain, NULL, fs->root_start + i * sizeof(DIR_ENT), + &fp_root); + } + lfn_check_orphaned(); + (void)check_dir(fs, &root, 0); + if (check_files(fs, root)) + return 1; + return subdirs(fs, NULL, &fp_root); +} diff --git a/dosfstools/src/check.h b/dosfstools/src/check.h new file mode 100644 index 000000000..277c44b6f --- /dev/null +++ b/dosfstools/src/check.h @@ -0,0 +1,39 @@ +/* check.h - Check and repair a PC/MS-DOS file system + + Copyright (C) 1993 Werner Almesberger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#ifndef _CHECK_H +#define _CHECK_H + +loff_t alloc_rootdir_entry(DOS_FS * fs, DIR_ENT * de, const char *pattern); + +/* Allocate a free slot in the root directory for a new file. The file name is + constructed after 'pattern', which must include a %d type format for printf + and expand to exactly 11 characters. The name actually used is written into + the 'de' structure, the rest of *de is cleared. The offset returned is to + where in the filesystem the entry belongs. */ + +int scan_root(DOS_FS * fs); + +/* Scans the root directory and recurses into all subdirectories. See check.c + for all the details. Returns a non-zero integer if the file system has to + be checked again. */ + +#endif diff --git a/dosfstools/src/common.c b/dosfstools/src/common.c new file mode 100644 index 000000000..51605a2aa --- /dev/null +++ b/dosfstools/src/common.c @@ -0,0 +1,118 @@ +/* common.c - Common functions + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#include +#include +#include +#include +#include + +#include "common.h" + +typedef struct _link { + void *data; + struct _link *next; +} LINK; + +void die(char *msg, ...) +{ + va_list args; + + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + fprintf(stderr, "\n"); + exit(1); +} + +void pdie(char *msg, ...) +{ + va_list args; + + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + fprintf(stderr, ":%s\n", strerror(errno)); + exit(1); +} + +void *alloc(int size) +{ + void *this; + + if ((this = malloc(size))) + return this; + pdie("malloc"); + return NULL; /* for GCC */ +} + +void *qalloc(void **root, int size) +{ + LINK *link; + + link = alloc(sizeof(LINK)); + link->next = *root; + *root = link; + return link->data = alloc(size); +} + +void qfree(void **root) +{ + LINK *this; + + while (*root) { + this = (LINK *) * root; + *root = this->next; + free(this->data); + free(this); + } +} + +int min(int a, int b) +{ + return a < b ? a : b; +} + +char get_key(char *valid, char *prompt) +{ + int ch, okay; + + while (1) { + if (prompt) + printf("%s ", prompt); + fflush(stdout); + while (ch = getchar(), ch == ' ' || ch == '\t') ; + if (ch == EOF) + exit(1); + if (!strchr(valid, okay = ch)) + okay = 0; + while (ch = getchar(), ch != '\n' && ch != EOF) ; + if (ch == EOF) + exit(1); + if (okay) + return okay; + printf("Invalid input.\n"); + } +} diff --git a/dosfstools/src/common.h b/dosfstools/src/common.h new file mode 100644 index 000000000..395eabb59 --- /dev/null +++ b/dosfstools/src/common.h @@ -0,0 +1,57 @@ +/* common.h - Common functions + + Copyright (C) 1993 Werner Almesberger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#include + +#ifndef _COMMON_H +#define _COMMON_H + +void die(char *msg, ...) __attribute((noreturn)); + +/* Displays a prinf-style message and terminates the program. */ + +void pdie(char *msg, ...) __attribute((noreturn)); + +/* Like die, but appends an error message according to the state of errno. */ + +void *alloc(int size); + +/* mallocs SIZE bytes and returns a pointer to the data. Terminates the program + if malloc fails. */ + +void *qalloc(void **root, int size); + +/* Like alloc, but registers the data area in a list described by ROOT. */ + +void qfree(void **root); + +/* Deallocates all qalloc'ed data areas described by ROOT. */ + +int min(int a, int b); + +/* Returns the smaller integer value of a and b. */ + +char get_key(char *valid, char *prompt); + +/* Displays PROMPT and waits for user input. Only characters in VALID are + accepted. Terminates the program on EOF. Returns the character. */ + +#endif diff --git a/dosfstools/src/dosfsck.c b/dosfstools/src/dosfsck.c new file mode 100644 index 000000000..a7a59e1a1 --- /dev/null +++ b/dosfstools/src/dosfsck.c @@ -0,0 +1,224 @@ +/* dosfsck.c - User interface + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#include "version.h" + +#include +#include +#include +#include +#include +#include + +#include "common.h" +#include "dosfsck.h" +#include "io.h" +#include "boot.h" +#include "fat.h" +#include "file.h" +#include "check.h" + +int interactive = 0, rw = 0, list = 0, test = 0, verbose = 0, write_immed = 0; +int atari_format = 0; +unsigned n_files = 0; +void *mem_queue = NULL; + +#ifdef USE_ANDROID_RETVALS +unsigned retandroid = 1; +#else +unsigned retandroid = 0; +#endif + +static void usage(char *name) +{ + fprintf(stderr, "usage: %s [-aAflrtvVwy] [-d path -d ...] " + "[-u path -u ...]\n%15sdevice\n", name, ""); + fprintf(stderr, " -a automatically repair the file system\n"); + fprintf(stderr, " -A toggle Atari file system format\n"); + fprintf(stderr, " -d path drop that file\n"); + fprintf(stderr, " -f ignored\n"); + fprintf(stderr, " -l list path names\n"); + fprintf(stderr, + " -n no-op, check non-interactively without changing\n"); + fprintf(stderr, " -p same as -a, for compat with other *fsck\n"); + fprintf(stderr, " -r interactively repair the file system\n"); + fprintf(stderr, " -t test for bad clusters\n"); + fprintf(stderr, " -u path try to undelete that (non-directory) file\n"); + fprintf(stderr, " -v verbose mode\n"); + fprintf(stderr, " -V perform a verification pass\n"); + fprintf(stderr, " -w write changes to disk immediately\n"); + fprintf(stderr, " -y same as -a, for compat with other *fsck\n"); + if (retandroid) { + exit(1); + } else { + exit(2); + } +} + +/* + * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant + * of MS-DOS filesystem by default. + */ +static void check_atari(void) +{ +#ifdef __mc68000__ + FILE *f; + char line[128], *p; + + if (!(f = fopen("/proc/hardware", "r"))) { + perror("/proc/hardware"); + return; + } + + while (fgets(line, sizeof(line), f)) { + if (strncmp(line, "Model:", 6) == 0) { + p = line + 6; + p += strspn(p, " \t"); + if (strncmp(p, "Atari ", 6) == 0) + atari_format = 1; + break; + } + } + fclose(f); +#endif +} + +int main(int argc, char **argv) +{ + DOS_FS fs; + int salvage_files, verify, c; + unsigned n_files_check = 0, n_files_verify = 0; + unsigned long free_clusters; + + memset(&fs, 0, sizeof(fs)); + rw = salvage_files = verify = 0; + interactive = 1; + check_atari(); + + while ((c = getopt(argc, argv, "Aad:flnprtu:vVwy")) != EOF) + switch (c) { + case 'A': /* toggle Atari format */ + atari_format = !atari_format; + break; + case 'a': + case 'p': + case 'y': + rw = 1; + interactive = 0; + salvage_files = 1; + break; + case 'd': + file_add(optarg, fdt_drop); + break; + case 'f': + salvage_files = 1; + break; + case 'l': + list = 1; + break; + case 'n': + rw = 0; + interactive = 0; + break; + case 'r': + rw = 1; + interactive = 1; + break; + case 't': + test = 1; + break; + case 'u': + file_add(optarg, fdt_undelete); + break; + case 'v': + verbose = 1; + printf("dosfsck " VERSION " (" VERSION_DATE ")\n"); + break; + case 'V': + verify = 1; + break; + case 'w': + write_immed = 1; + break; + default: + usage(argv[0]); + } + if ((test || write_immed) && !rw) { + fprintf(stderr, "-t and -w require -a or -r\n"); + if (retandroid) { + exit(1); + } else { + exit(2); + } + } + if (optind != argc - 1) + usage(argv[0]); + + printf("dosfsck " VERSION ", " VERSION_DATE ", FAT32, LFN\n"); + fs_open(argv[optind], rw); + read_boot(&fs); + if (verify) + printf("Starting check/repair pass.\n"); + while (read_fat(&fs), scan_root(&fs)) + qfree(&mem_queue); + if (test) + fix_bad(&fs); + if (salvage_files && 0) + reclaim_file(&fs); + else + reclaim_free(&fs); + free_clusters = update_free(&fs); + file_unused(); + qfree(&mem_queue); + n_files_check = n_files; + if (verify) { + n_files = 0; + printf("Starting verification pass.\n"); + read_fat(&fs); + scan_root(&fs); + reclaim_free(&fs); + qfree(&mem_queue); + n_files_verify = n_files; + } + + if (fs_changed()) { + if (rw) { + if (interactive) + rw = get_key("yn", "Perform changes ? (y/n)") == 'y'; + else + printf("Performing changes.\n"); + } else + printf("Leaving file system unchanged.\n"); + } + + printf("%s: %u files, %lu/%lu clusters\n", argv[optind], + n_files, fs.clusters - free_clusters, fs.clusters); + + if (retandroid) { + return fs_close(rw) ? 4 : 0; + } else { + return fs_close(rw) ? 1 : 0; + } +} diff --git a/dosfstools/src/dosfsck.h b/dosfstools/src/dosfsck.h new file mode 100644 index 000000000..6f53c72cf --- /dev/null +++ b/dosfstools/src/dosfsck.h @@ -0,0 +1,218 @@ +/* dosfsck.h - Common data structures and global variables + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#ifndef _DOSFSCK_H +#define _DOSFSCK_H + +#include +#define _LINUX_STAT_H /* hack to avoid inclusion of */ +#define _LINUX_STRING_H_ /* hack to avoid inclusion of */ +#define _LINUX_FS_H /* hack to avoid inclusion of */ + +#ifdef _USING_BIONIC_ +#include +#endif + +#include +#include + +#include + +#undef CF_LE_W +#undef CF_LE_L +#undef CT_LE_W +#undef CT_LE_L + +#if __BYTE_ORDER == __BIG_ENDIAN +#include +#define CF_LE_W(v) bswap_16(v) +#define CF_LE_L(v) bswap_32(v) +#define CT_LE_W(v) CF_LE_W(v) +#define CT_LE_L(v) CF_LE_L(v) +#else +#define CF_LE_W(v) (v) +#define CF_LE_L(v) (v) +#define CT_LE_W(v) (v) +#define CT_LE_L(v) (v) +#endif /* __BIG_ENDIAN */ + +#define VFAT_LN_ATTR (ATTR_RO | ATTR_HIDDEN | ATTR_SYS | ATTR_VOLUME) + +/* ++roman: Use own definition of boot sector structure -- the kernel headers' + * name for it is msdos_boot_sector in 2.0 and fat_boot_sector in 2.1 ... */ +struct boot_sector { + __u8 ignored[3]; /* Boot strap short or near jump */ + __u8 system_id[8]; /* Name - can be used to special case + partition manager volumes */ + __u8 sector_size[2]; /* bytes per logical sector */ + __u8 cluster_size; /* sectors/cluster */ + __u16 reserved; /* reserved sectors */ + __u8 fats; /* number of FATs */ + __u8 dir_entries[2]; /* root directory entries */ + __u8 sectors[2]; /* number of sectors */ + __u8 media; /* media code (unused) */ + __u16 fat_length; /* sectors/FAT */ + __u16 secs_track; /* sectors per track */ + __u16 heads; /* number of heads */ + __u32 hidden; /* hidden sectors (unused) */ + __u32 total_sect; /* number of sectors (if sectors == 0) */ + + /* The following fields are only used by FAT32 */ + __u32 fat32_length; /* sectors/FAT */ + __u16 flags; /* bit 8: fat mirroring, low 4: active fat */ + __u8 version[2]; /* major, minor filesystem version */ + __u32 root_cluster; /* first cluster in root directory */ + __u16 info_sector; /* filesystem info sector */ + __u16 backup_boot; /* backup boot sector */ + __u8 reserved2[12]; /* Unused */ + + __u8 drive_number; /* Logical Drive Number */ + __u8 reserved3; /* Unused */ + + __u8 extended_sig; /* Extended Signature (0x29) */ + __u32 serial; /* Serial number */ + __u8 label[11]; /* FS label */ + __u8 fs_type[8]; /* FS Type */ + + /* fill up to 512 bytes */ + __u8 junk[422]; +} __attribute__ ((packed)); + +struct boot_sector_16 { + __u8 ignored[3]; /* Boot strap short or near jump */ + __u8 system_id[8]; /* Name - can be used to special case + partition manager volumes */ + __u8 sector_size[2]; /* bytes per logical sector */ + __u8 cluster_size; /* sectors/cluster */ + __u16 reserved; /* reserved sectors */ + __u8 fats; /* number of FATs */ + __u8 dir_entries[2]; /* root directory entries */ + __u8 sectors[2]; /* number of sectors */ + __u8 media; /* media code (unused) */ + __u16 fat_length; /* sectors/FAT */ + __u16 secs_track; /* sectors per track */ + __u16 heads; /* number of heads */ + __u32 hidden; /* hidden sectors (unused) */ + __u32 total_sect; /* number of sectors (if sectors == 0) */ + + __u8 drive_number; /* Logical Drive Number */ + __u8 reserved2; /* Unused */ + + __u8 extended_sig; /* Extended Signature (0x29) */ + __u32 serial; /* Serial number */ + __u8 label[11]; /* FS label */ + __u8 fs_type[8]; /* FS Type */ + + /* fill up to 512 bytes */ + __u8 junk[450]; +} __attribute__ ((packed)); + +struct info_sector { + __u32 magic; /* Magic for info sector ('RRaA') */ + __u8 junk[0x1dc]; + __u32 reserved1; /* Nothing as far as I can tell */ + __u32 signature; /* 0x61417272 ('rrAa') */ + __u32 free_clusters; /* Free cluster count. -1 if unknown */ + __u32 next_cluster; /* Most recently allocated cluster. */ + __u32 reserved2[3]; + __u16 reserved3; + __u16 boot_sign; +}; + +typedef struct { + __u8 name[8], ext[3]; /* name and extension */ + __u8 attr; /* attribute bits */ + __u8 lcase; /* Case for base and extension */ + __u8 ctime_ms; /* Creation time, milliseconds */ + __u16 ctime; /* Creation time */ + __u16 cdate; /* Creation date */ + __u16 adate; /* Last access date */ + __u16 starthi; /* High 16 bits of cluster in FAT32 */ + __u16 time, date, start; /* time, date and first cluster */ + __u32 size; /* file size (in bytes) */ +} __attribute__ ((packed)) DIR_ENT; + +typedef struct _dos_file { + DIR_ENT dir_ent; + char *lfn; + loff_t offset; + loff_t lfn_offset; + struct _dos_file *parent; /* parent directory */ + struct _dos_file *next; /* next entry */ + struct _dos_file *first; /* first entry (directory only) */ +} DOS_FILE; + +typedef struct { + unsigned long value; + unsigned long reserved; +} FAT_ENTRY; + +typedef struct { + int nfats; + loff_t fat_start; + unsigned int fat_size; /* unit is bytes */ + unsigned int fat_bits; /* size of a FAT entry */ + unsigned int eff_fat_bits; /* # of used bits in a FAT entry */ + unsigned long root_cluster; /* 0 for old-style root dir */ + loff_t root_start; + unsigned int root_entries; + loff_t data_start; + unsigned int cluster_size; + unsigned long clusters; + loff_t fsinfo_start; /* 0 if not present */ + long free_clusters; + loff_t backupboot_start; /* 0 if not present */ + unsigned char *fat; + DOS_FILE **cluster_owner; + char *label; +} DOS_FS; + +#ifndef offsetof +#define offsetof(t,e) ((int)&(((t *)0)->e)) +#endif + +extern int interactive, rw, list, verbose, test, write_immed; +extern int atari_format; +extern unsigned n_files; +extern void *mem_queue; +extern unsigned retandroid; + +/* value to use as end-of-file marker */ +#define FAT_EOF(fs) ((atari_format ? 0xfff : 0xff8) | FAT_EXTD(fs)) +#define FAT_IS_EOF(fs,v) ((unsigned long)(v) >= (0xff8|FAT_EXTD(fs))) +/* value to mark bad clusters */ +#define FAT_BAD(fs) (0xff7 | FAT_EXTD(fs)) +/* range of values used for bad clusters */ +#define FAT_MIN_BAD(fs) ((atari_format ? 0xff0 : 0xff7) | FAT_EXTD(fs)) +#define FAT_MAX_BAD(fs) ((atari_format ? 0xff7 : 0xff7) | FAT_EXTD(fs)) +#define FAT_IS_BAD(fs,v) ((v) >= FAT_MIN_BAD(fs) && (v) <= FAT_MAX_BAD(fs)) + +/* return -16 as a number with fs->fat_bits bits */ +#define FAT_EXTD(fs) (((1 << fs->eff_fat_bits)-1) & ~0xf) + +/* marker for files with no 8.3 name */ +#define FAT_NO_83NAME 32 + +#endif diff --git a/dosfstools/src/dosfslabel.c b/dosfstools/src/dosfslabel.c new file mode 100644 index 000000000..5e2d2824f --- /dev/null +++ b/dosfstools/src/dosfslabel.c @@ -0,0 +1,128 @@ +/* dosfslabel.c - User interface + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + Copyright (C) 2007 Red Hat, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#include "version.h" + +#include +#include +#include +#include +#include +#include + +#ifdef _USING_BIONIC_ +#include +#endif + +#include "common.h" +#include "dosfsck.h" +#include "io.h" +#include "boot.h" +#include "fat.h" +#include "file.h" +#include "check.h" + +int interactive = 0, rw = 0, list = 0, test = 0, verbose = 0, write_immed = 0; +int atari_format = 0; +unsigned n_files = 0; +void *mem_queue = NULL; + +static void usage(int error) +{ + FILE *f = error ? stderr : stdout; + int status = error ? 1 : 0; + + fprintf(f, "usage: dosfslabel device [label]\n"); + exit(status); +} + +/* + * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant + * of MS-DOS filesystem by default. + */ +static void check_atari(void) +{ +#ifdef __mc68000__ + FILE *f; + char line[128], *p; + + if (!(f = fopen("/proc/hardware", "r"))) { + perror("/proc/hardware"); + return; + } + + while (fgets(line, sizeof(line), f)) { + if (strncmp(line, "Model:", 6) == 0) { + p = line + 6; + p += strspn(p, " \t"); + if (strncmp(p, "Atari ", 6) == 0) + atari_format = 1; + break; + } + } + fclose(f); +#endif +} + +int main(int argc, char *argv[]) +{ + DOS_FS fs; + rw = 0; + + char *device = NULL; + char *label = NULL; + + check_atari(); + + if (argc < 2 || argc > 3) + usage(1); + + if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) + usage(0); + else if (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version")) { + printf("dosfslabel " VERSION ", " VERSION_DATE ", FAT32, LFN\n"); + exit(0); + } + + device = argv[1]; + if (argc == 3) { + label = argv[2]; + if (strlen(label) > 11) { + fprintf(stderr, + "dosfslabel: labels can be no longer than 11 characters\n"); + exit(1); + } + rw = 1; + } + + fs_open(device, rw); + read_boot(&fs); + if (!rw) { + fprintf(stdout, "%s\n", fs.label); + exit(0); + } + + write_label(&fs, label); + fs_close(rw); + return 0; +} diff --git a/dosfstools/src/fat.c b/dosfstools/src/fat.c new file mode 100644 index 000000000..5a0dfb0f2 --- /dev/null +++ b/dosfstools/src/fat.c @@ -0,0 +1,547 @@ +/* fat.c - Read/write access to the FAT + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#include +#include +#include +#include + +#include "common.h" +#include "dosfsck.h" +#include "io.h" +#include "check.h" +#include "fat.h" + +/** + * Fetch the FAT entry for a specified cluster. + * + * @param[out] entry Cluster to which cluster of interest is linked + * @param[in] fat FAT table for the partition + * @param[in] cluster Cluster of interest + * @param[in] fs Information from the FAT boot sectors (bits per FAT entry) + */ +void get_fat(FAT_ENTRY * entry, void *fat, unsigned long cluster, DOS_FS * fs) +{ + unsigned char *ptr; + + switch (fs->fat_bits) { + case 12: + ptr = &((unsigned char *)fat)[cluster * 3 / 2]; + entry->value = 0xfff & (cluster & 1 ? (ptr[0] >> 4) | (ptr[1] << 4) : + (ptr[0] | ptr[1] << 8)); + break; + case 16: + entry->value = CF_LE_W(((unsigned short *)fat)[cluster]); + break; + case 32: + /* According to M$, the high 4 bits of a FAT32 entry are reserved and + * are not part of the cluster number. So we cut them off. */ + { + unsigned long e = CF_LE_L(((unsigned int *)fat)[cluster]); + entry->value = e & 0xfffffff; + entry->reserved = e >> 28; + } + break; + default: + die("Bad FAT entry size: %d bits.", fs->fat_bits); + } +} + +/** + * Build a bookkeeping structure from the partition's FAT table. + * If the partition has multiple FATs and they don't agree, try to pick a winner, + * and queue a command to overwrite the loser. + * One error that is fixed here is a cluster that links to something out of range. + * + * @param[inout] fs Information about the filesystem + */ +void read_fat(DOS_FS * fs) +{ + int eff_size; + unsigned long i; + void *first, *second = NULL; + int first_ok, second_ok; + unsigned long total_num_clusters; + + /* Clean up from previous pass */ + free(fs->fat); + free(fs->cluster_owner); + fs->fat = NULL; + fs->cluster_owner = NULL; + + total_num_clusters = fs->clusters + 2UL; + eff_size = (total_num_clusters * fs->fat_bits + 7) / 8ULL; + first = alloc(eff_size); + fs_read(fs->fat_start, eff_size, first); + if (fs->nfats > 1) { + second = alloc(eff_size); + fs_read(fs->fat_start + fs->fat_size, eff_size, second); + } + if (second && memcmp(first, second, eff_size) != 0) { + FAT_ENTRY first_media, second_media; + get_fat(&first_media, first, 0, fs); + get_fat(&second_media, second, 0, fs); + first_ok = (first_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs); + second_ok = (second_media.value & FAT_EXTD(fs)) == FAT_EXTD(fs); + if (first_ok && !second_ok) { + printf("FATs differ - using first FAT.\n"); + fs_write(fs->fat_start + fs->fat_size, eff_size, first); + } + if (!first_ok && second_ok) { + printf("FATs differ - using second FAT.\n"); + fs_write(fs->fat_start, eff_size, second); + memcpy(first, second, eff_size); + } + if (first_ok && second_ok) { + if (interactive) { + printf("FATs differ but appear to be intact. Use which FAT ?\n" + "1) Use first FAT\n2) Use second FAT\n"); + if (get_key("12", "?") == '1') { + fs_write(fs->fat_start + fs->fat_size, eff_size, first); + } else { + fs_write(fs->fat_start, eff_size, second); + memcpy(first, second, eff_size); + } + } else { + printf("FATs differ but appear to be intact. Using first " + "FAT.\n"); + fs_write(fs->fat_start + fs->fat_size, eff_size, first); + } + } + if (!first_ok && !second_ok) { + printf("Both FATs appear to be corrupt. Giving up.\n"); + exit(1); + } + } + if (second) { + free(second); + } + fs->fat = (unsigned char *)first; + + fs->cluster_owner = alloc(total_num_clusters * sizeof(DOS_FILE *)); + memset(fs->cluster_owner, 0, (total_num_clusters * sizeof(DOS_FILE *))); + + /* Truncate any cluster chains that link to something out of range */ + for (i = 2; i < fs->clusters + 2; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + if (curEntry.value == 1) { + printf("Cluster %ld out of range (1). Setting to EOF.\n", i - 2); + set_fat(fs, i, -1); + } + if (curEntry.value >= fs->clusters + 2 && + (curEntry.value < FAT_MIN_BAD(fs))) { + printf("Cluster %ld out of range (%ld > %ld). Setting to EOF.\n", + i - 2, curEntry.value, fs->clusters + 2 - 1); + set_fat(fs, i, -1); + } + } +} + +/** + * Update the FAT entry for a specified cluster + * (i.e., change the cluster it links to). + * Queue a command to write out this change. + * + * @param[in,out] fs Information about the filesystem + * @param[in] cluster Cluster to change + * @param[in] new Cluster to link to + * Special values: + * 0 == free cluster + * -1 == end-of-chain + * -2 == bad cluster + */ +void set_fat(DOS_FS * fs, unsigned long cluster, unsigned long new) +{ + unsigned char *data = NULL; + int size; + loff_t offs; + + if ((long)new == -1) + new = FAT_EOF(fs); + else if ((long)new == -2) + new = FAT_BAD(fs); + switch (fs->fat_bits) { + case 12: + data = fs->fat + cluster * 3 / 2; + offs = fs->fat_start + cluster * 3 / 2; + if (cluster & 1) { + FAT_ENTRY prevEntry; + get_fat(&prevEntry, fs->fat, cluster - 1, fs); + data[0] = ((new & 0xf) << 4) | (prevEntry.value >> 8); + data[1] = new >> 4; + } else { + FAT_ENTRY subseqEntry; + get_fat(&subseqEntry, fs->fat, cluster + 1, fs); + data[0] = new & 0xff; + data[1] = (new >> 8) | (cluster == fs->clusters - 1 ? 0 : + (0xff & subseqEntry.value) << 4); + } + size = 2; + break; + case 16: + data = fs->fat + cluster * 2; + offs = fs->fat_start + cluster * 2; + *(unsigned short *)data = CT_LE_W(new); + size = 2; + break; + case 32: + { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, cluster, fs); + + data = fs->fat + cluster * 4; + offs = fs->fat_start + cluster * 4; + /* According to M$, the high 4 bits of a FAT32 entry are reserved and + * are not part of the cluster number. So we never touch them. */ + *(unsigned long *)data = CT_LE_L((new & 0xfffffff) | + (curEntry.reserved << 28)); + size = 4; + } + break; + default: + die("Bad FAT entry size: %d bits.", fs->fat_bits); + } + fs_write(offs, size, data); + if (fs->nfats > 1) { + fs_write(offs + fs->fat_size, size, data); + } +} + +int bad_cluster(DOS_FS * fs, unsigned long cluster) +{ + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, cluster, fs); + + return FAT_IS_BAD(fs, curEntry.value); +} + +/** + * Get the cluster to which the specified cluster is linked. + * If the linked cluster is marked bad, abort. + * + * @param[in] fs Information about the filesystem + * @param[in] cluster Cluster to follow + * + * @return -1 'cluster' is at the end of the chain + * @return Other values Next cluster in this chain + */ +unsigned long next_cluster(DOS_FS * fs, unsigned long cluster) +{ + unsigned long value; + FAT_ENTRY curEntry; + + get_fat(&curEntry, fs->fat, cluster, fs); + + value = curEntry.value; + if (FAT_IS_BAD(fs, value)) + die("Internal error: next_cluster on bad cluster"); + return FAT_IS_EOF(fs, value) ? -1 : value; +} + +loff_t cluster_start(DOS_FS * fs, unsigned long cluster) +{ + return fs->data_start + ((loff_t) cluster - + 2) * (unsigned long long)fs->cluster_size; +} + +/** + * Update internal bookkeeping to show that the specified cluster belongs + * to the specified dentry. + * + * @param[in,out] fs Information about the filesystem + * @param[in] cluster Cluster being assigned + * @param[in] owner Information on dentry that owns this cluster + * (may be NULL) + */ +void set_owner(DOS_FS * fs, unsigned long cluster, DOS_FILE * owner) +{ + if (fs->cluster_owner == NULL) + die("Internal error: attempt to set owner in non-existent table"); + + if (owner && fs->cluster_owner[cluster] + && (fs->cluster_owner[cluster] != owner)) + die("Internal error: attempt to change file owner"); + fs->cluster_owner[cluster] = owner; +} + +DOS_FILE *get_owner(DOS_FS * fs, unsigned long cluster) +{ + if (fs->cluster_owner == NULL) + return NULL; + else + return fs->cluster_owner[cluster]; +} + +void fix_bad(DOS_FS * fs) +{ + unsigned long i; + + if (verbose) + printf("Checking for bad clusters.\n"); + for (i = 2; i < fs->clusters + 2; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + + if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value)) + if (!fs_test(cluster_start(fs, i), fs->cluster_size)) { + printf("Cluster %lu is unreadable.\n", i); + set_fat(fs, i, -2); + } + } +} + +void reclaim_free(DOS_FS * fs) +{ + int reclaimed; + unsigned long i; + + if (verbose) + printf("Checking for unused clusters.\n"); + reclaimed = 0; + for (i = 2; i < fs->clusters + 2; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + + if (!get_owner(fs, i) && curEntry.value && + !FAT_IS_BAD(fs, curEntry.value)) { + set_fat(fs, i, 0); + reclaimed++; + } + } + if (reclaimed) + printf("Reclaimed %d unused cluster%s (%llu bytes).\n", reclaimed, + reclaimed == 1 ? "" : "s", + (unsigned long long)reclaimed * fs->cluster_size); +} + +/** + * Assign the specified owner to all orphan chains (except cycles). + * Break cross-links between orphan chains. + * + * @param[in,out] fs Information about the filesystem + * @param[in] owner dentry to be assigned ownership of orphans + * @param[in,out] num_refs For each orphan cluster [index], how many + * clusters link to it. + * @param[in] start_cluster Where to start scanning for orphans + */ +static void tag_free(DOS_FS * fs, DOS_FILE * owner, unsigned long *num_refs, + unsigned long start_cluster) +{ + int prev; + unsigned long i, walk; + + if (start_cluster == 0) + start_cluster = 2; + + for (i = start_cluster; i < fs->clusters + 2; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + + /* If the current entry is the head of an un-owned chain... */ + if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) && + !get_owner(fs, i) && !num_refs[i]) { + prev = 0; + /* Walk the chain, claiming ownership as we go */ + for (walk = i; walk != -1; walk = next_cluster(fs, walk)) { + if (!get_owner(fs, walk)) { + set_owner(fs, walk, owner); + } else { + /* We've run into cross-links between orphaned chains, + * or a cycle with a tail. + * Terminate this orphan chain (break the link) + */ + set_fat(fs, prev, -1); + + /* This is not necessary because 'walk' is owned and thus + * will never become the head of a chain (the only case + * that would matter during reclaim to files). + * It's easier to decrement than to prove that it's + * unnecessary. + */ + num_refs[walk]--; + break; + } + prev = walk; + } + } + } +} + +/** + * Recover orphan chains to files, handling any cycles or cross-links. + * + * @param[in,out] fs Information about the filesystem + */ +void reclaim_file(DOS_FS * fs) +{ + DOS_FILE orphan; + int reclaimed, files; + int changed = 0; + unsigned long i, next, walk; + unsigned long *num_refs = NULL; /* Only for orphaned clusters */ + unsigned long total_num_clusters; + + if (verbose) + printf("Reclaiming unconnected clusters.\n"); + + total_num_clusters = fs->clusters + 2UL; + num_refs = alloc(total_num_clusters * sizeof(unsigned long)); + memset(num_refs, 0, (total_num_clusters * sizeof(unsigned long))); + + /* Guarantee that all orphan chains (except cycles) end cleanly + * with an end-of-chain mark. + */ + + for (i = 2; i < total_num_clusters; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + + next = curEntry.value; + if (!get_owner(fs, i) && next && next < fs->clusters + 2) { + /* Cluster is linked, but not owned (orphan) */ + FAT_ENTRY nextEntry; + get_fat(&nextEntry, fs->fat, next, fs); + + /* Mark it end-of-chain if it links into an owned cluster, + * a free cluster, or a bad cluster. + */ + if (get_owner(fs, next) || !nextEntry.value || + FAT_IS_BAD(fs, nextEntry.value)) + set_fat(fs, i, -1); + else + num_refs[next]++; + } + } + + /* Scan until all the orphans are accounted for, + * and all cycles and cross-links are broken + */ + do { + tag_free(fs, &orphan, num_refs, changed); + changed = 0; + + /* Any unaccounted-for orphans must be part of a cycle */ + for (i = 2; i < total_num_clusters; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + + if (curEntry.value && !FAT_IS_BAD(fs, curEntry.value) && + !get_owner(fs, i)) { + if (!num_refs[curEntry.value]--) + die("Internal error: num_refs going below zero"); + set_fat(fs, i, -1); + changed = curEntry.value; + printf("Broke cycle at cluster %lu in free chain.\n", i); + + /* If we've created a new chain head, + * tag_free() can claim it + */ + if (num_refs[curEntry.value] == 0) + break; + } + } + } + while (changed); + + /* Now we can start recovery */ + files = reclaimed = 0; + for (i = 2; i < total_num_clusters; i++) + /* If this cluster is the head of an orphan chain... */ + if (get_owner(fs, i) == &orphan && !num_refs[i]) { + DIR_ENT de; + loff_t offset; + files++; + offset = alloc_rootdir_entry(fs, &de, "FSCK%04d"); + de.start = CT_LE_W(i & 0xffff); + if (fs->fat_bits == 32) + de.starthi = CT_LE_W(i >> 16); + for (walk = i; walk > 0 && walk != -1; + walk = next_cluster(fs, walk)) { + de.size = CT_LE_L(CF_LE_L(de.size) + fs->cluster_size); + reclaimed++; + } + fs_write(offset, sizeof(DIR_ENT), &de); + } + if (reclaimed) + printf("Reclaimed %d unused cluster%s (%llu bytes) in %d chain%s.\n", + reclaimed, reclaimed == 1 ? "" : "s", + (unsigned long long)reclaimed * fs->cluster_size, files, + files == 1 ? "" : "s"); + + free(num_refs); +} + +unsigned long update_free(DOS_FS * fs) +{ + unsigned long i; + unsigned long free = 0; + int do_set = 0; + + for (i = 2; i < fs->clusters + 2; i++) { + FAT_ENTRY curEntry; + get_fat(&curEntry, fs->fat, i, fs); + + if (!get_owner(fs, i) && !FAT_IS_BAD(fs, curEntry.value)) + ++free; + } + + if (!fs->fsinfo_start) + return free; + + if (verbose) + printf("Checking free cluster summary.\n"); + if (fs->free_clusters != 0xFFFFFFFF) { + if (free != fs->free_clusters) { + printf("Free cluster summary wrong (%ld vs. really %ld)\n", + fs->free_clusters, free); + if (interactive) + printf("1) Correct\n2) Don't correct\n"); + else + printf(" Auto-correcting.\n"); + if (!interactive || get_key("12", "?") == '1') + do_set = 1; + } + } else { + printf("Free cluster summary uninitialized (should be %ld)\n", free); + if (rw) { + if (interactive) + printf("1) Set it\n2) Leave it uninitialized\n"); + else + printf(" Auto-setting.\n"); + if (!interactive || get_key("12", "?") == '1') + do_set = 1; + } + } + + if (do_set) { + unsigned long le_free = CT_LE_L(free); + fs->free_clusters = free; + fs_write(fs->fsinfo_start + offsetof(struct info_sector, free_clusters), + sizeof(le_free), &le_free); + } + + return free; +} diff --git a/dosfstools/src/fat.h b/dosfstools/src/fat.h new file mode 100644 index 000000000..13ac1b345 --- /dev/null +++ b/dosfstools/src/fat.h @@ -0,0 +1,84 @@ +/* fat.h - Read/write access to the FAT + + Copyright (C) 1993 Werner Almesberger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#ifndef _FAT_H +#define _FAT_H + +void read_fat(DOS_FS * fs); + +/* Loads the FAT of the file system described by FS. Initializes the FAT, + replaces broken FATs and rejects invalid cluster entries. */ + +void get_fat(FAT_ENTRY * entry, void *fat, unsigned long cluster, DOS_FS * fs); + +/* Retrieve the FAT entry (next chained cluster) for CLUSTER. */ + +void set_fat(DOS_FS * fs, unsigned long cluster, unsigned long new); + +/* Changes the value of the CLUSTERth cluster of the FAT of FS to NEW. Special + values of NEW are -1 (EOF, 0xff8 or 0xfff8) and -2 (bad sector, 0xff7 or + 0xfff7) */ + +int bad_cluster(DOS_FS * fs, unsigned long cluster); + +/* Returns a non-zero integer if the CLUSTERth cluster is marked as bad or zero + otherwise. */ + +unsigned long next_cluster(DOS_FS * fs, unsigned long cluster); + +/* Returns the number of the cluster following CLUSTER, or -1 if this is the + last cluster of the respective cluster chain. CLUSTER must not be a bad + cluster. */ + +loff_t cluster_start(DOS_FS * fs, unsigned long cluster); + +/* Returns the byte offset of CLUSTER, relative to the respective device. */ + +void set_owner(DOS_FS * fs, unsigned long cluster, DOS_FILE * owner); + +/* Sets the owner pointer of the respective cluster to OWNER. If OWNER was NULL + before, it can be set to NULL or any non-NULL value. Otherwise, only NULL is + accepted as the new value. */ + +DOS_FILE *get_owner(DOS_FS * fs, unsigned long cluster); + +/* Returns the owner of the repective cluster or NULL if the cluster has no + owner. */ + +void fix_bad(DOS_FS * fs); + +/* Scans the disk for currently unused bad clusters and marks them as bad. */ + +void reclaim_free(DOS_FS * fs); + +/* Marks all allocated, but unused clusters as free. */ + +void reclaim_file(DOS_FS * fs); + +/* Scans the FAT for chains of allocated, but unused clusters and creates files + for them in the root directory. Also tries to fix all inconsistencies (e.g. + loops, shared clusters, etc.) in the process. */ + +unsigned long update_free(DOS_FS * fs); + +/* Updates free cluster count in FSINFO sector. */ + +#endif diff --git a/dosfstools/src/file.c b/dosfstools/src/file.c new file mode 100644 index 000000000..a73b73f5e --- /dev/null +++ b/dosfstools/src/file.c @@ -0,0 +1,282 @@ +/* file.c - Additional file attributes + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#include +#include +#include +#include +#include + +#define _LINUX_STAT_H /* hack to avoid inclusion of */ +#define _LINUX_STRING_H_ /* hack to avoid inclusion of */ +#define _LINUX_FS_H /* hack to avoid inclusion of */ + +#include + +#include + +#include "common.h" +#include "file.h" + +FDSC *fp_root = NULL; + +static void put_char(char **p, unsigned char c) +{ + if ((c >= ' ' && c < 0x7f) || c >= 0xa0) + *(*p)++ = c; + else { + *(*p)++ = '\\'; + *(*p)++ = '0' + (c >> 6); + *(*p)++ = '0' + ((c >> 3) & 7); + *(*p)++ = '0' + (c & 7); + } +} + +/** + * Construct the "pretty-printed" representation of the name in a short directory entry. + * + * @param[in] fixed Pointer to name[0] of a DIR_ENT + * + * @return Pointer to static string containing pretty "8.3" equivalent of the + * name in the directory entry. + */ +char *file_name(unsigned char *fixed) +{ + static char path[MSDOS_NAME * 4 + 2]; + char *p; + int i, j; + + p = path; + for (i = j = 0; i < 8; i++) + if (fixed[i] != ' ') { + while (j++ < i) + *p++ = ' '; + put_char(&p, fixed[i]); + } + if (strncmp((const char *)(fixed + 8), " ", 3)) { + *p++ = '.'; + for (i = j = 0; i < 3; i++) + if (fixed[i + 8] != ' ') { + while (j++ < i) + *p++ = ' '; + put_char(&p, fixed[i + 8]); + } + } + *p = 0; + return path; +} + +int file_cvt(unsigned char *name, unsigned char *fixed) +{ + unsigned char c; + int size, ext, cnt; + + size = 8; + ext = 0; + while (*name) { + c = *name; + if (c < ' ' || c > 0x7e || strchr("*?<>|\"/", c)) { + printf("Invalid character in name. Use \\ooo for special " + "characters.\n"); + return 0; + } + if (c == '.') { + if (ext) { + printf("Duplicate dots in name.\n"); + return 0; + } + while (size--) + *fixed++ = ' '; + size = 3; + ext = 1; + name++; + continue; + } + if (c == '\\') { + c = 0; + for (cnt = 3; cnt; cnt--) { + if (*name < '0' || *name > '7') { + printf("Invalid octal character.\n"); + return 0; + } + c = c * 8 + *name++ - '0'; + } + if (cnt < 4) { + printf("Expected three octal digits.\n"); + return 0; + } + name += 3; + } + if (islower(c)) + c = toupper(c); + if (size) { + *fixed++ = c; + size--; + } + name++; + } + if (*name || size == 8) + return 0; + if (!ext) { + while (size--) + *fixed++ = ' '; + size = 3; + } + while (size--) + *fixed++ = ' '; + return 1; +} + +void file_add(char *path, FD_TYPE type) +{ + FDSC **current, *walk; + char name[MSDOS_NAME]; + char *here; + + current = &fp_root; + if (*path != '/') + die("%s: Absolute path required.", path); + path++; + while (1) { + if ((here = strchr(path, '/'))) + *here = 0; + if (!file_cvt((unsigned char *)path, (unsigned char *)name)) + exit(2); + for (walk = *current; walk; walk = walk->next) + if (!here && (!strncmp(name, walk->name, MSDOS_NAME) || (type == + fdt_undelete + && + !strncmp + (name + 1, + walk->name + + 1, + MSDOS_NAME + - 1)))) + die("Ambiguous name: \"%s\"", path); + else if (here && !strncmp(name, walk->name, MSDOS_NAME)) + break; + if (!walk) { + walk = alloc(sizeof(FDSC)); + strncpy(walk->name, name, MSDOS_NAME); + walk->type = here ? fdt_none : type; + walk->first = NULL; + walk->next = *current; + *current = walk; + } + current = &walk->first; + if (!here) + break; + *here = '/'; + path = here + 1; + } +} + +FDSC **file_cd(FDSC ** curr, char *fixed) +{ + FDSC **walk; + + if (!curr || !*curr) + return NULL; + for (walk = curr; *walk; walk = &(*walk)->next) + if (!strncmp((*walk)->name, fixed, MSDOS_NAME) && (*walk)->first) + return &(*walk)->first; + return NULL; +} + +static FDSC **file_find(FDSC ** dir, char *fixed) +{ + if (!dir || !*dir) + return NULL; + if (*(unsigned char *)fixed == DELETED_FLAG) { + while (*dir) { + if (!strncmp((*dir)->name + 1, fixed + 1, MSDOS_NAME - 1) + && !(*dir)->first) + return dir; + dir = &(*dir)->next; + } + return NULL; + } + while (*dir) { + if (!strncmp((*dir)->name, fixed, MSDOS_NAME) && !(*dir)->first) + return dir; + dir = &(*dir)->next; + } + return NULL; +} + +/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no + such file exists or if CURR is NULL. */ +FD_TYPE file_type(FDSC ** curr, char *fixed) +{ + FDSC **this; + + if ((this = file_find(curr, fixed))) + return (*this)->type; + return fdt_none; +} + +void file_modify(FDSC ** curr, char *fixed) +{ + FDSC **this, *next; + + if (!(this = file_find(curr, fixed))) + die("Internal error: file_find failed"); + switch ((*this)->type) { + case fdt_drop: + printf("Dropping %s\n", file_name((unsigned char *)fixed)); + *(unsigned char *)fixed = DELETED_FLAG; + break; + case fdt_undelete: + *fixed = *(*this)->name; + printf("Undeleting %s\n", file_name((unsigned char *)fixed)); + break; + default: + die("Internal error: file_modify"); + } + next = (*this)->next; + free(*this); + *this = next; +} + +static void report_unused(FDSC * this) +{ + FDSC *next; + + while (this) { + next = this->next; + if (this->first) + report_unused(this->first); + else if (this->type != fdt_none) + printf("Warning: did not %s file %s\n", this->type == fdt_drop ? + "drop" : "undelete", file_name((unsigned char *)this->name)); + free(this); + this = next; + } +} + +void file_unused(void) +{ + report_unused(fp_root); +} diff --git a/dosfstools/src/file.h b/dosfstools/src/file.h new file mode 100644 index 000000000..40bd58a92 --- /dev/null +++ b/dosfstools/src/file.h @@ -0,0 +1,69 @@ +/* file.h - Additional file attributes + + Copyright (C) 1993 Werner Almesberger + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#ifndef _FILE_H +#define _FILE_H + +typedef enum { fdt_none, fdt_drop, fdt_undelete } FD_TYPE; + +typedef struct _fptr { + char name[MSDOS_NAME]; + FD_TYPE type; + struct _fptr *first; /* first entry */ + struct _fptr *next; /* next file in directory */ +} FDSC; + +extern FDSC *fp_root; + +char *file_name(unsigned char *fixed); + +/* Returns a pointer to a pretty-printed representation of a fixed MS-DOS file + name. */ + +int file_cvt(unsigned char *name, unsigned char *fixed); + +/* Converts a pretty-printed file name to the fixed MS-DOS format. Returns a + non-zero integer on success, zero on failure. */ + +void file_add(char *path, FD_TYPE type); + +/* Define special attributes for a path. TYPE can be either FDT_DROP or + FDT_UNDELETE. */ + +FDSC **file_cd(FDSC ** curr, char *fixed); + +/* Returns a pointer to the directory descriptor of the subdirectory FIXED of + CURR, or NULL if no such subdirectory exists. */ + +FD_TYPE file_type(FDSC ** curr, char *fixed); + +/* Returns the attribute of the file FIXED in directory CURR or FDT_NONE if no + such file exists or if CURR is NULL. */ + +void file_modify(FDSC ** curr, char *fixed); + +/* Performs the necessary operation on the entry of CURR that is named FIXED. */ + +void file_unused(void); + +/* Displays warnings for all unused file attributes. */ + +#endif diff --git a/dosfstools/src/io.c b/dosfstools/src/io.c new file mode 100644 index 000000000..a703c2db1 --- /dev/null +++ b/dosfstools/src/io.c @@ -0,0 +1,230 @@ +/* io.c - Virtual disk input/output + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* + * Thu Feb 26 01:15:36 CET 1998: Martin Schulze + * Fixed nasty bug that caused every file with a name like + * xxxxxxxx.xxx to be treated as bad name that needed to be fixed. + */ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#define _LARGEFILE64_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dosfsck.h" +#include "common.h" +#include "io.h" + +typedef struct _change { + void *data; + loff_t pos; + int size; + struct _change *next; +} CHANGE; + +static CHANGE *changes, *last; +static int fd, did_change = 0; + +unsigned device_no; + +#ifdef __DJGPP__ +#include "volume.h" /* DOS lowlevel disk access functions */ +loff_t llseek(int fd, loff_t offset, int whence) +{ + if ((whence != SEEK_SET) || (fd == 4711)) + return -1; /* only those supported */ + return VolumeSeek(offset); +} + +#define open OpenVolume +#define close CloseVolume +#define read(a,b,c) ReadVolume(b,c) +#define write(a,b,c) WriteVolume(b,c) +#else +loff_t llseek(int fd, loff_t offset, int whence) +{ + return (loff_t) lseek64(fd, (off64_t) offset, whence); +} +#endif + +void fs_open(char *path, int rw) +{ + struct stat stbuf; + + if ((fd = open(path, rw ? O_RDWR : O_RDONLY)) < 0) { + perror("open"); + exit(6); + } + changes = last = NULL; + did_change = 0; + +#ifndef _DJGPP_ + if (fstat(fd, &stbuf) < 0) + pdie("fstat %s", path); + device_no = S_ISBLK(stbuf.st_mode) ? (stbuf.st_rdev >> 8) & 0xff : 0; +#else + if (IsWorkingOnImageFile()) { + if (fstat(GetVolumeHandle(), &stbuf) < 0) + pdie("fstat image %s", path); + device_no = 0; + } else { + /* return 2 for floppy, 1 for ramdisk, 7 for loopback */ + /* used by boot.c in Atari mode: floppy always FAT12, */ + /* loopback / ramdisk only FAT12 if usual floppy size, */ + /* harddisk always FAT16 on Atari... */ + device_no = (GetVolumeHandle() < 2) ? 2 : 1; + /* telling "floppy" for A:/B:, "ramdisk" for the rest */ + } +#endif +} + +/** + * Read data from the partition, accounting for any pending updates that are + * queued for writing. + * + * @param[in] pos Byte offset, relative to the beginning of the partition, + * at which to read + * @param[in] size Number of bytes to read + * @param[out] data Where to put the data read + */ +void fs_read(loff_t pos, int size, void *data) +{ + CHANGE *walk; + int got; + + if (llseek(fd, pos, 0) != pos) + pdie("Seek to %lld", pos); + if ((got = read(fd, data, size)) < 0) + pdie("Read %d bytes at %lld", size, pos); + if (got != size) + die("Got %d bytes instead of %d at %lld", got, size, pos); + for (walk = changes; walk; walk = walk->next) { + if (walk->pos < pos + size && walk->pos + walk->size > pos) { + if (walk->pos < pos) + memcpy(data, (char *)walk->data + pos - walk->pos, min(size, + walk->size + - pos + + walk->pos)); + else + memcpy((char *)data + walk->pos - pos, walk->data, + min(walk->size, size + pos - walk->pos)); + } + } +} + +int fs_test(loff_t pos, int size) +{ + void *scratch; + int okay; + + if (llseek(fd, pos, 0) != pos) + pdie("Seek to %lld", pos); + scratch = alloc(size); + okay = read(fd, scratch, size) == size; + free(scratch); + return okay; +} + +void fs_write(loff_t pos, int size, void *data) +{ + CHANGE *new; + int did; + + if (write_immed) { + did_change = 1; + if (llseek(fd, pos, 0) != pos) + pdie("Seek to %lld", pos); + if ((did = write(fd, data, size)) == size) + return; + if (did < 0) + pdie("Write %d bytes at %lld", size, pos); + die("Wrote %d bytes instead of %d at %lld", did, size, pos); + } + new = alloc(sizeof(CHANGE)); + new->pos = pos; + memcpy(new->data = alloc(new->size = size), data, size); + new->next = NULL; + if (last) + last->next = new; + else + changes = new; + last = new; +} + +static void fs_flush(void) +{ + CHANGE *this; + int size; + + while (changes) { + this = changes; + changes = changes->next; + if (llseek(fd, this->pos, 0) != this->pos) + fprintf(stderr, + "Seek to %lld failed: %s\n Did not write %d bytes.\n", + (long long)this->pos, strerror(errno), this->size); + else if ((size = write(fd, this->data, this->size)) < 0) + fprintf(stderr, "Writing %d bytes at %lld failed: %s\n", this->size, + (long long)this->pos, strerror(errno)); + else if (size != this->size) + fprintf(stderr, "Wrote %d bytes instead of %d bytes at %lld." + "\n", size, this->size, (long long)this->pos); + free(this->data); + free(this); + } +} + +int fs_close(int write) +{ + CHANGE *next; + int changed; + + changed = ! !changes; + if (write) + fs_flush(); + else + while (changes) { + next = changes->next; + free(changes->data); + free(changes); + changes = next; + } + if (close(fd) < 0) + pdie("closing file system"); + return changed || did_change; +} + +int fs_changed(void) +{ + return ! !changes || did_change; +} diff --git a/dosfstools/src/io.h b/dosfstools/src/io.h new file mode 100644 index 000000000..2db4ea7ac --- /dev/null +++ b/dosfstools/src/io.h @@ -0,0 +1,70 @@ +/* io.h - Virtual disk input/output + + Copyright (C) 1993 Werner Almesberger + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* FAT32, VFAT, Atari format support, and various fixes additions May 1998 + * by Roman Hodek */ + +#ifndef _IO_H +#define _IO_H + +#include /* for loff_t */ + +loff_t llseek(int fd, loff_t offset, int whence); + +/* lseek() analogue for large offsets. */ + +void fs_open(char *path, int rw); + +/* Opens the file system PATH. If RW is zero, the file system is opened + read-only, otherwise, it is opened read-write. */ + +void fs_read(loff_t pos, int size, void *data); + +/* Reads SIZE bytes starting at POS into DATA. Performs all applicable + changes. */ + +int fs_test(loff_t pos, int size); + +/* Returns a non-zero integer if SIZE bytes starting at POS can be read without + errors. Otherwise, it returns zero. */ + +void fs_write(loff_t pos, int size, void *data); + +/* If write_immed is non-zero, SIZE bytes are written from DATA to the disk, + starting at POS. If write_immed is zero, the change is added to a list in + memory. */ + +int fs_close(int write); + +/* Closes the file system, performs all pending changes if WRITE is non-zero + and removes the list of changes. Returns a non-zero integer if the file + system has been changed since the last fs_open, zero otherwise. */ + +int fs_changed(void); + +/* Determines whether the file system has changed. See fs_close. */ + +extern unsigned device_no; + +/* Major number of device (0 if file) and size (in 512 byte sectors) */ + +#endif diff --git a/dosfstools/src/lfn.c b/dosfstools/src/lfn.c new file mode 100644 index 000000000..736491cb0 --- /dev/null +++ b/dosfstools/src/lfn.c @@ -0,0 +1,502 @@ +/* lfn.c - Functions for handling VFAT long filenames + + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#include +#include +#include +#include +#include + +#include "common.h" +#include "io.h" +#include "dosfsck.h" +#include "lfn.h" +#include "file.h" + +typedef struct { + __u8 id; /* sequence number for slot */ + __u8 name0_4[10]; /* first 5 characters in name */ + __u8 attr; /* attribute byte */ + __u8 reserved; /* always 0 */ + __u8 alias_checksum; /* checksum for 8.3 alias */ + __u8 name5_10[12]; /* 6 more characters in name */ + __u16 start; /* starting cluster number, 0 in long slots */ + __u8 name11_12[4]; /* last 2 characters in name */ +} LFN_ENT; + +#define LFN_ID_START 0x40 +#define LFN_ID_SLOTMASK 0x1f + +#define CHARS_PER_LFN 13 + +/* These modul-global vars represent the state of the LFN parser */ +unsigned char *lfn_unicode = NULL; +unsigned char lfn_checksum; +int lfn_slot = -1; +loff_t *lfn_offsets = NULL; +int lfn_parts = 0; + +static unsigned char fat_uni2esc[64] = { + '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', + 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', + 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', + 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', + 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', + 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z', '+', '-' +}; + +/* This defines which unicode chars are directly convertable to ISO-8859-1 */ +#define UNICODE_CONVERTABLE(cl,ch) (ch == 0 && (cl < 0x80 || cl >= 0xa0)) + +/* for maxlen param */ +#define UNTIL_0 INT_MAX + +/* Convert name part in 'lfn' from unicode to ASCII */ +#define CNV_THIS_PART(lfn) \ + ({ \ + unsigned char __part_uni[CHARS_PER_LFN*2]; \ + copy_lfn_part( __part_uni, lfn ); \ + cnv_unicode( __part_uni, CHARS_PER_LFN, 0 ); \ + }) + +/* Convert name parts collected so far (from previous slots) from unicode to + * ASCII */ +#define CNV_PARTS_SO_FAR() \ + (cnv_unicode( lfn_unicode+(lfn_slot*CHARS_PER_LFN*2), \ + lfn_parts*CHARS_PER_LFN, 0 )) + +/* This function converts an unicode string to a normal ASCII string, assuming + * ISO-8859-1 charset. Characters not in 8859-1 are converted to the same + * escape notation as used by the kernel, i.e. the uuencode-like ":xxx" */ +static char *cnv_unicode(const unsigned char *uni, int maxlen, int use_q) +{ + const unsigned char *up; + unsigned char *out, *cp; + int len, val; + + for (len = 0, up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]); + up += 2) { + if (UNICODE_CONVERTABLE(up[0], up[1])) + ++len; + else + len += 4; + } + cp = out = use_q ? qalloc(&mem_queue, len + 1) : alloc(len + 1); + + for (up = uni; (up - uni) / 2 < maxlen && (up[0] || up[1]); up += 2) { + if (UNICODE_CONVERTABLE(up[0], up[1])) + *cp++ = up[0]; + else { + /* here the same escape notation is used as in the Linux kernel */ + *cp++ = ':'; + val = (up[1] << 8) + up[0]; + cp[2] = fat_uni2esc[val & 0x3f]; + val >>= 6; + cp[1] = fat_uni2esc[val & 0x3f]; + val >>= 6; + cp[0] = fat_uni2esc[val & 0x3f]; + cp += 3; + } + } + *cp = 0; + + return (char *)out; +} + +static void copy_lfn_part(unsigned char *dst, LFN_ENT * lfn) +{ + memcpy(dst, lfn->name0_4, 10); + memcpy(dst + 10, lfn->name5_10, 12); + memcpy(dst + 22, lfn->name11_12, 4); +} + +static void clear_lfn_slots(int start, int end) +{ + int i; + LFN_ENT empty; + + /* New dir entry is zeroed except first byte, which is set to 0xe5. + * This is to avoid that some FAT-reading OSes (not Linux! ;) stop reading + * a directory at the first zero entry... + */ + memset(&empty, 0, sizeof(empty)); + empty.id = DELETED_FLAG; + + for (i = start; i <= end; ++i) { + fs_write(lfn_offsets[i], sizeof(LFN_ENT), &empty); + } +} + +void lfn_fix_checksum(loff_t from, loff_t to, const char *short_name) +{ + int i; + __u8 sum; + for (sum = 0, i = 0; i < 11; i++) + sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + short_name[i]; + + for (; from < to; from += sizeof(LFN_ENT)) { + fs_write(from + offsetof(LFN_ENT, alias_checksum), sizeof(sum), &sum); + } +} + +void lfn_reset(void) +{ + if (lfn_unicode) + free(lfn_unicode); + lfn_unicode = NULL; + if (lfn_offsets) + free(lfn_offsets); + lfn_offsets = NULL; + lfn_slot = -1; +} + +/* This function is only called with de->attr == VFAT_LN_ATTR. It stores part + * of the long name. */ +void lfn_add_slot(DIR_ENT * de, loff_t dir_offset) +{ + LFN_ENT *lfn = (LFN_ENT *) de; + int slot = lfn->id & LFN_ID_SLOTMASK; + unsigned offset; + + if (lfn_slot == 0) + lfn_check_orphaned(); + + if (de->attr != VFAT_LN_ATTR) + die("lfn_add_slot called with non-LFN directory entry"); + + if (lfn->id & LFN_ID_START && slot != 0) { + if (lfn_slot != -1) { + int can_clear = 0; + /* There is already a LFN "in progess", so it is an error that a + * new start entry is here. */ + /* Causes: 1) if slot# == expected: start bit set mysteriously, 2) + * old LFN overwritten by new one */ + /* Fixes: 1) delete previous LFN 2) if slot# == expected and + * checksum ok: clear start bit */ + /* XXX: Should delay that until next LFN known (then can better + * display the name) */ + printf("A new long file name starts within an old one.\n"); + if (slot == lfn_slot && lfn->alias_checksum == lfn_checksum) { + char *part1 = CNV_THIS_PART(lfn); + char *part2 = CNV_PARTS_SO_FAR(); + printf(" It could be that the LFN start bit is wrong here\n" + " if \"%s\" seems to match \"%s\".\n", part1, part2); + free(part1); + free(part2); + can_clear = 1; + } + if (interactive) { + printf("1: Delete previous LFN\n2: Leave it as it is.\n"); + if (can_clear) + printf("3: Clear start bit and concatenate LFNs\n"); + } else + printf(" Not auto-correcting this.\n"); + if (interactive) { + switch (get_key(can_clear ? "123" : "12", "?")) { + case '1': + clear_lfn_slots(0, lfn_parts - 1); + lfn_reset(); + break; + case '2': + break; + case '3': + lfn->id &= ~LFN_ID_START; + fs_write(dir_offset + offsetof(LFN_ENT, id), + sizeof(lfn->id), &lfn->id); + break; + } + } + } + lfn_slot = slot; + lfn_checksum = lfn->alias_checksum; + lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2); + lfn_offsets = alloc(lfn_slot * sizeof(loff_t)); + lfn_parts = 0; + } else if (lfn_slot == -1 && slot != 0) { + /* No LFN in progress, but slot found; start bit missing */ + /* Causes: 1) start bit got lost, 2) Previous slot with start bit got + * lost */ + /* Fixes: 1) delete LFN, 2) set start bit */ + char *part = CNV_THIS_PART(lfn); + printf("Long filename fragment \"%s\" found outside a LFN " + "sequence.\n (Maybe the start bit is missing on the " + "last fragment)\n", part); + if (interactive) { + printf("1: Delete fragment\n2: Leave it as it is.\n" + "3: Set start bit\n"); + } else + printf(" Not auto-correcting this.\n"); + switch (interactive ? get_key("123", "?") : '2') { + case '1': + if (!lfn_offsets) + lfn_offsets = alloc(sizeof(loff_t)); + lfn_offsets[0] = dir_offset; + clear_lfn_slots(0, 0); + lfn_reset(); + return; + case '2': + lfn_reset(); + return; + case '3': + lfn->id |= LFN_ID_START; + fs_write(dir_offset + offsetof(LFN_ENT, id), + sizeof(lfn->id), &lfn->id); + lfn_slot = slot; + lfn_checksum = lfn->alias_checksum; + lfn_unicode = alloc((lfn_slot * CHARS_PER_LFN + 1) * 2); + lfn_offsets = alloc(lfn_slot * sizeof(loff_t)); + lfn_parts = 0; + break; + } + } else if (slot != lfn_slot) { + /* wrong sequence number */ + /* Causes: 1) seq-no destroyed */ + /* Fixes: 1) delete LFN, 2) fix number (maybe only if following parts + * are ok?, maybe only if checksum is ok?) (Attention: space + * for name was allocated before!) */ + int can_fix = 0; + printf("Unexpected long filename sequence number " + "(%d vs. expected %d).\n", slot, lfn_slot); + if (lfn->alias_checksum == lfn_checksum && lfn_slot > 0) { + char *part1 = CNV_THIS_PART(lfn); + char *part2 = CNV_PARTS_SO_FAR(); + printf(" It could be that just the number is wrong\n" + " if \"%s\" seems to match \"%s\".\n", part1, part2); + free(part1); + free(part2); + can_fix = 1; + } + if (interactive) { + printf + ("1: Delete LFN\n2: Leave it as it is (and ignore LFN so far)\n"); + if (can_fix) + printf("3: Correct sequence number\n"); + } else + printf(" Not auto-correcting this.\n"); + switch (interactive ? get_key(can_fix ? "123" : "12", "?") : '2') { + case '1': + if (!lfn_offsets) { + lfn_offsets = alloc(sizeof(loff_t)); + lfn_parts = 0; + } + lfn_offsets[lfn_parts++] = dir_offset; + clear_lfn_slots(0, lfn_parts - 1); + lfn_reset(); + return; + case '2': + lfn_reset(); + return; + case '3': + lfn->id = (lfn->id & ~LFN_ID_SLOTMASK) | lfn_slot; + fs_write(dir_offset + offsetof(LFN_ENT, id), + sizeof(lfn->id), &lfn->id); + break; + } + } + + if (lfn->alias_checksum != lfn_checksum) { + /* checksum mismatch */ + /* Causes: 1) checksum field here destroyed */ + /* Fixes: 1) delete LFN, 2) fix checksum */ + printf("Checksum in long filename part wrong " + "(%02x vs. expected %02x).\n", + lfn->alias_checksum, lfn_checksum); + if (interactive) { + printf("1: Delete LFN\n2: Leave it as it is.\n" + "3: Correct checksum\n"); + } else + printf(" Not auto-correcting this.\n"); + if (interactive) { + switch (get_key("123", "?")) { + case '1': + lfn_offsets[lfn_parts++] = dir_offset; + clear_lfn_slots(0, lfn_parts - 1); + lfn_reset(); + return; + case '2': + break; + case '3': + lfn->alias_checksum = lfn_checksum; + fs_write(dir_offset + offsetof(LFN_ENT, alias_checksum), + sizeof(lfn->alias_checksum), &lfn->alias_checksum); + break; + } + } + } + + if (lfn_slot != -1) { + lfn_slot--; + offset = lfn_slot * CHARS_PER_LFN * 2; + copy_lfn_part(lfn_unicode + offset, lfn); + if (lfn->id & LFN_ID_START) + lfn_unicode[offset + 26] = lfn_unicode[offset + 27] = 0; + lfn_offsets[lfn_parts++] = dir_offset; + } + + if (lfn->reserved != 0) { + printf("Reserved field in VFAT long filename slot is not 0 " + "(but 0x%02x).\n", lfn->reserved); + if (interactive) + printf("1: Fix.\n2: Leave it.\n"); + else + printf("Auto-setting to 0.\n"); + if (!interactive || get_key("12", "?") == '1') { + lfn->reserved = 0; + fs_write(dir_offset + offsetof(LFN_ENT, reserved), + sizeof(lfn->reserved), &lfn->reserved); + } + } + if (lfn->start != CT_LE_W(0)) { + printf("Start cluster field in VFAT long filename slot is not 0 " + "(but 0x%04x).\n", lfn->start); + if (interactive) + printf("1: Fix.\n2: Leave it.\n"); + else + printf("Auto-setting to 0.\n"); + if (!interactive || get_key("12", "?") == '1') { + lfn->start = CT_LE_W(0); + fs_write(dir_offset + offsetof(LFN_ENT, start), + sizeof(lfn->start), &lfn->start); + } + } +} + +/* This function is always called when de->attr != VFAT_LN_ATTR is found, to + * retrieve the previously constructed LFN. */ +char *lfn_get(DIR_ENT * de, loff_t * lfn_offset) +{ + char *lfn; + __u8 sum; + int i; + + *lfn_offset = 0; + if (de->attr == VFAT_LN_ATTR) + die("lfn_get called with LFN directory entry"); + +#if 0 + if (de->lcase) + printf("lcase=%02x\n", de->lcase); +#endif + + if (lfn_slot == -1) + /* no long name for this file */ + return NULL; + + if (lfn_slot != 0) { + /* The long name isn't finished yet. */ + /* Causes: 1) LFN slot overwritten by non-VFAT aware tool */ + /* Fixes: 1) delete LFN 2) move overwriting entry to somewhere else + * and let user enter missing part of LFN (hard to do :-() + * 3) renumber entries and truncate name */ + char *long_name = CNV_PARTS_SO_FAR(); + char *short_name = file_name(de->name); + printf("Unfinished long file name \"%s\".\n" + " (Start may have been overwritten by %s)\n", + long_name, short_name); + free(long_name); + if (interactive) { + printf("1: Delete LFN\n2: Leave it as it is.\n" + "3: Fix numbering (truncates long name and attaches " + "it to short name %s)\n", short_name); + } else + printf(" Not auto-correcting this.\n"); + switch (interactive ? get_key("123", "?") : '2') { + case '1': + clear_lfn_slots(0, lfn_parts - 1); + lfn_reset(); + return NULL; + case '2': + lfn_reset(); + return NULL; + case '3': + for (i = 0; i < lfn_parts; ++i) { + __u8 id = (lfn_parts - i) | (i == 0 ? LFN_ID_START : 0); + fs_write(lfn_offsets[i] + offsetof(LFN_ENT, id), + sizeof(id), &id); + } + memmove(lfn_unicode, lfn_unicode + lfn_slot * CHARS_PER_LFN * 2, + lfn_parts * CHARS_PER_LFN * 2); + break; + } + } + + for (sum = 0, i = 0; i < 11; i++) + sum = (((sum & 1) << 7) | ((sum & 0xfe) >> 1)) + de->name[i]; + if (sum != lfn_checksum) { + /* checksum doesn't match, long name doesn't apply to this alias */ + /* Causes: 1) alias renamed */ + /* Fixes: 1) Fix checksum in LFN entries */ + char *long_name = CNV_PARTS_SO_FAR(); + char *short_name = file_name(de->name); + printf("Wrong checksum for long file name \"%s\".\n" + " (Short name %s may have changed without updating the long name)\n", + long_name, short_name); + free(long_name); + if (interactive) { + printf("1: Delete LFN\n2: Leave it as it is.\n" + "3: Fix checksum (attaches to short name %s)\n", short_name); + } else + printf(" Not auto-correcting this.\n"); + if (interactive) { + switch (get_key("123", "?")) { + case '1': + clear_lfn_slots(0, lfn_parts - 1); + lfn_reset(); + return NULL; + case '2': + lfn_reset(); + return NULL; + case '3': + for (i = 0; i < lfn_parts; ++i) { + fs_write(lfn_offsets[i] + offsetof(LFN_ENT, alias_checksum), + sizeof(sum), &sum); + } + break; + } + } + } + + *lfn_offset = lfn_offsets[0]; + lfn = cnv_unicode(lfn_unicode, UNTIL_0, 1); + lfn_reset(); + return (lfn); +} + +void lfn_check_orphaned(void) +{ + char *long_name; + + if (lfn_slot == -1) + return; + + long_name = CNV_PARTS_SO_FAR(); + printf("Orphaned long file name part \"%s\"\n", long_name); + if (interactive) + printf("1: Delete.\n2: Leave it.\n"); + else + printf(" Auto-deleting.\n"); + if (!interactive || get_key("12", "?") == '1') { + clear_lfn_slots(0, lfn_parts - 1); + } + lfn_reset(); +} diff --git a/dosfstools/src/lfn.h b/dosfstools/src/lfn.h new file mode 100644 index 000000000..2ea44a725 --- /dev/null +++ b/dosfstools/src/lfn.h @@ -0,0 +1,38 @@ +/* lfn.h - Functions for handling VFAT long filenames + + Copyright (C) 1998 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#ifndef _LFN_H +#define _LFN_H + +void lfn_reset(void); +/* Reset the state of the LFN parser. */ + +void lfn_add_slot(DIR_ENT * de, loff_t dir_offset); +/* Process a dir slot that is a VFAT LFN entry. */ + +char *lfn_get(DIR_ENT * de, loff_t * lfn_offset); +/* Retrieve the long name for the proper dir entry. */ + +void lfn_check_orphaned(void); + +void lfn_fix_checksum(loff_t from, loff_t to, const char *short_name); + +#endif diff --git a/dosfstools/src/mkdosfs.c b/dosfstools/src/mkdosfs.c new file mode 100644 index 000000000..9456ccd3a --- /dev/null +++ b/dosfstools/src/mkdosfs.c @@ -0,0 +1,1733 @@ +/* mkdosfs.c - utility to create FAT/MS-DOS filesystems + + Copyright (C) 1991 Linus Torvalds + Copyright (C) 1992-1993 Remy Card + Copyright (C) 1993-1994 David Hudson + Copyright (C) 1998 H. Peter Anvin + Copyright (C) 1998-2005 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +/* Description: Utility to allow an MS-DOS filesystem to be created + under Linux. A lot of the basic structure of this program has been + borrowed from Remy Card's "mke2fs" code. + + As far as possible the aim here is to make the "mkdosfs" command + look almost identical to the other Linux filesystem make utilties, + eg bad blocks are still specified as blocks, not sectors, but when + it comes down to it, DOS is tied to the idea of a sector (512 bytes + as a rule), and not the block. For example the boot block does not + occupy a full cluster. + + Fixes/additions May 1998 by Roman Hodek + : + - Atari format support + - New options -A, -S, -C + - Support for filesystems > 2GB + - FAT32 support */ + +/* Include the header files */ + +#include "version.h" + +#include +#include +#if defined(_USING_BIONIC_) +#include +#else +#include +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#if __BYTE_ORDER == __BIG_ENDIAN + +#include +#ifdef __le16_to_cpu +/* ++roman: 2.1 kernel headers define these function, they're probably more + * efficient then coding the swaps machine-independently. */ +#define CF_LE_W __le16_to_cpu +#define CF_LE_L __le32_to_cpu +#define CT_LE_W __cpu_to_le16 +#define CT_LE_L __cpu_to_le32 +#else +#define CF_LE_W(v) ((((v) & 0xff) << 8) | (((v) >> 8) & 0xff)) +#define CF_LE_L(v) (((unsigned)(v)>>24) | (((unsigned)(v)>>8)&0xff00) | \ + (((unsigned)(v)<<8)&0xff0000) | ((unsigned)(v)<<24)) +#define CT_LE_W(v) CF_LE_W(v) +#define CT_LE_L(v) CF_LE_L(v) +#endif /* defined(__le16_to_cpu) */ + +#else + +#define CF_LE_W(v) (v) +#define CF_LE_L(v) (v) +#define CT_LE_W(v) (v) +#define CT_LE_L(v) (v) + +#endif /* __BIG_ENDIAN */ + +/* In earlier versions, an own llseek() was used, but glibc lseek() is + * sufficient (or even better :) for 64 bit offsets in the meantime */ +#define llseek lseek + +/* Constant definitions */ + +#define TRUE 1 /* Boolean constants */ +#define FALSE 0 + +#define TEST_BUFFER_BLOCKS 16 +#define HARD_SECTOR_SIZE 512 +#define SECTORS_PER_BLOCK ( BLOCK_SIZE / HARD_SECTOR_SIZE ) + +/* Macro definitions */ + +/* Report a failure message and return a failure error code */ + +#define die( str ) fatal_error( "%s: " str "\n" ) + +/* Mark a cluster in the FAT as bad */ + +#define mark_sector_bad( sector ) mark_FAT_sector( sector, FAT_BAD ) + +/* Compute ceil(a/b) */ + +inline int cdiv(int a, int b) +{ + return (a + b - 1) / b; +} + +/* MS-DOS filesystem structures -- I included them here instead of + including linux/msdos_fs.h since that doesn't include some fields we + need */ + +#define ATTR_RO 1 /* read-only */ +#define ATTR_HIDDEN 2 /* hidden */ +#define ATTR_SYS 4 /* system */ +#define ATTR_VOLUME 8 /* volume label */ +#define ATTR_DIR 16 /* directory */ +#define ATTR_ARCH 32 /* archived */ + +#define ATTR_NONE 0 /* no attribute bits */ +#define ATTR_UNUSED (ATTR_VOLUME | ATTR_ARCH | ATTR_SYS | ATTR_HIDDEN) + /* attribute bits that are copied "as is" */ + +/* FAT values */ +#define FAT_EOF (atari_format ? 0x0fffffff : 0x0ffffff8) +#define FAT_BAD 0x0ffffff7 + +#define MSDOS_EXT_SIGN 0x29 /* extended boot sector signature */ +#define MSDOS_FAT12_SIGN "FAT12 " /* FAT12 filesystem signature */ +#define MSDOS_FAT16_SIGN "FAT16 " /* FAT16 filesystem signature */ +#define MSDOS_FAT32_SIGN "FAT32 " /* FAT32 filesystem signature */ + +#define BOOT_SIGN 0xAA55 /* Boot sector magic number */ + +#define MAX_CLUST_12 ((1 << 12) - 16) +#define MAX_CLUST_16 ((1 << 16) - 16) +#define MIN_CLUST_32 65529 +/* M$ says the high 4 bits of a FAT32 FAT entry are reserved and don't belong + * to the cluster number. So the max. cluster# is based on 2^28 */ +#define MAX_CLUST_32 ((1 << 28) - 16) + +#define FAT12_THRESHOLD 4085 + +#define OLDGEMDOS_MAX_SECTORS 32765 +#define GEMDOS_MAX_SECTORS 65531 +#define GEMDOS_MAX_SECTOR_SIZE (16*1024) + +#define BOOTCODE_SIZE 448 +#define BOOTCODE_FAT32_SIZE 420 + +/* __attribute__ ((packed)) is used on all structures to make gcc ignore any + * alignments */ + +struct msdos_volume_info { + __u8 drive_number; /* BIOS drive number */ + __u8 RESERVED; /* Unused */ + __u8 ext_boot_sign; /* 0x29 if fields below exist (DOS 3.3+) */ + __u8 volume_id[4]; /* Volume ID number */ + __u8 volume_label[11]; /* Volume label */ + __u8 fs_type[8]; /* Typically FAT12 or FAT16 */ +} __attribute__ ((packed)); + +struct msdos_boot_sector { + __u8 boot_jump[3]; /* Boot strap short or near jump */ + __u8 system_id[8]; /* Name - can be used to special case + partition manager volumes */ + __u8 sector_size[2]; /* bytes per logical sector */ + __u8 cluster_size; /* sectors/cluster */ + __u16 reserved; /* reserved sectors */ + __u8 fats; /* number of FATs */ + __u8 dir_entries[2]; /* root directory entries */ + __u8 sectors[2]; /* number of sectors */ + __u8 media; /* media code (unused) */ + __u16 fat_length; /* sectors/FAT */ + __u16 secs_track; /* sectors per track */ + __u16 heads; /* number of heads */ + __u32 hidden; /* hidden sectors (unused) */ + __u32 total_sect; /* number of sectors (if sectors == 0) */ + union { + struct { + struct msdos_volume_info vi; + __u8 boot_code[BOOTCODE_SIZE]; + } __attribute__ ((packed)) _oldfat; + struct { + __u32 fat32_length; /* sectors/FAT */ + __u16 flags; /* bit 8: fat mirroring, low 4: active fat */ + __u8 version[2]; /* major, minor filesystem version */ + __u32 root_cluster; /* first cluster in root directory */ + __u16 info_sector; /* filesystem info sector */ + __u16 backup_boot; /* backup boot sector */ + __u16 reserved2[6]; /* Unused */ + struct msdos_volume_info vi; + __u8 boot_code[BOOTCODE_FAT32_SIZE]; + } __attribute__ ((packed)) _fat32; + } __attribute__ ((packed)) fstype; + __u16 boot_sign; +} __attribute__ ((packed)); +#define fat32 fstype._fat32 +#define oldfat fstype._oldfat + +struct fat32_fsinfo { + __u32 reserved1; /* Nothing as far as I can tell */ + __u32 signature; /* 0x61417272L */ + __u32 free_clusters; /* Free cluster count. -1 if unknown */ + __u32 next_cluster; /* Most recently allocated cluster. + * Unused under Linux. */ + __u32 reserved2[4]; +}; + +struct msdos_dir_entry { + char name[8], ext[3]; /* name and extension */ + __u8 attr; /* attribute bits */ + __u8 lcase; /* Case for base and extension */ + __u8 ctime_ms; /* Creation time, milliseconds */ + __u16 ctime; /* Creation time */ + __u16 cdate; /* Creation date */ + __u16 adate; /* Last access date */ + __u16 starthi; /* high 16 bits of first cl. (FAT32) */ + __u16 time, date, start; /* time, date and first cluster */ + __u32 size; /* file size (in bytes) */ +} __attribute__ ((packed)); + +/* The "boot code" we put into the filesystem... it writes a message and + tells the user to try again */ + +char dummy_boot_jump[3] = { 0xeb, 0x3c, 0x90 }; + +char dummy_boot_jump_m68k[2] = { 0x60, 0x1c }; + +#define MSG_OFFSET_OFFSET 3 +char dummy_boot_code[BOOTCODE_SIZE] = "\x0e" /* push cs */ + "\x1f" /* pop ds */ + "\xbe\x5b\x7c" /* mov si, offset message_txt */ + /* write_msg: */ + "\xac" /* lodsb */ + "\x22\xc0" /* and al, al */ + "\x74\x0b" /* jz key_press */ + "\x56" /* push si */ + "\xb4\x0e" /* mov ah, 0eh */ + "\xbb\x07\x00" /* mov bx, 0007h */ + "\xcd\x10" /* int 10h */ + "\x5e" /* pop si */ + "\xeb\xf0" /* jmp write_msg */ + /* key_press: */ + "\x32\xe4" /* xor ah, ah */ + "\xcd\x16" /* int 16h */ + "\xcd\x19" /* int 19h */ + "\xeb\xfe" /* foo: jmp foo */ + /* message_txt: */ + "This is not a bootable disk. Please insert a bootable floppy and\r\n" + "press any key to try again ... \r\n"; + +#define MESSAGE_OFFSET 29 /* Offset of message in above code */ + +/* Global variables - the root of all evil :-) - see these and weep! */ + +static char *program_name = "mkdosfs"; /* Name of the program */ +static char *device_name = NULL; /* Name of the device on which to create the filesystem */ +static int atari_format = 0; /* Use Atari variation of MS-DOS FS format */ +static int check = FALSE; /* Default to no readablity checking */ +static int verbose = 0; /* Default to verbose mode off */ +static long volume_id; /* Volume ID number */ +static time_t create_time; /* Creation time */ +static struct timeval create_timeval; /* Creation time */ +static char volume_name[] = " "; /* Volume name */ +static unsigned long long blocks; /* Number of blocks in filesystem */ +static int sector_size = 512; /* Size of a logical sector */ +static int sector_size_set = 0; /* User selected sector size */ +static int backup_boot = 0; /* Sector# of backup boot sector */ +static int reserved_sectors = 0; /* Number of reserved sectors */ +static int badblocks = 0; /* Number of bad blocks in the filesystem */ +static int nr_fats = 2; /* Default number of FATs to produce */ +static int size_fat = 0; /* Size in bits of FAT entries */ +static int size_fat_by_user = 0; /* 1 if FAT size user selected */ +static int dev = -1; /* FS block device file handle */ +static int ignore_full_disk = 0; /* Ignore warning about 'full' disk devices */ +static off_t currently_testing = 0; /* Block currently being tested (if autodetect bad blocks) */ +static struct msdos_boot_sector bs; /* Boot sector data */ +static int start_data_sector; /* Sector number for the start of the data area */ +static int start_data_block; /* Block number for the start of the data area */ +static unsigned char *fat; /* File allocation table */ +static unsigned alloced_fat_length; /* # of FAT sectors we can keep in memory */ +static unsigned char *info_sector; /* FAT32 info sector */ +static struct msdos_dir_entry *root_dir; /* Root directory */ +static int size_root_dir; /* Size of the root directory in bytes */ +static int sectors_per_cluster = 0; /* Number of sectors per disk cluster */ +static int root_dir_entries = 0; /* Number of root directory entries */ +static char *blank_sector; /* Blank sector - all zeros */ +static int hidden_sectors = 0; /* Number of hidden sectors */ +static int malloc_entire_fat = FALSE; /* Whether we should malloc() the entire FAT or not */ +static int align_structures = TRUE; /* Whether to enforce alignment */ +static int orphaned_sectors = 0; /* Sectors that exist in the last block of filesystem */ + +/* Function prototype definitions */ + +static void fatal_error(const char *fmt_string) __attribute__ ((noreturn)); +static void mark_FAT_cluster(int cluster, unsigned int value); +static void mark_FAT_sector(int sector, unsigned int value); +static long do_check(char *buffer, int try, off_t current_block); +static void alarm_intr(int alnum); +static void check_blocks(void); +static void get_list_blocks(char *filename); +static int valid_offset(int fd, loff_t offset); +static unsigned long long count_blocks(char *filename, int *remainder); +static void check_mount(char *device_name); +static void establish_params(int device_num, int size); +static void setup_tables(void); +static void write_tables(void); + +/* The function implementations */ + +/* Handle the reporting of fatal errors. Volatile to let gcc know that this doesn't return */ + +static void fatal_error(const char *fmt_string) +{ + fprintf(stderr, fmt_string, program_name, device_name); + exit(1); /* The error exit code is 1! */ +} + +/* Mark the specified cluster as having a particular value */ + +static void mark_FAT_cluster(int cluster, unsigned int value) +{ + switch (size_fat) { + case 12: + value &= 0x0fff; + if (((cluster * 3) & 0x1) == 0) { + fat[3 * cluster / 2] = (unsigned char)(value & 0x00ff); + fat[(3 * cluster / 2) + 1] = + (unsigned char)((fat[(3 * cluster / 2) + 1] & 0x00f0) + | ((value & 0x0f00) >> 8)); + } else { + fat[3 * cluster / 2] = + (unsigned char)((fat[3 * cluster / 2] & 0x000f) | + ((value & 0x000f) << 4)); + fat[(3 * cluster / 2) + 1] = (unsigned char)((value & 0x0ff0) >> 4); + } + break; + + case 16: + value &= 0xffff; + fat[2 * cluster] = (unsigned char)(value & 0x00ff); + fat[(2 * cluster) + 1] = (unsigned char)(value >> 8); + break; + + case 32: + value &= 0xfffffff; + fat[4 * cluster] = (unsigned char)(value & 0x000000ff); + fat[(4 * cluster) + 1] = (unsigned char)((value & 0x0000ff00) >> 8); + fat[(4 * cluster) + 2] = (unsigned char)((value & 0x00ff0000) >> 16); + fat[(4 * cluster) + 3] = (unsigned char)((value & 0xff000000) >> 24); + break; + + default: + die("Bad FAT size (not 12, 16, or 32)"); + } +} + +/* Mark a specified sector as having a particular value in it's FAT entry */ + +static void mark_FAT_sector(int sector, unsigned int value) +{ + int cluster; + + cluster = (sector - start_data_sector) / (int)(bs.cluster_size) / + (sector_size / HARD_SECTOR_SIZE); + if (cluster < 0) + die("Invalid cluster number in mark_FAT_sector: probably bug!"); + + mark_FAT_cluster(cluster, value); +} + +/* Perform a test on a block. Return the number of blocks that could be read successfully */ + +static long do_check(char *buffer, int try, off_t current_block) +{ + long got; + + if (llseek(dev, current_block * BLOCK_SIZE, SEEK_SET) /* Seek to the correct location */ + !=current_block * BLOCK_SIZE) + die("seek failed during testing for blocks"); + + got = read(dev, buffer, try * BLOCK_SIZE); /* Try reading! */ + if (got < 0) + got = 0; + + if (got & (BLOCK_SIZE - 1)) + printf("Unexpected values in do_check: probably bugs\n"); + got /= BLOCK_SIZE; + + return got; +} + +/* Alarm clock handler - display the status of the quest for bad blocks! Then retrigger the alarm for five senconds + later (so we can come here again) */ + +static void alarm_intr(int alnum) +{ + if (currently_testing >= blocks) + return; + + signal(SIGALRM, alarm_intr); + alarm(5); + if (!currently_testing) + return; + + printf("%lld... ", (unsigned long long)currently_testing); + fflush(stdout); +} + +static void check_blocks(void) +{ + int try, got; + int i; + static char blkbuf[BLOCK_SIZE * TEST_BUFFER_BLOCKS]; + + if (verbose) { + printf("Searching for bad blocks "); + fflush(stdout); + } + currently_testing = 0; + if (verbose) { + signal(SIGALRM, alarm_intr); + alarm(5); + } + try = TEST_BUFFER_BLOCKS; + while (currently_testing < blocks) { + if (currently_testing + try > blocks) + try = blocks - currently_testing; + got = do_check(blkbuf, try, currently_testing); + currently_testing += got; + if (got == try) { + try = TEST_BUFFER_BLOCKS; + continue; + } else + try = 1; + if (currently_testing < start_data_block) + die("bad blocks before data-area: cannot make fs"); + + for (i = 0; i < SECTORS_PER_BLOCK; i++) /* Mark all of the sectors in the block as bad */ + mark_sector_bad(currently_testing * SECTORS_PER_BLOCK + i); + badblocks++; + currently_testing++; + } + + if (verbose) + printf("\n"); + + if (badblocks) + printf("%d bad block%s\n", badblocks, (badblocks > 1) ? "s" : ""); +} + +static void get_list_blocks(char *filename) +{ + int i; + FILE *listfile; + unsigned long blockno; + + listfile = fopen(filename, "r"); + if (listfile == (FILE *) NULL) + die("Can't open file of bad blocks"); + + while (!feof(listfile)) { + fscanf(listfile, "%ld\n", &blockno); + for (i = 0; i < SECTORS_PER_BLOCK; i++) /* Mark all of the sectors in the block as bad */ + mark_sector_bad(blockno * SECTORS_PER_BLOCK + i); + badblocks++; + } + fclose(listfile); + + if (badblocks) + printf("%d bad block%s\n", badblocks, (badblocks > 1) ? "s" : ""); +} + +/* Given a file descriptor and an offset, check whether the offset is a valid offset for the file - return FALSE if it + isn't valid or TRUE if it is */ + +static int valid_offset(int fd, loff_t offset) +{ + char ch; + + if (llseek(fd, offset, SEEK_SET) < 0) + return FALSE; + if (read(fd, &ch, 1) < 1) + return FALSE; + return TRUE; +} + +/* Given a filename, look to see how many blocks of BLOCK_SIZE are present, returning the answer */ + +static unsigned long long count_blocks(char *filename, int *remainder) + +{ + loff_t high, low; + int fd; + + if ((fd = open(filename, O_RDONLY)) < 0) { + perror(filename); + exit(1); + } + + /* first try SEEK_END, which should work on most devices nowadays */ + if ((low = llseek(fd, 0, SEEK_END)) <= 0) { + low = 0; + for (high = 1; valid_offset(fd, high); high *= 2) + low = high; + while (low < high - 1) { + const loff_t mid = (low + high) / 2; + if (valid_offset(fd, mid)) + low = mid; + else + high = mid; + } + ++low; + } + + close(fd); + *remainder = (low%BLOCK_SIZE)/sector_size; + return(low / BLOCK_SIZE); +} + +/* Check to see if the specified device is currently mounted - abort if it is */ + +static void check_mount(char *device_name) +{ +#if ! defined(_USING_BIONIC_) + FILE *f; + struct mntent *mnt; + + if ((f = setmntent(MOUNTED, "r")) == NULL) + return; + while ((mnt = getmntent(f)) != NULL) + if (strcmp(device_name, mnt->mnt_fsname) == 0) + die("%s contains a mounted file system."); + endmntent(f); +#endif +} + +/* Establish the geometry and media parameters for the device */ + +static void establish_params(int device_num, int size) +{ + long loop_size; + struct hd_geometry geometry; + struct floppy_struct param; + int def_root_dir_entries = 512; + + if ((0 == device_num) || ((device_num & 0xff00) == 0x0200)) + /* file image or floppy disk */ + { + if (0 == device_num) { + param.size = size / 512; + switch (param.size) { + case 720: + param.sect = 9; + param.head = 2; + break; + case 1440: + param.sect = 9; + param.head = 2; + break; + case 2400: + param.sect = 15; + param.head = 2; + break; + case 2880: + param.sect = 18; + param.head = 2; + break; + case 5760: + param.sect = 36; + param.head = 2; + break; + default: + /* fake values */ + param.sect = 32; + param.head = 64; + break; + } + + } else { /* is a floppy diskette */ + + if (ioctl(dev, FDGETPRM, ¶m)) /* Can we get the diskette geometry? */ + die("unable to get diskette geometry for '%s'"); + } + bs.secs_track = CT_LE_W(param.sect); /* Set up the geometry information */ + bs.heads = CT_LE_W(param.head); + switch (param.size) { /* Set up the media descriptor byte */ + case 720: /* 5.25", 2, 9, 40 - 360K */ + bs.media = (char)0xfd; + bs.cluster_size = (char)2; + def_root_dir_entries = 112; + break; + + case 1440: /* 3.5", 2, 9, 80 - 720K */ + bs.media = (char)0xf9; + bs.cluster_size = (char)2; + def_root_dir_entries = 112; + break; + + case 2400: /* 5.25", 2, 15, 80 - 1200K */ + bs.media = (char)0xf9; + bs.cluster_size = (char)(atari_format ? 2 : 1); + def_root_dir_entries = 224; + break; + + case 5760: /* 3.5", 2, 36, 80 - 2880K */ + bs.media = (char)0xf0; + bs.cluster_size = (char)2; + def_root_dir_entries = 224; + break; + + case 2880: /* 3.5", 2, 18, 80 - 1440K */ +floppy_default: + bs.media = (char)0xf0; + bs.cluster_size = (char)(atari_format ? 2 : 1); + def_root_dir_entries = 224; + break; + + default: /* Anything else */ + if (0 == device_num) + goto def_hd_params; + else + goto floppy_default; + } + } else if ((device_num & 0xff00) == 0x0700) { /* This is a loop device */ + if (ioctl(dev, BLKGETSIZE, &loop_size)) + die("unable to get loop device size"); + + switch (loop_size) { /* Assuming the loop device -> floppy later */ + case 720: /* 5.25", 2, 9, 40 - 360K */ + bs.secs_track = CF_LE_W(9); + bs.heads = CF_LE_W(2); + bs.media = (char)0xfd; + bs.cluster_size = (char)2; + def_root_dir_entries = 112; + break; + + case 1440: /* 3.5", 2, 9, 80 - 720K */ + bs.secs_track = CF_LE_W(9); + bs.heads = CF_LE_W(2); + bs.media = (char)0xf9; + bs.cluster_size = (char)2; + def_root_dir_entries = 112; + break; + + case 2400: /* 5.25", 2, 15, 80 - 1200K */ + bs.secs_track = CF_LE_W(15); + bs.heads = CF_LE_W(2); + bs.media = (char)0xf9; + bs.cluster_size = (char)(atari_format ? 2 : 1); + def_root_dir_entries = 224; + break; + + case 5760: /* 3.5", 2, 36, 80 - 2880K */ + bs.secs_track = CF_LE_W(36); + bs.heads = CF_LE_W(2); + bs.media = (char)0xf0; + bs.cluster_size = (char)2; + bs.dir_entries[0] = (char)224; + bs.dir_entries[1] = (char)0; + break; + + case 2880: /* 3.5", 2, 18, 80 - 1440K */ + bs.secs_track = CF_LE_W(18); + bs.heads = CF_LE_W(2); + bs.media = (char)0xf0; + bs.cluster_size = (char)(atari_format ? 2 : 1); + def_root_dir_entries = 224; + break; + + default: /* Anything else: default hd setup */ + printf("Loop device does not match a floppy size, using " + "default hd params\n"); + bs.secs_track = CT_LE_W(32); /* these are fake values... */ + bs.heads = CT_LE_W(64); + goto def_hd_params; + } + } else + /* Must be a hard disk then! */ + { + /* Can we get the drive geometry? (Note I'm not too sure about */ + /* whether to use HDIO_GETGEO or HDIO_REQ) */ + if (ioctl(dev, HDIO_GETGEO, &geometry) || geometry.sectors == 0 + || geometry.heads == 0) { + printf("unable to get drive geometry, using default 255/63\n"); + bs.secs_track = CT_LE_W(63); + bs.heads = CT_LE_W(255); + } else { + bs.secs_track = CT_LE_W(geometry.sectors); /* Set up the geometry information */ + bs.heads = CT_LE_W(geometry.heads); + } +def_hd_params: + bs.media = (char)0xf8; /* Set up the media descriptor for a hard drive */ + if (!size_fat && blocks * SECTORS_PER_BLOCK > 1064960) { + if (verbose) + printf("Auto-selecting FAT32 for large filesystem\n"); + size_fat = 32; + } + if (size_fat == 32) { + /* For FAT32, try to do the same as M$'s format command + * (see http://www.win.tue.nl/~aeb/linux/fs/fat/fatgen103.pdf p. 20): + * fs size <= 260M: 0.5k clusters + * fs size <= 8G: 4k clusters + * fs size <= 16G: 8k clusters + * fs size > 16G: 16k clusters + */ + unsigned long sz_mb = + (blocks + (1 << (20 - BLOCK_SIZE_BITS)) - 1) >> (20 - + BLOCK_SIZE_BITS); + bs.cluster_size = + sz_mb > 16 * 1024 ? 32 : sz_mb > 8 * 1024 ? 16 : sz_mb > + 260 ? 8 : 1; + } else { + /* FAT12 and FAT16: start at 4 sectors per cluster */ + bs.cluster_size = (char)4; + } + } + + if (!root_dir_entries) + root_dir_entries = def_root_dir_entries; +} + +/* + * If alignment is enabled, round the first argument up to the second; the + * latter must be a power of two. + */ +static unsigned int align_object(unsigned int sectors, unsigned int clustsize) +{ + if (align_structures) + return (sectors + clustsize - 1) & ~(clustsize - 1); + else + return sectors; +} + +/* Create the filesystem data tables */ + +static void setup_tables(void) +{ + unsigned num_sectors; + unsigned cluster_count = 0, fat_length; + struct tm *ctime; + struct msdos_volume_info *vi = + (size_fat == 32 ? &bs.fat32.vi : &bs.oldfat.vi); + + if (atari_format) + /* On Atari, the first few bytes of the boot sector are assigned + * differently: The jump code is only 2 bytes (and m68k machine code + * :-), then 6 bytes filler (ignored), then 3 byte serial number. */ + memcpy(bs.system_id - 1, "mkdosf", 6); + else + strcpy((char *)bs.system_id, "mkdosfs"); + if (sectors_per_cluster) + bs.cluster_size = (char)sectors_per_cluster; + if (size_fat == 32) { + /* Under FAT32, the root dir is in a cluster chain, and this is + * signalled by bs.dir_entries being 0. */ + root_dir_entries = 0; + } + + if (atari_format) { + bs.system_id[5] = (unsigned char)(volume_id & 0x000000ff); + bs.system_id[6] = (unsigned char)((volume_id & 0x0000ff00) >> 8); + bs.system_id[7] = (unsigned char)((volume_id & 0x00ff0000) >> 16); + } else { + vi->volume_id[0] = (unsigned char)(volume_id & 0x000000ff); + vi->volume_id[1] = (unsigned char)((volume_id & 0x0000ff00) >> 8); + vi->volume_id[2] = (unsigned char)((volume_id & 0x00ff0000) >> 16); + vi->volume_id[3] = (unsigned char)(volume_id >> 24); + } + + if (!atari_format) { + memcpy(vi->volume_label, volume_name, 11); + + memcpy(bs.boot_jump, dummy_boot_jump, 3); + /* Patch in the correct offset to the boot code */ + bs.boot_jump[1] = ((size_fat == 32 ? + (char *)&bs.fat32.boot_code : + (char *)&bs.oldfat.boot_code) - (char *)&bs) - 2; + + if (size_fat == 32) { + int offset = (char *)&bs.fat32.boot_code - + (char *)&bs + MESSAGE_OFFSET + 0x7c00; + if (dummy_boot_code[BOOTCODE_FAT32_SIZE - 1]) + printf("Warning: message too long; truncated\n"); + dummy_boot_code[BOOTCODE_FAT32_SIZE - 1] = 0; + memcpy(bs.fat32.boot_code, dummy_boot_code, BOOTCODE_FAT32_SIZE); + bs.fat32.boot_code[MSG_OFFSET_OFFSET] = offset & 0xff; + bs.fat32.boot_code[MSG_OFFSET_OFFSET + 1] = offset >> 8; + } else { + memcpy(bs.oldfat.boot_code, dummy_boot_code, BOOTCODE_SIZE); + } + bs.boot_sign = CT_LE_W(BOOT_SIGN); + } else { + memcpy(bs.boot_jump, dummy_boot_jump_m68k, 2); + } + if (verbose >= 2) + printf("Boot jump code is %02x %02x\n", + bs.boot_jump[0], bs.boot_jump[1]); + + if (!reserved_sectors) + reserved_sectors = (size_fat == 32) ? 32 : 1; + else { + if (size_fat == 32 && reserved_sectors < 2) + die("On FAT32 at least 2 reserved sectors are needed."); + } + bs.reserved = CT_LE_W(reserved_sectors); + if (verbose >= 2) + printf("Using %d reserved sectors\n", reserved_sectors); + bs.fats = (char)nr_fats; + if (!atari_format || size_fat == 32) + bs.hidden = CT_LE_L(hidden_sectors); + else { + /* In Atari format, hidden is a 16 bit field */ + __u16 hidden = CT_LE_W(hidden_sectors); + if (hidden_sectors & ~0xffff) + die("#hidden doesn't fit in 16bit field of Atari format\n"); + memcpy(&bs.hidden, &hidden, 2); + } + + num_sectors = (long long)(blocks *BLOCK_SIZE / sector_size)+orphaned_sectors; + + if (!atari_format) { + unsigned fatdata1216; /* Sectors for FATs + data area (FAT12/16) */ + unsigned fatdata32; /* Sectors for FATs + data area (FAT32) */ + unsigned fatlength12, fatlength16, fatlength32; + unsigned maxclust12, maxclust16, maxclust32; + unsigned clust12, clust16, clust32; + int maxclustsize; + unsigned root_dir_sectors = cdiv(root_dir_entries * 32, sector_size); + + /* + * If the filesystem is 8192 sectors or less (4 MB with 512-byte + * sectors, i.e. floppy size), don't align the data structures. + */ + if (num_sectors <= 8192) { + if (align_structures && verbose >= 2) + printf("Disabling alignment due to tiny filesystem\n"); + + align_structures = FALSE; + } + + if (sectors_per_cluster) + bs.cluster_size = maxclustsize = sectors_per_cluster; + else + /* An initial guess for bs.cluster_size should already be set */ + maxclustsize = 128; + + do { + fatdata32 = num_sectors + - align_object(reserved_sectors, bs.cluster_size); + fatdata1216 = fatdata32 + - align_object(root_dir_sectors, bs.cluster_size); + + if (verbose >= 2) + printf("Trying with %d sectors/cluster:\n", bs.cluster_size); + + /* The factor 2 below avoids cut-off errors for nr_fats == 1. + * The "nr_fats*3" is for the reserved first two FAT entries */ + clust12 = 2 * ((long long)fatdata1216 * sector_size + nr_fats * 3) / + (2 * (int)bs.cluster_size * sector_size + nr_fats * 3); + fatlength12 = cdiv(((clust12 + 2) * 3 + 1) >> 1, sector_size); + fatlength12 = align_object(fatlength12, bs.cluster_size); + /* Need to recalculate number of clusters, since the unused parts of the + * FATS and data area together could make up space for an additional, + * not really present cluster. */ + clust12 = (fatdata1216 - nr_fats * fatlength12) / bs.cluster_size; + maxclust12 = (fatlength12 * 2 * sector_size) / 3; + if (maxclust12 > MAX_CLUST_12) + maxclust12 = MAX_CLUST_12; + if (verbose >= 2) + printf("FAT12: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n", + clust12, fatlength12, maxclust12, MAX_CLUST_12); + if (clust12 > maxclust12 - 2) { + clust12 = 0; + if (verbose >= 2) + printf("FAT12: too much clusters\n"); + } + + clust16 = ((long long)fatdata1216 * sector_size + nr_fats * 4) / + ((int)bs.cluster_size * sector_size + nr_fats * 2); + fatlength16 = cdiv((clust16 + 2) * 2, sector_size); + fatlength16 = align_object(fatlength16, bs.cluster_size); + /* Need to recalculate number of clusters, since the unused parts of the + * FATS and data area together could make up space for an additional, + * not really present cluster. */ + clust16 = (fatdata1216 - nr_fats * fatlength16) / bs.cluster_size; + maxclust16 = (fatlength16 * sector_size) / 2; + if (maxclust16 > MAX_CLUST_16) + maxclust16 = MAX_CLUST_16; + if (verbose >= 2) + printf("FAT16: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n", + clust16, fatlength16, maxclust16, MAX_CLUST_16); + if (clust16 > maxclust16 - 2) { + if (verbose >= 2) + printf("FAT16: too much clusters\n"); + clust16 = 0; + } + /* The < 4078 avoids that the filesystem will be misdetected as having a + * 12 bit FAT. */ + if (clust16 < FAT12_THRESHOLD + && !(size_fat_by_user && size_fat == 16)) { + if (verbose >= 2) + printf(clust16 < FAT12_THRESHOLD ? + "FAT16: would be misdetected as FAT12\n" : + "FAT16: too much clusters\n"); + clust16 = 0; + } + + clust32 = ((long long)fatdata32 * sector_size + nr_fats * 8) / + ((int)bs.cluster_size * sector_size + nr_fats * 4); + fatlength32 = cdiv((clust32 + 2) * 4, sector_size); + fatlength32 = align_object(fatlength32, bs.cluster_size); + /* Need to recalculate number of clusters, since the unused parts of the + * FATS and data area together could make up space for an additional, + * not really present cluster. */ + clust32 = (fatdata32 - nr_fats * fatlength32) / bs.cluster_size; + maxclust32 = (fatlength32 * sector_size) / 4; + if (maxclust32 > MAX_CLUST_32) + maxclust32 = MAX_CLUST_32; + if (clust32 && clust32 < MIN_CLUST_32 + && !(size_fat_by_user && size_fat == 32)) { + clust32 = 0; + if (verbose >= 2) + printf("FAT32: not enough clusters (%d)\n", MIN_CLUST_32); + } + if (verbose >= 2) + printf("FAT32: #clu=%u, fatlen=%u, maxclu=%u, limit=%u\n", + clust32, fatlength32, maxclust32, MAX_CLUST_32); + if (clust32 > maxclust32) { + clust32 = 0; + if (verbose >= 2) + printf("FAT32: too much clusters\n"); + } + + if ((clust12 && (size_fat == 0 || size_fat == 12)) || + (clust16 && (size_fat == 0 || size_fat == 16)) || + (clust32 && size_fat == 32)) + break; + + bs.cluster_size <<= 1; + } while (bs.cluster_size && bs.cluster_size <= maxclustsize); + + /* Use the optimal FAT size if not specified; + * FAT32 is (not yet) choosen automatically */ + if (!size_fat) { + size_fat = (clust16 > clust12) ? 16 : 12; + if (verbose >= 2) + printf("Choosing %d bits for FAT\n", size_fat); + } + + switch (size_fat) { + case 12: + cluster_count = clust12; + fat_length = fatlength12; + bs.fat_length = CT_LE_W(fatlength12); + memcpy(vi->fs_type, MSDOS_FAT12_SIGN, 8); + break; + + case 16: + if (clust16 < FAT12_THRESHOLD) { + if (size_fat_by_user) { + fprintf(stderr, "WARNING: Not enough clusters for a " + "16 bit FAT! The filesystem will be\n" + "misinterpreted as having a 12 bit FAT without " + "mount option \"fat=16\".\n"); + } else { + fprintf(stderr, "This filesystem has an unfortunate size. " + "A 12 bit FAT cannot provide\n" + "enough clusters, but a 16 bit FAT takes up a little " + "bit more space so that\n" + "the total number of clusters becomes less than the " + "threshold value for\n" + "distinction between 12 and 16 bit FATs.\n"); + die("Make the file system a bit smaller manually."); + } + } + cluster_count = clust16; + fat_length = fatlength16; + bs.fat_length = CT_LE_W(fatlength16); + memcpy(vi->fs_type, MSDOS_FAT16_SIGN, 8); + break; + + case 32: + if (clust32 < MIN_CLUST_32) + fprintf(stderr, + "WARNING: Not enough clusters for a 32 bit FAT!\n"); + cluster_count = clust32; + fat_length = fatlength32; + bs.fat_length = CT_LE_W(0); + bs.fat32.fat32_length = CT_LE_L(fatlength32); + memcpy(vi->fs_type, MSDOS_FAT32_SIGN, 8); + root_dir_entries = 0; + break; + + default: + die("FAT not 12, 16 or 32 bits"); + } + + /* Adjust the reserved number of sectors for alignment */ + reserved_sectors = align_object(reserved_sectors, bs.cluster_size); + bs.reserved = CT_LE_W(reserved_sectors); + + /* Adjust the number of root directory entries to help enforce alignment */ + if (align_structures) { + root_dir_entries = align_object(root_dir_sectors, bs.cluster_size) + * (sector_size >> 5); + } + } else { + unsigned clusters, maxclust, fatdata; + + /* GEMDOS always uses a 12 bit FAT on floppies, and always a 16 bit FAT on + * hard disks. So use 12 bit if the size of the file system suggests that + * this fs is for a floppy disk, if the user hasn't explicitly requested a + * size. + */ + if (!size_fat) + size_fat = (num_sectors == 1440 || num_sectors == 2400 || + num_sectors == 2880 || num_sectors == 5760) ? 12 : 16; + if (verbose >= 2) + printf("Choosing %d bits for FAT\n", size_fat); + + /* Atari format: cluster size should be 2, except explicitly requested by + * the user, since GEMDOS doesn't like other cluster sizes very much. + * Instead, tune the sector size for the FS to fit. + */ + bs.cluster_size = sectors_per_cluster ? sectors_per_cluster : 2; + if (!sector_size_set) { + while (num_sectors > GEMDOS_MAX_SECTORS) { + num_sectors >>= 1; + sector_size <<= 1; + } + } + if (verbose >= 2) + printf("Sector size must be %d to have less than %d log. sectors\n", + sector_size, GEMDOS_MAX_SECTORS); + + /* Check if there are enough FAT indices for how much clusters we have */ + do { + fatdata = num_sectors - cdiv(root_dir_entries * 32, sector_size) - + reserved_sectors; + /* The factor 2 below avoids cut-off errors for nr_fats == 1 and + * size_fat == 12 + * The "2*nr_fats*size_fat/8" is for the reserved first two FAT entries + */ + clusters = + (2 * + ((long long)fatdata * sector_size - + 2 * nr_fats * size_fat / 8)) / (2 * ((int)bs.cluster_size * + sector_size + + nr_fats * size_fat / 8)); + fat_length = cdiv((clusters + 2) * size_fat / 8, sector_size); + /* Need to recalculate number of clusters, since the unused parts of the + * FATS and data area together could make up space for an additional, + * not really present cluster. */ + clusters = (fatdata - nr_fats * fat_length) / bs.cluster_size; + maxclust = (fat_length * sector_size * 8) / size_fat; + if (verbose >= 2) + printf("ss=%d: #clu=%d, fat_len=%d, maxclu=%d\n", + sector_size, clusters, fat_length, maxclust); + + /* last 10 cluster numbers are special (except FAT32: 4 high bits rsvd); + * first two numbers are reserved */ + if (maxclust <= + (size_fat == 32 ? MAX_CLUST_32 : (1 << size_fat) - 0x10) + && clusters <= maxclust - 2) + break; + if (verbose >= 2) + printf(clusters > maxclust - 2 ? + "Too many clusters\n" : "FAT too big\n"); + + /* need to increment sector_size once more to */ + if (sector_size_set) + die("With this sector size, the maximum number of FAT entries " + "would be exceeded."); + num_sectors >>= 1; + sector_size <<= 1; + } while (sector_size <= GEMDOS_MAX_SECTOR_SIZE); + + if (sector_size > GEMDOS_MAX_SECTOR_SIZE) + die("Would need a sector size > 16k, which GEMDOS can't work with"); + + cluster_count = clusters; + if (size_fat != 32) + bs.fat_length = CT_LE_W(fat_length); + else { + bs.fat_length = 0; + bs.fat32.fat32_length = CT_LE_L(fat_length); + } + } + + bs.sector_size[0] = (char)(sector_size & 0x00ff); + bs.sector_size[1] = (char)((sector_size & 0xff00) >> 8); + + bs.dir_entries[0] = (char)(root_dir_entries & 0x00ff); + bs.dir_entries[1] = (char)((root_dir_entries & 0xff00) >> 8); + + if (size_fat == 32) { + /* set up additional FAT32 fields */ + bs.fat32.flags = CT_LE_W(0); + bs.fat32.version[0] = 0; + bs.fat32.version[1] = 0; + bs.fat32.root_cluster = CT_LE_L(2); + bs.fat32.info_sector = CT_LE_W(1); + if (!backup_boot) + backup_boot = (reserved_sectors >= 7) ? 6 : + (reserved_sectors >= 2) ? reserved_sectors - 1 : 0; + else { + if (backup_boot == 1) + die("Backup boot sector must be after sector 1"); + else if (backup_boot >= reserved_sectors) + die("Backup boot sector must be a reserved sector"); + } + if (verbose >= 2) + printf("Using sector %d as backup boot sector (0 = none)\n", + backup_boot); + bs.fat32.backup_boot = CT_LE_W(backup_boot); + memset(&bs.fat32.reserved2, 0, sizeof(bs.fat32.reserved2)); + } + + if (atari_format) { + /* Just some consistency checks */ + if (num_sectors >= GEMDOS_MAX_SECTORS) + die("GEMDOS can't handle more than 65531 sectors"); + else if (num_sectors >= OLDGEMDOS_MAX_SECTORS) + printf("Warning: More than 32765 sector need TOS 1.04 " + "or higher.\n"); + } + if (num_sectors >= 65536) { + bs.sectors[0] = (char)0; + bs.sectors[1] = (char)0; + bs.total_sect = CT_LE_L(num_sectors); + } else { + bs.sectors[0] = (char)(num_sectors & 0x00ff); + bs.sectors[1] = (char)((num_sectors & 0xff00) >> 8); + if (!atari_format) + bs.total_sect = CT_LE_L(0); + } + + if (!atari_format) + vi->ext_boot_sign = MSDOS_EXT_SIGN; + + if (!cluster_count) { + if (sectors_per_cluster) /* If yes, die if we'd spec'd sectors per cluster */ + die("Too many clusters for file system - try more sectors per cluster"); + else + die("Attempting to create a too large file system"); + } + + /* The two following vars are in hard sectors, i.e. 512 byte sectors! */ + start_data_sector = (reserved_sectors + nr_fats * fat_length) * + (sector_size / HARD_SECTOR_SIZE); + start_data_block = (start_data_sector + SECTORS_PER_BLOCK - 1) / + SECTORS_PER_BLOCK; + + if (blocks < start_data_block + 32) /* Arbitrary undersize file system! */ + die("Too few blocks for viable file system"); + + if (verbose) { + printf("%s has %d head%s and %d sector%s per track,\n", + device_name, CF_LE_W(bs.heads), + (CF_LE_W(bs.heads) != 1) ? "s" : "", CF_LE_W(bs.secs_track), + (CF_LE_W(bs.secs_track) != 1) ? "s" : ""); + printf("logical sector size is %d,\n", sector_size); + printf("using 0x%02x media descriptor, with %d sectors;\n", + (int)(bs.media), num_sectors); + printf("file system has %d %d-bit FAT%s and %d sector%s per cluster.\n", + (int)(bs.fats), size_fat, (bs.fats != 1) ? "s" : "", + (int)(bs.cluster_size), (bs.cluster_size != 1) ? "s" : ""); + printf("FAT size is %d sector%s, and provides %d cluster%s.\n", + fat_length, (fat_length != 1) ? "s" : "", + cluster_count, (cluster_count != 1) ? "s" : ""); + printf("There %s %u reserved sector%s.\n", + (reserved_sectors != 1) ? "are" : "is", + reserved_sectors, (reserved_sectors != 1) ? "s" : ""); + + if (size_fat != 32) { + unsigned root_dir_entries = + bs.dir_entries[0] + ((bs.dir_entries[1]) * 256); + unsigned root_dir_sectors = + cdiv(root_dir_entries * 32, sector_size); + printf("Root directory contains %u slots and uses %u sectors.\n", + root_dir_entries, root_dir_sectors); + } + printf("Volume ID is %08lx, ", volume_id & + (atari_format ? 0x00ffffff : 0xffffffff)); + if (strcmp(volume_name, " ")) + printf("volume label %s.\n", volume_name); + else + printf("no volume label.\n"); + } + + /* Make the file allocation tables! */ + + if (malloc_entire_fat) + alloced_fat_length = fat_length; + else + alloced_fat_length = 1; + + if ((fat = + (unsigned char *)malloc(alloced_fat_length * sector_size)) == NULL) + die("unable to allocate space for FAT image in memory"); + + memset(fat, 0, alloced_fat_length * sector_size); + + mark_FAT_cluster(0, 0xffffffff); /* Initial fat entries */ + mark_FAT_cluster(1, 0xffffffff); + fat[0] = (unsigned char)bs.media; /* Put media type in first byte! */ + if (size_fat == 32) { + /* Mark cluster 2 as EOF (used for root dir) */ + mark_FAT_cluster(2, FAT_EOF); + } + + /* Make the root directory entries */ + + size_root_dir = (size_fat == 32) ? + bs.cluster_size * sector_size : + (((int)bs.dir_entries[1] * 256 + (int)bs.dir_entries[0]) * + sizeof(struct msdos_dir_entry)); + if ((root_dir = (struct msdos_dir_entry *)malloc(size_root_dir)) == NULL) { + free(fat); /* Tidy up before we die! */ + die("unable to allocate space for root directory in memory"); + } + + memset(root_dir, 0, size_root_dir); + if (memcmp(volume_name, " ", 11)) { + struct msdos_dir_entry *de = &root_dir[0]; + memcpy(de->name, volume_name, 8); + memcpy(de->ext, volume_name + 8, 3); + de->attr = ATTR_VOLUME; + ctime = localtime(&create_time); + de->time = CT_LE_W((unsigned short)((ctime->tm_sec >> 1) + + (ctime->tm_min << 5) + + (ctime->tm_hour << 11))); + de->date = + CT_LE_W((unsigned short)(ctime->tm_mday + + ((ctime->tm_mon + 1) << 5) + + ((ctime->tm_year - 80) << 9))); + de->ctime_ms = 0; + de->ctime = de->time; + de->cdate = de->date; + de->adate = de->date; + de->starthi = CT_LE_W(0); + de->start = CT_LE_W(0); + de->size = CT_LE_L(0); + } + + if (size_fat == 32) { + /* For FAT32, create an info sector */ + struct fat32_fsinfo *info; + + if (!(info_sector = malloc(sector_size))) + die("Out of memory"); + memset(info_sector, 0, sector_size); + /* fsinfo structure is at offset 0x1e0 in info sector by observation */ + info = (struct fat32_fsinfo *)(info_sector + 0x1e0); + + /* Info sector magic */ + info_sector[0] = 'R'; + info_sector[1] = 'R'; + info_sector[2] = 'a'; + info_sector[3] = 'A'; + + /* Magic for fsinfo structure */ + info->signature = CT_LE_L(0x61417272); + /* We've allocated cluster 2 for the root dir. */ + info->free_clusters = CT_LE_L(cluster_count - 1); + info->next_cluster = CT_LE_L(2); + + /* Info sector also must have boot sign */ + *(__u16 *) (info_sector + 0x1fe) = CT_LE_W(BOOT_SIGN); + } + + if (!(blank_sector = malloc(sector_size))) + die("Out of memory"); + memset(blank_sector, 0, sector_size); +} + +/* Write the new filesystem's data tables to wherever they're going to end up! */ + +#define error(str) \ + do { \ + free (fat); \ + if (info_sector) free (info_sector); \ + free (root_dir); \ + die (str); \ + } while(0) + +#define seekto(pos,errstr) \ + do { \ + loff_t __pos = (pos); \ + if (llseek (dev, __pos, SEEK_SET) != __pos) \ + error ("seek to " errstr " failed whilst writing tables"); \ + } while(0) + +#define writebuf(buf,size,errstr) \ + do { \ + int __size = (size); \ + if (write (dev, buf, __size) != __size) \ + error ("failed whilst writing " errstr); \ + } while(0) + +static void write_tables(void) +{ + int x; + int fat_length; + + fat_length = (size_fat == 32) ? + CF_LE_L(bs.fat32.fat32_length) : CF_LE_W(bs.fat_length); + + seekto(0, "start of device"); + /* clear all reserved sectors */ + for (x = 0; x < reserved_sectors; ++x) + writebuf(blank_sector, sector_size, "reserved sector"); + /* seek back to sector 0 and write the boot sector */ + seekto(0, "boot sector"); + writebuf((char *)&bs, sizeof(struct msdos_boot_sector), "boot sector"); + /* on FAT32, write the info sector and backup boot sector */ + if (size_fat == 32) { + seekto(CF_LE_W(bs.fat32.info_sector) * sector_size, "info sector"); + writebuf(info_sector, 512, "info sector"); + if (backup_boot != 0) { + seekto(backup_boot * sector_size, "backup boot sector"); + writebuf((char *)&bs, sizeof(struct msdos_boot_sector), + "backup boot sector"); + } + } + /* seek to start of FATS and write them all */ + seekto(reserved_sectors * sector_size, "first FAT"); + for (x = 1; x <= nr_fats; x++) { + int y; + int blank_fat_length = fat_length - alloced_fat_length; + writebuf(fat, alloced_fat_length * sector_size, "FAT"); + for (y = 0; y < blank_fat_length; y++) + writebuf(blank_sector, sector_size, "FAT"); + } + /* Write the root directory directly after the last FAT. This is the root + * dir area on FAT12/16, and the first cluster on FAT32. */ + writebuf((char *)root_dir, size_root_dir, "root directory"); + + if (blank_sector) + free(blank_sector); + if (info_sector) + free(info_sector); + free(root_dir); /* Free up the root directory space from setup_tables */ + free(fat); /* Free up the fat table space reserved during setup_tables */ +} + +/* Report the command usage and return a failure error code */ + +void usage(void) +{ + fatal_error("\ +Usage: mkdosfs [-a][-A][-c][-C][-v][-I][-l bad-block-file][-b backup-boot-sector]\n\ + [-m boot-msg-file][-n volume-name][-i volume-id]\n\ + [-s sectors-per-cluster][-S logical-sector-size][-f number-of-FATs]\n\ + [-h hidden-sectors][-F fat-size][-r root-dir-entries][-R reserved-sectors]\n\ + /dev/name [blocks]\n"); +} + +/* + * ++roman: On m68k, check if this is an Atari; if yes, turn on Atari variant + * of MS-DOS filesystem by default. + */ +static void check_atari(void) +{ +#ifdef __mc68000__ + FILE *f; + char line[128], *p; + + if (!(f = fopen("/proc/hardware", "r"))) { + perror("/proc/hardware"); + return; + } + + while (fgets(line, sizeof(line), f)) { + if (strncmp(line, "Model:", 6) == 0) { + p = line + 6; + p += strspn(p, " \t"); + if (strncmp(p, "Atari ", 6) == 0) + atari_format = 1; + break; + } + } + fclose(f); +#endif +} + +/* The "main" entry point into the utility - we pick up the options and attempt to process them in some sort of sensible + way. In the event that some/all of the options are invalid we need to tell the user so that something can be done! */ + +int main(int argc, char **argv) +{ + int c; + char *tmp; + char *listfile = NULL; + FILE *msgfile; + struct stat statbuf; + int i = 0, pos, ch; + int create = 0; + unsigned long long cblocks = 0; + int min_sector_size; + + if (argc && *argv) { /* What's the program name? */ + char *p; + program_name = *argv; + if ((p = strrchr(program_name, '/'))) + program_name = p + 1; + } + + gettimeofday(&create_timeval, NULL); + create_time = create_timeval.tv_sec; + volume_id = (u_int32_t) ((create_timeval.tv_sec << 20) | create_timeval.tv_usec); /* Default volume ID = creation time, fudged for more uniqueness */ + check_atari(); + + printf("%s " VERSION " (" VERSION_DATE ")\n", program_name); + + while ((c = getopt(argc, argv, "aAb:cCf:F:Ii:l:m:n:r:R:s:S:h:v")) != EOF) + /* Scan the command line for options */ + switch (c) { + case 'A': /* toggle Atari format */ + atari_format = !atari_format; + break; + + case 'a': /* a : skip alignment */ + align_structures = FALSE; + break; + + case 'b': /* b : location of backup boot sector */ + backup_boot = (int)strtol(optarg, &tmp, 0); + if (*tmp || backup_boot < 2 || backup_boot > 0xffff) { + printf("Bad location for backup boot sector : %s\n", optarg); + usage(); + } + break; + + case 'c': /* c : Check FS as we build it */ + check = TRUE; + malloc_entire_fat = TRUE; /* Need to be able to mark clusters bad */ + break; + + case 'C': /* C : Create a new file */ + create = TRUE; + break; + + case 'f': /* f : Choose number of FATs */ + nr_fats = (int)strtol(optarg, &tmp, 0); + if (*tmp || nr_fats < 1 || nr_fats > 4) { + printf("Bad number of FATs : %s\n", optarg); + usage(); + } + break; + + case 'F': /* F : Choose FAT size */ + size_fat = (int)strtol(optarg, &tmp, 0); + if (*tmp || (size_fat != 12 && size_fat != 16 && size_fat != 32)) { + printf("Bad FAT type : %s\n", optarg); + usage(); + } + size_fat_by_user = 1; + break; + + case 'h': /* h : number of hidden sectors */ + hidden_sectors = (int)strtol(optarg, &tmp, 0); + if (*tmp || hidden_sectors < 0) { + printf("Bad number of hidden sectors : %s\n", optarg); + usage(); + } + break; + + case 'I': + ignore_full_disk = 1; + break; + + case 'i': /* i : specify volume ID */ + volume_id = strtoul(optarg, &tmp, 16); + if (*tmp) { + printf("Volume ID must be a hexadecimal number\n"); + usage(); + } + break; + + case 'l': /* l : Bad block filename */ + listfile = optarg; + malloc_entire_fat = TRUE; /* Need to be able to mark clusters bad */ + break; + + case 'm': /* m : Set boot message */ + if (strcmp(optarg, "-")) { + msgfile = fopen(optarg, "r"); + if (!msgfile) + perror(optarg); + } else + msgfile = stdin; + + if (msgfile) { + /* The boot code ends at offset 448 and needs a null terminator */ + i = MESSAGE_OFFSET; + pos = 0; /* We are at beginning of line */ + do { + ch = getc(msgfile); + switch (ch) { + case '\r': /* Ignore CRs */ + case '\0': /* and nulls */ + break; + + case '\n': /* LF -> CR+LF if necessary */ + if (pos) { /* If not at beginning of line */ + dummy_boot_code[i++] = '\r'; + pos = 0; + } + dummy_boot_code[i++] = '\n'; + break; + + case '\t': /* Expand tabs */ + do { + dummy_boot_code[i++] = ' '; + pos++; + } + while (pos % 8 && i < BOOTCODE_SIZE - 1); + break; + + case EOF: + dummy_boot_code[i++] = '\0'; /* Null terminator */ + break; + + default: + dummy_boot_code[i++] = ch; /* Store character */ + pos++; /* Advance position */ + break; + } + } + while (ch != EOF && i < BOOTCODE_SIZE - 1); + + /* Fill up with zeros */ + while (i < BOOTCODE_SIZE - 1) + dummy_boot_code[i++] = '\0'; + dummy_boot_code[BOOTCODE_SIZE - 1] = '\0'; /* Just in case */ + + if (ch != EOF) + printf("Warning: message too long; truncated\n"); + + if (msgfile != stdin) + fclose(msgfile); + } + break; + + case 'n': /* n : Volume name */ + sprintf(volume_name, "%-11.11s", optarg); + break; + + case 'r': /* r : Root directory entries */ + root_dir_entries = (int)strtol(optarg, &tmp, 0); + if (*tmp || root_dir_entries < 16 || root_dir_entries > 32768) { + printf("Bad number of root directory entries : %s\n", optarg); + usage(); + } + break; + + case 'R': /* R : number of reserved sectors */ + reserved_sectors = (int)strtol(optarg, &tmp, 0); + if (*tmp || reserved_sectors < 1 || reserved_sectors > 0xffff) { + printf("Bad number of reserved sectors : %s\n", optarg); + usage(); + } + break; + + case 's': /* s : Sectors per cluster */ + sectors_per_cluster = (int)strtol(optarg, &tmp, 0); + if (*tmp || (sectors_per_cluster != 1 && sectors_per_cluster != 2 + && sectors_per_cluster != 4 && sectors_per_cluster != 8 + && sectors_per_cluster != 16 + && sectors_per_cluster != 32 + && sectors_per_cluster != 64 + && sectors_per_cluster != 128)) { + printf("Bad number of sectors per cluster : %s\n", optarg); + usage(); + } + break; + + case 'S': /* S : Sector size */ + sector_size = (int)strtol(optarg, &tmp, 0); + if (*tmp || (sector_size != 512 && sector_size != 1024 && + sector_size != 2048 && sector_size != 4096 && + sector_size != 8192 && sector_size != 16384 && + sector_size != 32768)) { + printf("Bad logical sector size : %s\n", optarg); + usage(); + } + sector_size_set = 1; + break; + + case 'v': /* v : Verbose execution */ + ++verbose; + break; + + default: + printf("Unknown option: %c\n", c); + usage(); + } + if (optind < argc) { + device_name = argv[optind]; /* Determine the number of blocks in the FS */ + + if (!device_name) { + printf("No device specified.\n"); + usage(); + } + + if (!create) + cblocks = count_blocks(device_name, &orphaned_sectors); /* Have a look and see! */ + } + if (optind == argc - 2) { /* Either check the user specified number */ + blocks = strtoull(argv[optind + 1], &tmp, 0); + if (!create && blocks != cblocks) { + fprintf(stderr, "Warning: block count mismatch: "); + fprintf(stderr, "found %llu but assuming %llu.\n", cblocks, blocks); + } + } else if (optind == argc - 1) { /* Or use value found */ + if (create) + die("Need intended size with -C."); + blocks = cblocks; + tmp = ""; + } else { + fprintf(stderr, "No device specified!\n"); + usage(); + } + if (*tmp) { + printf("Bad block count : %s\n", argv[optind + 1]); + usage(); + } + + if (check && listfile) /* Auto and specified bad block handling are mutually */ + die("-c and -l are incompatible"); /* exclusive of each other! */ + + if (!create) { + check_mount(device_name); /* Is the device already mounted? */ + dev = open(device_name, O_EXCL | O_RDWR); /* Is it a suitable device to build the FS on? */ + if (dev < 0) { + fprintf(stderr, "%s: unable to open %s: %s\n", program_name, + device_name, strerror(errno)); + exit(1); /* The error exit code is 1! */ + } + } else { + loff_t offset = blocks * BLOCK_SIZE - 1; + char null = 0; + /* create the file */ + dev = open(device_name, O_EXCL | O_RDWR | O_CREAT | O_TRUNC, 0666); + if (dev < 0) + die("unable to create %s"); + /* seek to the intended end-1, and write one byte. this creates a + * sparse-as-possible file of appropriate size. */ + if (llseek(dev, offset, SEEK_SET) != offset) + die("seek failed"); + if (write(dev, &null, 1) < 0) + die("write failed"); + if (llseek(dev, 0, SEEK_SET) != 0) + die("seek failed"); + } + + if (fstat(dev, &statbuf) < 0) + die("unable to stat %s"); + if (!S_ISBLK(statbuf.st_mode)) { + statbuf.st_rdev = 0; + check = 0; + } else + /* + * Ignore any 'full' fixed disk devices, if -I is not given. + * On a MO-disk one doesn't need partitions. The filesytem can go + * directly to the whole disk. Under other OSes this is known as + * the 'superfloppy' format. As I don't know how to find out if + * this is a MO disk I introduce a -I (ignore) switch. -Joey + */ + if (!ignore_full_disk && ((statbuf.st_rdev & 0xff3f) == 0x0300 || /* hda, hdb */ + (statbuf.st_rdev & 0xff0f) == 0x0800 || /* sd */ + (statbuf.st_rdev & 0xff3f) == 0x0d00 || /* xd */ + (statbuf.st_rdev & 0xff3f) == 0x1600) /* hdc, hdd */ + ) + die("Device partition expected, not making filesystem on entire device '%s' (use -I to override)"); + + if (sector_size_set) { + if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0) + if (sector_size < min_sector_size) { + sector_size = min_sector_size; + fprintf(stderr, + "Warning: sector size was set to %d (minimal for this device)\n", + sector_size); + } + } else { + if (ioctl(dev, BLKSSZGET, &min_sector_size) >= 0) { + sector_size = min_sector_size; + sector_size_set = 1; + } + } + + if (sector_size > 4096) + fprintf(stderr, + "Warning: sector size is set to %d > 4096, such filesystem will not propably mount\n", + sector_size); + + establish_params(statbuf.st_rdev, statbuf.st_size); + /* Establish the media parameters */ + + setup_tables(); /* Establish the file system tables */ + + if (check) /* Determine any bad block locations and mark them */ + check_blocks(); + else if (listfile) + get_list_blocks(listfile); + + write_tables(); /* Write the file system tables away! */ + + exit(0); /* Terminate with no errors! */ +} diff --git a/dosfstools/src/version.h b/dosfstools/src/version.h new file mode 100644 index 000000000..6379103b6 --- /dev/null +++ b/dosfstools/src/version.h @@ -0,0 +1,28 @@ +/* version.h + + Copyright (C) 1998-2005 Roman Hodek + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + On Debian systems, the complete text of the GNU General Public License + can be found in /usr/share/common-licenses/GPL-3 file. +*/ + +#ifndef _version_h +#define _version_h + +#define VERSION "3.0.12" +#define VERSION_DATE "29 Oct 2011" + +#endif diff --git a/exfat/COPYING b/exfat/COPYING new file mode 100644 index 000000000..94a9ed024 --- /dev/null +++ b/exfat/COPYING @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/exfat/ChangeLog b/exfat/ChangeLog new file mode 100644 index 000000000..e42a095e0 --- /dev/null +++ b/exfat/ChangeLog @@ -0,0 +1,70 @@ +0.9.8 (2012-08-09) + +* The mkfs utility can now create huge file systems (up to several exabytes). +* Fixed handling of characters beyond Basic Multilingual Plane. +* Echo messages to syslog only if stderr is not connected to a terminal. + +0.9.7 (2012-03-08) + +* Out-of-the-box FreeBSD support (via ublio library). +* Fixed "missing EOD entry" error (could happen while reading directory that +consists of several clusters). +* Fixed interpretation of minutes field in files timestamps (minutes could be +displayed incorrectly). +* Fixed mtime seconds field initialization for newly created file (mtime could +be 1 sec less than creation time). +* SConscript now respects CC, CCFLAGS and LDFLAGS environment variables. + +0.9.6 (2012-01-14) + +* Fixed write performance regression introduced in 0.9.4. +* Mount in read-only mode if the device is write-protected. +* Set ctime to mtime to ensure we don't break programs that rely on ctime +(e.g. rsync considered that all files are outdated) [Eldad Zack]. +* Indicate that FS in not clean when it was not cleanly unmounted. +* Utilities are now compatible with GNU/Hurd. +* Fixed several memory leaks that could occur on error handling paths. +* Improved handling of corrupted file systems. + +0.9.5 (2011-05-15) + +* Fixed erasing of the root directory cluster when creating a new FS with +mkexfatfs. This bug could cause mkexfatfs to produce invalid FS. +* Utilities are not linked with libfuse anymore. +* Ensure that the path being opened is either a device or a regular file. + +0.9.4 (2011-03-05) + +* Introduced exfat-utils: dumpexfat, exfatfsck, mkexfatfs, exfatlabel. +* Fixed "Invalid argument" error while mounting a volume from a disk with sector size greater than 512 bytes. +* Wait for all data to be flushed to disk on unmount. +* Kernel cache is no longer flushed on open. This can slightly improve read performance by avoiding extra read requests from kernel to user-space. +* Allow to unmount volumes as user (fusermount -u) if they were mounted from the very same user [Tino Lange]. +* Errors and warnings are now duplicated to syslog. + +0.9.3 (2010-09-25) + +* Directories now can shrink. +* Improved timestamps resolution from 2 sec to 1 sec. +* Fixed timestamps displaying under Mac OS X when compiled for i386 or ppc. +* Fixed FS size displaying for non-GNU systems. + +0.9.2 (2010-07-24) + +* Fixed a bug which could cause the whole directory to become unreadable after renaming a file in it. +* Support for Solaris and various *BSD [Albert Lee]. +* Improved error handling on corrupted volumes. +* Improved allowed file name characters filter. +* Added man page. + +0.9.1 (2010-06-12) + +* Implemented automounting (util-linux-ng 2.18 or later is required). +* Fixed mounting when cluster bitmap is larger than expected. +* Fixed crash on statfs() when root directory contains error. +* Fixed bugs specific to big-endian machines. +* Other bugfixes. + +0.9.0 (2010-03-21) + +* Initial release. diff --git a/exfat/dump/dumpexfat.8 b/exfat/dump/dumpexfat.8 new file mode 100644 index 000000000..36fb28ad1 --- /dev/null +++ b/exfat/dump/dumpexfat.8 @@ -0,0 +1,46 @@ +.\" Copyright (C) 2011 Andrew Nayenko +.\" +.TH DUMPEXFAT 8 "February 2011" +.SH NAME +.B dumpexfat +\- dump exFAT file system +.SH SYNOPSIS +.B exfatlabel +[ +.B \-s +] +[ +.B \-u +] +[ +.B \-v +] +.I device + +.SH DESCRIPTION +.B dumpexfat +dumps details about exFAT file system including low-level info. All sizes are +in bytes. + +.SH OPTIONS +Command line options available: +.TP +.B \-s +Dump only info from super block. May be useful for heavily corrupted file +systems. +.TP +.B \-u +Dump ranges of used sectors starting from 0 and separated with spaces. May be +useful for backup tools. +.TP +.BI \-v +Print version and copyright. + +.SH EXIT CODES +Zero is returned on success. Any other code means an error. + +.SH AUTHOR +Andrew Nayenko + +.SH SEE ALSO +.BR mkexfatfs (8) diff --git a/exfat/dump/main.c b/exfat/dump/main.c new file mode 100644 index 000000000..d7ea2a365 --- /dev/null +++ b/exfat/dump/main.c @@ -0,0 +1,180 @@ +/* + main.c (08.11.10) + Prints detailed information about exFAT volume. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include + +static void print_generic_info(const struct exfat_super_block* sb) +{ + printf("Volume serial number 0x%08x\n", + le32_to_cpu(sb->volume_serial)); + printf("FS version %hhu.%hhu\n", + sb->version.major, sb->version.minor); + printf("Sector size %10u\n", + SECTOR_SIZE(*sb)); + printf("Cluster size %10u\n", + CLUSTER_SIZE(*sb)); +} + +static void print_sector_info(const struct exfat_super_block* sb) +{ + printf("Sectors count %10"PRIu64"\n", + le64_to_cpu(sb->sector_count)); +} + +static void print_cluster_info(const struct exfat_super_block* sb) +{ + printf("Clusters count %10u\n", + le32_to_cpu(sb->cluster_count)); +} + +static void print_other_info(const struct exfat_super_block* sb) +{ + printf("First sector %10"PRIu64"\n", + le64_to_cpu(sb->sector_start)); + printf("FAT first sector %10u\n", + le32_to_cpu(sb->fat_sector_start)); + printf("FAT sectors count %10u\n", + le32_to_cpu(sb->fat_sector_count)); + printf("First cluster sector %10u\n", + le32_to_cpu(sb->cluster_sector_start)); + printf("Root directory cluster %10u\n", + le32_to_cpu(sb->rootdir_cluster)); + printf("Volume state 0x%04hx\n", + le16_to_cpu(sb->volume_state)); + printf("FATs count %10hhu\n", + sb->fat_count); + printf("Drive number 0x%02hhx\n", + sb->drive_no); + printf("Allocated space %9hhu%%\n", + sb->allocated_percent); +} + +static int dump_sb(const char* spec) +{ + struct exfat_dev* dev; + struct exfat_super_block sb; + + dev = exfat_open(spec, EXFAT_MODE_RO); + if (dev == NULL) + return 1; + + if (exfat_read(dev, &sb, sizeof(struct exfat_super_block)) < 0) + { + exfat_close(dev); + exfat_error("failed to read from `%s'", spec); + return 1; + } + if (memcmp(sb.oem_name, "EXFAT ", sizeof(sb.oem_name)) != 0) + { + exfat_close(dev); + exfat_error("exFAT file system is not found on `%s'", spec); + return 1; + } + + print_generic_info(&sb); + print_sector_info(&sb); + print_cluster_info(&sb); + print_other_info(&sb); + + exfat_close(dev); + return 0; +} + +static void dump_sectors(struct exfat* ef) +{ + off_t a = 0, b = 0; + + printf("Used sectors "); + while (exfat_find_used_sectors(ef, &a, &b) == 0) + printf(" %"PRIu64"-%"PRIu64, a, b); + puts(""); +} + +static int dump_full(const char* spec, bool used_sectors) +{ + struct exfat ef; + uint32_t free_clusters; + uint64_t free_sectors; + + if (exfat_mount(&ef, spec, "ro") != 0) + return 1; + + free_clusters = exfat_count_free_clusters(&ef); + free_sectors = (uint64_t) free_clusters << ef.sb->spc_bits; + + printf("Volume label %15s\n", exfat_get_label(&ef)); + print_generic_info(ef.sb); + print_sector_info(ef.sb); + printf("Free sectors %10"PRIu64"\n", free_sectors); + print_cluster_info(ef.sb); + printf("Free clusters %10u\n", free_clusters); + print_other_info(ef.sb); + if (used_sectors) + dump_sectors(&ef); + + exfat_unmount(&ef); + return 0; +} + +static void usage(const char* prog) +{ + fprintf(stderr, "Usage: %s [-s] [-u] [-v] \n", prog); + exit(1); +} + +int main(int argc, char* argv[]) +{ + char** pp; + const char* spec = NULL; + bool sb_only = false; + bool used_sectors = false; + + printf("dumpexfat %u.%u.%u\n", + EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); + + for (pp = argv + 1; *pp; pp++) + { + if (strcmp(*pp, "-s") == 0) + sb_only = true; + else if (strcmp(*pp, "-u") == 0) + used_sectors = true; + else if (strcmp(*pp, "-v") == 0) + { + puts("Copyright (C) 2011, 2012 Andrew Nayenko"); + return 0; + } + else if (spec == NULL) + spec = *pp; + else + usage(argv[0]); + } + if (spec == NULL) + usage(argv[0]); + + if (sb_only) + return dump_sb(spec); + + return dump_full(spec, used_sectors); +} diff --git a/exfat/exfat-fuse/Android.mk b/exfat/exfat-fuse/Android.mk new file mode 100644 index 000000000..96278352f --- /dev/null +++ b/exfat/exfat-fuse/Android.mk @@ -0,0 +1,22 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE := exfat-fuse +LOCAL_MODULE_CLASS := RECOVERY_EXECUTABLES +LOCAL_MODULE_TAGS := eng +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +LOCAL_MODULE_TAGS := optional +LOCAL_CFLAGS = -D_FILE_OFFSET_BITS=64 +LOCAL_SRC_FILES = main.c +LOCAL_C_INCLUDES += $(LOCAL_PATH) \ + bootable/recovery/exfat/libexfat \ + bootable/recovery/fuse/include +LOCAL_SHARED_LIBRARIES += libz libc libexfat libdl +LOCAL_STATIC_LIBRARIES += libfuse + +include $(BUILD_EXECUTABLE) + +# We need this so that the installed files could be picked up based on the +# local module name +ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \ + $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS) diff --git a/exfat/exfat-fuse/main.c b/exfat/exfat-fuse/main.c new file mode 100644 index 000000000..6771096f3 --- /dev/null +++ b/exfat/exfat-fuse/main.c @@ -0,0 +1,543 @@ +/* + main.c (01.09.09) + FUSE-based exFAT implementation. Requires FUSE 2.6 or later. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#define FUSE_USE_VERSION 26 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define exfat_debug(format, ...) + +#if !defined(FUSE_VERSION) || (FUSE_VERSION < 26) + #error FUSE 2.6 or later is required +#endif + +const char* default_options = "ro_fallback,allow_other,blkdev,big_writes"; + +struct exfat ef; + +static struct exfat_node* get_node(const struct fuse_file_info* fi) +{ + return (struct exfat_node*) (size_t) fi->fh; +} + +static void set_node(struct fuse_file_info* fi, struct exfat_node* node) +{ + fi->fh = (uint64_t) (size_t) node; +} + +static int fuse_exfat_getattr(const char* path, struct stat* stbuf) +{ + struct exfat_node* node; + int rc; + + exfat_debug("[%s] %s", __func__, path); + + rc = exfat_lookup(&ef, &node, path); + if (rc != 0) + return rc; + + exfat_stat(&ef, node, stbuf); + exfat_put_node(&ef, node); + return 0; +} + +static int fuse_exfat_truncate(const char* path, off64_t size) +{ + struct exfat_node* node; + int rc; + + exfat_debug("[%s] %s, %"PRId64, __func__, path, size); + + rc = exfat_lookup(&ef, &node, path); + if (rc != 0) + return rc; + + rc = exfat_truncate(&ef, node, size); + exfat_put_node(&ef, node); + return rc; +} + +static int fuse_exfat_readdir(const char* path, void* buffer, + fuse_fill_dir_t filler, off64_t offset, struct fuse_file_info* fi) +{ + struct exfat_node* parent; + struct exfat_node* node; + struct exfat_iterator it; + int rc; + char name[EXFAT_NAME_MAX + 1]; + + exfat_debug("[%s] %s", __func__, path); + + rc = exfat_lookup(&ef, &parent, path); + if (rc != 0) + return rc; + if (!(parent->flags & EXFAT_ATTRIB_DIR)) + { + exfat_put_node(&ef, parent); + exfat_error("`%s' is not a directory (0x%x)", path, parent->flags); + return -ENOTDIR; + } + + filler(buffer, ".", NULL, 0); + filler(buffer, "..", NULL, 0); + + rc = exfat_opendir(&ef, parent, &it); + if (rc != 0) + { + exfat_put_node(&ef, parent); + exfat_error("failed to open directory `%s'", path); + return rc; + } + while ((node = exfat_readdir(&ef, &it))) + { + exfat_get_name(node, name, EXFAT_NAME_MAX); + exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__, + name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", + node->size, node->start_cluster); + filler(buffer, name, NULL, 0); + exfat_put_node(&ef, node); + } + exfat_closedir(&ef, &it); + exfat_put_node(&ef, parent); + return 0; +} + +static int fuse_exfat_open(const char* path, struct fuse_file_info* fi) +{ + struct exfat_node* node; + int rc; + + exfat_debug("[%s] %s", __func__, path); + + rc = exfat_lookup(&ef, &node, path); + if (rc != 0) + return rc; + set_node(fi, node); + fi->keep_cache = 1; + return 0; +} + +static int fuse_exfat_release(const char* path, struct fuse_file_info* fi) +{ + exfat_debug("[%s] %s", __func__, path); + exfat_put_node(&ef, get_node(fi)); + return 0; +} + +static int fuse_exfat_read(const char* path, char* buffer, size_t size, + off64_t offset, struct fuse_file_info* fi) +{ + exfat_debug("[%s] %s (%zu bytes)", __func__, path, size); + if (exfat_generic_pread(&ef, get_node(fi), buffer, size, offset) != size) + return EOF; + return size; +} + +static int fuse_exfat_write(const char* path, const char* buffer, size_t size, + off64_t offset, struct fuse_file_info* fi) +{ + exfat_debug("[%s] %s (%zu bytes)", __func__, path, size); + if (exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset) != size) + return EOF; + return size; +} + +static int fuse_exfat_unlink(const char* path) +{ + struct exfat_node* node; + int rc; + + exfat_debug("[%s] %s", __func__, path); + + rc = exfat_lookup(&ef, &node, path); + if (rc != 0) + return rc; + + rc = exfat_unlink(&ef, node); + exfat_put_node(&ef, node); + return rc; +} + +static int fuse_exfat_rmdir(const char* path) +{ + struct exfat_node* node; + int rc; + + exfat_debug("[%s] %s", __func__, path); + + rc = exfat_lookup(&ef, &node, path); + if (rc != 0) + return rc; + + rc = exfat_rmdir(&ef, node); + exfat_put_node(&ef, node); + return rc; +} + +static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev) +{ + exfat_debug("[%s] %s 0%ho", __func__, path, mode); + return exfat_mknod(&ef, path); +} + +static int fuse_exfat_mkdir(const char* path, mode_t mode) +{ + exfat_debug("[%s] %s 0%ho", __func__, path, mode); + return exfat_mkdir(&ef, path); +} + +static int fuse_exfat_rename(const char* old_path, const char* new_path) +{ + exfat_debug("[%s] %s => %s", __func__, old_path, new_path); + return exfat_rename(&ef, old_path, new_path); +} + +static int fuse_exfat_utimens(const char* path, const struct timespec tv[2]) +{ + struct exfat_node* node; + int rc; + + exfat_debug("[%s] %s", __func__, path); + + rc = exfat_lookup(&ef, &node, path); + if (rc != 0) + return rc; + + exfat_utimes(node, tv); + exfat_put_node(&ef, node); + return 0; +} + +#ifdef __APPLE__ +static int fuse_exfat_chmod(const char* path, mode_t mode) +{ + exfat_debug("[%s] %s 0%ho", __func__, path, mode); + /* make OS X utilities happy */ + return 0; +} +#endif + +static int fuse_exfat_statfs(const char* path, struct statvfs* sfs) +{ + exfat_debug("[%s]", __func__); + + sfs->f_bsize = CLUSTER_SIZE(*ef.sb); + sfs->f_frsize = CLUSTER_SIZE(*ef.sb); + sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits; + sfs->f_bavail = exfat_count_free_clusters(&ef); + sfs->f_bfree = sfs->f_bavail; + sfs->f_namemax = EXFAT_NAME_MAX; + + /* + Below are fake values because in exFAT there is + a) no simple way to count files; + b) no such thing as inode; + So here we assume that inode = cluster. + */ + sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits; + sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits; + sfs->f_ffree = sfs->f_bavail; + + return 0; +} + +static void* fuse_exfat_init(struct fuse_conn_info* fci) +{ + exfat_debug("[%s]", __func__); +#ifdef FUSE_CAP_BIG_WRITES + fci->want |= FUSE_CAP_BIG_WRITES; +#endif + return NULL; +} + +static void fuse_exfat_destroy(void* unused) +{ + exfat_debug("[%s]", __func__); + exfat_unmount(&ef); +} + +static void usage(const char* prog) +{ + fprintf(stderr, "Usage: %s [-d] [-o options] [-v] \n", prog); + exit(1); +} + +static struct fuse_operations fuse_exfat_ops = +{ + .getattr = fuse_exfat_getattr, + .truncate = fuse_exfat_truncate, + .readdir = fuse_exfat_readdir, + .open = fuse_exfat_open, + .release = fuse_exfat_release, + .read = fuse_exfat_read, + .write = fuse_exfat_write, + .unlink = fuse_exfat_unlink, + .rmdir = fuse_exfat_rmdir, + .mknod = fuse_exfat_mknod, + .mkdir = fuse_exfat_mkdir, + .rename = fuse_exfat_rename, + .utimens = fuse_exfat_utimens, +#ifdef __APPLE__ + .chmod = fuse_exfat_chmod, +#endif + .statfs = fuse_exfat_statfs, + .init = fuse_exfat_init, + .destroy = fuse_exfat_destroy, +}; + +static char* add_option(char* options, const char* name, const char* value) +{ + size_t size; + + if (value) + size = strlen(options) + strlen(name) + strlen(value) + 3; + else + size = strlen(options) + strlen(name) + 2; + + options = realloc(options, size); + if (options == NULL) + { + exfat_error("failed to reallocate options string"); + return NULL; + } + strcat(options, ","); + strcat(options, name); + if (value) + { + strcat(options, "="); + strcat(options, value); + } + return options; +} + +static char* add_fsname_option(char* options, const char* spec) +{ + char spec_abs[PATH_MAX]; + + if (realpath(spec, spec_abs) == NULL) + { + free(options); + exfat_error("failed to get absolute path for `%s'", spec); + return NULL; + } + return add_option(options, "fsname", spec_abs); +} + +static char* add_user_option(char* options) +{ + struct passwd* pw; + + if (getuid() == 0) + return options; + + pw = getpwuid(getuid()); + if (pw == NULL || pw->pw_name == NULL) + { + free(options); + exfat_error("failed to determine username"); + return NULL; + } + return add_option(options, "user", pw->pw_name); +} + +static char* add_blksize_option(char* options, long cluster_size) +{ + long page_size = sysconf(_SC_PAGESIZE); + char blksize[20]; + + if (page_size < 1) + page_size = 0x1000; + + snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size)); + return add_option(options, "blksize", blksize); +} + +static char* add_fuse_options(char* options, const char* spec) +{ + options = add_fsname_option(options, spec); + if (options == NULL) + return NULL; + options = add_user_option(options); + if (options == NULL) + return NULL; + options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb)); + if (options == NULL) + return NULL; + + return options; +} + +int main(int argc, char* argv[]) +{ + struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL); + struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL); + const char* spec = NULL; + const char* mount_point = NULL; + char* mount_options; + int debug = 0; + struct fuse_chan* fc = NULL; + struct fuse* fh = NULL; + char** pp; + + printf("FUSE exfat %u.%u.%u\n", + EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); + + mount_options = strdup(default_options); + if (mount_options == NULL) + { + exfat_error("failed to allocate options string"); + return 1; + } + + for (pp = argv + 1; *pp; pp++) + { + if (strcmp(*pp, "-o") == 0) + { + pp++; + if (*pp == NULL) + usage(argv[0]); + mount_options = add_option(mount_options, *pp, NULL); + if (mount_options == NULL) + return 1; + } + else if (strcmp(*pp, "-d") == 0) + debug = 1; + else if (strcmp(*pp, "-v") == 0) + { + free(mount_options); + puts("Copyright (C) 2010-2012 Andrew Nayenko"); + return 0; + } + else if (spec == NULL) + spec = *pp; + else if (mount_point == NULL) + mount_point = *pp; + else + { + free(mount_options); + usage(argv[0]); + } + } + if (spec == NULL || mount_point == NULL) + { + free(mount_options); + usage(argv[0]); + } + + if (exfat_mount(&ef, spec, mount_options) != 0) + { + free(mount_options); + return 1; + } + + if (ef.ro == -1) /* read-only fallback was used */ + { + mount_options = add_option(mount_options, "ro", NULL); + if (mount_options == NULL) + { + exfat_unmount(&ef); + return 1; + } + } + + mount_options = add_fuse_options(mount_options, spec); + if (mount_options == NULL) + { + exfat_unmount(&ef); + return 1; + } + + /* create arguments for fuse_mount() */ + if (fuse_opt_add_arg(&mount_args, "exfat") != 0 || + fuse_opt_add_arg(&mount_args, "-o") != 0 || + fuse_opt_add_arg(&mount_args, mount_options) != 0) + { + exfat_unmount(&ef); + free(mount_options); + return 1; + } + + free(mount_options); + + /* create FUSE mount point */ + fc = fuse_mount(mount_point, &mount_args); + fuse_opt_free_args(&mount_args); + if (fc == NULL) + { + exfat_unmount(&ef); + return 1; + } + + /* create arguments for fuse_new() */ + if (fuse_opt_add_arg(&newfs_args, "") != 0 || + (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0)) + { + fuse_unmount(mount_point, fc); + exfat_unmount(&ef); + return 1; + } + + /* create new FUSE file system */ + fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops, + sizeof(struct fuse_operations), NULL); + fuse_opt_free_args(&newfs_args); + if (fh == NULL) + { + fuse_unmount(mount_point, fc); + exfat_unmount(&ef); + return 1; + } + + /* exit session on HUP, TERM and INT signals and ignore PIPE signal */ + if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0) + { + fuse_unmount(mount_point, fc); + fuse_destroy(fh); + exfat_unmount(&ef); + exfat_error("failed to set signal handlers"); + return 1; + } + + /* go to background (unless "-d" option is passed) and run FUSE + main loop */ + if (fuse_daemonize(debug) == 0) + { + if (fuse_loop(fh) != 0) + exfat_error("FUSE loop failure"); + } + else + exfat_error("failed to daemonize"); + + fuse_remove_signal_handlers(fuse_get_session(fh)); + /* note that fuse_unmount() must be called BEFORE fuse_destroy() */ + fuse_unmount(mount_point, fc); + fuse_destroy(fh); + return 0; +} diff --git a/exfat/exfat-fuse/mount.exfat-fuse.8 b/exfat/exfat-fuse/mount.exfat-fuse.8 new file mode 100644 index 000000000..83d2e6301 --- /dev/null +++ b/exfat/exfat-fuse/mount.exfat-fuse.8 @@ -0,0 +1,76 @@ +.\" Copyright (C) 2010 Andrew Nayenko +.\" +.TH EXFAT-FUSE 8 "July 2010" +.SH NAME +mount.exfat-fuse \- mount an exFAT file system +.SH SYNOPSIS +.B mount.exfat-fuse +[ +.B \-d +] +[ +.B \-o +.I options +] +[ +.B \-v +] +.I device dir + +.SH DESCRIPTION +.B mount.exfat-fuse +is a free exFAT file system implementation with write support. exFAT is a +simple file system created by Microsoft. It is intended to replace FAT32 +removing some of it's limitations. exFAT is a standard FS for SDXC memory +cards. + +.SH COMMAND LINE OPTIONS +Command line options available: +.TP +.BI \-d +Enable debug logging and do not detach from shell. +.TP +.BI \-o " options" +File system specific options. For more details see +.B FILE SYSTEM OPTIONS +section below. +.TP +.BI \-v +Print version and copyright. + +.SH FILE SYSTEM OPTIONS +.TP +.BI umask= value +Set the umask (the bitmask of the permissions that are +.B not +present, in octal). +The default is the umask of the current process. +.TP +.BI dmask= value +Set the umask for directories only. +.TP +.BI fmask= value +Set the umask for files only. +.TP +.BI uid= n +Set the owner for all files and directories. +The default is the owner of the current process. +.TP +.BI gid= n +Set the group for all files and directories. +The default is the group of the current process. +.TP +.BI ro +Mount the file system in read only mode. +.TP +.BI noatime +Do not update access time when file is read. + +.SH EXIT CODES +Zero is returned on successful mount. Any other code means an error. + +.SH AUTHOR +Andrew Nayenko + +.SH SEE ALSO +.BR mount (8) diff --git a/exfat/fsck/exfatfsck b/exfat/fsck/exfatfsck new file mode 100755 index 000000000..610faed43 Binary files /dev/null and b/exfat/fsck/exfatfsck differ diff --git a/exfat/fsck/exfatfsck.8 b/exfat/fsck/exfatfsck.8 new file mode 100644 index 000000000..985500cd7 --- /dev/null +++ b/exfat/fsck/exfatfsck.8 @@ -0,0 +1,32 @@ +.\" Copyright (C) 2011 Andrew Nayenko +.\" +.TH EXFATFSCK 8 "February 2011" +.SH NAME +.B exfatfsck +\- check an exFAT file system +.SH SYNOPSIS +.B exfatfsck +[ +.B \-v +] +.I device + +.SH DESCRIPTION +.B exfatfsck +checks an exFAT file system for errors. Note that it cannot repair corrupted +FS, it just reports found errors. + +.SH COMMAND LINE OPTIONS +Command line options available: +.TP +.BI \-v +Print version and copyright. + +.SH EXIT CODES +Zero is returned if errors were not found. Any other code means an error. + +.SH AUTHOR +Andrew Nayenko + +.SH SEE ALSO +.BR fsck (8) diff --git a/exfat/fsck/main.c b/exfat/fsck/main.c new file mode 100644 index 000000000..c90e3f94b --- /dev/null +++ b/exfat/fsck/main.c @@ -0,0 +1,172 @@ +/* + main.c (02.09.09) + exFAT file system checker. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include + +#define exfat_debug(format, ...) + +uint64_t files_count, directories_count; + +static int nodeck(struct exfat* ef, struct exfat_node* node) +{ + const cluster_t cluster_size = CLUSTER_SIZE(*ef->sb); + cluster_t clusters = (node->size + cluster_size - 1) / cluster_size; + cluster_t c = node->start_cluster; + int rc = 0; + + while (clusters--) + { + if (CLUSTER_INVALID(c)) + { + char name[EXFAT_NAME_MAX + 1]; + + exfat_get_name(node, name, EXFAT_NAME_MAX); + exfat_error("file `%s' has invalid cluster 0x%x", name, c); + rc = 1; + break; + } + if (BMAP_GET(ef->cmap.chunk, c - EXFAT_FIRST_DATA_CLUSTER) == 0) + { + char name[EXFAT_NAME_MAX + 1]; + + exfat_get_name(node, name, EXFAT_NAME_MAX); + exfat_error("cluster 0x%x of file `%s' is not allocated", c, name); + rc = 1; + } + c = exfat_next_cluster(ef, node, c); + } + return rc; +} + +static void dirck(struct exfat* ef, const char* path) +{ + struct exfat_node* parent; + struct exfat_node* node; + struct exfat_iterator it; + int rc; + size_t path_length; + char* entry_path; + + if (exfat_lookup(ef, &parent, path) != 0) + exfat_bug("directory `%s' is not found", path); + if (!(parent->flags & EXFAT_ATTRIB_DIR)) + exfat_bug("`%s' is not a directory (0x%x)", path, parent->flags); + if (nodeck(ef, parent) != 0) + return; + + path_length = strlen(path); + entry_path = malloc(path_length + 1 + EXFAT_NAME_MAX); + if (entry_path == NULL) + { + exfat_error("out of memory"); + return; + } + strcpy(entry_path, path); + strcat(entry_path, "/"); + + rc = exfat_opendir(ef, parent, &it); + if (rc != 0) + { + free(entry_path); + exfat_put_node(ef, parent); + exfat_error("failed to open directory `%s'", path); + return; + } + while ((node = exfat_readdir(ef, &it))) + { + exfat_get_name(node, entry_path + path_length + 1, EXFAT_NAME_MAX); + exfat_debug("%s: %s, %"PRIu64" bytes, cluster %u", entry_path, + IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented", + node->size, node->start_cluster); + if (node->flags & EXFAT_ATTRIB_DIR) + { + directories_count++; + dirck(ef, entry_path); + } + else + { + files_count++; + nodeck(ef, node); + } + exfat_put_node(ef, node); + } + exfat_closedir(ef, &it); + exfat_put_node(ef, parent); + free(entry_path); +} + +static void fsck(struct exfat* ef) +{ + exfat_print_info(ef->sb, exfat_count_free_clusters(ef)); + dirck(ef, ""); +} + +static void usage(const char* prog) +{ + fprintf(stderr, "Usage: %s [-v] \n", prog); + exit(1); +} + +int main(int argc, char* argv[]) +{ + char** pp; + const char* spec = NULL; + struct exfat ef; + + printf("exfatfsck %u.%u.%u\n", + EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); + + for (pp = argv + 1; *pp; pp++) + { + if (strcmp(*pp, "-v") == 0) + { + puts("Copyright (C) 2011, 2012 Andrew Nayenko"); + return 0; + } + else if (spec == NULL) + spec = *pp; + else + usage(argv[0]); + } + if (spec == NULL) + usage(argv[0]); + + if (exfat_mount(&ef, spec, "ro") != 0) + return 1; + + printf("Checking file system on %s.\n", spec); + fsck(&ef); + exfat_unmount(&ef); + printf("Totally %"PRIu64" directories and %"PRIu64" files.\n", + directories_count, files_count); + + fputs("File system checking finished. ", stdout); + if (exfat_errors != 0) + { + printf("ERRORS FOUND: %d.\n", exfat_errors); + return 1; + } + puts("No errors found."); + return 0; +} diff --git a/exfat/label/exfatlabel.8 b/exfat/label/exfatlabel.8 new file mode 100644 index 000000000..429f3cc02 --- /dev/null +++ b/exfat/label/exfatlabel.8 @@ -0,0 +1,48 @@ +.\" Copyright (C) 2011 Andrew Nayenko +.\" +.TH EXFATLABEL 8 "February 2011" +.SH NAME +.B exfatlabel +\- get or set an exFAT file system label +.SH SYNOPSIS +.B exfatlabel +[ +.B \-v +] +.I device +[ +.I label +] + +.SH DESCRIPTION +.B exfatlabel +reads or changes an exFAT file system label (volume name). + +If +.I label +argument is present, +.B exfatlabel +sets the new volume name. Label can be up to 15 characters. This limit is +shorter if characters beyond Unicode BMP are used because internally label +is stored in UTF-16. + +If +.I label +argument is omitted, +.B exfatlabel +just prints current volume name. + +.SH COMMAND LINE OPTIONS +Command line options available: +.TP +.BI \-v +Print version and copyright. + +.SH EXIT CODES +Zero is returned on success. Any other code means an error. + +.SH AUTHOR +Andrew Nayenko + +.SH SEE ALSO +.BR mkexfatfs (8) diff --git a/exfat/label/main.c b/exfat/label/main.c new file mode 100644 index 000000000..b3a3d3036 --- /dev/null +++ b/exfat/label/main.c @@ -0,0 +1,61 @@ +/* + main.c (20.01.11) + Prints or changes exFAT volume label. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include + +int main(int argc, char* argv[]) +{ + char** pp; + struct exfat ef; + int rc = 0; + + for (pp = argv + 1; *pp; pp++) + if (strcmp(*pp, "-v") == 0) + { + printf("exfatlabel %u.%u.%u\n", EXFAT_VERSION_MAJOR, + EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); + puts("Copyright (C) 2011, 2012 Andrew Nayenko"); + return 0; + } + + if (argc != 2 && argc != 3) + { + fprintf(stderr, "Usage: %s [-v] [label]\n", argv[0]); + return 1; + } + + if (argv[2]) + { + if (exfat_mount(&ef, argv[1], "") != 0) + return 1; + rc = (exfat_set_label(&ef, argv[2]) != 0); + } + else + { + if (exfat_mount(&ef, argv[1], "ro") != 0) + return 1; + puts(exfat_get_label(&ef)); + } + + exfat_unmount(&ef); + return rc; +} diff --git a/exfat/libexfat/Android.mk b/exfat/libexfat/Android.mk new file mode 100644 index 000000000..0b7f963a7 --- /dev/null +++ b/exfat/libexfat/Android.mk @@ -0,0 +1,13 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := libexfat +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +LOCAL_CFLAGS = -D_FILE_OFFSET_BITS=64 +LOCAL_SRC_FILES = cluster.c io.c log.c lookup.c mount.c node.c time.c utf.c utils.c +LOCAL_C_INCLUDES += $(LOCAL_PATH) \ +LOCAL_SHARED_LIBRARIES += libc + +include $(BUILD_SHARED_LIBRARY) diff --git a/exfat/libexfat/byteorder.h b/exfat/libexfat/byteorder.h new file mode 100644 index 000000000..4850efb3d --- /dev/null +++ b/exfat/libexfat/byteorder.h @@ -0,0 +1,109 @@ +/* + byteorder.h (12.01.10) + Endianness stuff. exFAT uses little-endian byte order. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef BYTEORDER_H_INCLUDED +#define BYTEORDER_H_INCLUDED + +#define __GLIBC__ +#include + +#if defined(__GLIBC__) + +#include +#include + +#elif defined(__APPLE__) + +#include +#include +#define bswap_16(x) OSSwapInt16(x) +#define bswap_32(x) OSSwapInt32(x) +#define bswap_64(x) OSSwapInt64(x) +#define __BYTE_ORDER BYTE_ORDER +#define __LITTLE_ENDIAN LITTLE_ENDIAN +#define __BIG_ENDIAN BIG_ENDIAN + +#elif defined(__FreeBSD__) || defined(__DragonFlyBSD__) || defined(__NetBSD__) + +#include +#define bswap_16(x) bswap16(x) +#define bswap_32(x) bswap32(x) +#define bswap_64(x) bswap64(x) +#define __BYTE_ORDER _BYTE_ORDER +#define __LITTLE_ENDIAN _LITTLE_ENDIAN +#define __BIG_ENDIAN _BIG_ENDIAN + +#elif defined(__OpenBSD__) + +#include +#define bswap_16(x) swap16(x) +#define bswap_32(x) swap32(x) +#define bswap_64(x) swap64(x) +#define __BYTE_ORDER _BYTE_ORDER +#define __LITTLE_ENDIAN _LITTLE_ENDIAN +#define __BIG_ENDIAN _BIG_ENDIAN + +#elif defined(__sun) + +#include +#define bswap_16(x) BSWAP_16(x) +#define bswap_32(x) BSWAP_32(x) +#define bswap_64(x) BSWAP_64(x) +#define __LITTLE_ENDIAN 1234 +#define __BIG_ENDIAN 4321 +#ifdef _LITTLE_ENDIAN +#define __BYTE_ORDER __LITTLE_ENDIAN +#else +#define __BYTE_ORDER __BIG_ENDIAN +#endif + +#else +#error No byte order macros available for your platform +#endif + +#define __BYTE_ORDER __LITTLE_ENDIAN +typedef struct { uint16_t __u16; } le16_t; +typedef struct { uint32_t __u32; } le32_t; +typedef struct { uint64_t __u64; } le64_t; + +#if __BYTE_ORDER == __LITTLE_ENDIAN +static inline uint16_t le16_to_cpu(le16_t v) { return v.__u16; } +static inline uint32_t le32_to_cpu(le32_t v) { return v.__u32; } +static inline uint64_t le64_to_cpu(le64_t v) { return v.__u64; } + +static inline le16_t cpu_to_le16(uint16_t v) { le16_t t = {v}; return t; } +static inline le32_t cpu_to_le32(uint32_t v) { le32_t t = {v}; return t; } +static inline le64_t cpu_to_le64(uint64_t v) { le64_t t = {v}; return t; } +#elif __BYTE_ORDER == __BIG_ENDIAN +static inline uint16_t le16_to_cpu(le16_t v) { return bswap_16(v.__u16); } +static inline uint32_t le32_to_cpu(le32_t v) { return bswap_32(v.__u32); } +static inline uint64_t le64_to_cpu(le64_t v) { return bswap_64(v.__u64); } + +static inline le16_t cpu_to_le16(uint16_t v) + { le16_t t = {bswap_16(v)}; return t; } +static inline le32_t cpu_to_le32(uint32_t v) + { le32_t t = {bswap_32(v)}; return t; } +static inline le64_t cpu_to_le64(uint64_t v) + { le64_t t = {bswap_64(v)}; return t; } +#else +#error Wow! You have a PDP machine?! +#endif + +#endif /* ifndef BYTEORDER_H_INCLUDED */ diff --git a/exfat/libexfat/cluster.c b/exfat/libexfat/cluster.c new file mode 100644 index 000000000..cb3e87165 --- /dev/null +++ b/exfat/libexfat/cluster.c @@ -0,0 +1,445 @@ +/* + cluster.c (03.09.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include + +/* + * Sector to absolute offset. + */ +static off64_t s2o(const struct exfat* ef, off64_t sector) +{ + return sector << ef->sb->sector_bits; +} + +/* + * Cluster to sector. + */ +static off64_t c2s(const struct exfat* ef, cluster_t cluster) +{ + if (cluster < EXFAT_FIRST_DATA_CLUSTER) + exfat_bug("invalid cluster number %u", cluster); + return le32_to_cpu(ef->sb->cluster_sector_start) + + ((off64_t) (cluster - EXFAT_FIRST_DATA_CLUSTER) << ef->sb->spc_bits); +} + +/* + * Cluster to absolute offset. + */ +off64_t exfat_c2o(const struct exfat* ef, cluster_t cluster) +{ + return s2o(ef, c2s(ef, cluster)); +} + +/* + * Sector to cluster. + */ +static cluster_t s2c(const struct exfat* ef, off64_t sector) +{ + return ((sector - le32_to_cpu(ef->sb->cluster_sector_start)) >> + ef->sb->spc_bits) + EXFAT_FIRST_DATA_CLUSTER; +} + +/* + * Size in bytes to size in clusters (rounded upwards). + */ +static uint32_t bytes2clusters(const struct exfat* ef, uint64_t bytes) +{ + uint64_t cluster_size = CLUSTER_SIZE(*ef->sb); + return (bytes + cluster_size - 1) / cluster_size; +} + +cluster_t exfat_next_cluster(const struct exfat* ef, + const struct exfat_node* node, cluster_t cluster) +{ + le32_t next; + off64_t fat_offset; + + if (cluster < EXFAT_FIRST_DATA_CLUSTER) + exfat_bug("bad cluster 0x%x", cluster); + + if (IS_CONTIGUOUS(*node)) + return cluster + 1; + fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start)) + + cluster * sizeof(cluster_t); + exfat_pread(ef->dev, &next, sizeof(next), fat_offset); + return le32_to_cpu(next); +} + +cluster_t exfat_advance_cluster(const struct exfat* ef, + struct exfat_node* node, uint32_t count) +{ + uint32_t i; + + if (node->fptr_index > count) + { + node->fptr_index = 0; + node->fptr_cluster = node->start_cluster; + } + + for (i = node->fptr_index; i < count; i++) + { + node->fptr_cluster = exfat_next_cluster(ef, node, node->fptr_cluster); + if (CLUSTER_INVALID(node->fptr_cluster)) + break; /* the caller should handle this and print appropriate + error message */ + } + node->fptr_index = count; + return node->fptr_cluster; +} + +static cluster_t find_bit_and_set(uint8_t* bitmap, cluster_t start, + cluster_t end) +{ + const cluster_t mid_start = (start + 7) / 8 * 8; + const cluster_t mid_end = end / 8 * 8; + cluster_t c; + cluster_t byte; + + for (c = start; c < mid_start; c++) + if (BMAP_GET(bitmap, c) == 0) + { + BMAP_SET(bitmap, c); + return c + EXFAT_FIRST_DATA_CLUSTER; + } + + for (byte = mid_start / 8; byte < mid_end / 8; byte++) + if (bitmap[byte] != 0xff) + { + cluster_t bit; + + for (bit = 0; bit < 8; bit++) + if (!(bitmap[byte] & (1u << bit))) + { + bitmap[byte] |= (1u << bit); + return byte * 8 + bit + EXFAT_FIRST_DATA_CLUSTER; + } + } + + for (c = mid_end; c < end; c++) + if (BMAP_GET(bitmap, c) == 0) + { + BMAP_SET(bitmap, c); + return c + EXFAT_FIRST_DATA_CLUSTER; + } + + return EXFAT_CLUSTER_END; +} + +void exfat_flush_cmap(struct exfat* ef) +{ + exfat_pwrite(ef->dev, ef->cmap.chunk, (ef->cmap.chunk_size + 7) / 8, + exfat_c2o(ef, ef->cmap.start_cluster)); + ef->cmap.dirty = false; +} + +static void set_next_cluster(const struct exfat* ef, int contiguous, + cluster_t current, cluster_t next) +{ + off64_t fat_offset; + le32_t next_le32; + + if (contiguous) + return; + fat_offset = s2o(ef, le32_to_cpu(ef->sb->fat_sector_start)) + + current * sizeof(cluster_t); + next_le32 = cpu_to_le32(next); + exfat_pwrite(ef->dev, &next_le32, sizeof(next_le32), fat_offset); +} + +static cluster_t allocate_cluster(struct exfat* ef, cluster_t hint) +{ + cluster_t cluster; + + hint -= EXFAT_FIRST_DATA_CLUSTER; + if (hint >= ef->cmap.chunk_size) + hint = 0; + + cluster = find_bit_and_set(ef->cmap.chunk, hint, ef->cmap.chunk_size); + if (cluster == EXFAT_CLUSTER_END) + cluster = find_bit_and_set(ef->cmap.chunk, 0, hint); + if (cluster == EXFAT_CLUSTER_END) + { + exfat_error("no free space left"); + return EXFAT_CLUSTER_END; + } + + ef->cmap.dirty = true; + return cluster; +} + +static void free_cluster(struct exfat* ef, cluster_t cluster) +{ + if (CLUSTER_INVALID(cluster)) + exfat_bug("freeing invalid cluster 0x%x", cluster); + if (cluster - EXFAT_FIRST_DATA_CLUSTER >= ef->cmap.size) + exfat_bug("freeing non-existing cluster 0x%x (0x%x)", cluster, + ef->cmap.size); + + BMAP_CLR(ef->cmap.chunk, cluster - EXFAT_FIRST_DATA_CLUSTER); + ef->cmap.dirty = true; +} + +static void make_noncontiguous(const struct exfat* ef, cluster_t first, + cluster_t last) +{ + cluster_t c; + + for (c = first; c < last; c++) + set_next_cluster(ef, 0, c, c + 1); +} + +static int shrink_file(struct exfat* ef, struct exfat_node* node, + uint32_t current, uint32_t difference); + +static int grow_file(struct exfat* ef, struct exfat_node* node, + uint32_t current, uint32_t difference) +{ + cluster_t previous; + cluster_t next; + uint32_t allocated = 0; + + if (difference == 0) + exfat_bug("zero clusters count passed"); + + if (node->start_cluster != EXFAT_CLUSTER_FREE) + { + /* get the last cluster of the file */ + previous = exfat_advance_cluster(ef, node, current - 1); + if (CLUSTER_INVALID(previous)) + { + exfat_error("invalid cluster 0x%x while growing", previous); + return -EIO; + } + } + else + { + if (node->fptr_index != 0) + exfat_bug("non-zero pointer index (%u)", node->fptr_index); + /* file does not have clusters (i.e. is empty), allocate + the first one for it */ + previous = allocate_cluster(ef, 0); + if (CLUSTER_INVALID(previous)) + return -ENOSPC; + node->fptr_cluster = node->start_cluster = previous; + allocated = 1; + /* file consists of only one cluster, so it's contiguous */ + node->flags |= EXFAT_ATTRIB_CONTIGUOUS; + } + + while (allocated < difference) + { + next = allocate_cluster(ef, previous + 1); + if (CLUSTER_INVALID(next)) + { + if (allocated != 0) + shrink_file(ef, node, current + allocated, allocated); + return -ENOSPC; + } + if (next != previous - 1 && IS_CONTIGUOUS(*node)) + { + /* it's a pity, but we are not able to keep the file contiguous + anymore */ + make_noncontiguous(ef, node->start_cluster, previous); + node->flags &= ~EXFAT_ATTRIB_CONTIGUOUS; + node->flags |= EXFAT_ATTRIB_DIRTY; + } + set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, next); + previous = next; + allocated++; + } + + set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, EXFAT_CLUSTER_END); + return 0; +} + +static int shrink_file(struct exfat* ef, struct exfat_node* node, + uint32_t current, uint32_t difference) +{ + cluster_t previous; + cluster_t next; + + if (difference == 0) + exfat_bug("zero difference passed"); + if (node->start_cluster == EXFAT_CLUSTER_FREE) + exfat_bug("unable to shrink empty file (%u clusters)", current); + if (current < difference) + exfat_bug("file underflow (%u < %u)", current, difference); + + /* crop the file */ + if (current > difference) + { + cluster_t last = exfat_advance_cluster(ef, node, + current - difference - 1); + if (CLUSTER_INVALID(last)) + { + exfat_error("invalid cluster 0x%x while shrinking", last); + return -EIO; + } + previous = exfat_next_cluster(ef, node, last); + set_next_cluster(ef, IS_CONTIGUOUS(*node), last, EXFAT_CLUSTER_END); + } + else + { + previous = node->start_cluster; + node->start_cluster = EXFAT_CLUSTER_FREE; + } + node->fptr_index = 0; + node->fptr_cluster = node->start_cluster; + + /* free remaining clusters */ + while (difference--) + { + if (CLUSTER_INVALID(previous)) + { + exfat_error("invalid cluster 0x%x while freeing after shrink", + previous); + return -EIO; + } + next = exfat_next_cluster(ef, node, previous); + set_next_cluster(ef, IS_CONTIGUOUS(*node), previous, + EXFAT_CLUSTER_FREE); + free_cluster(ef, previous); + previous = next; + } + return 0; +} + +static void erase_raw(struct exfat* ef, size_t size, off64_t offset) +{ + exfat_pwrite(ef->dev, ef->zero_cluster, size, offset); +} + +static int erase_range(struct exfat* ef, struct exfat_node* node, + uint64_t begin, uint64_t end) +{ + uint64_t cluster_boundary; + cluster_t cluster; + + if (begin >= end) + return 0; + + cluster_boundary = (begin | (CLUSTER_SIZE(*ef->sb) - 1)) + 1; + cluster = exfat_advance_cluster(ef, node, + begin / CLUSTER_SIZE(*ef->sb)); + if (CLUSTER_INVALID(cluster)) + { + exfat_error("invalid cluster 0x%x while erasing", cluster); + return -EIO; + } + /* erase from the beginning to the closest cluster boundary */ + erase_raw(ef, MIN(cluster_boundary, end) - begin, + exfat_c2o(ef, cluster) + begin % CLUSTER_SIZE(*ef->sb)); + /* erase whole clusters */ + while (cluster_boundary < end) + { + cluster = exfat_next_cluster(ef, node, cluster); + /* the cluster cannot be invalid because we have just allocated it */ + if (CLUSTER_INVALID(cluster)) + exfat_bug("invalid cluster 0x%x after allocation", cluster); + erase_raw(ef, CLUSTER_SIZE(*ef->sb), exfat_c2o(ef, cluster)); + cluster_boundary += CLUSTER_SIZE(*ef->sb); + } + return 0; +} + +int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size) +{ + uint32_t c1 = bytes2clusters(ef, node->size); + uint32_t c2 = bytes2clusters(ef, size); + int rc = 0; + + if (node->references == 0 && node->parent) + exfat_bug("no references, node changes can be lost"); + + if (node->size == size) + return 0; + + if (c1 < c2) + rc = grow_file(ef, node, c1, c2 - c1); + else if (c1 > c2) + rc = shrink_file(ef, node, c1, c1 - c2); + + if (rc != 0) + return rc; + + rc = erase_range(ef, node, node->size, size); + if (rc != 0) + return rc; + + exfat_update_mtime(node); + node->size = size; + node->flags |= EXFAT_ATTRIB_DIRTY; + return 0; +} + +uint32_t exfat_count_free_clusters(const struct exfat* ef) +{ + uint32_t free_clusters = 0; + uint32_t i; + + for (i = 0; i < ef->cmap.size; i++) + if (BMAP_GET(ef->cmap.chunk, i) == 0) + free_clusters++; + return free_clusters; +} + +static int find_used_clusters(const struct exfat* ef, + cluster_t* a, cluster_t* b) +{ + const cluster_t end = le32_to_cpu(ef->sb->cluster_count); + + /* find first used cluster */ + for (*a = *b + 1; *a < end; (*a)++) + if (BMAP_GET(ef->cmap.chunk, *a - EXFAT_FIRST_DATA_CLUSTER)) + break; + if (*a >= end) + return 1; + + /* find last contiguous used cluster */ + for (*b = *a; *b < end; (*b)++) + if (BMAP_GET(ef->cmap.chunk, *b - EXFAT_FIRST_DATA_CLUSTER) == 0) + { + (*b)--; + break; + } + + return 0; +} + +int exfat_find_used_sectors(const struct exfat* ef, off64_t* a, off64_t* b) +{ + cluster_t ca, cb; + + if (*a == 0 && *b == 0) + ca = cb = EXFAT_FIRST_DATA_CLUSTER - 1; + else + { + ca = s2c(ef, *a); + cb = s2c(ef, *b); + } + if (find_used_clusters(ef, &ca, &cb) != 0) + return 1; + if (*a != 0 || *b != 0) + *a = c2s(ef, ca); + *b = c2s(ef, cb) + (CLUSTER_SIZE(*ef->sb) - 1) / SECTOR_SIZE(*ef->sb); + return 0; +} diff --git a/exfat/libexfat/exfat.h b/exfat/libexfat/exfat.h new file mode 100644 index 000000000..6f152670d --- /dev/null +++ b/exfat/libexfat/exfat.h @@ -0,0 +1,215 @@ +/* + exfat.h (29.08.09) + Definitions of structures and constants used in exFAT file system + implementation. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef EXFAT_H_INCLUDED +#define EXFAT_H_INCLUDED + +#include +#include +#include +#include +#include +#include +#include "exfatfs.h" +#include "version.h" + +#define EXFAT_NAME_MAX 256 +#define EXFAT_ATTRIB_CONTIGUOUS 0x10000 +#define EXFAT_ATTRIB_CACHED 0x20000 +#define EXFAT_ATTRIB_DIRTY 0x40000 +#define EXFAT_ATTRIB_UNLINKED 0x80000 +#define IS_CONTIGUOUS(node) (((node).flags & EXFAT_ATTRIB_CONTIGUOUS) != 0) +#define SECTOR_SIZE(sb) (1 << (sb).sector_bits) +#define CLUSTER_SIZE(sb) (SECTOR_SIZE(sb) << (sb).spc_bits) +#define CLUSTER_INVALID(c) \ + ((c) < EXFAT_FIRST_DATA_CLUSTER || (c) > EXFAT_LAST_DATA_CLUSTER) + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#define DIV_ROUND_UP(x, d) (((x) + (d) - 1) / (d)) +#define ROUND_UP(x, d) (DIV_ROUND_UP(x, d) * (d)) + +#define BMAP_GET(bitmap, index) \ + (((uint8_t*) bitmap)[(index) / 8] & (1u << ((index) % 8))) +#define BMAP_SET(bitmap, index) \ + ((uint8_t*) bitmap)[(index) / 8] |= (1u << ((index) % 8)) +#define BMAP_CLR(bitmap, index) \ + ((uint8_t*) bitmap)[(index) / 8] &= ~(1u << ((index) % 8)) + +struct exfat_node +{ + struct exfat_node* parent; + struct exfat_node* child; + struct exfat_node* next; + struct exfat_node* prev; + + int references; + uint32_t fptr_index; + cluster_t fptr_cluster; + cluster_t entry_cluster; + off64_t entry_offset; + cluster_t start_cluster; + int flags; + uint64_t size; + time_t mtime, atime; + le16_t name[EXFAT_NAME_MAX + 1]; +}; + +enum exfat_mode +{ + EXFAT_MODE_RO, + EXFAT_MODE_RW, + EXFAT_MODE_ANY, +}; + +struct exfat_dev; + +struct exfat +{ + struct exfat_dev* dev; + struct exfat_super_block* sb; + le16_t* upcase; + size_t upcase_chars; + struct exfat_node* root; + struct + { + cluster_t start_cluster; + uint32_t size; /* in bits */ + uint8_t* chunk; + uint32_t chunk_size; /* in bits */ + bool dirty; + } + cmap; + char label[EXFAT_ENAME_MAX * 6 + 1]; /* a character can occupy up to + 6 bytes in UTF-8 */ + void* zero_cluster; + int dmask, fmask; + uid_t uid; + gid_t gid; + int ro; + bool noatime; +}; + +/* in-core nodes iterator */ +struct exfat_iterator +{ + struct exfat_node* parent; + struct exfat_node* current; +}; + +struct exfat_human_bytes +{ + uint64_t value; + const char* unit; +}; + +extern int exfat_errors; + +void exfat_bug(const char* format, ...) + __attribute__((format(printf, 1, 2), noreturn)); +void exfat_error(const char* format, ...) + __attribute__((format(printf, 1, 2))); +void exfat_warn(const char* format, ...) + __attribute__((format(printf, 1, 2))); +void exfat_debug(const char* format, ...) + __attribute__((format(printf, 1, 2))); + +struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode); +int exfat_close(struct exfat_dev* dev); +int exfat_fsync(struct exfat_dev* dev); +enum exfat_mode exfat_get_mode(const struct exfat_dev* dev); +off64_t exfat_get_size(const struct exfat_dev* dev); +off64_t exfat_seek(struct exfat_dev* dev, off64_t offset, int whence); +ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size); +ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size); +void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size, + off64_t offset); +void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size, + off64_t offset); +ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node, + void* buffer, size_t size, off64_t offset); +ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node, + const void* buffer, size_t size, off64_t offset); + +int exfat_opendir(struct exfat* ef, struct exfat_node* dir, + struct exfat_iterator* it); +void exfat_closedir(struct exfat* ef, struct exfat_iterator* it); +struct exfat_node* exfat_readdir(struct exfat* ef, struct exfat_iterator* it); +int exfat_lookup(struct exfat* ef, struct exfat_node** node, + const char* path); +int exfat_split(struct exfat* ef, struct exfat_node** parent, + struct exfat_node** node, le16_t* name, const char* path); + +off64_t exfat_c2o(const struct exfat* ef, cluster_t cluster); +cluster_t exfat_next_cluster(const struct exfat* ef, + const struct exfat_node* node, cluster_t cluster); +cluster_t exfat_advance_cluster(const struct exfat* ef, + struct exfat_node* node, uint32_t count); +void exfat_flush_cmap(struct exfat* ef); +int exfat_truncate(struct exfat* ef, struct exfat_node* node, uint64_t size); +uint32_t exfat_count_free_clusters(const struct exfat* ef); +int exfat_find_used_sectors(const struct exfat* ef, off64_t* a, off64_t* b); + +void exfat_stat(const struct exfat* ef, const struct exfat_node* node, + struct stat* stbuf); +void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n); +uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry); +uint16_t exfat_add_checksum(const void* entry, uint16_t sum); +le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1, + const struct exfat_entry_meta2* meta2, const le16_t* name); +uint32_t exfat_vbr_start_checksum(const void* sector, size_t size); +uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum); +le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name); +void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb); +void exfat_print_info(const struct exfat_super_block* sb, + uint32_t free_clusters); + +int utf16_to_utf8(char* output, const le16_t* input, size_t outsize, + size_t insize); +int utf8_to_utf16(le16_t* output, const char* input, size_t outsize, + size_t insize); +size_t utf16_length(const le16_t* str); + +struct exfat_node* exfat_get_node(struct exfat_node* node); +void exfat_put_node(struct exfat* ef, struct exfat_node* node); +int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir); +void exfat_reset_cache(struct exfat* ef); +void exfat_flush_node(struct exfat* ef, struct exfat_node* node); +int exfat_unlink(struct exfat* ef, struct exfat_node* node); +int exfat_rmdir(struct exfat* ef, struct exfat_node* node); +int exfat_mknod(struct exfat* ef, const char* path); +int exfat_mkdir(struct exfat* ef, const char* path); +int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path); +void exfat_utimes(struct exfat_node* node, const struct timespec tv[2]); +void exfat_update_atime(struct exfat_node* node); +void exfat_update_mtime(struct exfat_node* node); +const char* exfat_get_label(struct exfat* ef); +int exfat_set_label(struct exfat* ef, const char* label); + +int exfat_mount(struct exfat* ef, const char* spec, const char* options); +void exfat_unmount(struct exfat* ef); + +time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec); +void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time, + uint8_t* centisec); +void exfat_tzset(void); + +#endif /* ifndef EXFAT_H_INCLUDED */ diff --git a/exfat/libexfat/exfatfs.h b/exfat/libexfat/exfatfs.h new file mode 100644 index 000000000..5a8e39fda --- /dev/null +++ b/exfat/libexfat/exfatfs.h @@ -0,0 +1,163 @@ +/* + exfatfs.h (29.08.09) + Definitions of structures and constants used in exFAT file system. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef EXFATFS_H_INCLUDED +#define EXFATFS_H_INCLUDED + +#include "byteorder.h" + +typedef uint32_t cluster_t; /* cluster number */ + +#define EXFAT_FIRST_DATA_CLUSTER 2 +#define EXFAT_LAST_DATA_CLUSTER 0xfffffff6 + +#define EXFAT_CLUSTER_FREE 0 /* free cluster */ +#define EXFAT_CLUSTER_BAD 0xfffffff7 /* cluster contains bad sector */ +#define EXFAT_CLUSTER_END 0xffffffff /* final cluster of file or directory */ + +#define EXFAT_STATE_MOUNTED 2 + +struct exfat_super_block +{ + uint8_t jump[3]; /* 0x00 jmp and nop instructions */ + uint8_t oem_name[8]; /* 0x03 "EXFAT " */ + uint8_t __unused1[53]; /* 0x0B always 0 */ + le64_t sector_start; /* 0x40 partition first sector */ + le64_t sector_count; /* 0x48 partition sectors count */ + le32_t fat_sector_start; /* 0x50 FAT first sector */ + le32_t fat_sector_count; /* 0x54 FAT sectors count */ + le32_t cluster_sector_start; /* 0x58 first cluster sector */ + le32_t cluster_count; /* 0x5C total clusters count */ + le32_t rootdir_cluster; /* 0x60 first cluster of the root dir */ + le32_t volume_serial; /* 0x64 volume serial number */ + struct /* 0x68 FS version */ + { + uint8_t minor; + uint8_t major; + } + version; + le16_t volume_state; /* 0x6A volume state flags */ + uint8_t sector_bits; /* 0x6C sector size as (1 << n) */ + uint8_t spc_bits; /* 0x6D sectors per cluster as (1 << n) */ + uint8_t fat_count; /* 0x6E always 1 */ + uint8_t drive_no; /* 0x6F always 0x80 */ + uint8_t allocated_percent; /* 0x70 percentage of allocated space */ + uint8_t __unused2[397]; /* 0x71 always 0 */ + le16_t boot_signature; /* the value of 0xAA55 */ +} +__attribute__((__packed__)); + +#define EXFAT_ENTRY_VALID 0x80 +#define EXFAT_ENTRY_CONTINUED 0x40 + +#define EXFAT_ENTRY_BITMAP (0x01 | EXFAT_ENTRY_VALID) +#define EXFAT_ENTRY_UPCASE (0x02 | EXFAT_ENTRY_VALID) +#define EXFAT_ENTRY_LABEL (0x03 | EXFAT_ENTRY_VALID) +#define EXFAT_ENTRY_FILE (0x05 | EXFAT_ENTRY_VALID) +#define EXFAT_ENTRY_FILE_INFO (0x00 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED) +#define EXFAT_ENTRY_FILE_NAME (0x01 | EXFAT_ENTRY_VALID | EXFAT_ENTRY_CONTINUED) + +struct exfat_entry /* common container for all entries */ +{ + uint8_t type; /* any of EXFAT_ENTRY_xxx */ + uint8_t data[31]; +} +__attribute__((__packed__)); + +#define EXFAT_ENAME_MAX 15 + +struct exfat_entry_bitmap /* allocated clusters bitmap */ +{ + uint8_t type; /* EXFAT_ENTRY_BITMAP */ + uint8_t __unknown1[19]; + le32_t start_cluster; + le64_t size; /* in bytes */ +} +__attribute__((__packed__)); + +struct exfat_entry_upcase /* upper case translation table */ +{ + uint8_t type; /* EXFAT_ENTRY_UPCASE */ + uint8_t __unknown1[3]; + le32_t checksum; + uint8_t __unknown2[12]; + le32_t start_cluster; + le64_t size; /* in bytes */ +} +__attribute__((__packed__)); + +struct exfat_entry_label /* volume label */ +{ + uint8_t type; /* EXFAT_ENTRY_LABEL */ + uint8_t length; /* number of characters */ + le16_t name[EXFAT_ENAME_MAX]; /* in UTF-16LE */ +} +__attribute__((__packed__)); + +#define EXFAT_ATTRIB_RO 0x01 +#define EXFAT_ATTRIB_HIDDEN 0x02 +#define EXFAT_ATTRIB_SYSTEM 0x04 +#define EXFAT_ATTRIB_VOLUME 0x08 +#define EXFAT_ATTRIB_DIR 0x10 +#define EXFAT_ATTRIB_ARCH 0x20 + +struct exfat_entry_meta1 /* file or directory info (part 1) */ +{ + uint8_t type; /* EXFAT_ENTRY_FILE */ + uint8_t continuations; + le16_t checksum; + le16_t attrib; /* combination of EXFAT_ATTRIB_xxx */ + le16_t __unknown1; + le16_t crtime, crdate; /* creation date and time */ + le16_t mtime, mdate; /* latest modification date and time */ + le16_t atime, adate; /* latest access date and time */ + uint8_t crtime_cs; /* creation time in cs (centiseconds) */ + uint8_t mtime_cs; /* latest modification time in cs */ + uint8_t __unknown2[10]; +} +__attribute__((__packed__)); + +#define EXFAT_FLAG_ALWAYS1 (1u << 0) +#define EXFAT_FLAG_CONTIGUOUS (1u << 1) + +struct exfat_entry_meta2 /* file or directory info (part 2) */ +{ + uint8_t type; /* EXFAT_ENTRY_FILE_INFO */ + uint8_t flags; /* combination of EXFAT_FLAG_xxx */ + uint8_t __unknown1; + uint8_t name_length; + le16_t name_hash; + le16_t __unknown2; + le64_t real_size; /* in bytes, equals to size */ + uint8_t __unknown3[4]; + le32_t start_cluster; + le64_t size; /* in bytes, equals to real_size */ +} +__attribute__((__packed__)); + +struct exfat_entry_name /* file or directory name */ +{ + uint8_t type; /* EXFAT_ENTRY_FILE_NAME */ + uint8_t __unknown; + le16_t name[EXFAT_ENAME_MAX]; /* in UTF-16LE */ +} +__attribute__((__packed__)); + +#endif /* ifndef EXFATFS_H_INCLUDED */ diff --git a/exfat/libexfat/io.c b/exfat/libexfat/io.c new file mode 100644 index 000000000..65df63a78 --- /dev/null +++ b/exfat/libexfat/io.c @@ -0,0 +1,385 @@ +/* + io.c (02.09.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include +#include +#include +#include +#include +#include +#ifdef __APPLE__ +#include +#endif +#ifdef USE_UBLIO +#include +#include +#endif + +struct exfat_dev +{ + int fd; + enum exfat_mode mode; + off64_t size; /* in bytes */ +#ifdef USE_UBLIO + off64_t pos; + ublio_filehandle_t ufh; +#endif +}; + +static int open_ro(const char* spec) +{ + return open(spec, O_RDONLY); +} + +static int open_rw(const char* spec) +{ + int fd = open(spec, O_RDWR); +#ifdef __linux__ + int ro = 0; + + /* + This ioctl is needed because after "blockdev --setro" kernel still + allows to open the device in read-write mode but fails writes. + */ + if (fd != -1 && ioctl(fd, BLKROGET, &ro) == 0 && ro) + { + close(fd); + return -1; + } +#endif + return fd; +} + +struct exfat_dev* exfat_open(const char* spec, enum exfat_mode mode) +{ + struct exfat_dev* dev; + struct stat stbuf; +#ifdef USE_UBLIO + struct ublio_param up; +#endif + + dev = malloc(sizeof(struct exfat_dev)); + if (dev == NULL) + { + exfat_error("failed to allocate memory for device structure"); + return NULL; + } + + switch (mode) + { + case EXFAT_MODE_RO: + dev->fd = open_ro(spec); + if (dev->fd == -1) + { + free(dev); + exfat_error("failed to open `%s' in read-only mode", spec); + return NULL; + } + dev->mode = EXFAT_MODE_RO; + break; + case EXFAT_MODE_RW: + dev->fd = open_rw(spec); + if (dev->fd == -1) + { + free(dev); + exfat_error("failed to open `%s' in read-write mode", spec); + return NULL; + } + dev->mode = EXFAT_MODE_RW; + break; + case EXFAT_MODE_ANY: + dev->fd = open_rw(spec); + if (dev->fd != -1) + { + dev->mode = EXFAT_MODE_RW; + break; + } + dev->fd = open_ro(spec); + if (dev->fd != -1) + { + dev->mode = EXFAT_MODE_RO; + exfat_warn("`%s' is write-protected, mounting read-only", spec); + break; + } + free(dev); + exfat_error("failed to open `%s'", spec); + return NULL; + } + + if (fstat(dev->fd, &stbuf) != 0) + { + close(dev->fd); + free(dev); + exfat_error("failed to fstat `%s'", spec); + return NULL; + } + if (!S_ISBLK(stbuf.st_mode) && + !S_ISCHR(stbuf.st_mode) && + !S_ISREG(stbuf.st_mode)) + { + close(dev->fd); + free(dev); + exfat_error("`%s' is neither a device, nor a regular file", spec); + return NULL; + } + +#ifdef __APPLE__ + if (!S_ISREG(stbuf.st_mode)) + { + uint32_t block_size = 0; + uint64_t blocks = 0; + + if (ioctl(dev->fd, DKIOCGETBLOCKSIZE, &block_size) != 0) + { + close(dev->fd); + free(dev); + exfat_error("failed to get block size"); + return NULL; + } + if (ioctl(dev->fd, DKIOCGETBLOCKCOUNT, &blocks) != 0) + { + close(dev->fd); + free(dev); + exfat_error("failed to get blocks count"); + return NULL; + } + dev->size = blocks * block_size; + } + else +#endif + { + /* works for Linux, FreeBSD, Solaris */ + dev->size = exfat_seek(dev, 0, SEEK_END); + if (dev->size <= 0) + { + close(dev->fd); + free(dev); + exfat_error("failed to get size of `%s'", spec); + return NULL; + } + if (exfat_seek(dev, 0, SEEK_SET) == -1) + { + close(dev->fd); + free(dev); + exfat_error("failed to seek to the beginning of `%s'", spec); + return NULL; + } + } + +#ifdef USE_UBLIO + memset(&up, 0, sizeof(struct ublio_param)); + up.up_blocksize = 256 * 1024; + up.up_items = 64; + up.up_grace = 32; + up.up_priv = &dev->fd; + + dev->pos = 0; + dev->ufh = ublio_open(&up); + if (dev->ufh == NULL) + { + close(dev->fd); + free(dev); + exfat_error("failed to initialize ublio"); + return NULL; + } +#endif + + return dev; +} + +int exfat_close(struct exfat_dev* dev) +{ +#ifdef USE_UBLIO + if (ublio_close(dev->ufh) != 0) + exfat_error("failed to close ublio"); +#endif + if (close(dev->fd) != 0) + { + free(dev); + exfat_error("failed to close device"); + return 1; + } + free(dev); + return 0; +} + +int exfat_fsync(struct exfat_dev* dev) +{ +#ifdef USE_UBLIO + if (ublio_fsync(dev->ufh) != 0) +#else + if (fsync(dev->fd) != 0) +#endif + { + exfat_error("fsync failed"); + return 1; + } + return 0; +} + +enum exfat_mode exfat_get_mode(const struct exfat_dev* dev) +{ + return dev->mode; +} + +off64_t exfat_get_size(const struct exfat_dev* dev) +{ + return dev->size; +} + +off64_t exfat_seek(struct exfat_dev* dev, off64_t offset, int whence) +{ +#ifdef USE_UBLIO + /* XXX SEEK_CUR will be handled incorrectly */ + return dev->pos = lseek64(dev->fd, offset, whence); +#else + return lseek64(dev->fd, offset, whence); +#endif +} + +ssize_t exfat_read(struct exfat_dev* dev, void* buffer, size_t size) +{ +#ifdef USE_UBLIO + ssize_t result = ublio_pread(dev->ufh, buffer, size, dev->pos); + if (result >= 0) + dev->pos += size; + return result; +#else + return read(dev->fd, buffer, size); +#endif +} + +ssize_t exfat_write(struct exfat_dev* dev, const void* buffer, size_t size) +{ +#ifdef USE_UBLIO + ssize_t result = ublio_pwrite(dev->ufh, buffer, size, dev->pos); + if (result >= 0) + dev->pos += size; + return result; +#else + return write(dev->fd, buffer, size); +#endif +} + +void exfat_pread(struct exfat_dev* dev, void* buffer, size_t size, + off64_t offset) +{ +#ifdef USE_UBLIO + if (ublio_pread(dev->ufh, buffer, size, offset) != size) +#else + if (pread64(dev->fd, buffer, size, offset) != size) +#endif + exfat_bug("failed to read %zu bytes from file at %"PRIu64, size, + (uint64_t) offset); +} + +void exfat_pwrite(struct exfat_dev* dev, const void* buffer, size_t size, + off64_t offset) +{ +#ifdef USE_UBLIO + if (ublio_pwrite(dev->ufh, buffer, size, offset) != size) +#else + if (pwrite64(dev->fd, buffer, size, offset) != size) +#endif + exfat_bug("failed to write %zu bytes to file at %"PRIu64, size, + (uint64_t) offset); +} + +ssize_t exfat_generic_pread(const struct exfat* ef, struct exfat_node* node, + void* buffer, size_t size, off64_t offset) +{ + cluster_t cluster; + char* bufp = buffer; + off64_t lsize, loffset, remainder; + + if (offset >= node->size) + return 0; + if (size == 0) + return 0; + + cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb)); + if (CLUSTER_INVALID(cluster)) + { + exfat_error("invalid cluster 0x%x while reading", cluster); + return -1; + } + + loffset = offset % CLUSTER_SIZE(*ef->sb); + remainder = MIN(size, node->size - offset); + while (remainder > 0) + { + if (CLUSTER_INVALID(cluster)) + { + exfat_error("invalid cluster 0x%x while reading", cluster); + return -1; + } + lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder); + exfat_pread(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset); + bufp += lsize; + loffset = 0; + remainder -= lsize; + cluster = exfat_next_cluster(ef, node, cluster); + } + if (!ef->ro && !ef->noatime) + exfat_update_atime(node); + return size - remainder; +} + +ssize_t exfat_generic_pwrite(struct exfat* ef, struct exfat_node* node, + const void* buffer, size_t size, off64_t offset) +{ + cluster_t cluster; + const char* bufp = buffer; + off64_t lsize, loffset, remainder; + printf("node: %s\n", node); + if (offset + size > node->size) + if (exfat_truncate(ef, node, offset + size) != 0) + return -1; + if (size == 0) + return 0; + + cluster = exfat_advance_cluster(ef, node, offset / CLUSTER_SIZE(*ef->sb)); + if (CLUSTER_INVALID(cluster)) + { + exfat_error("invalid cluster 0x%x while writing", cluster); + return -1; + } + + loffset = offset % CLUSTER_SIZE(*ef->sb); + remainder = size; + while (remainder > 0) + { + if (CLUSTER_INVALID(cluster)) + { + exfat_error("invalid cluster 0x%x while writing", cluster); + return -1; + } + lsize = MIN(CLUSTER_SIZE(*ef->sb) - loffset, remainder); + exfat_pwrite(ef->dev, bufp, lsize, exfat_c2o(ef, cluster) + loffset); + bufp += lsize; + loffset = 0; + remainder -= lsize; + cluster = exfat_next_cluster(ef, node, cluster); + } + exfat_update_mtime(node); + return size - remainder; +} diff --git a/exfat/libexfat/log.c b/exfat/libexfat/log.c new file mode 100644 index 000000000..8b589b406 --- /dev/null +++ b/exfat/libexfat/log.c @@ -0,0 +1,108 @@ +/* + log.c (02.09.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include +#include + +int exfat_errors; + +/* + * This message means an internal bug in exFAT implementation. + */ +void exfat_bug(const char* format, ...) +{ + va_list ap, aq; + + va_start(ap, format); + va_copy(aq, ap); + + fflush(stdout); + fputs("BUG: ", stderr); + vfprintf(stderr, format, ap); + va_end(ap); + fputs(".\n", stderr); + + if (!isatty(STDERR_FILENO)) + vsyslog(LOG_CRIT, format, aq); + va_end(aq); + + abort(); +} + +/* + * This message means an error in exFAT file system. + */ +void exfat_error(const char* format, ...) +{ + va_list ap, aq; + + exfat_errors++; + va_start(ap, format); + va_copy(aq, ap); + + fflush(stdout); + fputs("ERROR: ", stderr); + vfprintf(stderr, format, ap); + va_end(ap); + fputs(".\n", stderr); + + if (!isatty(STDERR_FILENO)) + vsyslog(LOG_ERR, format, aq); + va_end(aq); +} + +/* + * This message means that there is something unexpected in exFAT file system + * that can be a potential problem. + */ +void exfat_warn(const char* format, ...) +{ + va_list ap, aq; + + va_start(ap, format); + va_copy(aq, ap); + + fflush(stdout); + fputs("WARN: ", stderr); + vfprintf(stderr, format, ap); + va_end(ap); + fputs(".\n", stderr); + + if (!isatty(STDERR_FILENO)) + vsyslog(LOG_WARNING, format, aq); + va_end(aq); +} + +/* + * Just debug message. Disabled by default. + */ +void exfat_debug(const char* format, ...) +{ + va_list ap; + + fflush(stdout); + fputs("DEBUG: ", stderr); + va_start(ap, format); + vfprintf(stderr, format, ap); + va_end(ap); + fputs(".\n", stderr); +} diff --git a/exfat/libexfat/lookup.c b/exfat/libexfat/lookup.c new file mode 100644 index 000000000..8c889b2b2 --- /dev/null +++ b/exfat/libexfat/lookup.c @@ -0,0 +1,223 @@ +/* + lookup.c (02.09.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include +#include + +int exfat_opendir(struct exfat* ef, struct exfat_node* dir, + struct exfat_iterator* it) +{ + int rc; + + exfat_get_node(dir); + it->parent = dir; + it->current = NULL; + rc = exfat_cache_directory(ef, dir); + if (rc != 0) + exfat_put_node(ef, dir); + return rc; +} + +void exfat_closedir(struct exfat* ef, struct exfat_iterator* it) +{ + exfat_put_node(ef, it->parent); + it->parent = NULL; + it->current = NULL; +} + +struct exfat_node* exfat_readdir(struct exfat* ef, struct exfat_iterator* it) +{ + if (it->current == NULL) + it->current = it->parent->child; + else + it->current = it->current->next; + + if (it->current != NULL) + return exfat_get_node(it->current); + else + return NULL; +} + +static int compare_char(struct exfat* ef, uint16_t a, uint16_t b) +{ + if (a >= ef->upcase_chars || b >= ef->upcase_chars) + return (int) a - (int) b; + + return (int) le16_to_cpu(ef->upcase[a]) - (int) le16_to_cpu(ef->upcase[b]); +} + +static int compare_name(struct exfat* ef, const le16_t* a, const le16_t* b) +{ + while (le16_to_cpu(*a) && le16_to_cpu(*b)) + { + int rc = compare_char(ef, le16_to_cpu(*a), le16_to_cpu(*b)); + if (rc != 0) + return rc; + a++; + b++; + } + return compare_char(ef, le16_to_cpu(*a), le16_to_cpu(*b)); +} + +static int lookup_name(struct exfat* ef, struct exfat_node* parent, + struct exfat_node** node, const char* name, size_t n) +{ + struct exfat_iterator it; + le16_t buffer[EXFAT_NAME_MAX + 1]; + int rc; + + *node = NULL; + + rc = utf8_to_utf16(buffer, name, EXFAT_NAME_MAX, n); + if (rc != 0) + return rc; + + rc = exfat_opendir(ef, parent, &it); + if (rc != 0) + return rc; + while ((*node = exfat_readdir(ef, &it))) + { + if (compare_name(ef, buffer, (*node)->name) == 0) + { + exfat_closedir(ef, &it); + return 0; + } + exfat_put_node(ef, *node); + } + exfat_closedir(ef, &it); + return -ENOENT; +} + +static size_t get_comp(const char* path, const char** comp) +{ + const char* end; + + *comp = path + strspn(path, "/"); /* skip leading slashes */ + end = strchr(*comp, '/'); + if (end == NULL) + return strlen(*comp); + else + return end - *comp; +} + +int exfat_lookup(struct exfat* ef, struct exfat_node** node, + const char* path) +{ + struct exfat_node* parent; + const char* p; + size_t n; + int rc; + + /* start from the root directory */ + parent = *node = exfat_get_node(ef->root); + for (p = path; (n = get_comp(p, &p)); p += n) + { + if (n == 1 && *p == '.') /* skip "." component */ + continue; + rc = lookup_name(ef, parent, node, p, n); + if (rc != 0) + { + exfat_put_node(ef, parent); + return rc; + } + exfat_put_node(ef, parent); + parent = *node; + } + return 0; +} + +static bool is_last_comp(const char* comp, size_t length) +{ + const char* p = comp + length; + + return get_comp(p, &p) == 0; +} + +static bool is_allowed(const char* comp, size_t length) +{ + size_t i; + + for (i = 0; i < length; i++) + switch (comp[i]) + { + case 0x01 ... 0x1f: + case '/': + case '\\': + case ':': + case '*': + case '?': + case '"': + case '<': + case '>': + case '|': + return false; + } + return true; +} + +int exfat_split(struct exfat* ef, struct exfat_node** parent, + struct exfat_node** node, le16_t* name, const char* path) +{ + const char* p; + size_t n; + int rc; + + memset(name, 0, (EXFAT_NAME_MAX + 1) * sizeof(le16_t)); + *parent = *node = exfat_get_node(ef->root); + for (p = path; (n = get_comp(p, &p)); p += n) + { + if (n == 1 && *p == '.') + continue; + if (is_last_comp(p, n)) + { + if (!is_allowed(p, n)) + { + /* contains characters that are not allowed */ + exfat_put_node(ef, *parent); + return -ENOENT; + } + rc = utf8_to_utf16(name, p, EXFAT_NAME_MAX, n); + if (rc != 0) + { + exfat_put_node(ef, *parent); + return rc; + } + + rc = lookup_name(ef, *parent, node, p, n); + if (rc != 0 && rc != -ENOENT) + { + exfat_put_node(ef, *parent); + return rc; + } + return 0; + } + rc = lookup_name(ef, *parent, node, p, n); + if (rc != 0) + { + exfat_put_node(ef, *parent); + return rc; + } + exfat_put_node(ef, *parent); + *parent = *node; + } + exfat_bug("impossible"); +} diff --git a/exfat/libexfat/mount.c b/exfat/libexfat/mount.c new file mode 100644 index 000000000..44fb58138 --- /dev/null +++ b/exfat/libexfat/mount.c @@ -0,0 +1,314 @@ +/* + mount.c (22.10.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include +#include +#include +#include + +static uint64_t rootdir_size(const struct exfat* ef) +{ + uint64_t clusters = 0; + cluster_t rootdir_cluster = le32_to_cpu(ef->sb->rootdir_cluster); + + while (!CLUSTER_INVALID(rootdir_cluster)) + { + clusters++; + /* root directory cannot be contiguous because there is no flag + to indicate this */ + rootdir_cluster = exfat_next_cluster(ef, ef->root, rootdir_cluster); + } + return clusters * CLUSTER_SIZE(*ef->sb); +} + +static const char* get_option(const char* options, const char* option_name) +{ + const char* p; + size_t length = strlen(option_name); + + for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name)) + if ((p == options || p[-1] == ',') && p[length] == '=') + return p + length + 1; + return NULL; +} + +static int get_int_option(const char* options, const char* option_name, + int base, int default_value) +{ + const char* p = get_option(options, option_name); + + if (p == NULL) + return default_value; + return strtol(p, NULL, base); +} + +static bool match_option(const char* options, const char* option_name) +{ + const char* p; + size_t length = strlen(option_name); + + for (p = strstr(options, option_name); p; p = strstr(p + 1, option_name)) + if ((p == options || p[-1] == ',') && + (p[length] == ',' || p[length] == '\0')) + return true; + return false; +} + +static void parse_options(struct exfat* ef, const char* options) +{ + int sys_umask = umask(0); + int opt_umask; + + umask(sys_umask); /* restore umask */ + opt_umask = get_int_option(options, "umask", 8, sys_umask); + ef->dmask = get_int_option(options, "dmask", 8, opt_umask) & 0777; + ef->fmask = get_int_option(options, "fmask", 8, opt_umask) & 0777; + + ef->uid = get_int_option(options, "uid", 10, geteuid()); + ef->gid = get_int_option(options, "gid", 10, getegid()); + + ef->noatime = match_option(options, "noatime"); +} + +static int verify_vbr_checksum(struct exfat_dev* dev, void* sector, + off64_t sector_size) +{ + uint32_t vbr_checksum; + int i; + + exfat_pread(dev, sector, sector_size, 0); + vbr_checksum = exfat_vbr_start_checksum(sector, sector_size); + for (i = 1; i < 11; i++) + { + exfat_pread(dev, sector, sector_size, i * sector_size); + vbr_checksum = exfat_vbr_add_checksum(sector, sector_size, + vbr_checksum); + } + exfat_pread(dev, sector, sector_size, i * sector_size); + for (i = 0; i < sector_size / sizeof(vbr_checksum); i++) + if (le32_to_cpu(((const le32_t*) sector)[i]) != vbr_checksum) + { + exfat_error("invalid VBR checksum 0x%x (expected 0x%x)", + le32_to_cpu(((const le32_t*) sector)[i]), vbr_checksum); + return 1; + } + return 0; +} + +static int commit_super_block(const struct exfat* ef) +{ + exfat_pwrite(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0); + return exfat_fsync(ef->dev); +} + +static int prepare_super_block(const struct exfat* ef) +{ + if (le16_to_cpu(ef->sb->volume_state) & EXFAT_STATE_MOUNTED) + exfat_warn("volume was not unmounted cleanly"); + + if (ef->ro) + return 0; + + ef->sb->volume_state = cpu_to_le16( + le16_to_cpu(ef->sb->volume_state) | EXFAT_STATE_MOUNTED); + return commit_super_block(ef); +} + +int exfat_mount(struct exfat* ef, const char* spec, const char* options) +{ + int rc; + enum exfat_mode mode; + + exfat_tzset(); + memset(ef, 0, sizeof(struct exfat)); + + parse_options(ef, options); + + if (match_option(options, "ro")) + mode = EXFAT_MODE_RO; + else if (match_option(options, "ro_fallback")) + mode = EXFAT_MODE_ANY; + else + mode = EXFAT_MODE_RW; + ef->dev = exfat_open(spec, mode); + if (ef->dev == NULL) + return -EIO; + if (exfat_get_mode(ef->dev) == EXFAT_MODE_RO) + { + if (mode == EXFAT_MODE_ANY) + ef->ro = -1; + else + ef->ro = 1; + } + + ef->sb = malloc(sizeof(struct exfat_super_block)); + if (ef->sb == NULL) + { + exfat_close(ef->dev); + exfat_error("failed to allocate memory for the super block"); + return -ENOMEM; + } + memset(ef->sb, 0, sizeof(struct exfat_super_block)); + + exfat_pread(ef->dev, ef->sb, sizeof(struct exfat_super_block), 0); + if (memcmp(ef->sb->oem_name, "EXFAT ", 8) != 0) + { + exfat_close(ef->dev); + free(ef->sb); + exfat_error("exFAT file system is not found"); + return -EIO; + } + if (ef->sb->version.major != 1 || ef->sb->version.minor != 0) + { + exfat_close(ef->dev); + exfat_error("unsupported exFAT version: %hhu.%hhu", + ef->sb->version.major, ef->sb->version.minor); + free(ef->sb); + return -EIO; + } + if (ef->sb->fat_count != 1) + { + exfat_close(ef->dev); + free(ef->sb); + exfat_error("unsupported FAT count: %hhu", ef->sb->fat_count); + return -EIO; + } + /* officially exFAT supports cluster size up to 32 MB */ + if ((int) ef->sb->sector_bits + (int) ef->sb->spc_bits > 25) + { + exfat_close(ef->dev); + free(ef->sb); + exfat_error("too big cluster size: 2^%d", + (int) ef->sb->sector_bits + (int) ef->sb->spc_bits); + return -EIO; + } + + ef->zero_cluster = malloc(CLUSTER_SIZE(*ef->sb)); + if (ef->zero_cluster == NULL) + { + exfat_close(ef->dev); + free(ef->sb); + exfat_error("failed to allocate zero sector"); + return -ENOMEM; + } + /* use zero_cluster as a temporary buffer for VBR checksum verification */ + if (verify_vbr_checksum(ef->dev, ef->zero_cluster, + SECTOR_SIZE(*ef->sb)) != 0) + { + free(ef->zero_cluster); + exfat_close(ef->dev); + free(ef->sb); + return -EIO; + } + memset(ef->zero_cluster, 0, CLUSTER_SIZE(*ef->sb)); + + ef->root = malloc(sizeof(struct exfat_node)); + if (ef->root == NULL) + { + free(ef->zero_cluster); + exfat_close(ef->dev); + free(ef->sb); + exfat_error("failed to allocate root node"); + return -ENOMEM; + } + memset(ef->root, 0, sizeof(struct exfat_node)); + ef->root->flags = EXFAT_ATTRIB_DIR; + ef->root->start_cluster = le32_to_cpu(ef->sb->rootdir_cluster); + ef->root->fptr_cluster = ef->root->start_cluster; + ef->root->name[0] = cpu_to_le16('\0'); + ef->root->size = rootdir_size(ef); + /* exFAT does not have time attributes for the root directory */ + ef->root->mtime = 0; + ef->root->atime = 0; + /* always keep at least 1 reference to the root node */ + exfat_get_node(ef->root); + + rc = exfat_cache_directory(ef, ef->root); + if (rc != 0) + goto error; + if (ef->upcase == NULL) + { + exfat_error("upcase table is not found"); + goto error; + } + if (ef->cmap.chunk == NULL) + { + exfat_error("clusters bitmap is not found"); + goto error; + } + + if (prepare_super_block(ef) != 0) + goto error; + + return 0; + +error: + exfat_put_node(ef, ef->root); + exfat_reset_cache(ef); + free(ef->root); + free(ef->zero_cluster); + exfat_close(ef->dev); + free(ef->sb); + return -EIO; +} + +static void finalize_super_block(struct exfat* ef) +{ + if (ef->ro) + return; + + ef->sb->volume_state = cpu_to_le16( + le16_to_cpu(ef->sb->volume_state) & ~EXFAT_STATE_MOUNTED); + + /* Some implementations set the percentage of allocated space to 0xff + on FS creation and never update it. In this case leave it as is. */ + if (ef->sb->allocated_percent != 0xff) + { + uint32_t free, total; + + free = exfat_count_free_clusters(ef); + total = le32_to_cpu(ef->sb->cluster_count); + ef->sb->allocated_percent = ((total - free) * 100 + total / 2) / total; + } + + commit_super_block(ef); +} + +void exfat_unmount(struct exfat* ef) +{ + exfat_put_node(ef, ef->root); + exfat_reset_cache(ef); + free(ef->root); + ef->root = NULL; + finalize_super_block(ef); + exfat_close(ef->dev); /* close descriptor immediately after fsync */ + ef->dev = NULL; + free(ef->zero_cluster); + ef->zero_cluster = NULL; + free(ef->cmap.chunk); + ef->cmap.chunk = NULL; + free(ef->sb); + ef->sb = NULL; + free(ef->upcase); + ef->upcase = NULL; + ef->upcase_chars = 0; +} diff --git a/exfat/libexfat/node.c b/exfat/libexfat/node.c new file mode 100644 index 000000000..bacb01d10 --- /dev/null +++ b/exfat/libexfat/node.c @@ -0,0 +1,1041 @@ +/* + node.c (09.10.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include +#include + +/* on-disk nodes iterator */ +struct iterator +{ + cluster_t cluster; + off64_t offset; + int contiguous; + char* chunk; +}; + +struct exfat_node* exfat_get_node(struct exfat_node* node) +{ + /* if we switch to multi-threaded mode we will need atomic + increment here and atomic decrement in exfat_put_node() */ + node->references++; + return node; +} + +void exfat_put_node(struct exfat* ef, struct exfat_node* node) +{ + if (--node->references < 0) + { + char buffer[EXFAT_NAME_MAX + 1]; + exfat_get_name(node, buffer, EXFAT_NAME_MAX); + exfat_bug("reference counter of `%s' is below zero", buffer); + } + + if (node->references == 0) + { + if (node->flags & EXFAT_ATTRIB_DIRTY) + exfat_flush_node(ef, node); + if (node->flags & EXFAT_ATTRIB_UNLINKED) + { + /* free all clusters and node structure itself */ + exfat_truncate(ef, node, 0); + free(node); + } + if (ef->cmap.dirty) + exfat_flush_cmap(ef); + } +} + +/** + * Cluster + offset from the beginning of the directory to absolute offset. + */ +static off64_t co2o(struct exfat* ef, cluster_t cluster, off64_t offset) +{ + return exfat_c2o(ef, cluster) + offset % CLUSTER_SIZE(*ef->sb); +} + +static int opendir(struct exfat* ef, const struct exfat_node* dir, + struct iterator* it) +{ + if (!(dir->flags & EXFAT_ATTRIB_DIR)) + exfat_bug("not a directory"); + it->cluster = dir->start_cluster; + it->offset = 0; + it->contiguous = IS_CONTIGUOUS(*dir); + it->chunk = malloc(CLUSTER_SIZE(*ef->sb)); + if (it->chunk == NULL) + { + exfat_error("out of memory"); + return -ENOMEM; + } + exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb), + exfat_c2o(ef, it->cluster)); + return 0; +} + +static void closedir(struct iterator* it) +{ + it->cluster = 0; + it->offset = 0; + it->contiguous = 0; + free(it->chunk); + it->chunk = NULL; +} + +static int fetch_next_entry(struct exfat* ef, const struct exfat_node* parent, + struct iterator* it) +{ + /* move iterator to the next entry in the directory */ + it->offset += sizeof(struct exfat_entry); + /* fetch the next cluster if needed */ + if ((it->offset & (CLUSTER_SIZE(*ef->sb) - 1)) == 0) + { + /* reached the end of directory; the caller should check this + condition too */ + if (it->offset >= parent->size) + return 0; + it->cluster = exfat_next_cluster(ef, parent, it->cluster); + if (CLUSTER_INVALID(it->cluster)) + { + exfat_error("invalid cluster 0x%x while reading directory", + it->cluster); + return 1; + } + exfat_pread(ef->dev, it->chunk, CLUSTER_SIZE(*ef->sb), + exfat_c2o(ef, it->cluster)); + } + return 0; +} + +static struct exfat_node* allocate_node(void) +{ + struct exfat_node* node = malloc(sizeof(struct exfat_node)); + if (node == NULL) + { + exfat_error("failed to allocate node"); + return NULL; + } + memset(node, 0, sizeof(struct exfat_node)); + return node; +} + +static void init_node_meta1(struct exfat_node* node, + const struct exfat_entry_meta1* meta1) +{ + node->flags = le16_to_cpu(meta1->attrib); + node->mtime = exfat_exfat2unix(meta1->mdate, meta1->mtime, + meta1->mtime_cs); + /* there is no centiseconds field for atime */ + node->atime = exfat_exfat2unix(meta1->adate, meta1->atime, 0); +} + +static void init_node_meta2(struct exfat_node* node, + const struct exfat_entry_meta2* meta2) +{ + node->size = le64_to_cpu(meta2->size); + node->start_cluster = le32_to_cpu(meta2->start_cluster); + node->fptr_cluster = node->start_cluster; + if (meta2->flags & EXFAT_FLAG_CONTIGUOUS) + node->flags |= EXFAT_ATTRIB_CONTIGUOUS; +} + +static const struct exfat_entry* get_entry_ptr(const struct exfat* ef, + const struct iterator* it) +{ + return (const struct exfat_entry*) + (it->chunk + it->offset % CLUSTER_SIZE(*ef->sb)); +} + +/* + * Reads one entry in directory at position pointed by iterator and fills + * node structure. + */ +static int readdir(struct exfat* ef, const struct exfat_node* parent, + struct exfat_node** node, struct iterator* it) +{ + int rc = -EIO; + const struct exfat_entry* entry; + const struct exfat_entry_meta1* meta1; + const struct exfat_entry_meta2* meta2; + const struct exfat_entry_name* file_name; + const struct exfat_entry_upcase* upcase; + const struct exfat_entry_bitmap* bitmap; + const struct exfat_entry_label* label; + uint8_t continuations = 0; + le16_t* namep = NULL; + uint16_t reference_checksum = 0; + uint16_t actual_checksum = 0; + uint64_t real_size = 0; + + *node = NULL; + + for (;;) + { + if (it->offset >= parent->size) + { + if (continuations != 0) + { + exfat_error("expected %hhu continuations", continuations); + goto error; + } + return -ENOENT; /* that's OK, means end of directory */ + } + + entry = get_entry_ptr(ef, it); + switch (entry->type) + { + case EXFAT_ENTRY_FILE: + if (continuations != 0) + { + exfat_error("expected %hhu continuations before new entry", + continuations); + goto error; + } + meta1 = (const struct exfat_entry_meta1*) entry; + continuations = meta1->continuations; + /* each file entry must have at least 2 continuations: + info and name */ + if (continuations < 2) + { + exfat_error("too few continuations (%hhu)", continuations); + goto error; + } + reference_checksum = le16_to_cpu(meta1->checksum); + actual_checksum = exfat_start_checksum(meta1); + *node = allocate_node(); + if (*node == NULL) + { + rc = -ENOMEM; + goto error; + } + /* new node has zero reference counter */ + (*node)->entry_cluster = it->cluster; + (*node)->entry_offset = it->offset; + init_node_meta1(*node, meta1); + namep = (*node)->name; + break; + + case EXFAT_ENTRY_FILE_INFO: + if (continuations < 2) + { + exfat_error("unexpected continuation (%hhu)", + continuations); + goto error; + } + meta2 = (const struct exfat_entry_meta2*) entry; + if (meta2->flags & ~(EXFAT_FLAG_ALWAYS1 | EXFAT_FLAG_CONTIGUOUS)) + { + exfat_error("unknown flags in meta2 (0x%hhx)", meta2->flags); + goto error; + } + init_node_meta2(*node, meta2); + actual_checksum = exfat_add_checksum(entry, actual_checksum); + real_size = le64_to_cpu(meta2->real_size); + /* empty files must be marked as non-contiguous */ + if ((*node)->size == 0 && (meta2->flags & EXFAT_FLAG_CONTIGUOUS)) + { + exfat_error("empty file marked as contiguous (0x%hhx)", + meta2->flags); + goto error; + } + /* directories must be aligned on at cluster boundary */ + if (((*node)->flags & EXFAT_ATTRIB_DIR) && + (*node)->size % CLUSTER_SIZE(*ef->sb) != 0) + { + exfat_error("directory has invalid size %"PRIu64" bytes", + (*node)->size); + goto error; + } + --continuations; + break; + + case EXFAT_ENTRY_FILE_NAME: + if (continuations == 0) + { + exfat_error("unexpected continuation"); + goto error; + } + file_name = (const struct exfat_entry_name*) entry; + actual_checksum = exfat_add_checksum(entry, actual_checksum); + + memcpy(namep, file_name->name, EXFAT_ENAME_MAX * sizeof(le16_t)); + namep += EXFAT_ENAME_MAX; + if (--continuations == 0) + { + /* + There are two fields that contain file size. Maybe they + plan to add compression support in the future and one of + those fields is visible (uncompressed) size and the other + is real (compressed) size. Anyway, currently it looks like + exFAT does not support compression and both fields must be + equal. + + There is an exception though: pagefile.sys (its real_size + is always 0). + */ + if (real_size != (*node)->size) + { + char buffer[EXFAT_NAME_MAX + 1]; + + exfat_get_name(*node, buffer, EXFAT_NAME_MAX); + exfat_error("`%s' real size does not equal to size " + "(%"PRIu64" != %"PRIu64")", buffer, + real_size, (*node)->size); + goto error; + } + if (actual_checksum != reference_checksum) + { + char buffer[EXFAT_NAME_MAX + 1]; + + exfat_get_name(*node, buffer, EXFAT_NAME_MAX); + exfat_error("`%s' has invalid checksum (0x%hx != 0x%hx)", + buffer, actual_checksum, reference_checksum); + goto error; + } + if (fetch_next_entry(ef, parent, it) != 0) + goto error; + return 0; /* entry completed */ + } + break; + + case EXFAT_ENTRY_UPCASE: + if (ef->upcase != NULL) + break; + upcase = (const struct exfat_entry_upcase*) entry; + if (CLUSTER_INVALID(le32_to_cpu(upcase->start_cluster))) + { + exfat_error("invalid cluster 0x%x in upcase table", + le32_to_cpu(upcase->start_cluster)); + goto error; + } + if (le64_to_cpu(upcase->size) == 0 || + le64_to_cpu(upcase->size) > 0xffff * sizeof(uint16_t) || + le64_to_cpu(upcase->size) % sizeof(uint16_t) != 0) + { + exfat_error("bad upcase table size (%"PRIu64" bytes)", + le64_to_cpu(upcase->size)); + goto error; + } + ef->upcase = malloc(le64_to_cpu(upcase->size)); + if (ef->upcase == NULL) + { + exfat_error("failed to allocate upcase table (%"PRIu64" bytes)", + le64_to_cpu(upcase->size)); + rc = -ENOMEM; + goto error; + } + ef->upcase_chars = le64_to_cpu(upcase->size) / sizeof(le16_t); + + exfat_pread(ef->dev, ef->upcase, le64_to_cpu(upcase->size), + exfat_c2o(ef, le32_to_cpu(upcase->start_cluster))); + break; + + case EXFAT_ENTRY_BITMAP: + bitmap = (const struct exfat_entry_bitmap*) entry; + ef->cmap.start_cluster = le32_to_cpu(bitmap->start_cluster); + if (CLUSTER_INVALID(ef->cmap.start_cluster)) + { + exfat_error("invalid cluster 0x%x in clusters bitmap", + ef->cmap.start_cluster); + goto error; + } + ef->cmap.size = le32_to_cpu(ef->sb->cluster_count) - + EXFAT_FIRST_DATA_CLUSTER; + if (le64_to_cpu(bitmap->size) < (ef->cmap.size + 7) / 8) + { + exfat_error("invalid clusters bitmap size: %"PRIu64 + " (expected at least %u)", + le64_to_cpu(bitmap->size), (ef->cmap.size + 7) / 8); + goto error; + } + /* FIXME bitmap can be rather big, up to 512 MB */ + ef->cmap.chunk_size = ef->cmap.size; + ef->cmap.chunk = malloc(le64_to_cpu(bitmap->size)); + if (ef->cmap.chunk == NULL) + { + exfat_error("failed to allocate clusters bitmap chunk " + "(%"PRIu64" bytes)", le64_to_cpu(bitmap->size)); + rc = -ENOMEM; + goto error; + } + + exfat_pread(ef->dev, ef->cmap.chunk, le64_to_cpu(bitmap->size), + exfat_c2o(ef, ef->cmap.start_cluster)); + break; + + case EXFAT_ENTRY_LABEL: + label = (const struct exfat_entry_label*) entry; + if (label->length > EXFAT_ENAME_MAX) + { + exfat_error("too long label (%hhu chars)", label->length); + goto error; + } + if (utf16_to_utf8(ef->label, label->name, + sizeof(ef->label), EXFAT_ENAME_MAX) != 0) + goto error; + break; + + default: + if (entry->type & EXFAT_ENTRY_VALID) + { + exfat_error("unknown entry type 0x%hhx", entry->type); + goto error; + } + break; + } + + if (fetch_next_entry(ef, parent, it) != 0) + goto error; + } + /* we never reach here */ + +error: + free(*node); + *node = NULL; + return rc; +} + +int exfat_cache_directory(struct exfat* ef, struct exfat_node* dir) +{ + struct iterator it; + int rc; + struct exfat_node* node; + struct exfat_node* current = NULL; + + if (dir->flags & EXFAT_ATTRIB_CACHED) + return 0; /* already cached */ + + rc = opendir(ef, dir, &it); + if (rc != 0) + return rc; + while ((rc = readdir(ef, dir, &node, &it)) == 0) + { + node->parent = dir; + if (current != NULL) + { + current->next = node; + node->prev = current; + } + else + dir->child = node; + + current = node; + } + closedir(&it); + + if (rc != -ENOENT) + { + /* rollback */ + for (current = dir->child; current; current = node) + { + node = current->next; + free(current); + } + dir->child = NULL; + return rc; + } + + dir->flags |= EXFAT_ATTRIB_CACHED; + return 0; +} + +static void reset_cache(struct exfat* ef, struct exfat_node* node) +{ + struct exfat_node* child; + struct exfat_node* next; + + for (child = node->child; child; child = next) + { + reset_cache(ef, child); + next = child->next; + free(child); + } + if (node->references != 0) + { + char buffer[EXFAT_NAME_MAX + 1]; + exfat_get_name(node, buffer, EXFAT_NAME_MAX); + exfat_warn("non-zero reference counter (%d) for `%s'", + node->references, buffer); + } + while (node->references--) + exfat_put_node(ef, node); + node->child = NULL; + node->flags &= ~EXFAT_ATTRIB_CACHED; +} + +void exfat_reset_cache(struct exfat* ef) +{ + reset_cache(ef, ef->root); +} + +void next_entry(struct exfat* ef, const struct exfat_node* parent, + cluster_t* cluster, off64_t* offset) +{ + *offset += sizeof(struct exfat_entry); + if (*offset % CLUSTER_SIZE(*ef->sb) == 0) + /* next cluster cannot be invalid */ + *cluster = exfat_next_cluster(ef, parent, *cluster); +} + +void exfat_flush_node(struct exfat* ef, struct exfat_node* node) +{ + cluster_t cluster; + off64_t offset; + off64_t meta1_offset, meta2_offset; + struct exfat_entry_meta1 meta1; + struct exfat_entry_meta2 meta2; + + if (ef->ro) + exfat_bug("unable to flush node to read-only FS"); + + if (node->parent == NULL) + return; /* do not flush unlinked node */ + + cluster = node->entry_cluster; + offset = node->entry_offset; + meta1_offset = co2o(ef, cluster, offset); + next_entry(ef, node->parent, &cluster, &offset); + meta2_offset = co2o(ef, cluster, offset); + + exfat_pread(ef->dev, &meta1, sizeof(meta1), meta1_offset); + if (meta1.type != EXFAT_ENTRY_FILE) + exfat_bug("invalid type of meta1: 0x%hhx", meta1.type); + meta1.attrib = cpu_to_le16(node->flags); + exfat_unix2exfat(node->mtime, &meta1.mdate, &meta1.mtime, &meta1.mtime_cs); + exfat_unix2exfat(node->atime, &meta1.adate, &meta1.atime, NULL); + + exfat_pread(ef->dev, &meta2, sizeof(meta2), meta2_offset); + if (meta2.type != EXFAT_ENTRY_FILE_INFO) + exfat_bug("invalid type of meta2: 0x%hhx", meta2.type); + meta2.size = meta2.real_size = cpu_to_le64(node->size); + meta2.start_cluster = cpu_to_le32(node->start_cluster); + meta2.flags = EXFAT_FLAG_ALWAYS1; + /* empty files must not be marked as contiguous */ + if (node->size != 0 && IS_CONTIGUOUS(*node)) + meta2.flags |= EXFAT_FLAG_CONTIGUOUS; + /* name hash remains unchanged, no need to recalculate it */ + + meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name); + + exfat_pwrite(ef->dev, &meta1, sizeof(meta1), meta1_offset); + exfat_pwrite(ef->dev, &meta2, sizeof(meta2), meta2_offset); + + node->flags &= ~EXFAT_ATTRIB_DIRTY; +} + +static void erase_entry(struct exfat* ef, struct exfat_node* node) +{ + cluster_t cluster = node->entry_cluster; + off64_t offset = node->entry_offset; + int name_entries = DIV_ROUND_UP(utf16_length(node->name), EXFAT_ENAME_MAX); + uint8_t entry_type; + + entry_type = EXFAT_ENTRY_FILE & ~EXFAT_ENTRY_VALID; + exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)); + + next_entry(ef, node->parent, &cluster, &offset); + entry_type = EXFAT_ENTRY_FILE_INFO & ~EXFAT_ENTRY_VALID; + exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)); + + while (name_entries--) + { + next_entry(ef, node->parent, &cluster, &offset); + entry_type = EXFAT_ENTRY_FILE_NAME & ~EXFAT_ENTRY_VALID; + exfat_pwrite(ef->dev, &entry_type, 1, co2o(ef, cluster, offset)); + } +} + +static void tree_detach(struct exfat_node* node) +{ + if (node->prev) + node->prev->next = node->next; + else /* this is the first node in the list */ + node->parent->child = node->next; + if (node->next) + node->next->prev = node->prev; + node->parent = NULL; + node->prev = NULL; + node->next = NULL; +} + +static void tree_attach(struct exfat_node* dir, struct exfat_node* node) +{ + node->parent = dir; + if (dir->child) + { + dir->child->prev = node; + node->next = dir->child; + } + dir->child = node; +} + +static int shrink_directory(struct exfat* ef, struct exfat_node* dir, + off64_t deleted_offset) +{ + const struct exfat_node* node; + const struct exfat_node* last_node; + uint64_t entries = 0; + uint64_t new_size; + int rc; + + if (!(dir->flags & EXFAT_ATTRIB_DIR)) + exfat_bug("attempted to shrink a file"); + if (!(dir->flags & EXFAT_ATTRIB_CACHED)) + exfat_bug("attempted to shrink uncached directory"); + + for (last_node = node = dir->child; node; node = node->next) + { + if (deleted_offset < node->entry_offset) + { + /* there are other entries after the removed one, no way to shrink + this directory */ + return 0; + } + if (last_node->entry_offset < node->entry_offset) + last_node = node; + } + + if (last_node) + { + /* offset of the last entry */ + entries += last_node->entry_offset / sizeof(struct exfat_entry); + /* two subentries with meta info */ + entries += 2; + /* subentries with file name */ + entries += DIV_ROUND_UP(utf16_length(last_node->name), + EXFAT_ENAME_MAX); + } + + new_size = DIV_ROUND_UP(entries * sizeof(struct exfat_entry), + CLUSTER_SIZE(*ef->sb)) * CLUSTER_SIZE(*ef->sb); + if (new_size == 0) /* directory always has at least 1 cluster */ + new_size = CLUSTER_SIZE(*ef->sb); + if (new_size == dir->size) + return 0; + rc = exfat_truncate(ef, dir, new_size); + if (rc != 0) + return rc; + return 0; +} + +static int delete(struct exfat* ef, struct exfat_node* node) +{ + struct exfat_node* parent = node->parent; + off64_t deleted_offset = node->entry_offset; + int rc; + + exfat_get_node(parent); + erase_entry(ef, node); + exfat_update_mtime(parent); + tree_detach(node); + rc = shrink_directory(ef, parent, deleted_offset); + exfat_put_node(ef, parent); + /* file clusters will be freed when node reference counter becomes 0 */ + node->flags |= EXFAT_ATTRIB_UNLINKED; + return rc; +} + +int exfat_unlink(struct exfat* ef, struct exfat_node* node) +{ + if (node->flags & EXFAT_ATTRIB_DIR) + return -EISDIR; + return delete(ef, node); +} + +int exfat_rmdir(struct exfat* ef, struct exfat_node* node) +{ + if (!(node->flags & EXFAT_ATTRIB_DIR)) + return -ENOTDIR; + /* check that directory is empty */ + exfat_cache_directory(ef, node); + if (node->child) + return -ENOTEMPTY; + return delete(ef, node); +} + +static int grow_directory(struct exfat* ef, struct exfat_node* dir, + uint64_t asize, uint32_t difference) +{ + return exfat_truncate(ef, dir, + DIV_ROUND_UP(asize + difference, CLUSTER_SIZE(*ef->sb)) + * CLUSTER_SIZE(*ef->sb)); +} + +static int find_slot(struct exfat* ef, struct exfat_node* dir, + cluster_t* cluster, off64_t* offset, int subentries) +{ + struct iterator it; + int rc; + const struct exfat_entry* entry; + int contiguous = 0; + + rc = opendir(ef, dir, &it); + if (rc != 0) + return rc; + for (;;) + { + if (contiguous == 0) + { + *cluster = it.cluster; + *offset = it.offset; + } + entry = get_entry_ptr(ef, &it); + if (entry->type & EXFAT_ENTRY_VALID) + contiguous = 0; + else + contiguous++; + if (contiguous == subentries) + break; /* suitable slot is found */ + if (it.offset + sizeof(struct exfat_entry) >= dir->size) + { + rc = grow_directory(ef, dir, dir->size, + (subentries - contiguous) * sizeof(struct exfat_entry)); + if (rc != 0) + { + closedir(&it); + return rc; + } + } + if (fetch_next_entry(ef, dir, &it) != 0) + { + closedir(&it); + return -EIO; + } + } + closedir(&it); + return 0; +} + +static int write_entry(struct exfat* ef, struct exfat_node* dir, + const le16_t* name, cluster_t cluster, off64_t offset, uint16_t attrib) +{ + struct exfat_node* node; + struct exfat_entry_meta1 meta1; + struct exfat_entry_meta2 meta2; + const size_t name_length = utf16_length(name); + const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX); + int i; + + node = allocate_node(); + if (node == NULL) + return -ENOMEM; + node->entry_cluster = cluster; + node->entry_offset = offset; + memcpy(node->name, name, name_length * sizeof(le16_t)); + + memset(&meta1, 0, sizeof(meta1)); + meta1.type = EXFAT_ENTRY_FILE; + meta1.continuations = 1 + name_entries; + meta1.attrib = cpu_to_le16(attrib); + exfat_unix2exfat(time(NULL), &meta1.crdate, &meta1.crtime, + &meta1.crtime_cs); + meta1.adate = meta1.mdate = meta1.crdate; + meta1.atime = meta1.mtime = meta1.crtime; + meta1.mtime_cs = meta1.crtime_cs; /* there is no atime_cs */ + + memset(&meta2, 0, sizeof(meta2)); + meta2.type = EXFAT_ENTRY_FILE_INFO; + meta2.flags = EXFAT_FLAG_ALWAYS1; + meta2.name_length = name_length; + meta2.name_hash = exfat_calc_name_hash(ef, node->name); + meta2.start_cluster = cpu_to_le32(EXFAT_CLUSTER_FREE); + + meta1.checksum = exfat_calc_checksum(&meta1, &meta2, node->name); + + exfat_pwrite(ef->dev, &meta1, sizeof(meta1), co2o(ef, cluster, offset)); + next_entry(ef, dir, &cluster, &offset); + exfat_pwrite(ef->dev, &meta2, sizeof(meta2), co2o(ef, cluster, offset)); + for (i = 0; i < name_entries; i++) + { + struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0}; + memcpy(name_entry.name, node->name + i * EXFAT_ENAME_MAX, + EXFAT_ENAME_MAX * sizeof(le16_t)); + next_entry(ef, dir, &cluster, &offset); + exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry), + co2o(ef, cluster, offset)); + } + + init_node_meta1(node, &meta1); + init_node_meta2(node, &meta2); + + tree_attach(dir, node); + exfat_update_mtime(dir); + return 0; +} + +static int create(struct exfat* ef, const char* path, uint16_t attrib) +{ + struct exfat_node* dir; + struct exfat_node* existing; + cluster_t cluster = EXFAT_CLUSTER_BAD; + off64_t offset = -1; + le16_t name[EXFAT_NAME_MAX + 1]; + int rc; + + rc = exfat_split(ef, &dir, &existing, name, path); + if (rc != 0) + return rc; + if (existing != NULL) + { + exfat_put_node(ef, existing); + exfat_put_node(ef, dir); + return -EEXIST; + } + + rc = find_slot(ef, dir, &cluster, &offset, + 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX)); + if (rc != 0) + { + exfat_put_node(ef, dir); + return rc; + } + rc = write_entry(ef, dir, name, cluster, offset, attrib); + exfat_put_node(ef, dir); + return rc; +} + +int exfat_mknod(struct exfat* ef, const char* path) +{ + return create(ef, path, EXFAT_ATTRIB_ARCH); +} + +int exfat_mkdir(struct exfat* ef, const char* path) +{ + int rc; + struct exfat_node* node; + + rc = create(ef, path, EXFAT_ATTRIB_ARCH | EXFAT_ATTRIB_DIR); + if (rc != 0) + return rc; + rc = exfat_lookup(ef, &node, path); + if (rc != 0) + return 0; + /* directories always have at least one cluster */ + rc = exfat_truncate(ef, node, CLUSTER_SIZE(*ef->sb)); + if (rc != 0) + { + delete(ef, node); + exfat_put_node(ef, node); + return rc; + } + exfat_put_node(ef, node); + return 0; +} + +static void rename_entry(struct exfat* ef, struct exfat_node* dir, + struct exfat_node* node, const le16_t* name, cluster_t new_cluster, + off64_t new_offset) +{ + struct exfat_entry_meta1 meta1; + struct exfat_entry_meta2 meta2; + cluster_t old_cluster = node->entry_cluster; + off64_t old_offset = node->entry_offset; + const size_t name_length = utf16_length(name); + const int name_entries = DIV_ROUND_UP(name_length, EXFAT_ENAME_MAX); + int i; + + exfat_pread(ef->dev, &meta1, sizeof(meta1), + co2o(ef, old_cluster, old_offset)); + next_entry(ef, node->parent, &old_cluster, &old_offset); + exfat_pread(ef->dev, &meta2, sizeof(meta2), + co2o(ef, old_cluster, old_offset)); + meta1.continuations = 1 + name_entries; + meta2.name_hash = exfat_calc_name_hash(ef, name); + meta2.name_length = name_length; + meta1.checksum = exfat_calc_checksum(&meta1, &meta2, name); + + erase_entry(ef, node); + + node->entry_cluster = new_cluster; + node->entry_offset = new_offset; + + exfat_pwrite(ef->dev, &meta1, sizeof(meta1), + co2o(ef, new_cluster, new_offset)); + next_entry(ef, dir, &new_cluster, &new_offset); + exfat_pwrite(ef->dev, &meta2, sizeof(meta2), + co2o(ef, new_cluster, new_offset)); + + for (i = 0; i < name_entries; i++) + { + struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0}; + memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX, + EXFAT_ENAME_MAX * sizeof(le16_t)); + next_entry(ef, dir, &new_cluster, &new_offset); + exfat_pwrite(ef->dev, &name_entry, sizeof(name_entry), + co2o(ef, new_cluster, new_offset)); + } + + memcpy(node->name, name, (EXFAT_NAME_MAX + 1) * sizeof(le16_t)); + tree_detach(node); + tree_attach(dir, node); +} + +int exfat_rename(struct exfat* ef, const char* old_path, const char* new_path) +{ + struct exfat_node* node; + struct exfat_node* existing; + struct exfat_node* dir; + cluster_t cluster = EXFAT_CLUSTER_BAD; + off64_t offset = -1; + le16_t name[EXFAT_NAME_MAX + 1]; + int rc; + + rc = exfat_lookup(ef, &node, old_path); + if (rc != 0) + return rc; + + rc = exfat_split(ef, &dir, &existing, name, new_path); + if (rc != 0) + { + exfat_put_node(ef, node); + return rc; + } + if (existing != NULL) + { + /* remove target if it's not the same node as source */ + if (existing != node) + { + if (existing->flags & EXFAT_ATTRIB_DIR) + { + if (node->flags & EXFAT_ATTRIB_DIR) + rc = exfat_rmdir(ef, existing); + else + rc = -ENOTDIR; + } + else + { + if (!(node->flags & EXFAT_ATTRIB_DIR)) + rc = exfat_unlink(ef, existing); + else + rc = -EISDIR; + } + exfat_put_node(ef, existing); + if (rc != 0) + { + exfat_put_node(ef, dir); + exfat_put_node(ef, node); + return rc; + } + } + else + exfat_put_node(ef, existing); + } + + rc = find_slot(ef, dir, &cluster, &offset, + 2 + DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX)); + if (rc != 0) + { + exfat_put_node(ef, dir); + exfat_put_node(ef, node); + return rc; + } + rename_entry(ef, dir, node, name, cluster, offset); + exfat_put_node(ef, dir); + exfat_put_node(ef, node); + return 0; +} + +void exfat_utimes(struct exfat_node* node, const struct timespec tv[2]) +{ + node->atime = tv[0].tv_sec; + node->mtime = tv[1].tv_sec; + node->flags |= EXFAT_ATTRIB_DIRTY; +} + +void exfat_update_atime(struct exfat_node* node) +{ + node->atime = time(NULL); + node->flags |= EXFAT_ATTRIB_DIRTY; +} + +void exfat_update_mtime(struct exfat_node* node) +{ + node->mtime = time(NULL); + node->flags |= EXFAT_ATTRIB_DIRTY; +} + +const char* exfat_get_label(struct exfat* ef) +{ + return ef->label; +} + +static int find_label(struct exfat* ef, cluster_t* cluster, off64_t* offset) +{ + struct iterator it; + int rc; + + rc = opendir(ef, ef->root, &it); + if (rc != 0) + return rc; + + for (;;) + { + if (it.offset >= ef->root->size) + { + closedir(&it); + return -ENOENT; + } + + if (get_entry_ptr(ef, &it)->type == EXFAT_ENTRY_LABEL) + { + *cluster = it.cluster; + *offset = it.offset; + closedir(&it); + return 0; + } + + if (fetch_next_entry(ef, ef->root, &it) != 0) + { + closedir(&it); + return -EIO; + } + } +} + +int exfat_set_label(struct exfat* ef, const char* label) +{ + le16_t label_utf16[EXFAT_ENAME_MAX + 1]; + int rc; + cluster_t cluster; + off64_t offset; + struct exfat_entry_label entry; + + memset(label_utf16, 0, sizeof(label_utf16)); + rc = utf8_to_utf16(label_utf16, label, EXFAT_ENAME_MAX, strlen(label)); + if (rc != 0) + return rc; + + rc = find_label(ef, &cluster, &offset); + if (rc == -ENOENT) + rc = find_slot(ef, ef->root, &cluster, &offset, 1); + if (rc != 0) + return rc; + + entry.type = EXFAT_ENTRY_LABEL; + entry.length = utf16_length(label_utf16); + memcpy(entry.name, label_utf16, sizeof(entry.name)); + if (entry.length == 0) + entry.type ^= EXFAT_ENTRY_VALID; + + exfat_pwrite(ef->dev, &entry, sizeof(struct exfat_entry_label), + co2o(ef, cluster, offset)); + return 0; +} diff --git a/exfat/libexfat/time.c b/exfat/libexfat/time.c new file mode 100644 index 000000000..890930eac --- /dev/null +++ b/exfat/libexfat/time.c @@ -0,0 +1,156 @@ +/* + time.c (03.02.12) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" + +/* timezone offset from UTC in seconds; positive for western timezones, + negative for eastern ones */ +static long exfat_timezone; + +#define SEC_IN_MIN 60ll +#define SEC_IN_HOUR (60 * SEC_IN_MIN) +#define SEC_IN_DAY (24 * SEC_IN_HOUR) +#define SEC_IN_YEAR (365 * SEC_IN_DAY) /* not leap year */ +/* Unix epoch started at 0:00:00 UTC 1 January 1970 */ +#define UNIX_EPOCH_YEAR 1970 +/* exFAT epoch started at 0:00:00 UTC 1 January 1980 */ +#define EXFAT_EPOCH_YEAR 1980 +/* number of years from Unix epoch to exFAT epoch */ +#define EPOCH_DIFF_YEAR (EXFAT_EPOCH_YEAR - UNIX_EPOCH_YEAR) +/* number of days from Unix epoch to exFAT epoch (considering leap days) */ +#define EPOCH_DIFF_DAYS (EPOCH_DIFF_YEAR * 365 + EPOCH_DIFF_YEAR / 4) +/* number of seconds from Unix epoch to exFAT epoch (considering leap days) */ +#define EPOCH_DIFF_SEC (EPOCH_DIFF_DAYS * SEC_IN_DAY) +/* number of leap years passed from exFAT epoch to the specified year + (excluding the specified year itself) */ +#define LEAP_YEARS(year) ((EXFAT_EPOCH_YEAR + (year) - 1) / 4 \ + - (EXFAT_EPOCH_YEAR - 1) / 4) +/* checks whether the specified year is leap */ +#define IS_LEAP_YEAR(year) ((EXFAT_EPOCH_YEAR + (year)) % 4 == 0) + +static const time_t days_in_year[] = +{ + /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */ + 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 +}; + +time_t exfat_exfat2unix(le16_t date, le16_t time, uint8_t centisec) +{ + time_t unix_time = EPOCH_DIFF_SEC; + uint16_t ndate = le16_to_cpu(date); + uint16_t ntime = le16_to_cpu(time); + + uint16_t day = ndate & 0x1f; /* 5 bits, 1-31 */ + uint16_t month = ndate >> 5 & 0xf; /* 4 bits, 1-12 */ + uint16_t year = ndate >> 9; /* 7 bits, 1-127 (+1980) */ + + uint16_t twosec = ntime & 0x1f; /* 5 bits, 0-29 (2 sec granularity) */ + uint16_t min = ntime >> 5 & 0x3f; /* 6 bits, 0-59 */ + uint16_t hour = ntime >> 11; /* 5 bits, 0-23 */ + + if (day == 0 || month == 0 || month > 12) + { + exfat_error("bad date %u-%02hu-%02hu", + year + EXFAT_EPOCH_YEAR, month, day); + return 0; + } + if (hour > 23 || min > 59 || twosec > 29) + { + exfat_error("bad time %hu:%02hu:%02u", + hour, min, twosec * 2); + return 0; + } + if (centisec > 199) + { + exfat_error("bad centiseconds count %hhu", centisec); + return 0; + } + + /* every 4th year between 1904 and 2096 is leap */ + unix_time += year * SEC_IN_YEAR + LEAP_YEARS(year) * SEC_IN_DAY; + unix_time += days_in_year[month] * SEC_IN_DAY; + /* if it's leap year and February has passed we should add 1 day */ + if ((EXFAT_EPOCH_YEAR + year) % 4 == 0 && month > 2) + unix_time += SEC_IN_DAY; + unix_time += (day - 1) * SEC_IN_DAY; + + unix_time += hour * SEC_IN_HOUR; + unix_time += min * SEC_IN_MIN; + /* exFAT represents time with 2 sec granularity */ + unix_time += twosec * 2; + unix_time += centisec / 100; + + /* exFAT stores timestamps in local time, so we correct it to UTC */ + unix_time += exfat_timezone; + + return unix_time; +} + +void exfat_unix2exfat(time_t unix_time, le16_t* date, le16_t* time, + uint8_t* centisec) +{ + time_t shift = EPOCH_DIFF_SEC + exfat_timezone; + uint16_t day, month, year; + uint16_t twosec, min, hour; + int days; + int i; + + /* time before exFAT epoch cannot be represented */ + if (unix_time < shift) + unix_time = shift; + + unix_time -= shift; + + days = unix_time / SEC_IN_DAY; + year = (4 * days) / (4 * 365 + 1); + days -= year * 365 + LEAP_YEARS(year); + month = 0; + for (i = 1; i <= 12; i++) + { + int leap_day = (IS_LEAP_YEAR(year) && i == 2); + int leap_sub = (IS_LEAP_YEAR(year) && i >= 3); + + if (i == 12 || days - leap_sub < days_in_year[i + 1] + leap_day) + { + month = i; + days -= days_in_year[i] + leap_sub; + break; + } + } + day = days + 1; + + hour = (unix_time % SEC_IN_DAY) / SEC_IN_HOUR; + min = (unix_time % SEC_IN_HOUR) / SEC_IN_MIN; + twosec = (unix_time % SEC_IN_MIN) / 2; + + *date = cpu_to_le16(day | (month << 5) | (year << 9)); + *time = cpu_to_le16(twosec | (min << 5) | (hour << 11)); + if (centisec) + *centisec = (unix_time % 2) * 100; +} + +void exfat_tzset(void) +{ + time_t now; + + tzset(); + now = time(NULL); + exfat_timezone = mktime(gmtime(&now)) - now; +} diff --git a/exfat/libexfat/utf.c b/exfat/libexfat/utf.c new file mode 100644 index 000000000..983c7930b --- /dev/null +++ b/exfat/libexfat/utf.c @@ -0,0 +1,229 @@ +/* + utf.c (13.09.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include + +static char* wchar_to_utf8(char* output, wchar_t wc, size_t outsize) +{ + if (wc <= 0x7f) + { + if (outsize < 1) + return NULL; + *output++ = (char) wc; + } + else if (wc <= 0x7ff) + { + if (outsize < 2) + return NULL; + *output++ = 0xc0 | (wc >> 6); + *output++ = 0x80 | (wc & 0x3f); + } + else if (wc <= 0xffff) + { + if (outsize < 3) + return NULL; + *output++ = 0xe0 | (wc >> 12); + *output++ = 0x80 | ((wc >> 6) & 0x3f); + *output++ = 0x80 | (wc & 0x3f); + } + else if (wc <= 0x1fffff) + { + if (outsize < 4) + return NULL; + *output++ = 0xf0 | (wc >> 18); + *output++ = 0x80 | ((wc >> 12) & 0x3f); + *output++ = 0x80 | ((wc >> 6) & 0x3f); + *output++ = 0x80 | (wc & 0x3f); + } + else if (wc <= 0x3ffffff) + { + if (outsize < 5) + return NULL; + *output++ = 0xf8 | (wc >> 24); + *output++ = 0x80 | ((wc >> 18) & 0x3f); + *output++ = 0x80 | ((wc >> 12) & 0x3f); + *output++ = 0x80 | ((wc >> 6) & 0x3f); + *output++ = 0x80 | (wc & 0x3f); + } + else if (wc <= 0x7fffffff) + { + if (outsize < 6) + return NULL; + *output++ = 0xfc | (wc >> 30); + *output++ = 0x80 | ((wc >> 24) & 0x3f); + *output++ = 0x80 | ((wc >> 18) & 0x3f); + *output++ = 0x80 | ((wc >> 12) & 0x3f); + *output++ = 0x80 | ((wc >> 6) & 0x3f); + *output++ = 0x80 | (wc & 0x3f); + } + else + return NULL; + + return output; +} + +static const le16_t* utf16_to_wchar(const le16_t* input, wchar_t* wc, + size_t insize) +{ + if ((le16_to_cpu(input[0]) & 0xfc00) == 0xd800) + { + if (insize < 2 || (le16_to_cpu(input[1]) & 0xfc00) != 0xdc00) + return NULL; + *wc = ((wchar_t) (le16_to_cpu(input[0]) & 0x3ff) << 10); + *wc |= (le16_to_cpu(input[1]) & 0x3ff); + *wc += 0x10000; + return input + 2; + } + else + { + *wc = le16_to_cpu(*input); + return input + 1; + } +} + +int utf16_to_utf8(char* output, const le16_t* input, size_t outsize, + size_t insize) +{ + const le16_t* inp = input; + char* outp = output; + wchar_t wc; + + while (inp - input < insize && le16_to_cpu(*inp)) + { + inp = utf16_to_wchar(inp, &wc, insize - (inp - input)); + if (inp == NULL) + { + exfat_error("illegal UTF-16 sequence"); + return -EILSEQ; + } + outp = wchar_to_utf8(outp, wc, outsize - (outp - output)); + if (outp == NULL) + { + exfat_error("name is too long"); + return -ENAMETOOLONG; + } + } + *outp = '\0'; + return 0; +} + +static const char* utf8_to_wchar(const char* input, wchar_t* wc, + size_t insize) +{ + if ((input[0] & 0x80) == 0 && insize >= 1) + { + *wc = (wchar_t) input[0]; + return input + 1; + } + if ((input[0] & 0xe0) == 0xc0 && insize >= 2) + { + *wc = (((wchar_t) input[0] & 0x1f) << 6) | + ((wchar_t) input[1] & 0x3f); + return input + 2; + } + if ((input[0] & 0xf0) == 0xe0 && insize >= 3) + { + *wc = (((wchar_t) input[0] & 0x0f) << 12) | + (((wchar_t) input[1] & 0x3f) << 6) | + ((wchar_t) input[2] & 0x3f); + return input + 3; + } + if ((input[0] & 0xf8) == 0xf0 && insize >= 4) + { + *wc = (((wchar_t) input[0] & 0x07) << 18) | + (((wchar_t) input[1] & 0x3f) << 12) | + (((wchar_t) input[2] & 0x3f) << 6) | + ((wchar_t) input[3] & 0x3f); + return input + 4; + } + if ((input[0] & 0xfc) == 0xf8 && insize >= 5) + { + *wc = (((wchar_t) input[0] & 0x03) << 24) | + (((wchar_t) input[1] & 0x3f) << 18) | + (((wchar_t) input[2] & 0x3f) << 12) | + (((wchar_t) input[3] & 0x3f) << 6) | + ((wchar_t) input[4] & 0x3f); + return input + 5; + } + if ((input[0] & 0xfe) == 0xfc && insize >= 6) + { + *wc = (((wchar_t) input[0] & 0x01) << 30) | + (((wchar_t) input[1] & 0x3f) << 24) | + (((wchar_t) input[2] & 0x3f) << 18) | + (((wchar_t) input[3] & 0x3f) << 12) | + (((wchar_t) input[4] & 0x3f) << 6) | + ((wchar_t) input[5] & 0x3f); + return input + 6; + } + return NULL; +} + +static le16_t* wchar_to_utf16(le16_t* output, wchar_t wc, size_t outsize) +{ + if (wc <= 0xffff) /* if character is from BMP */ + { + if (outsize == 0) + return NULL; + output[0] = cpu_to_le16(wc); + return output + 1; + } + if (outsize < 2) + return NULL; + wc -= 0x10000; + output[0] = cpu_to_le16(0xd800 | ((wc >> 10) & 0x3ff)); + output[1] = cpu_to_le16(0xdc00 | (wc & 0x3ff)); + return output + 2; +} + +int utf8_to_utf16(le16_t* output, const char* input, size_t outsize, + size_t insize) +{ + const char* inp = input; + le16_t* outp = output; + wchar_t wc; + + while (inp - input < insize && *inp) + { + inp = utf8_to_wchar(inp, &wc, insize - (inp - input)); + if (inp == NULL) + { + exfat_error("illegal UTF-8 sequence"); + return -EILSEQ; + } + outp = wchar_to_utf16(outp, wc, outsize - (outp - output)); + if (outp == NULL) + { + exfat_error("name is too long"); + return -ENAMETOOLONG; + } + } + *outp = cpu_to_le16(0); + return 0; +} + +size_t utf16_length(const le16_t* str) +{ + size_t i = 0; + + while (le16_to_cpu(str[i])) + i++; + return i; +} diff --git a/exfat/libexfat/utils.c b/exfat/libexfat/utils.c new file mode 100644 index 000000000..14b4791fe --- /dev/null +++ b/exfat/libexfat/utils.c @@ -0,0 +1,176 @@ +/* + utils.c (04.09.09) + exFAT file system implementation library. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "exfat.h" +#include +#include +#include + +void exfat_stat(const struct exfat* ef, const struct exfat_node* node, + struct stat* stbuf) +{ + memset(stbuf, 0, sizeof(struct stat)); + if (node->flags & EXFAT_ATTRIB_DIR) + stbuf->st_mode = S_IFDIR | (0777 & ~ef->dmask); + else + stbuf->st_mode = S_IFREG | (0777 & ~ef->fmask); + stbuf->st_nlink = 1; + stbuf->st_uid = ef->uid; + stbuf->st_gid = ef->gid; + stbuf->st_size = node->size; + stbuf->st_blocks = DIV_ROUND_UP(node->size, CLUSTER_SIZE(*ef->sb)) * + CLUSTER_SIZE(*ef->sb) / 512; + stbuf->st_mtime = node->mtime; + stbuf->st_atime = node->atime; + /* set ctime to mtime to ensure we don't break programs that rely on ctime + (e.g. rsync) */ + stbuf->st_ctime = node->mtime; +} + +void exfat_get_name(const struct exfat_node* node, char* buffer, size_t n) +{ + if (utf16_to_utf8(buffer, node->name, n, EXFAT_NAME_MAX) != 0) + exfat_bug("failed to convert name to UTF-8"); +} + +uint16_t exfat_start_checksum(const struct exfat_entry_meta1* entry) +{ + uint16_t sum = 0; + int i; + + for (i = 0; i < sizeof(struct exfat_entry); i++) + if (i != 2 && i != 3) /* skip checksum field itself */ + sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i]; + return sum; +} + +uint16_t exfat_add_checksum(const void* entry, uint16_t sum) +{ + int i; + + for (i = 0; i < sizeof(struct exfat_entry); i++) + sum = ((sum << 15) | (sum >> 1)) + ((const uint8_t*) entry)[i]; + return sum; +} + +le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1, + const struct exfat_entry_meta2* meta2, const le16_t* name) +{ + uint16_t checksum; + const int name_entries = DIV_ROUND_UP(utf16_length(name), EXFAT_ENAME_MAX); + int i; + + checksum = exfat_start_checksum(meta1); + checksum = exfat_add_checksum(meta2, checksum); + for (i = 0; i < name_entries; i++) + { + struct exfat_entry_name name_entry = {EXFAT_ENTRY_FILE_NAME, 0}; + memcpy(name_entry.name, name + i * EXFAT_ENAME_MAX, + EXFAT_ENAME_MAX * sizeof(le16_t)); + checksum = exfat_add_checksum(&name_entry, checksum); + } + return cpu_to_le16(checksum); +} + +uint32_t exfat_vbr_start_checksum(const void* sector, size_t size) +{ + size_t i; + uint32_t sum = 0; + + for (i = 0; i < size; i++) + /* skip volume_state and allocated_percent fields */ + if (i != 0x6a && i != 0x6b && i != 0x70) + sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i]; + return sum; +} + +uint32_t exfat_vbr_add_checksum(const void* sector, size_t size, uint32_t sum) +{ + size_t i; + + for (i = 0; i < size; i++) + sum = ((sum << 31) | (sum >> 1)) + ((const uint8_t*) sector)[i]; + return sum; +} + +le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name) +{ + size_t i; + size_t length = utf16_length(name); + uint16_t hash = 0; + + for (i = 0; i < length; i++) + { + uint16_t c = le16_to_cpu(name[i]); + + /* convert to upper case */ + if (c < ef->upcase_chars) + c = le16_to_cpu(ef->upcase[c]); + + hash = ((hash << 15) | (hash >> 1)) + (c & 0xff); + hash = ((hash << 15) | (hash >> 1)) + (c >> 8); + } + return cpu_to_le16(hash); +} + +void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb) +{ + size_t i; + /* 16 EB (minus 1 byte) is the largest size that can be represented by + uint64_t */ + const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB", "EB"}; + uint64_t divisor = 1; + uint64_t temp = 0; + + for (i = 0; ; i++, divisor *= 1024) + { + temp = (value + divisor / 2) / divisor; + + if (temp == 0) + break; + if (temp / 1024 * 1024 == temp) + continue; + if (temp < 10240) + break; + } + hb->value = temp; + hb->unit = units[i]; +} + +void exfat_print_info(const struct exfat_super_block* sb, + uint32_t free_clusters) +{ + struct exfat_human_bytes hb; + off64_t total_space = le64_to_cpu(sb->sector_count) * SECTOR_SIZE(*sb); + off64_t avail_space = (off64_t) free_clusters * CLUSTER_SIZE(*sb); + + printf("File system version %hhu.%hhu\n", + sb->version.major, sb->version.minor); + exfat_humanize_bytes(SECTOR_SIZE(*sb), &hb); + printf("Sector size %10"PRIu64" %s\n", hb.value, hb.unit); + exfat_humanize_bytes(CLUSTER_SIZE(*sb), &hb); + printf("Cluster size %10"PRIu64" %s\n", hb.value, hb.unit); + exfat_humanize_bytes(total_space, &hb); + printf("Volume size %10"PRIu64" %s\n", hb.value, hb.unit); + exfat_humanize_bytes(total_space - avail_space, &hb); + printf("Used space %10"PRIu64" %s\n", hb.value, hb.unit); + exfat_humanize_bytes(avail_space, &hb); + printf("Available space %10"PRIu64" %s\n", hb.value, hb.unit); +} diff --git a/exfat/libexfat/version.h b/exfat/libexfat/version.h new file mode 100644 index 000000000..f35cb4fdf --- /dev/null +++ b/exfat/libexfat/version.h @@ -0,0 +1,28 @@ +/* + version.h (12.06.10) + Version constants. + + Copyright (C) 2010-2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef VERSION_H_INCLUDED +#define VERSION_H_INCLUDED + +#define EXFAT_VERSION_MAJOR 0 +#define EXFAT_VERSION_MINOR 9 +#define EXFAT_VERSION_PATCH 8 + +#endif /* ifndef VERSION_H_INCLUDED */ diff --git a/exfat/mkfs/Android.mk b/exfat/mkfs/Android.mk new file mode 100644 index 000000000..bccd7c9ac --- /dev/null +++ b/exfat/mkfs/Android.mk @@ -0,0 +1,21 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := mkexfatfs +LOCAL_MODULE_TAGS := optional +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin +LOCAL_CFLAGS = -D_FILE_OFFSET_BITS=64 +LOCAL_SRC_FILES = cbm.c fat.c main.c mkexfat.c rootdir.c uct.c uctc.c vbr.c +LOCAL_C_INCLUDES += $(LOCAL_PATH) \ + bootable/recovery/exfat/libexfat \ + bootable/recovery/fuse/include +LOCAL_SHARED_LIBRARIES += libz libc libexfat libdl +LOCAL_STATIC_LIBRARIES += libfuse + +include $(BUILD_EXECUTABLE) + +# We need this so that the installed files could be picked up based on the +# local module name +ALL_MODULES.$(LOCAL_MODULE).INSTALLED := \ + $(ALL_MODULES.$(LOCAL_MODULE).INSTALLED) $(SYMLINKS) diff --git a/exfat/mkfs/cbm.c b/exfat/mkfs/cbm.c new file mode 100644 index 000000000..6d948b4cd --- /dev/null +++ b/exfat/mkfs/cbm.c @@ -0,0 +1,74 @@ +/* + cbm.c (09.11.10) + Clusters Bitmap creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "cbm.h" +#include "fat.h" +#include "uct.h" +#include "rootdir.h" + +static off64_t cbm_alignment(void) +{ + return get_cluster_size(); +} + +static off64_t cbm_size(void) +{ + return DIV_ROUND_UP( + (get_volume_size() - get_position(&cbm)) / get_cluster_size(), + CHAR_BIT); +} + +static int cbm_write(struct exfat_dev* dev) +{ + uint32_t allocated_clusters = + DIV_ROUND_UP(cbm.get_size(), get_cluster_size()) + + DIV_ROUND_UP(uct.get_size(), get_cluster_size()) + + DIV_ROUND_UP(rootdir.get_size(), get_cluster_size()); + size_t bitmap_size = DIV_ROUND_UP(allocated_clusters, CHAR_BIT); + uint8_t* bitmap = malloc(bitmap_size); + size_t i; + + if (bitmap == NULL) + { + exfat_error("failed to allocate bitmap of %zu bytes", bitmap_size); + return 1; + } + + for (i = 0; i < bitmap_size * CHAR_BIT; i++) + if (i < allocated_clusters) + BMAP_SET(bitmap, i); + else + BMAP_CLR(bitmap, i); + if (exfat_write(dev, bitmap, bitmap_size) < 0) + { + exfat_error("failed to write bitmap of %zu bytes", bitmap_size); + return 1; + } + free(bitmap); + return 0; +} + +const struct fs_object cbm = +{ + .get_alignment = cbm_alignment, + .get_size = cbm_size, + .write = cbm_write, +}; diff --git a/exfat/mkfs/cbm.h b/exfat/mkfs/cbm.h new file mode 100644 index 000000000..176504c0f --- /dev/null +++ b/exfat/mkfs/cbm.h @@ -0,0 +1,28 @@ +/* + cbm.h (09.11.10) + Clusters Bitmap creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_CBM_H_INCLUDED +#define MKFS_CBM_H_INCLUDED + +#include "mkexfat.h" + +extern const struct fs_object cbm; + +#endif /* ifndef MKFS_CBM_H_INCLUDED */ diff --git a/exfat/mkfs/fat.c b/exfat/mkfs/fat.c new file mode 100644 index 000000000..82feff8ba --- /dev/null +++ b/exfat/mkfs/fat.c @@ -0,0 +1,86 @@ +/* + fat.c (09.11.10) + File Allocation Table creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "fat.h" +#include "cbm.h" +#include "uct.h" +#include "rootdir.h" + +static off64_t fat_alignment(void) +{ + return (off64_t) 128 * get_sector_size(); +} + +static off64_t fat_size(void) +{ + return get_volume_size() / get_cluster_size() * sizeof(cluster_t); +} + +static cluster_t fat_write_entry(struct exfat_dev* dev, cluster_t cluster, + cluster_t value) +{ + le32_t fat_entry = cpu_to_le32(value); + if (exfat_write(dev, &fat_entry, sizeof(fat_entry)) < 0) + { + exfat_error("failed to write FAT entry 0x%x", value); + return 0; + } + return cluster + 1; +} + +static cluster_t fat_write_entries(struct exfat_dev* dev, cluster_t cluster, + uint64_t length) +{ + cluster_t end = cluster + DIV_ROUND_UP(length, get_cluster_size()); + + while (cluster < end - 1) + { + cluster = fat_write_entry(dev, cluster, cluster + 1); + if (cluster == 0) + return 0; + } + return fat_write_entry(dev, cluster, EXFAT_CLUSTER_END); +} + +static int fat_write(struct exfat_dev* dev) +{ + cluster_t c = 0; + + if (!(c = fat_write_entry(dev, c, 0xfffffff8))) /* media type */ + return 1; + if (!(c = fat_write_entry(dev, c, 0xffffffff))) /* some weird constant */ + return 1; + if (!(c = fat_write_entries(dev, c, cbm.get_size()))) + return 1; + if (!(c = fat_write_entries(dev, c, uct.get_size()))) + return 1; + if (!(c = fat_write_entries(dev, c, rootdir.get_size()))) + return 1; + + return 0; +} + +const struct fs_object fat = +{ + .get_alignment = fat_alignment, + .get_size = fat_size, + .write = fat_write, +}; diff --git a/exfat/mkfs/fat.h b/exfat/mkfs/fat.h new file mode 100644 index 000000000..8d7b86c10 --- /dev/null +++ b/exfat/mkfs/fat.h @@ -0,0 +1,28 @@ +/* + fat.h (09.11.10) + File Allocation Table creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_FAT_H_INCLUDED +#define MKFS_FAT_H_INCLUDED + +#include "mkexfat.h" + +extern const struct fs_object fat; + +#endif /* ifndef MKFS_FAT_H_INCLUDED */ diff --git a/exfat/mkfs/main.c b/exfat/mkfs/main.c new file mode 100644 index 000000000..9feb750ee --- /dev/null +++ b/exfat/mkfs/main.c @@ -0,0 +1,269 @@ +/* + main.c (15.08.10) + Creates exFAT file system. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "mkexfat.h" +#include "vbr.h" +#include "fat.h" +#include "cbm.h" +#include "uct.h" +#include "rootdir.h" + +const struct fs_object* objects[] = +{ + &vbr, + &vbr, + &fat, + /* clusters heap */ + &cbm, + &uct, + &rootdir, + NULL, +}; + +static struct +{ + int sector_bits; + int spc_bits; + off64_t volume_size; + le16_t volume_label[EXFAT_ENAME_MAX + 1]; + uint32_t volume_serial; + uint64_t first_sector; +} +param; + +int get_sector_bits(void) +{ + return param.sector_bits; +} + +int get_spc_bits(void) +{ + return param.spc_bits; +} + +off64_t get_volume_size(void) +{ + return param.volume_size; +} + +const le16_t* get_volume_label(void) +{ + return param.volume_label; +} + +uint32_t get_volume_serial(void) +{ + return param.volume_serial; +} + +uint64_t get_first_sector(void) +{ + return param.first_sector; +} + +int get_sector_size(void) +{ + return 1 << get_sector_bits(); +} + +int get_cluster_size(void) +{ + return get_sector_size() << get_spc_bits(); +} + +static int setup_spc_bits(int sector_bits, int user_defined, off64_t volume_size) +{ + int i; + + if (user_defined != -1) + { + off64_t cluster_size = 1 << sector_bits << user_defined; + if (volume_size / cluster_size > EXFAT_LAST_DATA_CLUSTER) + { + struct exfat_human_bytes chb, vhb; + + exfat_humanize_bytes(cluster_size, &chb); + exfat_humanize_bytes(volume_size, &vhb); + exfat_error("cluster size %"PRIu64" %s is too small for " + "%"PRIu64" %s volume, try -s %d", + chb.value, chb.unit, + vhb.value, vhb.unit, + 1 << setup_spc_bits(sector_bits, -1, volume_size)); + return -1; + } + return user_defined; + } + + if (volume_size < 256ull * 1024 * 1024) + return MAX(0, 12 - sector_bits); /* 4 KB */ + if (volume_size < 32ull * 1024 * 1024 * 1024) + return MAX(0, 15 - sector_bits); /* 32 KB */ + + for (i = 17; ; i++) /* 128 KB or more */ + if (DIV_ROUND_UP(volume_size, 1 << i) <= EXFAT_LAST_DATA_CLUSTER) + return MAX(0, i - sector_bits); +} + +static int setup_volume_label(le16_t label[EXFAT_ENAME_MAX + 1], const char* s) +{ + memset(label, 0, (EXFAT_ENAME_MAX + 1) * sizeof(le16_t)); + if (s == NULL) + return 0; + return utf8_to_utf16(label, s, EXFAT_ENAME_MAX, strlen(s)); +} + +static uint32_t setup_volume_serial(uint32_t user_defined) +{ + struct timeval now; + + if (user_defined != 0) + return user_defined; + + if (gettimeofday(&now, NULL) != 0) + { + exfat_error("failed to form volume id"); + return 0; + } + return (now.tv_sec << 20) | now.tv_usec; +} + +static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits, + const char* volume_label, uint32_t volume_serial, + uint64_t first_sector) +{ + param.sector_bits = sector_bits; + param.first_sector = first_sector; + param.volume_size = exfat_get_size(dev); + + param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size); + if (param.spc_bits == -1) + return 1; + + if (setup_volume_label(param.volume_label, volume_label) != 0) + return 1; + + param.volume_serial = setup_volume_serial(volume_serial); + if (param.volume_serial == 0) + return 1; + + return mkfs(dev, param.volume_size); +} + +static int logarithm2(int n) +{ + int i; + + for (i = 0; i < sizeof(int) * CHAR_BIT - 1; i++) + if ((1 << i) == n) + return i; + return -1; +} + +static void usage(const char* prog) +{ + fprintf(stderr, "Usage: %s [-i volume-id] [-n label] " + "[-p partition-first-sector] " + "[-s sectors-per-cluster] [-v] \n", prog); + exit(1); +} + +int main(int argc, char* argv[]) +{ + const char* spec = NULL; + char** pp; + int spc_bits = -1; + const char* volume_label = NULL; + uint32_t volume_serial = 0; + uint64_t first_sector = 0; + struct exfat_dev* dev; + + printf("mkexfatfs %u.%u.%u\n", + EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH); + + for (pp = argv + 1; *pp; pp++) + { + if (strcmp(*pp, "-s") == 0) + { + pp++; + if (*pp == NULL) + usage(argv[0]); + spc_bits = logarithm2(atoi(*pp)); + if (spc_bits < 0) + { + exfat_error("invalid option value: `%s'", *pp); + return 1; + } + } + else if (strcmp(*pp, "-n") == 0) + { + pp++; + if (*pp == NULL) + usage(argv[0]); + volume_label = *pp; + } + else if (strcmp(*pp, "-i") == 0) + { + pp++; + if (*pp == NULL) + usage(argv[0]); + volume_serial = strtol(*pp, NULL, 16); + } + else if (strcmp(*pp, "-p") == 0) + { + pp++; + if (*pp == NULL) + usage(argv[0]); + first_sector = strtoll(*pp, NULL, 10); + } + else if (strcmp(*pp, "-v") == 0) + { + puts("Copyright (C) 2011, 2012 Andrew Nayenko"); + return 0; + } + else if (spec == NULL) + spec = *pp; + else + usage(argv[0]); + } + if (spec == NULL) + usage(argv[0]); + + dev = exfat_open(spec, EXFAT_MODE_RW); + if (dev == NULL) + return 1; + if (setup(dev, 9, spc_bits, volume_label, volume_serial, + first_sector) != 0) + { + exfat_close(dev); + return 1; + } + if (exfat_close(dev) != 0) + return 1; + printf("File system created successfully.\n"); + return 0; +} diff --git a/exfat/mkfs/mkexfat.c b/exfat/mkfs/mkexfat.c new file mode 100644 index 000000000..7018aaae2 --- /dev/null +++ b/exfat/mkfs/mkexfat.c @@ -0,0 +1,160 @@ +/* + mkexfat.c (22.04.12) + FS creation engine. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include +#include +#include +#include +#include "mkexfat.h" + +static int check_size(off64_t volume_size) +{ + const struct fs_object** pp; + off64_t position = 0; + + for (pp = objects; *pp; pp++) + { + position = ROUND_UP(position, (*pp)->get_alignment()); + position += (*pp)->get_size(); + } + + if (position > volume_size) + { + struct exfat_human_bytes vhb; + + exfat_humanize_bytes(volume_size, &vhb); + exfat_error("too small device (%"PRIu64" %s)", vhb.value, vhb.unit); + return 1; + } + + return 0; + +} + +static int erase_object(struct exfat_dev* dev, const void* block, + size_t block_size, off64_t start, off64_t size) +{ + const off64_t block_count = DIV_ROUND_UP(size, block_size); + off64_t i; + + if (exfat_seek(dev, start, SEEK_SET) == (off64_t) -1) + { + exfat_error("seek to 0x%"PRIx64" failed", start); + return 1; + } + for (i = 0; i < size; i += block_size) + { + if (exfat_write(dev, block, MIN(size - i, block_size)) < 0) + { + exfat_error("failed to erase block %"PRIu64"/%"PRIu64 + " at 0x%"PRIx64, i + 1, block_count, start); + return 1; + } + } + return 0; +} + +static int erase(struct exfat_dev* dev) +{ + const struct fs_object** pp; + off64_t position = 0; + const size_t block_size = 1024 * 1024; + void* block = malloc(block_size); + + if (block == NULL) + { + exfat_error("failed to allocate erase block of %zu bytes", block_size); + return 1; + } + memset(block, 0, block_size); + + for (pp = objects; *pp; pp++) + { + position = ROUND_UP(position, (*pp)->get_alignment()); + if (erase_object(dev, block, block_size, position, + (*pp)->get_size()) != 0) + { + free(block); + return 1; + } + position += (*pp)->get_size(); + } + + free(block); + return 0; +} + +static int create(struct exfat_dev* dev) +{ + const struct fs_object** pp; + off64_t position = 0; + + for (pp = objects; *pp; pp++) + { + position = ROUND_UP(position, (*pp)->get_alignment()); + if (exfat_seek(dev, position, SEEK_SET) == (off64_t) -1) + { + exfat_error("seek to 0x%"PRIx64" failed", position); + return 1; + } + if ((*pp)->write(dev) != 0) + return 1; + position += (*pp)->get_size(); + } + return 0; +} + +int mkfs(struct exfat_dev* dev, off64_t volume_size) +{ + if (check_size(volume_size) != 0) + return 1; + + fputs("Creating... ", stdout); + fflush(stdout); + if (erase(dev) != 0) + return 1; + if (create(dev) != 0) + return 1; + puts("done."); + + fputs("Flushing... ", stdout); + fflush(stdout); + if (exfat_fsync(dev) != 0) + return 1; + puts("done."); + + return 0; +} + +off64_t get_position(const struct fs_object* object) +{ + const struct fs_object** pp; + off64_t position = 0; + + for (pp = objects; *pp; pp++) + { + position = ROUND_UP(position, (*pp)->get_alignment()); + if (*pp == object) + return position; + position += (*pp)->get_size(); + } + exfat_bug("unknown object"); +} diff --git a/exfat/mkfs/mkexfat.h b/exfat/mkfs/mkexfat.h new file mode 100644 index 000000000..4634d0d95 --- /dev/null +++ b/exfat/mkfs/mkexfat.h @@ -0,0 +1,47 @@ +/* + mkexfat.h (09.11.10) + FS creation engine. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_MKEXFAT_H_INCLUDED +#define MKFS_MKEXFAT_H_INCLUDED + +#include + +struct fs_object +{ + off64_t (*get_alignment)(void); + off64_t (*get_size)(void); + int (*write)(struct exfat_dev* dev); +}; + +extern const struct fs_object* objects[]; + +int get_sector_bits(void); +int get_spc_bits(void); +off64_t get_volume_size(void); +const le16_t* get_volume_label(void); +uint32_t get_volume_serial(void); +uint64_t get_first_sector(void); +int get_sector_size(void); +int get_cluster_size(void); + +int mkfs(struct exfat_dev* dev, off64_t volume_size); +off64_t get_position(const struct fs_object* object); + +#endif /* ifndef MKFS_MKEXFAT_H_INCLUDED */ diff --git a/exfat/mkfs/mkexfatfs.8 b/exfat/mkfs/mkexfatfs.8 new file mode 100644 index 000000000..7d3ffecc1 --- /dev/null +++ b/exfat/mkfs/mkexfatfs.8 @@ -0,0 +1,68 @@ +.\" Copyright (C) 2011 Andrew Nayenko +.\" +.TH MKEXFATFS 8 "January 2011" +.SH NAME +.B mkexfatfs +\- create an exFAT file system +.SH SYNOPSIS +.B mkexfatfs +[ +.B \-i +.I volume-id +] +[ +.B \-n +.I volume-name +] +[ +.B \-p +.I partition-first-sector +] +[ +.B \-s +.I sectors-per-cluster +] +[ +.B \-v +] +.I device + +.SH DESCRIPTION +.B mkexfatfs +creates an exFAT file system on a block device. +.I device +is a special file corresponding to the device. + +.SH OPTIONS +Command line options available: +.TP +.BI \-i " volume-id" +A 32-bit hexadecimal number. By default a value based on current time is set. +.TP +.BI \-n " volume-name" +Volume name (label), up to 15 characters. By default no label is set. +.TP +.BI \-p " partition-first-sector" +First sector of the partition starting from the beginning of the whole disk. +exFAT super block has a field for this value but in fact it's optional and +does not affect anything. Default is 0. +.TP +.BI \-s " sectors-per-cluster" +Number of physical sectors per cluster (cluster is an allocation unit in +exFAT). Must be a power of 2, i.e. 1, 2, 4, 8, etc. Cluster size can not +exceed 32 MB. Default cluster sizes are: +4 KB if volume size is less than 256 MB, +32 KB if volume size is from 256 MB to 32 GB, +128 KB if volume size is 32 GB or larger. +.TP +.BI \-v +Print version and copyright. + +.SH EXIT CODES +Zero is returned on successful creation. Any other code means an error. + +.SH AUTHOR +Andrew Nayenko + +.SH SEE ALSO +.BR mkfs (8) diff --git a/exfat/mkfs/rootdir.c b/exfat/mkfs/rootdir.c new file mode 100644 index 000000000..ddc476f4c --- /dev/null +++ b/exfat/mkfs/rootdir.c @@ -0,0 +1,100 @@ +/* + rootdir.c (09.11.10) + Root directory creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "rootdir.h" +#include "uct.h" +#include "cbm.h" +#include "uctc.h" + +static off64_t rootdir_alignment(void) +{ + return get_cluster_size(); +} + +static off64_t rootdir_size(void) +{ + return get_cluster_size(); +} + +static void init_label_entry(struct exfat_entry_label* label_entry) +{ + memset(label_entry, 0, sizeof(struct exfat_entry_label)); + label_entry->type = EXFAT_ENTRY_LABEL ^ EXFAT_ENTRY_VALID; + + if (utf16_length(get_volume_label()) == 0) + return; + + memcpy(label_entry->name, get_volume_label(), + EXFAT_ENAME_MAX * sizeof(le16_t)); + label_entry->length = utf16_length(get_volume_label()); + label_entry->type |= EXFAT_ENTRY_VALID; +} + +static void init_bitmap_entry(struct exfat_entry_bitmap* bitmap_entry) +{ + memset(bitmap_entry, 0, sizeof(struct exfat_entry_bitmap)); + bitmap_entry->type = EXFAT_ENTRY_BITMAP; + bitmap_entry->start_cluster = cpu_to_le32(EXFAT_FIRST_DATA_CLUSTER); + bitmap_entry->size = cpu_to_le64(cbm.get_size()); +} + +static void init_upcase_entry(struct exfat_entry_upcase* upcase_entry) +{ + size_t i; + uint32_t sum = 0; + + for (i = 0; i < sizeof(upcase_table); i++) + sum = ((sum << 31) | (sum >> 1)) + upcase_table[i]; + + memset(upcase_entry, 0, sizeof(struct exfat_entry_upcase)); + upcase_entry->type = EXFAT_ENTRY_UPCASE; + upcase_entry->checksum = cpu_to_le32(sum); + upcase_entry->start_cluster = cpu_to_le32( + (get_position(&uct) - get_position(&cbm)) / get_cluster_size() + + EXFAT_FIRST_DATA_CLUSTER); + upcase_entry->size = cpu_to_le64(sizeof(upcase_table)); +} + +static int rootdir_write(struct exfat_dev* dev) +{ + struct exfat_entry_label label_entry; + struct exfat_entry_bitmap bitmap_entry; + struct exfat_entry_upcase upcase_entry; + + init_label_entry(&label_entry); + init_bitmap_entry(&bitmap_entry); + init_upcase_entry(&upcase_entry); + + if (exfat_write(dev, &label_entry, sizeof(struct exfat_entry)) < 0) + return 1; + if (exfat_write(dev, &bitmap_entry, sizeof(struct exfat_entry)) < 0) + return 1; + if (exfat_write(dev, &upcase_entry, sizeof(struct exfat_entry)) < 0) + return 1; + return 0; +} + +const struct fs_object rootdir = +{ + .get_alignment = rootdir_alignment, + .get_size = rootdir_size, + .write = rootdir_write, +}; diff --git a/exfat/mkfs/rootdir.h b/exfat/mkfs/rootdir.h new file mode 100644 index 000000000..64e533243 --- /dev/null +++ b/exfat/mkfs/rootdir.h @@ -0,0 +1,28 @@ +/* + rootdir.h (09.11.10) + Root directory creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_ROOTDIR_H_INCLUDED +#define MKFS_ROOTDIR_H_INCLUDED + +#include "mkexfat.h" + +extern const struct fs_object rootdir; + +#endif /* ifndef MKFS_ROOTDIR_H_INCLUDED */ diff --git a/exfat/mkfs/uct.c b/exfat/mkfs/uct.c new file mode 100644 index 000000000..e6fd2c02e --- /dev/null +++ b/exfat/mkfs/uct.c @@ -0,0 +1,50 @@ +/* + uct.c (09.11.10) + Upper Case Table creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "uct.h" +#include "uctc.h" + +static off64_t uct_alignment(void) +{ + return get_cluster_size(); +} + +static off64_t uct_size(void) +{ + return sizeof(upcase_table); +} + +static int uct_write(struct exfat_dev* dev) +{ + if (exfat_write(dev, upcase_table, sizeof(upcase_table)) < 0) + { + exfat_error("failed to write upcase table of %zu bytes", + sizeof(upcase_table)); + return 1; + } + return 0; +} + +const struct fs_object uct = +{ + .get_alignment = uct_alignment, + .get_size = uct_size, + .write = uct_write, +}; diff --git a/exfat/mkfs/uct.h b/exfat/mkfs/uct.h new file mode 100644 index 000000000..4b4f024cb --- /dev/null +++ b/exfat/mkfs/uct.h @@ -0,0 +1,28 @@ +/* + uct.h (09.11.10) + Upper Case Table creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_UCT_H_INCLUDED +#define MKFS_UCT_H_INCLUDED + +#include "mkexfat.h" + +extern const struct fs_object uct; + +#endif /* ifndef MKFS_UCT_H_INCLUDED */ diff --git a/exfat/mkfs/uctc.c b/exfat/mkfs/uctc.c new file mode 100644 index 000000000..573981168 --- /dev/null +++ b/exfat/mkfs/uctc.c @@ -0,0 +1,755 @@ +/* + uctc.c (30.04.12) + Upper Case Table contents. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include "uctc.h" + +uint8_t upcase_table[5836] = +{ + 0x00, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, + 0x04, 0x00, 0x05, 0x00, 0x06, 0x00, 0x07, 0x00, + 0x08, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x0b, 0x00, + 0x0c, 0x00, 0x0d, 0x00, 0x0e, 0x00, 0x0f, 0x00, + 0x10, 0x00, 0x11, 0x00, 0x12, 0x00, 0x13, 0x00, + 0x14, 0x00, 0x15, 0x00, 0x16, 0x00, 0x17, 0x00, + 0x18, 0x00, 0x19, 0x00, 0x1a, 0x00, 0x1b, 0x00, + 0x1c, 0x00, 0x1d, 0x00, 0x1e, 0x00, 0x1f, 0x00, + 0x20, 0x00, 0x21, 0x00, 0x22, 0x00, 0x23, 0x00, + 0x24, 0x00, 0x25, 0x00, 0x26, 0x00, 0x27, 0x00, + 0x28, 0x00, 0x29, 0x00, 0x2a, 0x00, 0x2b, 0x00, + 0x2c, 0x00, 0x2d, 0x00, 0x2e, 0x00, 0x2f, 0x00, + 0x30, 0x00, 0x31, 0x00, 0x32, 0x00, 0x33, 0x00, + 0x34, 0x00, 0x35, 0x00, 0x36, 0x00, 0x37, 0x00, + 0x38, 0x00, 0x39, 0x00, 0x3a, 0x00, 0x3b, 0x00, + 0x3c, 0x00, 0x3d, 0x00, 0x3e, 0x00, 0x3f, 0x00, + 0x40, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, + 0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00, + 0x48, 0x00, 0x49, 0x00, 0x4a, 0x00, 0x4b, 0x00, + 0x4c, 0x00, 0x4d, 0x00, 0x4e, 0x00, 0x4f, 0x00, + 0x50, 0x00, 0x51, 0x00, 0x52, 0x00, 0x53, 0x00, + 0x54, 0x00, 0x55, 0x00, 0x56, 0x00, 0x57, 0x00, + 0x58, 0x00, 0x59, 0x00, 0x5a, 0x00, 0x5b, 0x00, + 0x5c, 0x00, 0x5d, 0x00, 0x5e, 0x00, 0x5f, 0x00, + 0x60, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00, + 0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00, + 0x48, 0x00, 0x49, 0x00, 0x4a, 0x00, 0x4b, 0x00, + 0x4c, 0x00, 0x4d, 0x00, 0x4e, 0x00, 0x4f, 0x00, + 0x50, 0x00, 0x51, 0x00, 0x52, 0x00, 0x53, 0x00, + 0x54, 0x00, 0x55, 0x00, 0x56, 0x00, 0x57, 0x00, + 0x58, 0x00, 0x59, 0x00, 0x5a, 0x00, 0x7b, 0x00, + 0x7c, 0x00, 0x7d, 0x00, 0x7e, 0x00, 0x7f, 0x00, + 0x80, 0x00, 0x81, 0x00, 0x82, 0x00, 0x83, 0x00, + 0x84, 0x00, 0x85, 0x00, 0x86, 0x00, 0x87, 0x00, + 0x88, 0x00, 0x89, 0x00, 0x8a, 0x00, 0x8b, 0x00, + 0x8c, 0x00, 0x8d, 0x00, 0x8e, 0x00, 0x8f, 0x00, + 0x90, 0x00, 0x91, 0x00, 0x92, 0x00, 0x93, 0x00, + 0x94, 0x00, 0x95, 0x00, 0x96, 0x00, 0x97, 0x00, + 0x98, 0x00, 0x99, 0x00, 0x9a, 0x00, 0x9b, 0x00, + 0x9c, 0x00, 0x9d, 0x00, 0x9e, 0x00, 0x9f, 0x00, + 0xa0, 0x00, 0xa1, 0x00, 0xa2, 0x00, 0xa3, 0x00, + 0xa4, 0x00, 0xa5, 0x00, 0xa6, 0x00, 0xa7, 0x00, + 0xa8, 0x00, 0xa9, 0x00, 0xaa, 0x00, 0xab, 0x00, + 0xac, 0x00, 0xad, 0x00, 0xae, 0x00, 0xaf, 0x00, + 0xb0, 0x00, 0xb1, 0x00, 0xb2, 0x00, 0xb3, 0x00, + 0xb4, 0x00, 0xb5, 0x00, 0xb6, 0x00, 0xb7, 0x00, + 0xb8, 0x00, 0xb9, 0x00, 0xba, 0x00, 0xbb, 0x00, + 0xbc, 0x00, 0xbd, 0x00, 0xbe, 0x00, 0xbf, 0x00, + 0xc0, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3, 0x00, + 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, + 0xc8, 0x00, 0xc9, 0x00, 0xca, 0x00, 0xcb, 0x00, + 0xcc, 0x00, 0xcd, 0x00, 0xce, 0x00, 0xcf, 0x00, + 0xd0, 0x00, 0xd1, 0x00, 0xd2, 0x00, 0xd3, 0x00, + 0xd4, 0x00, 0xd5, 0x00, 0xd6, 0x00, 0xd7, 0x00, + 0xd8, 0x00, 0xd9, 0x00, 0xda, 0x00, 0xdb, 0x00, + 0xdc, 0x00, 0xdd, 0x00, 0xde, 0x00, 0xdf, 0x00, + 0xc0, 0x00, 0xc1, 0x00, 0xc2, 0x00, 0xc3, 0x00, + 0xc4, 0x00, 0xc5, 0x00, 0xc6, 0x00, 0xc7, 0x00, + 0xc8, 0x00, 0xc9, 0x00, 0xca, 0x00, 0xcb, 0x00, + 0xcc, 0x00, 0xcd, 0x00, 0xce, 0x00, 0xcf, 0x00, + 0xd0, 0x00, 0xd1, 0x00, 0xd2, 0x00, 0xd3, 0x00, + 0xd4, 0x00, 0xd5, 0x00, 0xd6, 0x00, 0xf7, 0x00, + 0xd8, 0x00, 0xd9, 0x00, 0xda, 0x00, 0xdb, 0x00, + 0xdc, 0x00, 0xdd, 0x00, 0xde, 0x00, 0x78, 0x01, + 0x00, 0x01, 0x00, 0x01, 0x02, 0x01, 0x02, 0x01, + 0x04, 0x01, 0x04, 0x01, 0x06, 0x01, 0x06, 0x01, + 0x08, 0x01, 0x08, 0x01, 0x0a, 0x01, 0x0a, 0x01, + 0x0c, 0x01, 0x0c, 0x01, 0x0e, 0x01, 0x0e, 0x01, + 0x10, 0x01, 0x10, 0x01, 0x12, 0x01, 0x12, 0x01, + 0x14, 0x01, 0x14, 0x01, 0x16, 0x01, 0x16, 0x01, + 0x18, 0x01, 0x18, 0x01, 0x1a, 0x01, 0x1a, 0x01, + 0x1c, 0x01, 0x1c, 0x01, 0x1e, 0x01, 0x1e, 0x01, + 0x20, 0x01, 0x20, 0x01, 0x22, 0x01, 0x22, 0x01, + 0x24, 0x01, 0x24, 0x01, 0x26, 0x01, 0x26, 0x01, + 0x28, 0x01, 0x28, 0x01, 0x2a, 0x01, 0x2a, 0x01, + 0x2c, 0x01, 0x2c, 0x01, 0x2e, 0x01, 0x2e, 0x01, + 0x30, 0x01, 0x31, 0x01, 0x32, 0x01, 0x32, 0x01, + 0x34, 0x01, 0x34, 0x01, 0x36, 0x01, 0x36, 0x01, + 0x38, 0x01, 0x39, 0x01, 0x39, 0x01, 0x3b, 0x01, + 0x3b, 0x01, 0x3d, 0x01, 0x3d, 0x01, 0x3f, 0x01, + 0x3f, 0x01, 0x41, 0x01, 0x41, 0x01, 0x43, 0x01, + 0x43, 0x01, 0x45, 0x01, 0x45, 0x01, 0x47, 0x01, + 0x47, 0x01, 0x49, 0x01, 0x4a, 0x01, 0x4a, 0x01, + 0x4c, 0x01, 0x4c, 0x01, 0x4e, 0x01, 0x4e, 0x01, + 0x50, 0x01, 0x50, 0x01, 0x52, 0x01, 0x52, 0x01, + 0x54, 0x01, 0x54, 0x01, 0x56, 0x01, 0x56, 0x01, + 0x58, 0x01, 0x58, 0x01, 0x5a, 0x01, 0x5a, 0x01, + 0x5c, 0x01, 0x5c, 0x01, 0x5e, 0x01, 0x5e, 0x01, + 0x60, 0x01, 0x60, 0x01, 0x62, 0x01, 0x62, 0x01, + 0x64, 0x01, 0x64, 0x01, 0x66, 0x01, 0x66, 0x01, + 0x68, 0x01, 0x68, 0x01, 0x6a, 0x01, 0x6a, 0x01, + 0x6c, 0x01, 0x6c, 0x01, 0x6e, 0x01, 0x6e, 0x01, + 0x70, 0x01, 0x70, 0x01, 0x72, 0x01, 0x72, 0x01, + 0x74, 0x01, 0x74, 0x01, 0x76, 0x01, 0x76, 0x01, + 0x78, 0x01, 0x79, 0x01, 0x79, 0x01, 0x7b, 0x01, + 0x7b, 0x01, 0x7d, 0x01, 0x7d, 0x01, 0x7f, 0x01, + 0x43, 0x02, 0x81, 0x01, 0x82, 0x01, 0x82, 0x01, + 0x84, 0x01, 0x84, 0x01, 0x86, 0x01, 0x87, 0x01, + 0x87, 0x01, 0x89, 0x01, 0x8a, 0x01, 0x8b, 0x01, + 0x8b, 0x01, 0x8d, 0x01, 0x8e, 0x01, 0x8f, 0x01, + 0x90, 0x01, 0x91, 0x01, 0x91, 0x01, 0x93, 0x01, + 0x94, 0x01, 0xf6, 0x01, 0x96, 0x01, 0x97, 0x01, + 0x98, 0x01, 0x98, 0x01, 0x3d, 0x02, 0x9b, 0x01, + 0x9c, 0x01, 0x9d, 0x01, 0x20, 0x02, 0x9f, 0x01, + 0xa0, 0x01, 0xa0, 0x01, 0xa2, 0x01, 0xa2, 0x01, + 0xa4, 0x01, 0xa4, 0x01, 0xa6, 0x01, 0xa7, 0x01, + 0xa7, 0x01, 0xa9, 0x01, 0xaa, 0x01, 0xab, 0x01, + 0xac, 0x01, 0xac, 0x01, 0xae, 0x01, 0xaf, 0x01, + 0xaf, 0x01, 0xb1, 0x01, 0xb2, 0x01, 0xb3, 0x01, + 0xb3, 0x01, 0xb5, 0x01, 0xb5, 0x01, 0xb7, 0x01, + 0xb8, 0x01, 0xb8, 0x01, 0xba, 0x01, 0xbb, 0x01, + 0xbc, 0x01, 0xbc, 0x01, 0xbe, 0x01, 0xf7, 0x01, + 0xc0, 0x01, 0xc1, 0x01, 0xc2, 0x01, 0xc3, 0x01, + 0xc4, 0x01, 0xc5, 0x01, 0xc4, 0x01, 0xc7, 0x01, + 0xc8, 0x01, 0xc7, 0x01, 0xca, 0x01, 0xcb, 0x01, + 0xca, 0x01, 0xcd, 0x01, 0xcd, 0x01, 0xcf, 0x01, + 0xcf, 0x01, 0xd1, 0x01, 0xd1, 0x01, 0xd3, 0x01, + 0xd3, 0x01, 0xd5, 0x01, 0xd5, 0x01, 0xd7, 0x01, + 0xd7, 0x01, 0xd9, 0x01, 0xd9, 0x01, 0xdb, 0x01, + 0xdb, 0x01, 0x8e, 0x01, 0xde, 0x01, 0xde, 0x01, + 0xe0, 0x01, 0xe0, 0x01, 0xe2, 0x01, 0xe2, 0x01, + 0xe4, 0x01, 0xe4, 0x01, 0xe6, 0x01, 0xe6, 0x01, + 0xe8, 0x01, 0xe8, 0x01, 0xea, 0x01, 0xea, 0x01, + 0xec, 0x01, 0xec, 0x01, 0xee, 0x01, 0xee, 0x01, + 0xf0, 0x01, 0xf1, 0x01, 0xf2, 0x01, 0xf1, 0x01, + 0xf4, 0x01, 0xf4, 0x01, 0xf6, 0x01, 0xf7, 0x01, + 0xf8, 0x01, 0xf8, 0x01, 0xfa, 0x01, 0xfa, 0x01, + 0xfc, 0x01, 0xfc, 0x01, 0xfe, 0x01, 0xfe, 0x01, + 0x00, 0x02, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, + 0x04, 0x02, 0x04, 0x02, 0x06, 0x02, 0x06, 0x02, + 0x08, 0x02, 0x08, 0x02, 0x0a, 0x02, 0x0a, 0x02, + 0x0c, 0x02, 0x0c, 0x02, 0x0e, 0x02, 0x0e, 0x02, + 0x10, 0x02, 0x10, 0x02, 0x12, 0x02, 0x12, 0x02, + 0x14, 0x02, 0x14, 0x02, 0x16, 0x02, 0x16, 0x02, + 0x18, 0x02, 0x18, 0x02, 0x1a, 0x02, 0x1a, 0x02, + 0x1c, 0x02, 0x1c, 0x02, 0x1e, 0x02, 0x1e, 0x02, + 0x20, 0x02, 0x21, 0x02, 0x22, 0x02, 0x22, 0x02, + 0x24, 0x02, 0x24, 0x02, 0x26, 0x02, 0x26, 0x02, + 0x28, 0x02, 0x28, 0x02, 0x2a, 0x02, 0x2a, 0x02, + 0x2c, 0x02, 0x2c, 0x02, 0x2e, 0x02, 0x2e, 0x02, + 0x30, 0x02, 0x30, 0x02, 0x32, 0x02, 0x32, 0x02, + 0x34, 0x02, 0x35, 0x02, 0x36, 0x02, 0x37, 0x02, + 0x38, 0x02, 0x39, 0x02, 0x65, 0x2c, 0x3b, 0x02, + 0x3b, 0x02, 0x3d, 0x02, 0x66, 0x2c, 0x3f, 0x02, + 0x40, 0x02, 0x41, 0x02, 0x41, 0x02, 0x43, 0x02, + 0x44, 0x02, 0x45, 0x02, 0x46, 0x02, 0x46, 0x02, + 0x48, 0x02, 0x48, 0x02, 0x4a, 0x02, 0x4a, 0x02, + 0x4c, 0x02, 0x4c, 0x02, 0x4e, 0x02, 0x4e, 0x02, + 0x50, 0x02, 0x51, 0x02, 0x52, 0x02, 0x81, 0x01, + 0x86, 0x01, 0x55, 0x02, 0x89, 0x01, 0x8a, 0x01, + 0x58, 0x02, 0x8f, 0x01, 0x5a, 0x02, 0x90, 0x01, + 0x5c, 0x02, 0x5d, 0x02, 0x5e, 0x02, 0x5f, 0x02, + 0x93, 0x01, 0x61, 0x02, 0x62, 0x02, 0x94, 0x01, + 0x64, 0x02, 0x65, 0x02, 0x66, 0x02, 0x67, 0x02, + 0x97, 0x01, 0x96, 0x01, 0x6a, 0x02, 0x62, 0x2c, + 0x6c, 0x02, 0x6d, 0x02, 0x6e, 0x02, 0x9c, 0x01, + 0x70, 0x02, 0x71, 0x02, 0x9d, 0x01, 0x73, 0x02, + 0x74, 0x02, 0x9f, 0x01, 0x76, 0x02, 0x77, 0x02, + 0x78, 0x02, 0x79, 0x02, 0x7a, 0x02, 0x7b, 0x02, + 0x7c, 0x02, 0x64, 0x2c, 0x7e, 0x02, 0x7f, 0x02, + 0xa6, 0x01, 0x81, 0x02, 0x82, 0x02, 0xa9, 0x01, + 0x84, 0x02, 0x85, 0x02, 0x86, 0x02, 0x87, 0x02, + 0xae, 0x01, 0x44, 0x02, 0xb1, 0x01, 0xb2, 0x01, + 0x45, 0x02, 0x8d, 0x02, 0x8e, 0x02, 0x8f, 0x02, + 0x90, 0x02, 0x91, 0x02, 0xb7, 0x01, 0x93, 0x02, + 0x94, 0x02, 0x95, 0x02, 0x96, 0x02, 0x97, 0x02, + 0x98, 0x02, 0x99, 0x02, 0x9a, 0x02, 0x9b, 0x02, + 0x9c, 0x02, 0x9d, 0x02, 0x9e, 0x02, 0x9f, 0x02, + 0xa0, 0x02, 0xa1, 0x02, 0xa2, 0x02, 0xa3, 0x02, + 0xa4, 0x02, 0xa5, 0x02, 0xa6, 0x02, 0xa7, 0x02, + 0xa8, 0x02, 0xa9, 0x02, 0xaa, 0x02, 0xab, 0x02, + 0xac, 0x02, 0xad, 0x02, 0xae, 0x02, 0xaf, 0x02, + 0xb0, 0x02, 0xb1, 0x02, 0xb2, 0x02, 0xb3, 0x02, + 0xb4, 0x02, 0xb5, 0x02, 0xb6, 0x02, 0xb7, 0x02, + 0xb8, 0x02, 0xb9, 0x02, 0xba, 0x02, 0xbb, 0x02, + 0xbc, 0x02, 0xbd, 0x02, 0xbe, 0x02, 0xbf, 0x02, + 0xc0, 0x02, 0xc1, 0x02, 0xc2, 0x02, 0xc3, 0x02, + 0xc4, 0x02, 0xc5, 0x02, 0xc6, 0x02, 0xc7, 0x02, + 0xc8, 0x02, 0xc9, 0x02, 0xca, 0x02, 0xcb, 0x02, + 0xcc, 0x02, 0xcd, 0x02, 0xce, 0x02, 0xcf, 0x02, + 0xd0, 0x02, 0xd1, 0x02, 0xd2, 0x02, 0xd3, 0x02, + 0xd4, 0x02, 0xd5, 0x02, 0xd6, 0x02, 0xd7, 0x02, + 0xd8, 0x02, 0xd9, 0x02, 0xda, 0x02, 0xdb, 0x02, + 0xdc, 0x02, 0xdd, 0x02, 0xde, 0x02, 0xdf, 0x02, + 0xe0, 0x02, 0xe1, 0x02, 0xe2, 0x02, 0xe3, 0x02, + 0xe4, 0x02, 0xe5, 0x02, 0xe6, 0x02, 0xe7, 0x02, + 0xe8, 0x02, 0xe9, 0x02, 0xea, 0x02, 0xeb, 0x02, + 0xec, 0x02, 0xed, 0x02, 0xee, 0x02, 0xef, 0x02, + 0xf0, 0x02, 0xf1, 0x02, 0xf2, 0x02, 0xf3, 0x02, + 0xf4, 0x02, 0xf5, 0x02, 0xf6, 0x02, 0xf7, 0x02, + 0xf8, 0x02, 0xf9, 0x02, 0xfa, 0x02, 0xfb, 0x02, + 0xfc, 0x02, 0xfd, 0x02, 0xfe, 0x02, 0xff, 0x02, + 0x00, 0x03, 0x01, 0x03, 0x02, 0x03, 0x03, 0x03, + 0x04, 0x03, 0x05, 0x03, 0x06, 0x03, 0x07, 0x03, + 0x08, 0x03, 0x09, 0x03, 0x0a, 0x03, 0x0b, 0x03, + 0x0c, 0x03, 0x0d, 0x03, 0x0e, 0x03, 0x0f, 0x03, + 0x10, 0x03, 0x11, 0x03, 0x12, 0x03, 0x13, 0x03, + 0x14, 0x03, 0x15, 0x03, 0x16, 0x03, 0x17, 0x03, + 0x18, 0x03, 0x19, 0x03, 0x1a, 0x03, 0x1b, 0x03, + 0x1c, 0x03, 0x1d, 0x03, 0x1e, 0x03, 0x1f, 0x03, + 0x20, 0x03, 0x21, 0x03, 0x22, 0x03, 0x23, 0x03, + 0x24, 0x03, 0x25, 0x03, 0x26, 0x03, 0x27, 0x03, + 0x28, 0x03, 0x29, 0x03, 0x2a, 0x03, 0x2b, 0x03, + 0x2c, 0x03, 0x2d, 0x03, 0x2e, 0x03, 0x2f, 0x03, + 0x30, 0x03, 0x31, 0x03, 0x32, 0x03, 0x33, 0x03, + 0x34, 0x03, 0x35, 0x03, 0x36, 0x03, 0x37, 0x03, + 0x38, 0x03, 0x39, 0x03, 0x3a, 0x03, 0x3b, 0x03, + 0x3c, 0x03, 0x3d, 0x03, 0x3e, 0x03, 0x3f, 0x03, + 0x40, 0x03, 0x41, 0x03, 0x42, 0x03, 0x43, 0x03, + 0x44, 0x03, 0x45, 0x03, 0x46, 0x03, 0x47, 0x03, + 0x48, 0x03, 0x49, 0x03, 0x4a, 0x03, 0x4b, 0x03, + 0x4c, 0x03, 0x4d, 0x03, 0x4e, 0x03, 0x4f, 0x03, + 0x50, 0x03, 0x51, 0x03, 0x52, 0x03, 0x53, 0x03, + 0x54, 0x03, 0x55, 0x03, 0x56, 0x03, 0x57, 0x03, + 0x58, 0x03, 0x59, 0x03, 0x5a, 0x03, 0x5b, 0x03, + 0x5c, 0x03, 0x5d, 0x03, 0x5e, 0x03, 0x5f, 0x03, + 0x60, 0x03, 0x61, 0x03, 0x62, 0x03, 0x63, 0x03, + 0x64, 0x03, 0x65, 0x03, 0x66, 0x03, 0x67, 0x03, + 0x68, 0x03, 0x69, 0x03, 0x6a, 0x03, 0x6b, 0x03, + 0x6c, 0x03, 0x6d, 0x03, 0x6e, 0x03, 0x6f, 0x03, + 0x70, 0x03, 0x71, 0x03, 0x72, 0x03, 0x73, 0x03, + 0x74, 0x03, 0x75, 0x03, 0x76, 0x03, 0x77, 0x03, + 0x78, 0x03, 0x79, 0x03, 0x7a, 0x03, 0xfd, 0x03, + 0xfe, 0x03, 0xff, 0x03, 0x7e, 0x03, 0x7f, 0x03, + 0x80, 0x03, 0x81, 0x03, 0x82, 0x03, 0x83, 0x03, + 0x84, 0x03, 0x85, 0x03, 0x86, 0x03, 0x87, 0x03, + 0x88, 0x03, 0x89, 0x03, 0x8a, 0x03, 0x8b, 0x03, + 0x8c, 0x03, 0x8d, 0x03, 0x8e, 0x03, 0x8f, 0x03, + 0x90, 0x03, 0x91, 0x03, 0x92, 0x03, 0x93, 0x03, + 0x94, 0x03, 0x95, 0x03, 0x96, 0x03, 0x97, 0x03, + 0x98, 0x03, 0x99, 0x03, 0x9a, 0x03, 0x9b, 0x03, + 0x9c, 0x03, 0x9d, 0x03, 0x9e, 0x03, 0x9f, 0x03, + 0xa0, 0x03, 0xa1, 0x03, 0xa2, 0x03, 0xa3, 0x03, + 0xa4, 0x03, 0xa5, 0x03, 0xa6, 0x03, 0xa7, 0x03, + 0xa8, 0x03, 0xa9, 0x03, 0xaa, 0x03, 0xab, 0x03, + 0x86, 0x03, 0x88, 0x03, 0x89, 0x03, 0x8a, 0x03, + 0xb0, 0x03, 0x91, 0x03, 0x92, 0x03, 0x93, 0x03, + 0x94, 0x03, 0x95, 0x03, 0x96, 0x03, 0x97, 0x03, + 0x98, 0x03, 0x99, 0x03, 0x9a, 0x03, 0x9b, 0x03, + 0x9c, 0x03, 0x9d, 0x03, 0x9e, 0x03, 0x9f, 0x03, + 0xa0, 0x03, 0xa1, 0x03, 0xa3, 0x03, 0xa3, 0x03, + 0xa4, 0x03, 0xa5, 0x03, 0xa6, 0x03, 0xa7, 0x03, + 0xa8, 0x03, 0xa9, 0x03, 0xaa, 0x03, 0xab, 0x03, + 0x8c, 0x03, 0x8e, 0x03, 0x8f, 0x03, 0xcf, 0x03, + 0xd0, 0x03, 0xd1, 0x03, 0xd2, 0x03, 0xd3, 0x03, + 0xd4, 0x03, 0xd5, 0x03, 0xd6, 0x03, 0xd7, 0x03, + 0xd8, 0x03, 0xd8, 0x03, 0xda, 0x03, 0xda, 0x03, + 0xdc, 0x03, 0xdc, 0x03, 0xde, 0x03, 0xde, 0x03, + 0xe0, 0x03, 0xe0, 0x03, 0xe2, 0x03, 0xe2, 0x03, + 0xe4, 0x03, 0xe4, 0x03, 0xe6, 0x03, 0xe6, 0x03, + 0xe8, 0x03, 0xe8, 0x03, 0xea, 0x03, 0xea, 0x03, + 0xec, 0x03, 0xec, 0x03, 0xee, 0x03, 0xee, 0x03, + 0xf0, 0x03, 0xf1, 0x03, 0xf9, 0x03, 0xf3, 0x03, + 0xf4, 0x03, 0xf5, 0x03, 0xf6, 0x03, 0xf7, 0x03, + 0xf7, 0x03, 0xf9, 0x03, 0xfa, 0x03, 0xfa, 0x03, + 0xfc, 0x03, 0xfd, 0x03, 0xfe, 0x03, 0xff, 0x03, + 0x00, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03, 0x04, + 0x04, 0x04, 0x05, 0x04, 0x06, 0x04, 0x07, 0x04, + 0x08, 0x04, 0x09, 0x04, 0x0a, 0x04, 0x0b, 0x04, + 0x0c, 0x04, 0x0d, 0x04, 0x0e, 0x04, 0x0f, 0x04, + 0x10, 0x04, 0x11, 0x04, 0x12, 0x04, 0x13, 0x04, + 0x14, 0x04, 0x15, 0x04, 0x16, 0x04, 0x17, 0x04, + 0x18, 0x04, 0x19, 0x04, 0x1a, 0x04, 0x1b, 0x04, + 0x1c, 0x04, 0x1d, 0x04, 0x1e, 0x04, 0x1f, 0x04, + 0x20, 0x04, 0x21, 0x04, 0x22, 0x04, 0x23, 0x04, + 0x24, 0x04, 0x25, 0x04, 0x26, 0x04, 0x27, 0x04, + 0x28, 0x04, 0x29, 0x04, 0x2a, 0x04, 0x2b, 0x04, + 0x2c, 0x04, 0x2d, 0x04, 0x2e, 0x04, 0x2f, 0x04, + 0x10, 0x04, 0x11, 0x04, 0x12, 0x04, 0x13, 0x04, + 0x14, 0x04, 0x15, 0x04, 0x16, 0x04, 0x17, 0x04, + 0x18, 0x04, 0x19, 0x04, 0x1a, 0x04, 0x1b, 0x04, + 0x1c, 0x04, 0x1d, 0x04, 0x1e, 0x04, 0x1f, 0x04, + 0x20, 0x04, 0x21, 0x04, 0x22, 0x04, 0x23, 0x04, + 0x24, 0x04, 0x25, 0x04, 0x26, 0x04, 0x27, 0x04, + 0x28, 0x04, 0x29, 0x04, 0x2a, 0x04, 0x2b, 0x04, + 0x2c, 0x04, 0x2d, 0x04, 0x2e, 0x04, 0x2f, 0x04, + 0x00, 0x04, 0x01, 0x04, 0x02, 0x04, 0x03, 0x04, + 0x04, 0x04, 0x05, 0x04, 0x06, 0x04, 0x07, 0x04, + 0x08, 0x04, 0x09, 0x04, 0x0a, 0x04, 0x0b, 0x04, + 0x0c, 0x04, 0x0d, 0x04, 0x0e, 0x04, 0x0f, 0x04, + 0x60, 0x04, 0x60, 0x04, 0x62, 0x04, 0x62, 0x04, + 0x64, 0x04, 0x64, 0x04, 0x66, 0x04, 0x66, 0x04, + 0x68, 0x04, 0x68, 0x04, 0x6a, 0x04, 0x6a, 0x04, + 0x6c, 0x04, 0x6c, 0x04, 0x6e, 0x04, 0x6e, 0x04, + 0x70, 0x04, 0x70, 0x04, 0x72, 0x04, 0x72, 0x04, + 0x74, 0x04, 0x74, 0x04, 0x76, 0x04, 0x76, 0x04, + 0x78, 0x04, 0x78, 0x04, 0x7a, 0x04, 0x7a, 0x04, + 0x7c, 0x04, 0x7c, 0x04, 0x7e, 0x04, 0x7e, 0x04, + 0x80, 0x04, 0x80, 0x04, 0x82, 0x04, 0x83, 0x04, + 0x84, 0x04, 0x85, 0x04, 0x86, 0x04, 0x87, 0x04, + 0x88, 0x04, 0x89, 0x04, 0x8a, 0x04, 0x8a, 0x04, + 0x8c, 0x04, 0x8c, 0x04, 0x8e, 0x04, 0x8e, 0x04, + 0x90, 0x04, 0x90, 0x04, 0x92, 0x04, 0x92, 0x04, + 0x94, 0x04, 0x94, 0x04, 0x96, 0x04, 0x96, 0x04, + 0x98, 0x04, 0x98, 0x04, 0x9a, 0x04, 0x9a, 0x04, + 0x9c, 0x04, 0x9c, 0x04, 0x9e, 0x04, 0x9e, 0x04, + 0xa0, 0x04, 0xa0, 0x04, 0xa2, 0x04, 0xa2, 0x04, + 0xa4, 0x04, 0xa4, 0x04, 0xa6, 0x04, 0xa6, 0x04, + 0xa8, 0x04, 0xa8, 0x04, 0xaa, 0x04, 0xaa, 0x04, + 0xac, 0x04, 0xac, 0x04, 0xae, 0x04, 0xae, 0x04, + 0xb0, 0x04, 0xb0, 0x04, 0xb2, 0x04, 0xb2, 0x04, + 0xb4, 0x04, 0xb4, 0x04, 0xb6, 0x04, 0xb6, 0x04, + 0xb8, 0x04, 0xb8, 0x04, 0xba, 0x04, 0xba, 0x04, + 0xbc, 0x04, 0xbc, 0x04, 0xbe, 0x04, 0xbe, 0x04, + 0xc0, 0x04, 0xc1, 0x04, 0xc1, 0x04, 0xc3, 0x04, + 0xc3, 0x04, 0xc5, 0x04, 0xc5, 0x04, 0xc7, 0x04, + 0xc7, 0x04, 0xc9, 0x04, 0xc9, 0x04, 0xcb, 0x04, + 0xcb, 0x04, 0xcd, 0x04, 0xcd, 0x04, 0xc0, 0x04, + 0xd0, 0x04, 0xd0, 0x04, 0xd2, 0x04, 0xd2, 0x04, + 0xd4, 0x04, 0xd4, 0x04, 0xd6, 0x04, 0xd6, 0x04, + 0xd8, 0x04, 0xd8, 0x04, 0xda, 0x04, 0xda, 0x04, + 0xdc, 0x04, 0xdc, 0x04, 0xde, 0x04, 0xde, 0x04, + 0xe0, 0x04, 0xe0, 0x04, 0xe2, 0x04, 0xe2, 0x04, + 0xe4, 0x04, 0xe4, 0x04, 0xe6, 0x04, 0xe6, 0x04, + 0xe8, 0x04, 0xe8, 0x04, 0xea, 0x04, 0xea, 0x04, + 0xec, 0x04, 0xec, 0x04, 0xee, 0x04, 0xee, 0x04, + 0xf0, 0x04, 0xf0, 0x04, 0xf2, 0x04, 0xf2, 0x04, + 0xf4, 0x04, 0xf4, 0x04, 0xf6, 0x04, 0xf6, 0x04, + 0xf8, 0x04, 0xf8, 0x04, 0xfa, 0x04, 0xfa, 0x04, + 0xfc, 0x04, 0xfc, 0x04, 0xfe, 0x04, 0xfe, 0x04, + 0x00, 0x05, 0x00, 0x05, 0x02, 0x05, 0x02, 0x05, + 0x04, 0x05, 0x04, 0x05, 0x06, 0x05, 0x06, 0x05, + 0x08, 0x05, 0x08, 0x05, 0x0a, 0x05, 0x0a, 0x05, + 0x0c, 0x05, 0x0c, 0x05, 0x0e, 0x05, 0x0e, 0x05, + 0x10, 0x05, 0x10, 0x05, 0x12, 0x05, 0x12, 0x05, + 0x14, 0x05, 0x15, 0x05, 0x16, 0x05, 0x17, 0x05, + 0x18, 0x05, 0x19, 0x05, 0x1a, 0x05, 0x1b, 0x05, + 0x1c, 0x05, 0x1d, 0x05, 0x1e, 0x05, 0x1f, 0x05, + 0x20, 0x05, 0x21, 0x05, 0x22, 0x05, 0x23, 0x05, + 0x24, 0x05, 0x25, 0x05, 0x26, 0x05, 0x27, 0x05, + 0x28, 0x05, 0x29, 0x05, 0x2a, 0x05, 0x2b, 0x05, + 0x2c, 0x05, 0x2d, 0x05, 0x2e, 0x05, 0x2f, 0x05, + 0x30, 0x05, 0x31, 0x05, 0x32, 0x05, 0x33, 0x05, + 0x34, 0x05, 0x35, 0x05, 0x36, 0x05, 0x37, 0x05, + 0x38, 0x05, 0x39, 0x05, 0x3a, 0x05, 0x3b, 0x05, + 0x3c, 0x05, 0x3d, 0x05, 0x3e, 0x05, 0x3f, 0x05, + 0x40, 0x05, 0x41, 0x05, 0x42, 0x05, 0x43, 0x05, + 0x44, 0x05, 0x45, 0x05, 0x46, 0x05, 0x47, 0x05, + 0x48, 0x05, 0x49, 0x05, 0x4a, 0x05, 0x4b, 0x05, + 0x4c, 0x05, 0x4d, 0x05, 0x4e, 0x05, 0x4f, 0x05, + 0x50, 0x05, 0x51, 0x05, 0x52, 0x05, 0x53, 0x05, + 0x54, 0x05, 0x55, 0x05, 0x56, 0x05, 0x57, 0x05, + 0x58, 0x05, 0x59, 0x05, 0x5a, 0x05, 0x5b, 0x05, + 0x5c, 0x05, 0x5d, 0x05, 0x5e, 0x05, 0x5f, 0x05, + 0x60, 0x05, 0x31, 0x05, 0x32, 0x05, 0x33, 0x05, + 0x34, 0x05, 0x35, 0x05, 0x36, 0x05, 0x37, 0x05, + 0x38, 0x05, 0x39, 0x05, 0x3a, 0x05, 0x3b, 0x05, + 0x3c, 0x05, 0x3d, 0x05, 0x3e, 0x05, 0x3f, 0x05, + 0x40, 0x05, 0x41, 0x05, 0x42, 0x05, 0x43, 0x05, + 0x44, 0x05, 0x45, 0x05, 0x46, 0x05, 0x47, 0x05, + 0x48, 0x05, 0x49, 0x05, 0x4a, 0x05, 0x4b, 0x05, + 0x4c, 0x05, 0x4d, 0x05, 0x4e, 0x05, 0x4f, 0x05, + 0x50, 0x05, 0x51, 0x05, 0x52, 0x05, 0x53, 0x05, + 0x54, 0x05, 0x55, 0x05, 0x56, 0x05, 0xff, 0xff, + 0xf6, 0x17, 0x63, 0x2c, 0x7e, 0x1d, 0x7f, 0x1d, + 0x80, 0x1d, 0x81, 0x1d, 0x82, 0x1d, 0x83, 0x1d, + 0x84, 0x1d, 0x85, 0x1d, 0x86, 0x1d, 0x87, 0x1d, + 0x88, 0x1d, 0x89, 0x1d, 0x8a, 0x1d, 0x8b, 0x1d, + 0x8c, 0x1d, 0x8d, 0x1d, 0x8e, 0x1d, 0x8f, 0x1d, + 0x90, 0x1d, 0x91, 0x1d, 0x92, 0x1d, 0x93, 0x1d, + 0x94, 0x1d, 0x95, 0x1d, 0x96, 0x1d, 0x97, 0x1d, + 0x98, 0x1d, 0x99, 0x1d, 0x9a, 0x1d, 0x9b, 0x1d, + 0x9c, 0x1d, 0x9d, 0x1d, 0x9e, 0x1d, 0x9f, 0x1d, + 0xa0, 0x1d, 0xa1, 0x1d, 0xa2, 0x1d, 0xa3, 0x1d, + 0xa4, 0x1d, 0xa5, 0x1d, 0xa6, 0x1d, 0xa7, 0x1d, + 0xa8, 0x1d, 0xa9, 0x1d, 0xaa, 0x1d, 0xab, 0x1d, + 0xac, 0x1d, 0xad, 0x1d, 0xae, 0x1d, 0xaf, 0x1d, + 0xb0, 0x1d, 0xb1, 0x1d, 0xb2, 0x1d, 0xb3, 0x1d, + 0xb4, 0x1d, 0xb5, 0x1d, 0xb6, 0x1d, 0xb7, 0x1d, + 0xb8, 0x1d, 0xb9, 0x1d, 0xba, 0x1d, 0xbb, 0x1d, + 0xbc, 0x1d, 0xbd, 0x1d, 0xbe, 0x1d, 0xbf, 0x1d, + 0xc0, 0x1d, 0xc1, 0x1d, 0xc2, 0x1d, 0xc3, 0x1d, + 0xc4, 0x1d, 0xc5, 0x1d, 0xc6, 0x1d, 0xc7, 0x1d, + 0xc8, 0x1d, 0xc9, 0x1d, 0xca, 0x1d, 0xcb, 0x1d, + 0xcc, 0x1d, 0xcd, 0x1d, 0xce, 0x1d, 0xcf, 0x1d, + 0xd0, 0x1d, 0xd1, 0x1d, 0xd2, 0x1d, 0xd3, 0x1d, + 0xd4, 0x1d, 0xd5, 0x1d, 0xd6, 0x1d, 0xd7, 0x1d, + 0xd8, 0x1d, 0xd9, 0x1d, 0xda, 0x1d, 0xdb, 0x1d, + 0xdc, 0x1d, 0xdd, 0x1d, 0xde, 0x1d, 0xdf, 0x1d, + 0xe0, 0x1d, 0xe1, 0x1d, 0xe2, 0x1d, 0xe3, 0x1d, + 0xe4, 0x1d, 0xe5, 0x1d, 0xe6, 0x1d, 0xe7, 0x1d, + 0xe8, 0x1d, 0xe9, 0x1d, 0xea, 0x1d, 0xeb, 0x1d, + 0xec, 0x1d, 0xed, 0x1d, 0xee, 0x1d, 0xef, 0x1d, + 0xf0, 0x1d, 0xf1, 0x1d, 0xf2, 0x1d, 0xf3, 0x1d, + 0xf4, 0x1d, 0xf5, 0x1d, 0xf6, 0x1d, 0xf7, 0x1d, + 0xf8, 0x1d, 0xf9, 0x1d, 0xfa, 0x1d, 0xfb, 0x1d, + 0xfc, 0x1d, 0xfd, 0x1d, 0xfe, 0x1d, 0xff, 0x1d, + 0x00, 0x1e, 0x00, 0x1e, 0x02, 0x1e, 0x02, 0x1e, + 0x04, 0x1e, 0x04, 0x1e, 0x06, 0x1e, 0x06, 0x1e, + 0x08, 0x1e, 0x08, 0x1e, 0x0a, 0x1e, 0x0a, 0x1e, + 0x0c, 0x1e, 0x0c, 0x1e, 0x0e, 0x1e, 0x0e, 0x1e, + 0x10, 0x1e, 0x10, 0x1e, 0x12, 0x1e, 0x12, 0x1e, + 0x14, 0x1e, 0x14, 0x1e, 0x16, 0x1e, 0x16, 0x1e, + 0x18, 0x1e, 0x18, 0x1e, 0x1a, 0x1e, 0x1a, 0x1e, + 0x1c, 0x1e, 0x1c, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, + 0x20, 0x1e, 0x20, 0x1e, 0x22, 0x1e, 0x22, 0x1e, + 0x24, 0x1e, 0x24, 0x1e, 0x26, 0x1e, 0x26, 0x1e, + 0x28, 0x1e, 0x28, 0x1e, 0x2a, 0x1e, 0x2a, 0x1e, + 0x2c, 0x1e, 0x2c, 0x1e, 0x2e, 0x1e, 0x2e, 0x1e, + 0x30, 0x1e, 0x30, 0x1e, 0x32, 0x1e, 0x32, 0x1e, + 0x34, 0x1e, 0x34, 0x1e, 0x36, 0x1e, 0x36, 0x1e, + 0x38, 0x1e, 0x38, 0x1e, 0x3a, 0x1e, 0x3a, 0x1e, + 0x3c, 0x1e, 0x3c, 0x1e, 0x3e, 0x1e, 0x3e, 0x1e, + 0x40, 0x1e, 0x40, 0x1e, 0x42, 0x1e, 0x42, 0x1e, + 0x44, 0x1e, 0x44, 0x1e, 0x46, 0x1e, 0x46, 0x1e, + 0x48, 0x1e, 0x48, 0x1e, 0x4a, 0x1e, 0x4a, 0x1e, + 0x4c, 0x1e, 0x4c, 0x1e, 0x4e, 0x1e, 0x4e, 0x1e, + 0x50, 0x1e, 0x50, 0x1e, 0x52, 0x1e, 0x52, 0x1e, + 0x54, 0x1e, 0x54, 0x1e, 0x56, 0x1e, 0x56, 0x1e, + 0x58, 0x1e, 0x58, 0x1e, 0x5a, 0x1e, 0x5a, 0x1e, + 0x5c, 0x1e, 0x5c, 0x1e, 0x5e, 0x1e, 0x5e, 0x1e, + 0x60, 0x1e, 0x60, 0x1e, 0x62, 0x1e, 0x62, 0x1e, + 0x64, 0x1e, 0x64, 0x1e, 0x66, 0x1e, 0x66, 0x1e, + 0x68, 0x1e, 0x68, 0x1e, 0x6a, 0x1e, 0x6a, 0x1e, + 0x6c, 0x1e, 0x6c, 0x1e, 0x6e, 0x1e, 0x6e, 0x1e, + 0x70, 0x1e, 0x70, 0x1e, 0x72, 0x1e, 0x72, 0x1e, + 0x74, 0x1e, 0x74, 0x1e, 0x76, 0x1e, 0x76, 0x1e, + 0x78, 0x1e, 0x78, 0x1e, 0x7a, 0x1e, 0x7a, 0x1e, + 0x7c, 0x1e, 0x7c, 0x1e, 0x7e, 0x1e, 0x7e, 0x1e, + 0x80, 0x1e, 0x80, 0x1e, 0x82, 0x1e, 0x82, 0x1e, + 0x84, 0x1e, 0x84, 0x1e, 0x86, 0x1e, 0x86, 0x1e, + 0x88, 0x1e, 0x88, 0x1e, 0x8a, 0x1e, 0x8a, 0x1e, + 0x8c, 0x1e, 0x8c, 0x1e, 0x8e, 0x1e, 0x8e, 0x1e, + 0x90, 0x1e, 0x90, 0x1e, 0x92, 0x1e, 0x92, 0x1e, + 0x94, 0x1e, 0x94, 0x1e, 0x96, 0x1e, 0x97, 0x1e, + 0x98, 0x1e, 0x99, 0x1e, 0x9a, 0x1e, 0x9b, 0x1e, + 0x9c, 0x1e, 0x9d, 0x1e, 0x9e, 0x1e, 0x9f, 0x1e, + 0xa0, 0x1e, 0xa0, 0x1e, 0xa2, 0x1e, 0xa2, 0x1e, + 0xa4, 0x1e, 0xa4, 0x1e, 0xa6, 0x1e, 0xa6, 0x1e, + 0xa8, 0x1e, 0xa8, 0x1e, 0xaa, 0x1e, 0xaa, 0x1e, + 0xac, 0x1e, 0xac, 0x1e, 0xae, 0x1e, 0xae, 0x1e, + 0xb0, 0x1e, 0xb0, 0x1e, 0xb2, 0x1e, 0xb2, 0x1e, + 0xb4, 0x1e, 0xb4, 0x1e, 0xb6, 0x1e, 0xb6, 0x1e, + 0xb8, 0x1e, 0xb8, 0x1e, 0xba, 0x1e, 0xba, 0x1e, + 0xbc, 0x1e, 0xbc, 0x1e, 0xbe, 0x1e, 0xbe, 0x1e, + 0xc0, 0x1e, 0xc0, 0x1e, 0xc2, 0x1e, 0xc2, 0x1e, + 0xc4, 0x1e, 0xc4, 0x1e, 0xc6, 0x1e, 0xc6, 0x1e, + 0xc8, 0x1e, 0xc8, 0x1e, 0xca, 0x1e, 0xca, 0x1e, + 0xcc, 0x1e, 0xcc, 0x1e, 0xce, 0x1e, 0xce, 0x1e, + 0xd0, 0x1e, 0xd0, 0x1e, 0xd2, 0x1e, 0xd2, 0x1e, + 0xd4, 0x1e, 0xd4, 0x1e, 0xd6, 0x1e, 0xd6, 0x1e, + 0xd8, 0x1e, 0xd8, 0x1e, 0xda, 0x1e, 0xda, 0x1e, + 0xdc, 0x1e, 0xdc, 0x1e, 0xde, 0x1e, 0xde, 0x1e, + 0xe0, 0x1e, 0xe0, 0x1e, 0xe2, 0x1e, 0xe2, 0x1e, + 0xe4, 0x1e, 0xe4, 0x1e, 0xe6, 0x1e, 0xe6, 0x1e, + 0xe8, 0x1e, 0xe8, 0x1e, 0xea, 0x1e, 0xea, 0x1e, + 0xec, 0x1e, 0xec, 0x1e, 0xee, 0x1e, 0xee, 0x1e, + 0xf0, 0x1e, 0xf0, 0x1e, 0xf2, 0x1e, 0xf2, 0x1e, + 0xf4, 0x1e, 0xf4, 0x1e, 0xf6, 0x1e, 0xf6, 0x1e, + 0xf8, 0x1e, 0xf8, 0x1e, 0xfa, 0x1e, 0xfb, 0x1e, + 0xfc, 0x1e, 0xfd, 0x1e, 0xfe, 0x1e, 0xff, 0x1e, + 0x08, 0x1f, 0x09, 0x1f, 0x0a, 0x1f, 0x0b, 0x1f, + 0x0c, 0x1f, 0x0d, 0x1f, 0x0e, 0x1f, 0x0f, 0x1f, + 0x08, 0x1f, 0x09, 0x1f, 0x0a, 0x1f, 0x0b, 0x1f, + 0x0c, 0x1f, 0x0d, 0x1f, 0x0e, 0x1f, 0x0f, 0x1f, + 0x18, 0x1f, 0x19, 0x1f, 0x1a, 0x1f, 0x1b, 0x1f, + 0x1c, 0x1f, 0x1d, 0x1f, 0x16, 0x1f, 0x17, 0x1f, + 0x18, 0x1f, 0x19, 0x1f, 0x1a, 0x1f, 0x1b, 0x1f, + 0x1c, 0x1f, 0x1d, 0x1f, 0x1e, 0x1f, 0x1f, 0x1f, + 0x28, 0x1f, 0x29, 0x1f, 0x2a, 0x1f, 0x2b, 0x1f, + 0x2c, 0x1f, 0x2d, 0x1f, 0x2e, 0x1f, 0x2f, 0x1f, + 0x28, 0x1f, 0x29, 0x1f, 0x2a, 0x1f, 0x2b, 0x1f, + 0x2c, 0x1f, 0x2d, 0x1f, 0x2e, 0x1f, 0x2f, 0x1f, + 0x38, 0x1f, 0x39, 0x1f, 0x3a, 0x1f, 0x3b, 0x1f, + 0x3c, 0x1f, 0x3d, 0x1f, 0x3e, 0x1f, 0x3f, 0x1f, + 0x38, 0x1f, 0x39, 0x1f, 0x3a, 0x1f, 0x3b, 0x1f, + 0x3c, 0x1f, 0x3d, 0x1f, 0x3e, 0x1f, 0x3f, 0x1f, + 0x48, 0x1f, 0x49, 0x1f, 0x4a, 0x1f, 0x4b, 0x1f, + 0x4c, 0x1f, 0x4d, 0x1f, 0x46, 0x1f, 0x47, 0x1f, + 0x48, 0x1f, 0x49, 0x1f, 0x4a, 0x1f, 0x4b, 0x1f, + 0x4c, 0x1f, 0x4d, 0x1f, 0x4e, 0x1f, 0x4f, 0x1f, + 0x50, 0x1f, 0x59, 0x1f, 0x52, 0x1f, 0x5b, 0x1f, + 0x54, 0x1f, 0x5d, 0x1f, 0x56, 0x1f, 0x5f, 0x1f, + 0x58, 0x1f, 0x59, 0x1f, 0x5a, 0x1f, 0x5b, 0x1f, + 0x5c, 0x1f, 0x5d, 0x1f, 0x5e, 0x1f, 0x5f, 0x1f, + 0x68, 0x1f, 0x69, 0x1f, 0x6a, 0x1f, 0x6b, 0x1f, + 0x6c, 0x1f, 0x6d, 0x1f, 0x6e, 0x1f, 0x6f, 0x1f, + 0x68, 0x1f, 0x69, 0x1f, 0x6a, 0x1f, 0x6b, 0x1f, + 0x6c, 0x1f, 0x6d, 0x1f, 0x6e, 0x1f, 0x6f, 0x1f, + 0xba, 0x1f, 0xbb, 0x1f, 0xc8, 0x1f, 0xc9, 0x1f, + 0xca, 0x1f, 0xcb, 0x1f, 0xda, 0x1f, 0xdb, 0x1f, + 0xf8, 0x1f, 0xf9, 0x1f, 0xea, 0x1f, 0xeb, 0x1f, + 0xfa, 0x1f, 0xfb, 0x1f, 0x7e, 0x1f, 0x7f, 0x1f, + 0x88, 0x1f, 0x89, 0x1f, 0x8a, 0x1f, 0x8b, 0x1f, + 0x8c, 0x1f, 0x8d, 0x1f, 0x8e, 0x1f, 0x8f, 0x1f, + 0x88, 0x1f, 0x89, 0x1f, 0x8a, 0x1f, 0x8b, 0x1f, + 0x8c, 0x1f, 0x8d, 0x1f, 0x8e, 0x1f, 0x8f, 0x1f, + 0x98, 0x1f, 0x99, 0x1f, 0x9a, 0x1f, 0x9b, 0x1f, + 0x9c, 0x1f, 0x9d, 0x1f, 0x9e, 0x1f, 0x9f, 0x1f, + 0x98, 0x1f, 0x99, 0x1f, 0x9a, 0x1f, 0x9b, 0x1f, + 0x9c, 0x1f, 0x9d, 0x1f, 0x9e, 0x1f, 0x9f, 0x1f, + 0xa8, 0x1f, 0xa9, 0x1f, 0xaa, 0x1f, 0xab, 0x1f, + 0xac, 0x1f, 0xad, 0x1f, 0xae, 0x1f, 0xaf, 0x1f, + 0xa8, 0x1f, 0xa9, 0x1f, 0xaa, 0x1f, 0xab, 0x1f, + 0xac, 0x1f, 0xad, 0x1f, 0xae, 0x1f, 0xaf, 0x1f, + 0xb8, 0x1f, 0xb9, 0x1f, 0xb2, 0x1f, 0xbc, 0x1f, + 0xb4, 0x1f, 0xb5, 0x1f, 0xb6, 0x1f, 0xb7, 0x1f, + 0xb8, 0x1f, 0xb9, 0x1f, 0xba, 0x1f, 0xbb, 0x1f, + 0xbc, 0x1f, 0xbd, 0x1f, 0xbe, 0x1f, 0xbf, 0x1f, + 0xc0, 0x1f, 0xc1, 0x1f, 0xc2, 0x1f, 0xc3, 0x1f, + 0xc4, 0x1f, 0xc5, 0x1f, 0xc6, 0x1f, 0xc7, 0x1f, + 0xc8, 0x1f, 0xc9, 0x1f, 0xca, 0x1f, 0xcb, 0x1f, + 0xc3, 0x1f, 0xcd, 0x1f, 0xce, 0x1f, 0xcf, 0x1f, + 0xd8, 0x1f, 0xd9, 0x1f, 0xd2, 0x1f, 0xd3, 0x1f, + 0xd4, 0x1f, 0xd5, 0x1f, 0xd6, 0x1f, 0xd7, 0x1f, + 0xd8, 0x1f, 0xd9, 0x1f, 0xda, 0x1f, 0xdb, 0x1f, + 0xdc, 0x1f, 0xdd, 0x1f, 0xde, 0x1f, 0xdf, 0x1f, + 0xe8, 0x1f, 0xe9, 0x1f, 0xe2, 0x1f, 0xe3, 0x1f, + 0xe4, 0x1f, 0xec, 0x1f, 0xe6, 0x1f, 0xe7, 0x1f, + 0xe8, 0x1f, 0xe9, 0x1f, 0xea, 0x1f, 0xeb, 0x1f, + 0xec, 0x1f, 0xed, 0x1f, 0xee, 0x1f, 0xef, 0x1f, + 0xf0, 0x1f, 0xf1, 0x1f, 0xf2, 0x1f, 0xf3, 0x1f, + 0xf4, 0x1f, 0xf5, 0x1f, 0xf6, 0x1f, 0xf7, 0x1f, + 0xf8, 0x1f, 0xf9, 0x1f, 0xfa, 0x1f, 0xfb, 0x1f, + 0xf3, 0x1f, 0xfd, 0x1f, 0xfe, 0x1f, 0xff, 0x1f, + 0x00, 0x20, 0x01, 0x20, 0x02, 0x20, 0x03, 0x20, + 0x04, 0x20, 0x05, 0x20, 0x06, 0x20, 0x07, 0x20, + 0x08, 0x20, 0x09, 0x20, 0x0a, 0x20, 0x0b, 0x20, + 0x0c, 0x20, 0x0d, 0x20, 0x0e, 0x20, 0x0f, 0x20, + 0x10, 0x20, 0x11, 0x20, 0x12, 0x20, 0x13, 0x20, + 0x14, 0x20, 0x15, 0x20, 0x16, 0x20, 0x17, 0x20, + 0x18, 0x20, 0x19, 0x20, 0x1a, 0x20, 0x1b, 0x20, + 0x1c, 0x20, 0x1d, 0x20, 0x1e, 0x20, 0x1f, 0x20, + 0x20, 0x20, 0x21, 0x20, 0x22, 0x20, 0x23, 0x20, + 0x24, 0x20, 0x25, 0x20, 0x26, 0x20, 0x27, 0x20, + 0x28, 0x20, 0x29, 0x20, 0x2a, 0x20, 0x2b, 0x20, + 0x2c, 0x20, 0x2d, 0x20, 0x2e, 0x20, 0x2f, 0x20, + 0x30, 0x20, 0x31, 0x20, 0x32, 0x20, 0x33, 0x20, + 0x34, 0x20, 0x35, 0x20, 0x36, 0x20, 0x37, 0x20, + 0x38, 0x20, 0x39, 0x20, 0x3a, 0x20, 0x3b, 0x20, + 0x3c, 0x20, 0x3d, 0x20, 0x3e, 0x20, 0x3f, 0x20, + 0x40, 0x20, 0x41, 0x20, 0x42, 0x20, 0x43, 0x20, + 0x44, 0x20, 0x45, 0x20, 0x46, 0x20, 0x47, 0x20, + 0x48, 0x20, 0x49, 0x20, 0x4a, 0x20, 0x4b, 0x20, + 0x4c, 0x20, 0x4d, 0x20, 0x4e, 0x20, 0x4f, 0x20, + 0x50, 0x20, 0x51, 0x20, 0x52, 0x20, 0x53, 0x20, + 0x54, 0x20, 0x55, 0x20, 0x56, 0x20, 0x57, 0x20, + 0x58, 0x20, 0x59, 0x20, 0x5a, 0x20, 0x5b, 0x20, + 0x5c, 0x20, 0x5d, 0x20, 0x5e, 0x20, 0x5f, 0x20, + 0x60, 0x20, 0x61, 0x20, 0x62, 0x20, 0x63, 0x20, + 0x64, 0x20, 0x65, 0x20, 0x66, 0x20, 0x67, 0x20, + 0x68, 0x20, 0x69, 0x20, 0x6a, 0x20, 0x6b, 0x20, + 0x6c, 0x20, 0x6d, 0x20, 0x6e, 0x20, 0x6f, 0x20, + 0x70, 0x20, 0x71, 0x20, 0x72, 0x20, 0x73, 0x20, + 0x74, 0x20, 0x75, 0x20, 0x76, 0x20, 0x77, 0x20, + 0x78, 0x20, 0x79, 0x20, 0x7a, 0x20, 0x7b, 0x20, + 0x7c, 0x20, 0x7d, 0x20, 0x7e, 0x20, 0x7f, 0x20, + 0x80, 0x20, 0x81, 0x20, 0x82, 0x20, 0x83, 0x20, + 0x84, 0x20, 0x85, 0x20, 0x86, 0x20, 0x87, 0x20, + 0x88, 0x20, 0x89, 0x20, 0x8a, 0x20, 0x8b, 0x20, + 0x8c, 0x20, 0x8d, 0x20, 0x8e, 0x20, 0x8f, 0x20, + 0x90, 0x20, 0x91, 0x20, 0x92, 0x20, 0x93, 0x20, + 0x94, 0x20, 0x95, 0x20, 0x96, 0x20, 0x97, 0x20, + 0x98, 0x20, 0x99, 0x20, 0x9a, 0x20, 0x9b, 0x20, + 0x9c, 0x20, 0x9d, 0x20, 0x9e, 0x20, 0x9f, 0x20, + 0xa0, 0x20, 0xa1, 0x20, 0xa2, 0x20, 0xa3, 0x20, + 0xa4, 0x20, 0xa5, 0x20, 0xa6, 0x20, 0xa7, 0x20, + 0xa8, 0x20, 0xa9, 0x20, 0xaa, 0x20, 0xab, 0x20, + 0xac, 0x20, 0xad, 0x20, 0xae, 0x20, 0xaf, 0x20, + 0xb0, 0x20, 0xb1, 0x20, 0xb2, 0x20, 0xb3, 0x20, + 0xb4, 0x20, 0xb5, 0x20, 0xb6, 0x20, 0xb7, 0x20, + 0xb8, 0x20, 0xb9, 0x20, 0xba, 0x20, 0xbb, 0x20, + 0xbc, 0x20, 0xbd, 0x20, 0xbe, 0x20, 0xbf, 0x20, + 0xc0, 0x20, 0xc1, 0x20, 0xc2, 0x20, 0xc3, 0x20, + 0xc4, 0x20, 0xc5, 0x20, 0xc6, 0x20, 0xc7, 0x20, + 0xc8, 0x20, 0xc9, 0x20, 0xca, 0x20, 0xcb, 0x20, + 0xcc, 0x20, 0xcd, 0x20, 0xce, 0x20, 0xcf, 0x20, + 0xd0, 0x20, 0xd1, 0x20, 0xd2, 0x20, 0xd3, 0x20, + 0xd4, 0x20, 0xd5, 0x20, 0xd6, 0x20, 0xd7, 0x20, + 0xd8, 0x20, 0xd9, 0x20, 0xda, 0x20, 0xdb, 0x20, + 0xdc, 0x20, 0xdd, 0x20, 0xde, 0x20, 0xdf, 0x20, + 0xe0, 0x20, 0xe1, 0x20, 0xe2, 0x20, 0xe3, 0x20, + 0xe4, 0x20, 0xe5, 0x20, 0xe6, 0x20, 0xe7, 0x20, + 0xe8, 0x20, 0xe9, 0x20, 0xea, 0x20, 0xeb, 0x20, + 0xec, 0x20, 0xed, 0x20, 0xee, 0x20, 0xef, 0x20, + 0xf0, 0x20, 0xf1, 0x20, 0xf2, 0x20, 0xf3, 0x20, + 0xf4, 0x20, 0xf5, 0x20, 0xf6, 0x20, 0xf7, 0x20, + 0xf8, 0x20, 0xf9, 0x20, 0xfa, 0x20, 0xfb, 0x20, + 0xfc, 0x20, 0xfd, 0x20, 0xfe, 0x20, 0xff, 0x20, + 0x00, 0x21, 0x01, 0x21, 0x02, 0x21, 0x03, 0x21, + 0x04, 0x21, 0x05, 0x21, 0x06, 0x21, 0x07, 0x21, + 0x08, 0x21, 0x09, 0x21, 0x0a, 0x21, 0x0b, 0x21, + 0x0c, 0x21, 0x0d, 0x21, 0x0e, 0x21, 0x0f, 0x21, + 0x10, 0x21, 0x11, 0x21, 0x12, 0x21, 0x13, 0x21, + 0x14, 0x21, 0x15, 0x21, 0x16, 0x21, 0x17, 0x21, + 0x18, 0x21, 0x19, 0x21, 0x1a, 0x21, 0x1b, 0x21, + 0x1c, 0x21, 0x1d, 0x21, 0x1e, 0x21, 0x1f, 0x21, + 0x20, 0x21, 0x21, 0x21, 0x22, 0x21, 0x23, 0x21, + 0x24, 0x21, 0x25, 0x21, 0x26, 0x21, 0x27, 0x21, + 0x28, 0x21, 0x29, 0x21, 0x2a, 0x21, 0x2b, 0x21, + 0x2c, 0x21, 0x2d, 0x21, 0x2e, 0x21, 0x2f, 0x21, + 0x30, 0x21, 0x31, 0x21, 0x32, 0x21, 0x33, 0x21, + 0x34, 0x21, 0x35, 0x21, 0x36, 0x21, 0x37, 0x21, + 0x38, 0x21, 0x39, 0x21, 0x3a, 0x21, 0x3b, 0x21, + 0x3c, 0x21, 0x3d, 0x21, 0x3e, 0x21, 0x3f, 0x21, + 0x40, 0x21, 0x41, 0x21, 0x42, 0x21, 0x43, 0x21, + 0x44, 0x21, 0x45, 0x21, 0x46, 0x21, 0x47, 0x21, + 0x48, 0x21, 0x49, 0x21, 0x4a, 0x21, 0x4b, 0x21, + 0x4c, 0x21, 0x4d, 0x21, 0x32, 0x21, 0x4f, 0x21, + 0x50, 0x21, 0x51, 0x21, 0x52, 0x21, 0x53, 0x21, + 0x54, 0x21, 0x55, 0x21, 0x56, 0x21, 0x57, 0x21, + 0x58, 0x21, 0x59, 0x21, 0x5a, 0x21, 0x5b, 0x21, + 0x5c, 0x21, 0x5d, 0x21, 0x5e, 0x21, 0x5f, 0x21, + 0x60, 0x21, 0x61, 0x21, 0x62, 0x21, 0x63, 0x21, + 0x64, 0x21, 0x65, 0x21, 0x66, 0x21, 0x67, 0x21, + 0x68, 0x21, 0x69, 0x21, 0x6a, 0x21, 0x6b, 0x21, + 0x6c, 0x21, 0x6d, 0x21, 0x6e, 0x21, 0x6f, 0x21, + 0x60, 0x21, 0x61, 0x21, 0x62, 0x21, 0x63, 0x21, + 0x64, 0x21, 0x65, 0x21, 0x66, 0x21, 0x67, 0x21, + 0x68, 0x21, 0x69, 0x21, 0x6a, 0x21, 0x6b, 0x21, + 0x6c, 0x21, 0x6d, 0x21, 0x6e, 0x21, 0x6f, 0x21, + 0x80, 0x21, 0x81, 0x21, 0x82, 0x21, 0x83, 0x21, + 0x83, 0x21, 0xff, 0xff, 0x4b, 0x03, 0xb6, 0x24, + 0xb7, 0x24, 0xb8, 0x24, 0xb9, 0x24, 0xba, 0x24, + 0xbb, 0x24, 0xbc, 0x24, 0xbd, 0x24, 0xbe, 0x24, + 0xbf, 0x24, 0xc0, 0x24, 0xc1, 0x24, 0xc2, 0x24, + 0xc3, 0x24, 0xc4, 0x24, 0xc5, 0x24, 0xc6, 0x24, + 0xc7, 0x24, 0xc8, 0x24, 0xc9, 0x24, 0xca, 0x24, + 0xcb, 0x24, 0xcc, 0x24, 0xcd, 0x24, 0xce, 0x24, + 0xcf, 0x24, 0xff, 0xff, 0x46, 0x07, 0x00, 0x2c, + 0x01, 0x2c, 0x02, 0x2c, 0x03, 0x2c, 0x04, 0x2c, + 0x05, 0x2c, 0x06, 0x2c, 0x07, 0x2c, 0x08, 0x2c, + 0x09, 0x2c, 0x0a, 0x2c, 0x0b, 0x2c, 0x0c, 0x2c, + 0x0d, 0x2c, 0x0e, 0x2c, 0x0f, 0x2c, 0x10, 0x2c, + 0x11, 0x2c, 0x12, 0x2c, 0x13, 0x2c, 0x14, 0x2c, + 0x15, 0x2c, 0x16, 0x2c, 0x17, 0x2c, 0x18, 0x2c, + 0x19, 0x2c, 0x1a, 0x2c, 0x1b, 0x2c, 0x1c, 0x2c, + 0x1d, 0x2c, 0x1e, 0x2c, 0x1f, 0x2c, 0x20, 0x2c, + 0x21, 0x2c, 0x22, 0x2c, 0x23, 0x2c, 0x24, 0x2c, + 0x25, 0x2c, 0x26, 0x2c, 0x27, 0x2c, 0x28, 0x2c, + 0x29, 0x2c, 0x2a, 0x2c, 0x2b, 0x2c, 0x2c, 0x2c, + 0x2d, 0x2c, 0x2e, 0x2c, 0x5f, 0x2c, 0x60, 0x2c, + 0x60, 0x2c, 0x62, 0x2c, 0x63, 0x2c, 0x64, 0x2c, + 0x65, 0x2c, 0x66, 0x2c, 0x67, 0x2c, 0x67, 0x2c, + 0x69, 0x2c, 0x69, 0x2c, 0x6b, 0x2c, 0x6b, 0x2c, + 0x6d, 0x2c, 0x6e, 0x2c, 0x6f, 0x2c, 0x70, 0x2c, + 0x71, 0x2c, 0x72, 0x2c, 0x73, 0x2c, 0x74, 0x2c, + 0x75, 0x2c, 0x75, 0x2c, 0x77, 0x2c, 0x78, 0x2c, + 0x79, 0x2c, 0x7a, 0x2c, 0x7b, 0x2c, 0x7c, 0x2c, + 0x7d, 0x2c, 0x7e, 0x2c, 0x7f, 0x2c, 0x80, 0x2c, + 0x80, 0x2c, 0x82, 0x2c, 0x82, 0x2c, 0x84, 0x2c, + 0x84, 0x2c, 0x86, 0x2c, 0x86, 0x2c, 0x88, 0x2c, + 0x88, 0x2c, 0x8a, 0x2c, 0x8a, 0x2c, 0x8c, 0x2c, + 0x8c, 0x2c, 0x8e, 0x2c, 0x8e, 0x2c, 0x90, 0x2c, + 0x90, 0x2c, 0x92, 0x2c, 0x92, 0x2c, 0x94, 0x2c, + 0x94, 0x2c, 0x96, 0x2c, 0x96, 0x2c, 0x98, 0x2c, + 0x98, 0x2c, 0x9a, 0x2c, 0x9a, 0x2c, 0x9c, 0x2c, + 0x9c, 0x2c, 0x9e, 0x2c, 0x9e, 0x2c, 0xa0, 0x2c, + 0xa0, 0x2c, 0xa2, 0x2c, 0xa2, 0x2c, 0xa4, 0x2c, + 0xa4, 0x2c, 0xa6, 0x2c, 0xa6, 0x2c, 0xa8, 0x2c, + 0xa8, 0x2c, 0xaa, 0x2c, 0xaa, 0x2c, 0xac, 0x2c, + 0xac, 0x2c, 0xae, 0x2c, 0xae, 0x2c, 0xb0, 0x2c, + 0xb0, 0x2c, 0xb2, 0x2c, 0xb2, 0x2c, 0xb4, 0x2c, + 0xb4, 0x2c, 0xb6, 0x2c, 0xb6, 0x2c, 0xb8, 0x2c, + 0xb8, 0x2c, 0xba, 0x2c, 0xba, 0x2c, 0xbc, 0x2c, + 0xbc, 0x2c, 0xbe, 0x2c, 0xbe, 0x2c, 0xc0, 0x2c, + 0xc0, 0x2c, 0xc2, 0x2c, 0xc2, 0x2c, 0xc4, 0x2c, + 0xc4, 0x2c, 0xc6, 0x2c, 0xc6, 0x2c, 0xc8, 0x2c, + 0xc8, 0x2c, 0xca, 0x2c, 0xca, 0x2c, 0xcc, 0x2c, + 0xcc, 0x2c, 0xce, 0x2c, 0xce, 0x2c, 0xd0, 0x2c, + 0xd0, 0x2c, 0xd2, 0x2c, 0xd2, 0x2c, 0xd4, 0x2c, + 0xd4, 0x2c, 0xd6, 0x2c, 0xd6, 0x2c, 0xd8, 0x2c, + 0xd8, 0x2c, 0xda, 0x2c, 0xda, 0x2c, 0xdc, 0x2c, + 0xdc, 0x2c, 0xde, 0x2c, 0xde, 0x2c, 0xe0, 0x2c, + 0xe0, 0x2c, 0xe2, 0x2c, 0xe2, 0x2c, 0xe4, 0x2c, + 0xe5, 0x2c, 0xe6, 0x2c, 0xe7, 0x2c, 0xe8, 0x2c, + 0xe9, 0x2c, 0xea, 0x2c, 0xeb, 0x2c, 0xec, 0x2c, + 0xed, 0x2c, 0xee, 0x2c, 0xef, 0x2c, 0xf0, 0x2c, + 0xf1, 0x2c, 0xf2, 0x2c, 0xf3, 0x2c, 0xf4, 0x2c, + 0xf5, 0x2c, 0xf6, 0x2c, 0xf7, 0x2c, 0xf8, 0x2c, + 0xf9, 0x2c, 0xfa, 0x2c, 0xfb, 0x2c, 0xfc, 0x2c, + 0xfd, 0x2c, 0xfe, 0x2c, 0xff, 0x2c, 0xa0, 0x10, + 0xa1, 0x10, 0xa2, 0x10, 0xa3, 0x10, 0xa4, 0x10, + 0xa5, 0x10, 0xa6, 0x10, 0xa7, 0x10, 0xa8, 0x10, + 0xa9, 0x10, 0xaa, 0x10, 0xab, 0x10, 0xac, 0x10, + 0xad, 0x10, 0xae, 0x10, 0xaf, 0x10, 0xb0, 0x10, + 0xb1, 0x10, 0xb2, 0x10, 0xb3, 0x10, 0xb4, 0x10, + 0xb5, 0x10, 0xb6, 0x10, 0xb7, 0x10, 0xb8, 0x10, + 0xb9, 0x10, 0xba, 0x10, 0xbb, 0x10, 0xbc, 0x10, + 0xbd, 0x10, 0xbe, 0x10, 0xbf, 0x10, 0xc0, 0x10, + 0xc1, 0x10, 0xc2, 0x10, 0xc3, 0x10, 0xc4, 0x10, + 0xc5, 0x10, 0xff, 0xff, 0x1b, 0xd2, 0x21, 0xff, + 0x22, 0xff, 0x23, 0xff, 0x24, 0xff, 0x25, 0xff, + 0x26, 0xff, 0x27, 0xff, 0x28, 0xff, 0x29, 0xff, + 0x2a, 0xff, 0x2b, 0xff, 0x2c, 0xff, 0x2d, 0xff, + 0x2e, 0xff, 0x2f, 0xff, 0x30, 0xff, 0x31, 0xff, + 0x32, 0xff, 0x33, 0xff, 0x34, 0xff, 0x35, 0xff, + 0x36, 0xff, 0x37, 0xff, 0x38, 0xff, 0x39, 0xff, + 0x3a, 0xff, 0x5b, 0xff, 0x5c, 0xff, 0x5d, 0xff, + 0x5e, 0xff, 0x5f, 0xff, 0x60, 0xff, 0x61, 0xff, + 0x62, 0xff, 0x63, 0xff, 0x64, 0xff, 0x65, 0xff, + 0x66, 0xff, 0x67, 0xff, 0x68, 0xff, 0x69, 0xff, + 0x6a, 0xff, 0x6b, 0xff, 0x6c, 0xff, 0x6d, 0xff, + 0x6e, 0xff, 0x6f, 0xff, 0x70, 0xff, 0x71, 0xff, + 0x72, 0xff, 0x73, 0xff, 0x74, 0xff, 0x75, 0xff, + 0x76, 0xff, 0x77, 0xff, 0x78, 0xff, 0x79, 0xff, + 0x7a, 0xff, 0x7b, 0xff, 0x7c, 0xff, 0x7d, 0xff, + 0x7e, 0xff, 0x7f, 0xff, 0x80, 0xff, 0x81, 0xff, + 0x82, 0xff, 0x83, 0xff, 0x84, 0xff, 0x85, 0xff, + 0x86, 0xff, 0x87, 0xff, 0x88, 0xff, 0x89, 0xff, + 0x8a, 0xff, 0x8b, 0xff, 0x8c, 0xff, 0x8d, 0xff, + 0x8e, 0xff, 0x8f, 0xff, 0x90, 0xff, 0x91, 0xff, + 0x92, 0xff, 0x93, 0xff, 0x94, 0xff, 0x95, 0xff, + 0x96, 0xff, 0x97, 0xff, 0x98, 0xff, 0x99, 0xff, + 0x9a, 0xff, 0x9b, 0xff, 0x9c, 0xff, 0x9d, 0xff, + 0x9e, 0xff, 0x9f, 0xff, 0xa0, 0xff, 0xa1, 0xff, + 0xa2, 0xff, 0xa3, 0xff, 0xa4, 0xff, 0xa5, 0xff, + 0xa6, 0xff, 0xa7, 0xff, 0xa8, 0xff, 0xa9, 0xff, + 0xaa, 0xff, 0xab, 0xff, 0xac, 0xff, 0xad, 0xff, + 0xae, 0xff, 0xaf, 0xff, 0xb0, 0xff, 0xb1, 0xff, + 0xb2, 0xff, 0xb3, 0xff, 0xb4, 0xff, 0xb5, 0xff, + 0xb6, 0xff, 0xb7, 0xff, 0xb8, 0xff, 0xb9, 0xff, + 0xba, 0xff, 0xbb, 0xff, 0xbc, 0xff, 0xbd, 0xff, + 0xbe, 0xff, 0xbf, 0xff, 0xc0, 0xff, 0xc1, 0xff, + 0xc2, 0xff, 0xc3, 0xff, 0xc4, 0xff, 0xc5, 0xff, + 0xc6, 0xff, 0xc7, 0xff, 0xc8, 0xff, 0xc9, 0xff, + 0xca, 0xff, 0xcb, 0xff, 0xcc, 0xff, 0xcd, 0xff, + 0xce, 0xff, 0xcf, 0xff, 0xd0, 0xff, 0xd1, 0xff, + 0xd2, 0xff, 0xd3, 0xff, 0xd4, 0xff, 0xd5, 0xff, + 0xd6, 0xff, 0xd7, 0xff, 0xd8, 0xff, 0xd9, 0xff, + 0xda, 0xff, 0xdb, 0xff, 0xdc, 0xff, 0xdd, 0xff, + 0xde, 0xff, 0xdf, 0xff, 0xe0, 0xff, 0xe1, 0xff, + 0xe2, 0xff, 0xe3, 0xff, 0xe4, 0xff, 0xe5, 0xff, + 0xe6, 0xff, 0xe7, 0xff, 0xe8, 0xff, 0xe9, 0xff, + 0xea, 0xff, 0xeb, 0xff, 0xec, 0xff, 0xed, 0xff, + 0xee, 0xff, 0xef, 0xff, 0xf0, 0xff, 0xf1, 0xff, + 0xf2, 0xff, 0xf3, 0xff, 0xf4, 0xff, 0xf5, 0xff, + 0xf6, 0xff, 0xf7, 0xff, 0xf8, 0xff, 0xf9, 0xff, + 0xfa, 0xff, 0xfb, 0xff, 0xfc, 0xff, 0xfd, 0xff, + 0xfe, 0xff, 0xff, 0xff +}; diff --git a/exfat/mkfs/uctc.h b/exfat/mkfs/uctc.h new file mode 100644 index 000000000..67d2bba6b --- /dev/null +++ b/exfat/mkfs/uctc.h @@ -0,0 +1,28 @@ +/* + uctc.h (30.10.10) + Upper Case Table declaration. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_UCTC_H_INCLUDED +#define MKFS_UCTC_H_INCLUDED + +#include + +extern uint8_t upcase_table[5836]; + +#endif /* ifndef MKFS_UCTC_H_INCLUDED */ diff --git a/exfat/mkfs/vbr.c b/exfat/mkfs/vbr.c new file mode 100644 index 000000000..80e7a61e6 --- /dev/null +++ b/exfat/mkfs/vbr.c @@ -0,0 +1,146 @@ +/* + vbr.c (09.11.10) + Volume Boot Record creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include +#include "vbr.h" +#include "fat.h" +#include "cbm.h" +#include "uct.h" +#include "rootdir.h" + +static off64_t vbr_alignment(void) +{ + return get_sector_size(); +} + +static off64_t vbr_size(void) +{ + return 12 * get_sector_size(); +} + +static void init_sb(struct exfat_super_block* sb) +{ + uint32_t clusters_max; + uint32_t fat_sectors; + + clusters_max = get_volume_size() / get_cluster_size(); + fat_sectors = DIV_ROUND_UP((off64_t) clusters_max * sizeof(cluster_t), + get_sector_size()); + + memset(sb, 0, sizeof(struct exfat_super_block)); + sb->jump[0] = 0xeb; + sb->jump[1] = 0x76; + sb->jump[2] = 0x90; + memcpy(sb->oem_name, "EXFAT ", sizeof(sb->oem_name)); + sb->sector_start = cpu_to_le64(get_first_sector()); + sb->sector_count = cpu_to_le64(get_volume_size() / get_sector_size()); + sb->fat_sector_start = cpu_to_le32( + fat.get_alignment() / get_sector_size()); + sb->fat_sector_count = cpu_to_le32(ROUND_UP( + le32_to_cpu(sb->fat_sector_start) + fat_sectors, + 1 << get_spc_bits()) - + le32_to_cpu(sb->fat_sector_start)); + sb->cluster_sector_start = cpu_to_le32( + get_position(&cbm) / get_sector_size()); + sb->cluster_count = cpu_to_le32(clusters_max - + ((le32_to_cpu(sb->fat_sector_start) + + le32_to_cpu(sb->fat_sector_count)) >> get_spc_bits())); + sb->rootdir_cluster = cpu_to_le32( + (get_position(&rootdir) - get_position(&cbm)) / get_cluster_size() + + EXFAT_FIRST_DATA_CLUSTER); + sb->volume_serial = cpu_to_le32(get_volume_serial()); + sb->version.major = 1; + sb->version.minor = 0; + sb->volume_state = cpu_to_le16(0); + sb->sector_bits = get_sector_bits(); + sb->spc_bits = get_spc_bits(); + sb->fat_count = 1; + sb->drive_no = 0x80; + sb->allocated_percent = 0; + sb->boot_signature = cpu_to_le16(0xaa55); +} + +static int vbr_write(struct exfat_dev* dev) +{ + struct exfat_super_block sb; + uint32_t checksum; + le32_t* sector = malloc(get_sector_size()); + size_t i; + + if (sector == NULL) + { + exfat_error("failed to allocate sector-sized block of memory"); + return 1; + } + + init_sb(&sb); + if (exfat_write(dev, &sb, sizeof(struct exfat_super_block)) < 0) + { + free(sector); + exfat_error("failed to write super block sector"); + return 1; + } + checksum = exfat_vbr_start_checksum(&sb, sizeof(struct exfat_super_block)); + + memset(sector, 0, get_sector_size()); + sector[get_sector_size() / sizeof(sector[0]) - 1] = + cpu_to_le32(0xaa550000); + for (i = 0; i < 8; i++) + { + if (exfat_write(dev, sector, get_sector_size()) < 0) + { + free(sector); + exfat_error("failed to write a sector with boot signature"); + return 1; + } + checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum); + } + + memset(sector, 0, get_sector_size()); + for (i = 0; i < 2; i++) + { + if (exfat_write(dev, sector, get_sector_size()) < 0) + { + free(sector); + exfat_error("failed to write an empty sector"); + return 1; + } + checksum = exfat_vbr_add_checksum(sector, get_sector_size(), checksum); + } + + for (i = 0; i < get_sector_size() / sizeof(sector[0]); i++) + sector[i] = cpu_to_le32(checksum); + if (exfat_write(dev, sector, get_sector_size()) < 0) + { + free(sector); + exfat_error("failed to write checksum sector"); + return 1; + } + + free(sector); + return 0; +} + +const struct fs_object vbr = +{ + .get_alignment = vbr_alignment, + .get_size = vbr_size, + .write = vbr_write, +}; diff --git a/exfat/mkfs/vbr.h b/exfat/mkfs/vbr.h new file mode 100644 index 000000000..c3a6362e0 --- /dev/null +++ b/exfat/mkfs/vbr.h @@ -0,0 +1,28 @@ +/* + vbr.h (09.11.10) + Volume Boot Record creation code. + + Copyright (C) 2011, 2012 Andrew Nayenko + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#ifndef MKFS_VBR_H_INCLUDED +#define MKFS_VBR_H_INCLUDED + +#include "mkexfat.h" + +extern const struct fs_object vbr; + +#endif /* ifndef MKFS_VBR_H_INCLUDED */ diff --git a/fixPermissions.cpp b/fixPermissions.cpp index 801e27374..16a3f33f2 100644 --- a/fixPermissions.cpp +++ b/fixPermissions.cpp @@ -220,44 +220,6 @@ int fixPermissions::pchmod(string fn, string mode) { return 0; } -int fixPermissions::removeDir(const string path) { - DIR *d = opendir(path.c_str()); - int r = 0; - string new_path; - - if (d == NULL) { - LOGE("Error opening '%s'\n", path.c_str()); - return -1; - } - - if (d) { - struct dirent *p; - while (!r && (p = readdir(d))) { - if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) - continue; - - new_path = path + "/"; - new_path.append(p->d_name); - if (p->d_type == DT_DIR) { - r = removeDir(new_path); - if (!r) - LOGI("Unable to removeDir '%s'\n", new_path.c_str()); - } else { - r = unlink(new_path.c_str()); - if (!r) - LOGI("Unable to unlink '%s'\n", new_path.c_str()); - } - } - closedir(d); - } - if (!r) - r = rmdir(path.c_str()); - if (!r) - LOGI("Unable to rmdir '%s'\n", path.c_str()); - - return r; -} - int fixPermissions::fixSystemApps() { temp = head; while (temp != NULL) { @@ -279,7 +241,7 @@ int fixPermissions::fixSystemApps() { if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") { if (debug) LOGI("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str()); - if (removeDir(temp->dDir) != 0) { + if (TWFunc::removeDir(temp->dDir, false) != 0) { LOGI("Unable to removeDir '%s'\n", temp->dDir.c_str()); return -1; } @@ -325,7 +287,7 @@ int fixPermissions::fixDataApps() { if (remove_data && TWFunc::Path_Exists(temp->dDir) && temp->appDir.size() >= 9 && temp->appDir.substr(0, 9) != "/mnt/asec") { if (debug) LOGI("Looking at '%s', removing data dir: '%s', appDir: '%s'", temp->codePath.c_str(), temp->dDir.c_str(), temp->appDir.c_str()); - if (removeDir(temp->dDir) != 0) { + if (TWFunc::removeDir(temp->dDir, false) != 0) { LOGI("Unable to removeDir '%s'\n", temp->dDir.c_str()); return -1; } diff --git a/fixPermissions.hpp b/fixPermissions.hpp index e19b1b387..491029a62 100644 --- a/fixPermissions.hpp +++ b/fixPermissions.hpp @@ -22,7 +22,6 @@ class fixPermissions { int pchmod(std::string fn, string mode); vector listAllDirectories(std::string path); vector listAllFiles(std::string path); - int removeDir(const std::string path); int getPackages(); int fixSystemApps(); int fixDataApps(); diff --git a/fuse/Android.mk b/fuse/Android.mk new file mode 100644 index 000000000..08559594b --- /dev/null +++ b/fuse/Android.mk @@ -0,0 +1,65 @@ +# Copyright (C) 2009 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + cuse_lowlevel.c \ + fuse.c \ + fuse_kern_chan.c \ + fuse_loop.c \ + fuse_loop_mt.c \ + fuse_lowlevel.c \ + fuse_mt.c fuse_opt.c \ + fuse_session.c \ + fuse_signals.c \ + helper.c \ + mount.c \ + mount_util.c \ + ulockmgr.c + +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/include + +LOCAL_SHARED_LIBRARIES := \ + libutils libdl + +LOCAL_CFLAGS := \ + -D_FILE_OFFSET_BITS=64 \ + -DFUSE_USE_VERSION=26 + +LOCAL_MODULE := libfuse +LOCAL_MODULE_TAGS := optional + +include $(BUILD_STATIC_LIBRARY) +#include $(BUILD_SHARED_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := \ + fusexmp.c + +LOCAL_C_INCLUDES := \ + $(LOCAL_PATH)/include + +LOCAL_CFLAGS := -D_FILE_OFFSET_BITS=64 + +LOCAL_MODULE := fusexmp +LOCAL_MODULE_TAGS := optional + +LOCAL_STATIC_LIBRARIES := libfuse + +include $(BUILD_EXECUTABLE) diff --git a/fuse/README b/fuse/README new file mode 100644 index 000000000..3d1f6f091 --- /dev/null +++ b/fuse/README @@ -0,0 +1,12 @@ +Libfuse for Android + +FUSE[1] is a framework to develop userspace file systems for linux. Since Android is based on linux, it won't be too difficult to port libfuse to Android. + +The main problem of building and running libfuse on Android is that the bionic c library lacks support for pthread_cancel(), which is necessary for libfuse multi-thread code. This stackoverflow entry[2] has suggested a solution, which uses SIGUSR1 as an alternative. It seems to work. + +Libfuse can be build with Android NDK[3]. If success, you will get libfuse.a and an example program fusexmp. To run the example, the android kernel should be built with FUSE kernel support. + +References: +[1]. http://fuse.sourceforge.net +[2]. http://stackoverflow.com/questions/4610086/pthread-cancel-alternatives-in-android-ndk +[3]. http://developer.android.com/sdk/ndk/index.html diff --git a/fuse/cuse_lowlevel.c b/fuse/cuse_lowlevel.c new file mode 100644 index 000000000..970df7fb7 --- /dev/null +++ b/fuse/cuse_lowlevel.c @@ -0,0 +1,371 @@ +/* + CUSE: Character device in Userspace + Copyright (C) 2008 SUSE Linux Products GmbH + Copyright (C) 2008 Tejun Heo + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "cuse_lowlevel.h" +#include "fuse_kernel.h" +#include "fuse_i.h" +#include "fuse_opt.h" +#include "fuse_misc.h" + +#include +#include +#include +#include +#include +#include + +struct cuse_data { + struct cuse_lowlevel_ops clop; + unsigned max_read; + unsigned dev_major; + unsigned dev_minor; + unsigned flags; + unsigned dev_info_len; + char dev_info[]; +}; + +static struct cuse_lowlevel_ops *req_clop(fuse_req_t req) +{ + return &req->f->cuse_data->clop; +} + +static void cuse_fll_open(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + (void)ino; + req_clop(req)->open(req, fi); +} + +static void cuse_fll_read(fuse_req_t req, fuse_ino_t ino, size_t size, + off64_t off, struct fuse_file_info *fi) +{ + (void)ino; + req_clop(req)->read(req, size, off, fi); +} + +static void cuse_fll_write(fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off64_t off, struct fuse_file_info *fi) +{ + (void)ino; + req_clop(req)->write(req, buf, size, off, fi); +} + +static void cuse_fll_flush(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + (void)ino; + req_clop(req)->flush(req, fi); +} + +static void cuse_fll_release(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + (void)ino; + req_clop(req)->release(req, fi); +} + +static void cuse_fll_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi) +{ + (void)ino; + req_clop(req)->fsync(req, datasync, fi); +} + +static void cuse_fll_ioctl(fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, + const void *in_buf, size_t in_bufsz, size_t out_bufsz) +{ + (void)ino; + req_clop(req)->ioctl(req, cmd, arg, fi, flags, in_buf, in_bufsz, + out_bufsz); +} + +static void cuse_fll_poll(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct fuse_pollhandle *ph) +{ + (void)ino; + req_clop(req)->poll(req, fi, ph); +} + +static size_t cuse_pack_info(int argc, const char **argv, char *buf) +{ + size_t size = 0; + int i; + + for (i = 0; i < argc; i++) { + size_t len; + + len = strlen(argv[i]) + 1; + size += len; + if (buf) { + memcpy(buf, argv[i], len); + buf += len; + } + } + + return size; +} + +static struct cuse_data *cuse_prep_data(const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop) +{ + struct cuse_data *cd; + size_t dev_info_len; + + dev_info_len = cuse_pack_info(ci->dev_info_argc, ci->dev_info_argv, + NULL); + + if (dev_info_len > CUSE_INIT_INFO_MAX) { + fprintf(stderr, "cuse: dev_info (%zu) too large, limit=%u\n", + dev_info_len, CUSE_INIT_INFO_MAX); + return NULL; + } + + cd = calloc(1, sizeof(*cd) + dev_info_len); + if (!cd) { + fprintf(stderr, "cuse: failed to allocate cuse_data\n"); + return NULL; + } + + memcpy(&cd->clop, clop, sizeof(cd->clop)); + cd->max_read = 131072; + cd->dev_major = ci->dev_major; + cd->dev_minor = ci->dev_minor; + cd->dev_info_len = dev_info_len; + cd->flags = ci->flags; + cuse_pack_info(ci->dev_info_argc, ci->dev_info_argv, cd->dev_info); + + return cd; +} + +struct fuse_session *cuse_lowlevel_new(struct fuse_args *args, + const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop, + void *userdata) +{ + struct fuse_lowlevel_ops lop; + struct cuse_data *cd; + struct fuse_session *se; + struct fuse_ll *ll; + + cd = cuse_prep_data(ci, clop); + if (!cd) + return NULL; + + memset(&lop, 0, sizeof(lop)); + lop.init = clop->init; + lop.destroy = clop->destroy; + lop.open = clop->open ? cuse_fll_open : NULL; + lop.read = clop->read ? cuse_fll_read : NULL; + lop.write = clop->write ? cuse_fll_write : NULL; + lop.flush = clop->flush ? cuse_fll_flush : NULL; + lop.release = clop->release ? cuse_fll_release : NULL; + lop.fsync = clop->fsync ? cuse_fll_fsync : NULL; + lop.ioctl = clop->ioctl ? cuse_fll_ioctl : NULL; + lop.poll = clop->poll ? cuse_fll_poll : NULL; + + se = fuse_lowlevel_new_common(args, &lop, sizeof(lop), userdata); + if (!se) { + free(cd); + return NULL; + } + ll = se->data; + ll->cuse_data = cd; + + return se; +} + +static int cuse_reply_init(fuse_req_t req, struct cuse_init_out *arg, + char *dev_info, unsigned dev_info_len) +{ + struct iovec iov[3]; + + iov[1].iov_base = arg; + iov[1].iov_len = sizeof(struct cuse_init_out); + iov[2].iov_base = dev_info; + iov[2].iov_len = dev_info_len; + + return fuse_send_reply_iov_nofree(req, 0, iov, 3); +} + +void cuse_lowlevel_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_init_in *arg = (struct fuse_init_in *) inarg; + struct cuse_init_out outarg; + struct fuse_ll *f = req->f; + struct cuse_data *cd = f->cuse_data; + size_t bufsize = fuse_chan_bufsize(req->ch); + struct cuse_lowlevel_ops *clop = req_clop(req); + + (void) nodeid; + if (f->debug) { + fprintf(stderr, "CUSE_INIT: %u.%u\n", arg->major, arg->minor); + fprintf(stderr, "flags=0x%08x\n", arg->flags); + } + f->conn.proto_major = arg->major; + f->conn.proto_minor = arg->minor; + f->conn.capable = 0; + f->conn.want = 0; + + if (arg->major < 7) { + fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n", + arg->major, arg->minor); + fuse_reply_err(req, EPROTO); + return; + } + + if (bufsize < FUSE_MIN_READ_BUFFER) { + fprintf(stderr, "fuse: warning: buffer size too small: %zu\n", + bufsize); + bufsize = FUSE_MIN_READ_BUFFER; + } + + bufsize -= 4096; + if (bufsize < f->conn.max_write) + f->conn.max_write = bufsize; + + f->got_init = 1; + if (f->op.init) + f->op.init(f->userdata, &f->conn); + + memset(&outarg, 0, sizeof(outarg)); + outarg.major = FUSE_KERNEL_VERSION; + outarg.minor = FUSE_KERNEL_MINOR_VERSION; + outarg.flags = cd->flags; + outarg.max_read = cd->max_read; + outarg.max_write = f->conn.max_write; + outarg.dev_major = cd->dev_major; + outarg.dev_minor = cd->dev_minor; + + if (f->debug) { + fprintf(stderr, " CUSE_INIT: %u.%u\n", + outarg.major, outarg.minor); + fprintf(stderr, " flags=0x%08x\n", outarg.flags); + fprintf(stderr, " max_read=0x%08x\n", outarg.max_read); + fprintf(stderr, " max_write=0x%08x\n", outarg.max_write); + fprintf(stderr, " dev_major=%u\n", outarg.dev_major); + fprintf(stderr, " dev_minor=%u\n", outarg.dev_minor); + fprintf(stderr, " dev_info: %.*s\n", cd->dev_info_len, + cd->dev_info); + } + + cuse_reply_init(req, &outarg, cd->dev_info, cd->dev_info_len); + + if (clop->init_done) + clop->init_done(f->userdata); + + fuse_free_req(req); +} + +struct fuse_session *cuse_lowlevel_setup(int argc, char *argv[], + const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop, + int *multithreaded, void *userdata) +{ + const char *devname = "/dev/cuse"; + static const struct fuse_opt kill_subtype_opts[] = { + FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_DISCARD), + FUSE_OPT_END + }; + struct fuse_args args = FUSE_ARGS_INIT(argc, argv); + struct fuse_session *se; + struct fuse_chan *ch; + int fd; + int foreground; + int res; + + res = fuse_parse_cmdline(&args, NULL, multithreaded, &foreground); + if (res == -1) + goto err_args; + + res = fuse_opt_parse(&args, NULL, kill_subtype_opts, NULL); + if (res == -1) + goto err_args; + + /* + * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos + * would ensue. + */ + do { + fd = open("/dev/null", O_RDWR); + if (fd > 2) + close(fd); + } while (fd >= 0 && fd <= 2); + + se = cuse_lowlevel_new(&args, ci, clop, userdata); + fuse_opt_free_args(&args); + if (se == NULL) + goto err_args; + + fd = open(devname, O_RDWR); + if (fd == -1) { + if (errno == ENODEV || errno == ENOENT) + fprintf(stderr, "fuse: device not found, try 'modprobe cuse' first\n"); + else + fprintf(stderr, "fuse: failed to open %s: %s\n", + devname, strerror(errno)); + goto err_se; + } + + ch = fuse_kern_chan_new(fd); + if (!ch) { + close(fd); + goto err_se; + } + + fuse_session_add_chan(se, ch); + + res = fuse_set_signal_handlers(se); + if (res == -1) + goto err_se; + + res = fuse_daemonize(foreground); + if (res == -1) + goto err_sig; + + return se; + +err_sig: + fuse_remove_signal_handlers(se); +err_se: + fuse_session_destroy(se); +err_args: + fuse_opt_free_args(&args); + return NULL; +} + +void cuse_lowlevel_teardown(struct fuse_session *se) +{ + fuse_remove_signal_handlers(se); + fuse_session_destroy(se); +} + +int cuse_lowlevel_main(int argc, char *argv[], const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop, void *userdata) +{ + struct fuse_session *se; + int multithreaded; + int res; + + se = cuse_lowlevel_setup(argc, argv, ci, clop, &multithreaded, + userdata); + if (se == NULL) + return 1; + + if (multithreaded) + res = fuse_session_loop_mt(se); + else + res = fuse_session_loop(se); + + cuse_lowlevel_teardown(se); + if (res == -1) + return 1; + + return 0; +} diff --git a/fuse/fuse.c b/fuse/fuse.c new file mode 100644 index 000000000..448c9cd50 --- /dev/null +++ b/fuse/fuse.c @@ -0,0 +1,3968 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + + +/* For pthread_rwlock_t */ +#define _GNU_SOURCE + +#include "fuse_i.h" +#include "fuse_lowlevel.h" +#include "fuse_opt.h" +#include "fuse_misc.h" +#include "fuse_common_compat.h" +#include "fuse_compat.h" +#include "fuse_kernel.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1 + +#define FUSE_UNKNOWN_INO 0xffffffff +#define OFFSET_MAX 0x7fffffffffffffffLL + +struct fuse_config { + unsigned int uid; + unsigned int gid; + unsigned int umask; + double entry_timeout; + double negative_timeout; + double attr_timeout; + double ac_attr_timeout; + int ac_attr_timeout_set; + int noforget; + int debug; + int hard_remove; + int use_ino; + int readdir_ino; + int set_mode; + int set_uid; + int set_gid; + int direct_io; + int kernel_cache; + int auto_cache; + int intr; + int intr_signal; + int help; + char *modules; +}; + +struct fuse_fs { + struct fuse_operations op; + struct fuse_module *m; + void *user_data; + int compat; + int debug; +}; + +struct fusemod_so { + void *handle; + int ctr; +}; + +struct lock_queue_element { + struct lock_queue_element *next; + pthread_cond_t cond; +}; + +struct fuse { + struct fuse_session *se; + struct node **name_table; + size_t name_table_size; + struct node **id_table; + size_t id_table_size; + fuse_ino_t ctr; + unsigned int generation; + unsigned int hidectr; + pthread_mutex_t lock; + struct fuse_config conf; + int intr_installed; + struct fuse_fs *fs; + int nullpath_ok; + int curr_ticket; + struct lock_queue_element *lockq; +}; + +struct lock { + int type; + off64_t start; + off64_t end; + pid_t pid; + uint64_t owner; + struct lock *next; +}; + +struct node { + struct node *name_next; + struct node *id_next; + fuse_ino_t nodeid; + unsigned int generation; + int refctr; + struct node *parent; + char *name; + uint64_t nlookup; + int open_count; + struct timespec stat_updated; + struct timespec mtime; + off64_t size; + struct lock *locks; + unsigned int is_hidden : 1; + unsigned int cache_valid : 1; + int treelock; + int ticket; +}; + +struct fuse_dh { + pthread_mutex_t lock; + struct fuse *fuse; + fuse_req_t req; + char *contents; + int allocated; + unsigned len; + unsigned size; + unsigned needlen; + int filled; + uint64_t fh; + int error; + fuse_ino_t nodeid; +}; + +/* old dir handle */ +struct fuse_dirhandle { + fuse_fill_dir_t filler; + void *buf; +}; + +struct fuse_context_i { + struct fuse_context ctx; + fuse_req_t req; +}; + +static pthread_key_t fuse_context_key; +static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER; +static int fuse_context_ref; +static struct fusemod_so *fuse_current_so; +static struct fuse_module *fuse_modules; + +static int fuse_load_so_name(const char *soname) +{ + struct fusemod_so *so; + + so = calloc(1, sizeof(struct fusemod_so)); + if (!so) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + + fuse_current_so = so; + so->handle = dlopen(soname, RTLD_NOW); + fuse_current_so = NULL; + if (!so->handle) { + fprintf(stderr, "fuse: %s\n", dlerror()); + goto err; + } + if (!so->ctr) { + fprintf(stderr, "fuse: %s did not register any modules\n", + soname); + goto err; + } + return 0; + +err: + if (so->handle) + dlclose(so->handle); + free(so); + return -1; +} + +static int fuse_load_so_module(const char *module) +{ + int res; + char *soname = malloc(strlen(module) + 64); + if (!soname) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + sprintf(soname, "libfusemod_%s.so", module); + res = fuse_load_so_name(soname); + free(soname); + return res; +} + +static struct fuse_module *fuse_find_module(const char *module) +{ + struct fuse_module *m; + for (m = fuse_modules; m; m = m->next) { + if (strcmp(module, m->name) == 0) { + m->ctr++; + break; + } + } + return m; +} + +static struct fuse_module *fuse_get_module(const char *module) +{ + struct fuse_module *m; + + pthread_mutex_lock(&fuse_context_lock); + m = fuse_find_module(module); + if (!m) { + int err = fuse_load_so_module(module); + if (!err) + m = fuse_find_module(module); + } + pthread_mutex_unlock(&fuse_context_lock); + return m; +} + +static void fuse_put_module(struct fuse_module *m) +{ + pthread_mutex_lock(&fuse_context_lock); + assert(m->ctr > 0); + m->ctr--; + if (!m->ctr && m->so) { + struct fusemod_so *so = m->so; + assert(so->ctr > 0); + so->ctr--; + if (!so->ctr) { + struct fuse_module **mp; + for (mp = &fuse_modules; *mp;) { + if ((*mp)->so == so) + *mp = (*mp)->next; + else + mp = &(*mp)->next; + } + dlclose(so->handle); + free(so); + } + } + pthread_mutex_unlock(&fuse_context_lock); +} + +static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid) +{ + size_t hash = nodeid % f->id_table_size; + struct node *node; + + for (node = f->id_table[hash]; node != NULL; node = node->id_next) + if (node->nodeid == nodeid) + return node; + + return NULL; +} + +static struct node *get_node(struct fuse *f, fuse_ino_t nodeid) +{ + struct node *node = get_node_nocheck(f, nodeid); + if (!node) { + fprintf(stderr, "fuse internal error: node %llu not found\n", + (unsigned long long) nodeid); + abort(); + } + return node; +} + +static void free_node(struct node *node) +{ + free(node->name); + free(node); +} + +static void unhash_id(struct fuse *f, struct node *node) +{ + size_t hash = node->nodeid % f->id_table_size; + struct node **nodep = &f->id_table[hash]; + + for (; *nodep != NULL; nodep = &(*nodep)->id_next) + if (*nodep == node) { + *nodep = node->id_next; + return; + } +} + +static void hash_id(struct fuse *f, struct node *node) +{ + size_t hash = node->nodeid % f->id_table_size; + node->id_next = f->id_table[hash]; + f->id_table[hash] = node; +} + +static unsigned int name_hash(struct fuse *f, fuse_ino_t parent, + const char *name) +{ + unsigned int hash = *name; + + if (hash) + for (name += 1; *name != '\0'; name++) + hash = (hash << 5) - hash + *name; + + return (hash + parent) % f->name_table_size; +} + +static void unref_node(struct fuse *f, struct node *node); + +static void unhash_name(struct fuse *f, struct node *node) +{ + if (node->name) { + size_t hash = name_hash(f, node->parent->nodeid, node->name); + struct node **nodep = &f->name_table[hash]; + + for (; *nodep != NULL; nodep = &(*nodep)->name_next) + if (*nodep == node) { + *nodep = node->name_next; + node->name_next = NULL; + unref_node(f, node->parent); + free(node->name); + node->name = NULL; + node->parent = NULL; + return; + } + fprintf(stderr, + "fuse internal error: unable to unhash node: %llu\n", + (unsigned long long) node->nodeid); + abort(); + } +} + +static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parentid, + const char *name) +{ + size_t hash = name_hash(f, parentid, name); + struct node *parent = get_node(f, parentid); + node->name = strdup(name); + if (node->name == NULL) + return -1; + + parent->refctr ++; + node->parent = parent; + node->name_next = f->name_table[hash]; + f->name_table[hash] = node; + return 0; +} + +static void delete_node(struct fuse *f, struct node *node) +{ + if (f->conf.debug) + fprintf(stderr, "DELETE: %llu\n", + (unsigned long long) node->nodeid); + + assert(node->treelock == 0); + assert(!node->name); + unhash_id(f, node); + free_node(node); +} + +static void unref_node(struct fuse *f, struct node *node) +{ + assert(node->refctr > 0); + node->refctr --; + if (!node->refctr) + delete_node(f, node); +} + +static fuse_ino_t next_id(struct fuse *f) +{ + do { + f->ctr = (f->ctr + 1) & 0xffffffff; + if (!f->ctr) + f->generation ++; + } while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO || + get_node_nocheck(f, f->ctr) != NULL); + return f->ctr; +} + +static struct node *lookup_node(struct fuse *f, fuse_ino_t parent, + const char *name) +{ + size_t hash = name_hash(f, parent, name); + struct node *node; + + for (node = f->name_table[hash]; node != NULL; node = node->name_next) + if (node->parent->nodeid == parent && + strcmp(node->name, name) == 0) + return node; + + return NULL; +} + +static struct node *find_node(struct fuse *f, fuse_ino_t parent, + const char *name) +{ + struct node *node; + + pthread_mutex_lock(&f->lock); + if (!name) + node = get_node(f, parent); + else + node = lookup_node(f, parent, name); + if (node == NULL) { + node = (struct node *) calloc(1, sizeof(struct node)); + if (node == NULL) + goto out_err; + + if (f->conf.noforget) + node->nlookup = 1; + node->refctr = 1; + node->nodeid = next_id(f); + node->generation = f->generation; + node->open_count = 0; + node->is_hidden = 0; + node->treelock = 0; + node->ticket = 0; + if (hash_name(f, node, parent, name) == -1) { + free(node); + node = NULL; + goto out_err; + } + hash_id(f, node); + } + node->nlookup ++; +out_err: + pthread_mutex_unlock(&f->lock); + return node; +} + +static char *add_name(char **buf, unsigned *bufsize, char *s, const char *name) +{ + size_t len = strlen(name); + + if (s - len <= *buf) { + unsigned pathlen = *bufsize - (s - *buf); + unsigned newbufsize = *bufsize; + char *newbuf; + + while (newbufsize < pathlen + len + 1) { + if (newbufsize >= 0x80000000) + newbufsize = 0xffffffff; + else + newbufsize *= 2; + } + + newbuf = realloc(*buf, newbufsize); + if (newbuf == NULL) + return NULL; + + *buf = newbuf; + s = newbuf + newbufsize - pathlen; + memmove(s, newbuf + *bufsize - pathlen, pathlen); + *bufsize = newbufsize; + } + s -= len; + strncpy(s, name, len); + s--; + *s = '/'; + + return s; +} + +static void unlock_path(struct fuse *f, fuse_ino_t nodeid, struct node *wnode, + struct node *end, int ticket) +{ + struct node *node; + + if (wnode) { + assert(wnode->treelock == -1); + wnode->treelock = 0; + if (!wnode->ticket) + wnode->ticket = ticket; + } + + for (node = get_node(f, nodeid); + node != end && node->nodeid != FUSE_ROOT_ID; node = node->parent) { + assert(node->treelock > 0); + node->treelock--; + if (!node->ticket) + node->ticket = ticket; + } +} + +static int try_get_path(struct fuse *f, fuse_ino_t nodeid, const char *name, + char **path, struct node **wnodep, int ticket) +{ + unsigned bufsize = 256; + char *buf; + char *s; + struct node *node; + struct node *wnode = NULL; + int err; + + *path = NULL; + + buf = malloc(bufsize); + if (buf == NULL) + return -ENOMEM; + + s = buf + bufsize - 1; + *s = '\0'; + + if (name != NULL) { + s = add_name(&buf, &bufsize, s, name); + err = -ENOMEM; + if (s == NULL) + goto out_free; + } + + if (wnodep) { + assert(ticket); + wnode = lookup_node(f, nodeid, name); + if (wnode) { + if (wnode->treelock != 0 || + (wnode->ticket && wnode->ticket != ticket)) { + if (!wnode->ticket) + wnode->ticket = ticket; + err = -EAGAIN; + goto out_free; + } + wnode->treelock = -1; + wnode->ticket = 0; + } + } + + err = 0; + for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID; + node = node->parent) { + err = -ENOENT; + if (node->name == NULL || node->parent == NULL) + goto out_unlock; + + err = -ENOMEM; + s = add_name(&buf, &bufsize, s, node->name); + if (s == NULL) + goto out_unlock; + + if (ticket) { + err = -EAGAIN; + if (node->treelock == -1 || + (node->ticket && node->ticket != ticket)) + goto out_unlock; + + node->treelock++; + node->ticket = 0; + } + } + + if (s[0]) + memmove(buf, s, bufsize - (s - buf)); + else + strcpy(buf, "/"); + + *path = buf; + if (wnodep) + *wnodep = wnode; + + return 0; + + out_unlock: + if (ticket) + unlock_path(f, nodeid, wnode, node, ticket); + out_free: + free(buf); + + return err; +} + +static void wake_up_first(struct fuse *f) +{ + if (f->lockq) + pthread_cond_signal(&f->lockq->cond); +} + +static void wake_up_next(struct lock_queue_element *qe) +{ + if (qe->next) + pthread_cond_signal(&qe->next->cond); +} + +static int get_ticket(struct fuse *f) +{ + do f->curr_ticket++; + while (f->curr_ticket == 0); + + return f->curr_ticket; +} + +static void debug_path(struct fuse *f, const char *msg, fuse_ino_t nodeid, + const char *name, int wr) +{ + if (f->conf.debug) { + struct node *wnode = NULL; + + if (wr) + wnode = lookup_node(f, nodeid, name); + + if (wnode) + fprintf(stderr, "%s %li (w)\n", msg, wnode->nodeid); + else + fprintf(stderr, "%s %li\n", msg, nodeid); + } +} + +static void queue_path(struct fuse *f, struct lock_queue_element *qe, + fuse_ino_t nodeid, const char *name, int wr) +{ + struct lock_queue_element **qp; + + debug_path(f, "QUEUE PATH", nodeid, name, wr); + pthread_cond_init(&qe->cond, NULL); + qe->next = NULL; + for (qp = &f->lockq; *qp != NULL; qp = &(*qp)->next); + *qp = qe; +} + +static void dequeue_path(struct fuse *f, struct lock_queue_element *qe, + fuse_ino_t nodeid, const char *name, int wr) +{ + struct lock_queue_element **qp; + + debug_path(f, "DEQUEUE PATH", nodeid, name, wr); + pthread_cond_destroy(&qe->cond); + for (qp = &f->lockq; *qp != qe; qp = &(*qp)->next); + *qp = qe->next; +} + +static void wait_on_path(struct fuse *f, struct lock_queue_element *qe, + fuse_ino_t nodeid, const char *name, int wr) +{ + debug_path(f, "WAIT ON PATH", nodeid, name, wr); + pthread_cond_wait(&qe->cond, &f->lock); +} + +static int get_path_common(struct fuse *f, fuse_ino_t nodeid, const char *name, + char **path, struct node **wnode) +{ + int err; + int ticket; + + pthread_mutex_lock(&f->lock); + ticket = get_ticket(f); + err = try_get_path(f, nodeid, name, path, wnode, ticket); + if (err == -EAGAIN) { + struct lock_queue_element qe; + + queue_path(f, &qe, nodeid, name, !!wnode); + do { + wait_on_path(f, &qe, nodeid, name, !!wnode); + err = try_get_path(f, nodeid, name, path, wnode, + ticket); + wake_up_next(&qe); + } while (err == -EAGAIN); + dequeue_path(f, &qe, nodeid, name, !!wnode); + } + pthread_mutex_unlock(&f->lock); + + return err; +} + +static int get_path(struct fuse *f, fuse_ino_t nodeid, char **path) +{ + return get_path_common(f, nodeid, NULL, path, NULL); +} + +static int get_path_nullok(struct fuse *f, fuse_ino_t nodeid, char **path) +{ + int err = get_path_common(f, nodeid, NULL, path, NULL); + + if (err == -ENOENT && f->nullpath_ok) + err = 0; + + return err; +} + +static int get_path_name(struct fuse *f, fuse_ino_t nodeid, const char *name, + char **path) +{ + return get_path_common(f, nodeid, name, path, NULL); +} + +static int get_path_wrlock(struct fuse *f, fuse_ino_t nodeid, const char *name, + char **path, struct node **wnode) +{ + return get_path_common(f, nodeid, name, path, wnode); +} + +static int try_get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1, + fuse_ino_t nodeid2, const char *name2, + char **path1, char **path2, + struct node **wnode1, struct node **wnode2, + int ticket) +{ + int err; + + /* FIXME: locking two paths needs deadlock checking */ + err = try_get_path(f, nodeid1, name1, path1, wnode1, ticket); + if (!err) { + err = try_get_path(f, nodeid2, name2, path2, wnode2, ticket); + if (err) + unlock_path(f, nodeid1, wnode1 ? *wnode1 : NULL, NULL, + ticket); + } + return err; +} + +static int get_path2(struct fuse *f, fuse_ino_t nodeid1, const char *name1, + fuse_ino_t nodeid2, const char *name2, + char **path1, char **path2, + struct node **wnode1, struct node **wnode2) +{ + int err; + int ticket; + + pthread_mutex_lock(&f->lock); + ticket = get_ticket(f); + err = try_get_path2(f, nodeid1, name1, nodeid2, name2, + path1, path2, wnode1, wnode2, ticket); + if (err == -EAGAIN) { + struct lock_queue_element qe; + + queue_path(f, &qe, nodeid1, name1, !!wnode1); + debug_path(f, " path2", nodeid2, name2, !!wnode2); + do { + wait_on_path(f, &qe, nodeid1, name1, !!wnode1); + debug_path(f, " path2", nodeid2, name2, !!wnode2); + err = try_get_path2(f, nodeid1, name1, nodeid2, name2, + path1, path2, wnode1, wnode2, + ticket); + wake_up_next(&qe); + } while (err == -EAGAIN); + dequeue_path(f, &qe, nodeid1, name1, !!wnode1); + debug_path(f, " path2", nodeid2, name2, !!wnode2); + } + pthread_mutex_unlock(&f->lock); + + return err; +} + +static void free_path_wrlock(struct fuse *f, fuse_ino_t nodeid, + struct node *wnode, char *path) +{ + pthread_mutex_lock(&f->lock); + unlock_path(f, nodeid, wnode, NULL, 0); + wake_up_first(f); + pthread_mutex_unlock(&f->lock); + free(path); +} + +static void free_path(struct fuse *f, fuse_ino_t nodeid, char *path) +{ + if (path) + free_path_wrlock(f, nodeid, NULL, path); +} + +static void free_path2(struct fuse *f, fuse_ino_t nodeid1, fuse_ino_t nodeid2, + struct node *wnode1, struct node *wnode2, + char *path1, char *path2) +{ + pthread_mutex_lock(&f->lock); + unlock_path(f, nodeid1, wnode1, NULL, 0); + unlock_path(f, nodeid2, wnode2, NULL, 0); + wake_up_first(f); + pthread_mutex_unlock(&f->lock); + free(path1); + free(path2); +} + +static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup) +{ + struct node *node; + if (nodeid == FUSE_ROOT_ID) + return; + pthread_mutex_lock(&f->lock); + node = get_node(f, nodeid); + + /* + * Node may still be locked due to interrupt idiocy in open, + * create and opendir + */ + while (node->nlookup == nlookup && node->treelock) { + struct lock_queue_element qe; + + queue_path(f, &qe, node->nodeid, NULL, 0); + do { + wait_on_path(f, &qe, node->nodeid, NULL, 0); + wake_up_next(&qe); + + } while (node->nlookup == nlookup && node->treelock); + dequeue_path(f, &qe, node->nodeid, NULL, 0); + } + + assert(node->nlookup >= nlookup); + node->nlookup -= nlookup; + if (!node->nlookup) { + unhash_name(f, node); + unref_node(f, node); + } + pthread_mutex_unlock(&f->lock); +} + +static void unlink_node(struct fuse *f, struct node *node) +{ + if (f->conf.noforget) { + assert(node->nlookup > 1); + node->nlookup--; + } + unhash_name(f, node); +} + +static void remove_node(struct fuse *f, fuse_ino_t dir, const char *name) +{ + struct node *node; + + pthread_mutex_lock(&f->lock); + node = lookup_node(f, dir, name); + if (node != NULL) + unlink_node(f, node); + pthread_mutex_unlock(&f->lock); +} + +static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname, + fuse_ino_t newdir, const char *newname, int hide) +{ + struct node *node; + struct node *newnode; + int err = 0; + + pthread_mutex_lock(&f->lock); + node = lookup_node(f, olddir, oldname); + newnode = lookup_node(f, newdir, newname); + if (node == NULL) + goto out; + + if (newnode != NULL) { + if (hide) { + fprintf(stderr, "fuse: hidden file got created during hiding\n"); + err = -EBUSY; + goto out; + } + unlink_node(f, newnode); + } + + unhash_name(f, node); + if (hash_name(f, node, newdir, newname) == -1) { + err = -ENOMEM; + goto out; + } + + if (hide) + node->is_hidden = 1; + +out: + pthread_mutex_unlock(&f->lock); + return err; +} + +static void set_stat(struct fuse *f, fuse_ino_t nodeid, struct stat *stbuf) +{ + if (!f->conf.use_ino) + stbuf->st_ino = nodeid; + if (f->conf.set_mode) + stbuf->st_mode = (stbuf->st_mode & S_IFMT) | + (0777 & ~f->conf.umask); + if (f->conf.set_uid) + stbuf->st_uid = f->conf.uid; + if (f->conf.set_gid) + stbuf->st_gid = f->conf.gid; +} + +static struct fuse *req_fuse(fuse_req_t req) +{ + return (struct fuse *) fuse_req_userdata(req); +} + +static void fuse_intr_sighandler(int sig) +{ + (void) sig; + /* Nothing to do */ +} + +struct fuse_intr_data { + pthread_t id; + pthread_cond_t cond; + int finished; +}; + +static void fuse_interrupt(fuse_req_t req, void *d_) +{ + struct fuse_intr_data *d = d_; + struct fuse *f = req_fuse(req); + + if (d->id == pthread_self()) + return; + + pthread_mutex_lock(&f->lock); + while (!d->finished) { + struct timeval now; + struct timespec timeout; + + pthread_kill(d->id, f->conf.intr_signal); + gettimeofday(&now, NULL); + timeout.tv_sec = now.tv_sec + 1; + timeout.tv_nsec = now.tv_usec * 1000; + pthread_cond_timedwait(&d->cond, &f->lock, &timeout); + } + pthread_mutex_unlock(&f->lock); +} + +static void fuse_do_finish_interrupt(struct fuse *f, fuse_req_t req, + struct fuse_intr_data *d) +{ + pthread_mutex_lock(&f->lock); + d->finished = 1; + pthread_cond_broadcast(&d->cond); + pthread_mutex_unlock(&f->lock); + fuse_req_interrupt_func(req, NULL, NULL); + pthread_cond_destroy(&d->cond); +} + +static void fuse_do_prepare_interrupt(fuse_req_t req, struct fuse_intr_data *d) +{ + d->id = pthread_self(); + pthread_cond_init(&d->cond, NULL); + d->finished = 0; + fuse_req_interrupt_func(req, fuse_interrupt, d); +} + +static inline void fuse_finish_interrupt(struct fuse *f, fuse_req_t req, + struct fuse_intr_data *d) +{ + if (f->conf.intr) + fuse_do_finish_interrupt(f, req, d); +} + +static inline void fuse_prepare_interrupt(struct fuse *f, fuse_req_t req, + struct fuse_intr_data *d) +{ + if (f->conf.intr) + fuse_do_prepare_interrupt(req, d); +} + +#ifndef __FreeBSD__ + +static int fuse_compat_open(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + int err; + if (!fs->compat || fs->compat >= 25) + err = fs->op.open(path, fi); + else if (fs->compat == 22) { + struct fuse_file_info_compat tmp; + memcpy(&tmp, fi, sizeof(tmp)); + err = ((struct fuse_operations_compat22 *) &fs->op)->open(path, + &tmp); + memcpy(fi, &tmp, sizeof(tmp)); + fi->fh = tmp.fh; + } else + err = ((struct fuse_operations_compat2 *) &fs->op) + ->open(path, fi->flags); + return err; +} + +static int fuse_compat_release(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + if (!fs->compat || fs->compat >= 22) + return fs->op.release(path, fi); + else + return ((struct fuse_operations_compat2 *) &fs->op) + ->release(path, fi->flags); +} + +static int fuse_compat_opendir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + if (!fs->compat || fs->compat >= 25) + return fs->op.opendir(path, fi); + else { + int err; + struct fuse_file_info_compat tmp; + memcpy(&tmp, fi, sizeof(tmp)); + err = ((struct fuse_operations_compat22 *) &fs->op) + ->opendir(path, &tmp); + memcpy(fi, &tmp, sizeof(tmp)); + fi->fh = tmp.fh; + return err; + } +} + +static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf, + struct statvfs *stbuf) +{ + stbuf->f_bsize = compatbuf->block_size; + stbuf->f_blocks = compatbuf->blocks; + stbuf->f_bfree = compatbuf->blocks_free; + stbuf->f_bavail = compatbuf->blocks_free; + stbuf->f_files = compatbuf->files; + stbuf->f_ffree = compatbuf->files_free; + stbuf->f_namemax = compatbuf->namelen; +} + +static void convert_statfs_old(struct statfs *oldbuf, struct statvfs *stbuf) +{ + stbuf->f_bsize = oldbuf->f_bsize; + stbuf->f_blocks = oldbuf->f_blocks; + stbuf->f_bfree = oldbuf->f_bfree; + stbuf->f_bavail = oldbuf->f_bavail; + stbuf->f_files = oldbuf->f_files; + stbuf->f_ffree = oldbuf->f_ffree; + stbuf->f_namemax = oldbuf->f_namelen; +} + +static int fuse_compat_statfs(struct fuse_fs *fs, const char *path, + struct statvfs *buf) +{ + int err; + + if (!fs->compat || fs->compat >= 25) { + err = fs->op.statfs(fs->compat == 25 ? "/" : path, buf); + } else if (fs->compat > 11) { + struct statfs oldbuf; + err = ((struct fuse_operations_compat22 *) &fs->op) + ->statfs("/", &oldbuf); + if (!err) + convert_statfs_old(&oldbuf, buf); + } else { + struct fuse_statfs_compat1 compatbuf; + memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1)); + err = ((struct fuse_operations_compat1 *) &fs->op) + ->statfs(&compatbuf); + if (!err) + convert_statfs_compat(&compatbuf, buf); + } + return err; +} + +#else /* __FreeBSD__ */ + +static inline int fuse_compat_open(struct fuse_fs *fs, char *path, + struct fuse_file_info *fi) +{ + return fs->op.open(path, fi); +} + +static inline int fuse_compat_release(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + return fs->op.release(path, fi); +} + +static inline int fuse_compat_opendir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + return fs->op.opendir(path, fi); +} + +static inline int fuse_compat_statfs(struct fuse_fs *fs, const char *path, + struct statvfs *buf) +{ + return fs->op.statfs(fs->compat == 25 ? "/" : path, buf); +} + +#endif /* __FreeBSD__ */ + +int fuse_fs_getattr(struct fuse_fs *fs, const char *path, struct stat *buf) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.getattr) { + if (fs->debug) + fprintf(stderr, "getattr %s\n", path); + + return fs->op.getattr(path, buf); + } else { + return -ENOSYS; + } +} + +int fuse_fs_fgetattr(struct fuse_fs *fs, const char *path, struct stat *buf, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fgetattr) { + if (fs->debug) + fprintf(stderr, "fgetattr[%llu] %s\n", + (unsigned long long) fi->fh, path); + + return fs->op.fgetattr(path, buf, fi); + } else if (path && fs->op.getattr) { + if (fs->debug) + fprintf(stderr, "getattr %s\n", path); + + return fs->op.getattr(path, buf); + } else { + return -ENOSYS; + } +} + +int fuse_fs_rename(struct fuse_fs *fs, const char *oldpath, + const char *newpath) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.rename) { + if (fs->debug) + fprintf(stderr, "rename %s %s\n", oldpath, newpath); + + return fs->op.rename(oldpath, newpath); + } else { + return -ENOSYS; + } +} + +int fuse_fs_unlink(struct fuse_fs *fs, const char *path) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.unlink) { + if (fs->debug) + fprintf(stderr, "unlink %s\n", path); + + return fs->op.unlink(path); + } else { + return -ENOSYS; + } +} + +int fuse_fs_rmdir(struct fuse_fs *fs, const char *path) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.rmdir) { + if (fs->debug) + fprintf(stderr, "rmdir %s\n", path); + + return fs->op.rmdir(path); + } else { + return -ENOSYS; + } +} + +int fuse_fs_symlink(struct fuse_fs *fs, const char *linkname, const char *path) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.symlink) { + if (fs->debug) + fprintf(stderr, "symlink %s %s\n", linkname, path); + + return fs->op.symlink(linkname, path); + } else { + return -ENOSYS; + } +} + +int fuse_fs_link(struct fuse_fs *fs, const char *oldpath, const char *newpath) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.link) { + if (fs->debug) + fprintf(stderr, "link %s %s\n", oldpath, newpath); + + return fs->op.link(oldpath, newpath); + } else { + return -ENOSYS; + } +} + +int fuse_fs_release(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.release) { + if (fs->debug) + fprintf(stderr, "release%s[%llu] flags: 0x%x\n", + fi->flush ? "+flush" : "", + (unsigned long long) fi->fh, fi->flags); + + return fuse_compat_release(fs, path, fi); + } else { + return 0; + } +} + +int fuse_fs_opendir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.opendir) { + int err; + + if (fs->debug) + fprintf(stderr, "opendir flags: 0x%x %s\n", fi->flags, + path); + + err = fuse_compat_opendir(fs, path, fi); + + if (fs->debug && !err) + fprintf(stderr, " opendir[%lli] flags: 0x%x %s\n", + (unsigned long long) fi->fh, fi->flags, path); + + return err; + } else { + return 0; + } +} + +int fuse_fs_open(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.open) { + int err; + + if (fs->debug) + fprintf(stderr, "open flags: 0x%x %s\n", fi->flags, + path); + + err = fuse_compat_open(fs, path, fi); + + if (fs->debug && !err) + fprintf(stderr, " open[%lli] flags: 0x%x %s\n", + (unsigned long long) fi->fh, fi->flags, path); + + return err; + } else { + return 0; + } +} + +int fuse_fs_read(struct fuse_fs *fs, const char *path, char *buf, size_t size, + off64_t off, struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.read) { + int res; + + if (fs->debug) + fprintf(stderr, + "read[%llu] %lu bytes from %llu flags: 0x%x\n", + (unsigned long long) fi->fh, + (unsigned long) size, (unsigned long long) off, + fi->flags); + + res = fs->op.read(path, buf, size, off, fi); + + if (fs->debug && res >= 0) + fprintf(stderr, " read[%llu] %u bytes from %llu\n", + (unsigned long long) fi->fh, res, + (unsigned long long) off); + if (res > (int) size) + fprintf(stderr, "fuse: read too many bytes\n"); + + return res; + } else { + return -ENOSYS; + } +} + +int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *buf, + size_t size, off64_t off, struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.write) { + int res; + + if (fs->debug) + fprintf(stderr, + "write%s[%llu] %lu bytes to %llu flags: 0x%x\n", + fi->writepage ? "page" : "", + (unsigned long long) fi->fh, + (unsigned long) size, (unsigned long long) off, + fi->flags); + + res = fs->op.write(path, buf, size, off, fi); + + if (fs->debug && res >= 0) + fprintf(stderr, " write%s[%llu] %u bytes to %llu\n", + fi->writepage ? "page" : "", + (unsigned long long) fi->fh, res, + (unsigned long long) off); + if (res > (int) size) + fprintf(stderr, "fuse: wrote too many bytes\n"); + + return res; + } else { + return -ENOSYS; + } +} + +int fuse_fs_fsync(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fsync) { + if (fs->debug) + fprintf(stderr, "fsync[%llu] datasync: %i\n", + (unsigned long long) fi->fh, datasync); + + return fs->op.fsync(path, datasync, fi); + } else { + return -ENOSYS; + } +} + +int fuse_fs_fsyncdir(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.fsyncdir) { + if (fs->debug) + fprintf(stderr, "fsyncdir[%llu] datasync: %i\n", + (unsigned long long) fi->fh, datasync); + + return fs->op.fsyncdir(path, datasync, fi); + } else { + return -ENOSYS; + } +} + +int fuse_fs_flush(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.flush) { + if (fs->debug) + fprintf(stderr, "flush[%llu]\n", + (unsigned long long) fi->fh); + + return fs->op.flush(path, fi); + } else { + return -ENOSYS; + } +} + +int fuse_fs_statfs(struct fuse_fs *fs, const char *path, struct statvfs *buf) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.statfs) { + if (fs->debug) + fprintf(stderr, "statfs %s\n", path); + + return fuse_compat_statfs(fs, path, buf); + } else { + buf->f_namemax = 255; + buf->f_bsize = 512; + return 0; + } +} + +int fuse_fs_releasedir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.releasedir) { + if (fs->debug) + fprintf(stderr, "releasedir[%llu] flags: 0x%x\n", + (unsigned long long) fi->fh, fi->flags); + + return fs->op.releasedir(path, fi); + } else { + return 0; + } +} + +static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type, + ino_t ino) +{ + int res; + struct stat stbuf; + + memset(&stbuf, 0, sizeof(stbuf)); + stbuf.st_mode = type << 12; + stbuf.st_ino = ino; + + res = dh->filler(dh->buf, name, &stbuf, 0); + return res ? -ENOMEM : 0; +} + +int fuse_fs_readdir(struct fuse_fs *fs, const char *path, void *buf, + fuse_fill_dir_t filler, off64_t off, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.readdir) { + if (fs->debug) + fprintf(stderr, "readdir[%llu] from %llu\n", + (unsigned long long) fi->fh, + (unsigned long long) off); + + return fs->op.readdir(path, buf, filler, off, fi); + } else if (fs->op.getdir) { + struct fuse_dirhandle dh; + + if (fs->debug) + fprintf(stderr, "getdir[%llu]\n", + (unsigned long long) fi->fh); + + dh.filler = filler; + dh.buf = buf; + return fs->op.getdir(path, &dh, fill_dir_old); + } else { + return -ENOSYS; + } +} + +int fuse_fs_create(struct fuse_fs *fs, const char *path, mode_t mode, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.create) { + int err; + + if (fs->debug) + fprintf(stderr, + "create flags: 0x%x %s 0%o umask=0%03o\n", + fi->flags, path, mode, + fuse_get_context()->umask); + + err = fs->op.create(path, mode, fi); + + if (fs->debug && !err) + fprintf(stderr, " create[%llu] flags: 0x%x %s\n", + (unsigned long long) fi->fh, fi->flags, path); + + return err; + } else { + return -ENOSYS; + } +} + +int fuse_fs_lock(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, int cmd, struct flock *lock) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.lock) { + if (fs->debug) + fprintf(stderr, "lock[%llu] %s %s start: %llu len: %llu pid: %llu\n", + (unsigned long long) fi->fh, + (cmd == F_GETLK ? "F_GETLK" : + (cmd == F_SETLK ? "F_SETLK" : + (cmd == F_SETLKW ? "F_SETLKW" : "???"))), + (lock->l_type == F_RDLCK ? "F_RDLCK" : + (lock->l_type == F_WRLCK ? "F_WRLCK" : + (lock->l_type == F_UNLCK ? "F_UNLCK" : + "???"))), + (unsigned long long) lock->l_start, + (unsigned long long) lock->l_len, + (unsigned long long) lock->l_pid); + + return fs->op.lock(path, fi, cmd, lock); + } else { + return -ENOSYS; + } +} + +int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.chown) { + if (fs->debug) + fprintf(stderr, "chown %s %lu %lu\n", path, + (unsigned long) uid, (unsigned long) gid); + + return fs->op.chown(path, uid, gid); + } else { + return -ENOSYS; + } +} + +int fuse_fs_truncate(struct fuse_fs *fs, const char *path, off64_t size) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.truncate) { + if (fs->debug) + fprintf(stderr, "truncate %s %llu\n", path, + (unsigned long long) size); + + return fs->op.truncate(path, size); + } else { + return -ENOSYS; + } +} + +int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off64_t size, + struct fuse_file_info *fi) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.ftruncate) { + if (fs->debug) + fprintf(stderr, "ftruncate[%llu] %s %llu\n", + (unsigned long long) fi->fh, path, + (unsigned long long) size); + + return fs->op.ftruncate(path, size, fi); + } else if (path && fs->op.truncate) { + if (fs->debug) + fprintf(stderr, "truncate %s %llu\n", path, + (unsigned long long) size); + + return fs->op.truncate(path, size); + } else { + return -ENOSYS; + } +} + +int fuse_fs_utimens(struct fuse_fs *fs, const char *path, + const struct timespec tv[2]) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.utimens) { + if (fs->debug) + fprintf(stderr, "utimens %s %li.%09lu %li.%09lu\n", + path, tv[0].tv_sec, tv[0].tv_nsec, + tv[1].tv_sec, tv[1].tv_nsec); + + return fs->op.utimens(path, tv); + } else if(fs->op.utime) { + struct utimbuf buf; + + if (fs->debug) + fprintf(stderr, "utime %s %li %li\n", path, + tv[0].tv_sec, tv[1].tv_sec); + + buf.actime = tv[0].tv_sec; + buf.modtime = tv[1].tv_sec; + return fs->op.utime(path, &buf); + } else { + return -ENOSYS; + } +} + +int fuse_fs_access(struct fuse_fs *fs, const char *path, int mask) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.access) { + if (fs->debug) + fprintf(stderr, "access %s 0%o\n", path, mask); + + return fs->op.access(path, mask); + } else { + return -ENOSYS; + } +} + +int fuse_fs_readlink(struct fuse_fs *fs, const char *path, char *buf, + size_t len) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.readlink) { + if (fs->debug) + fprintf(stderr, "readlink %s %lu\n", path, + (unsigned long) len); + + return fs->op.readlink(path, buf, len); + } else { + return -ENOSYS; + } +} + +int fuse_fs_mknod(struct fuse_fs *fs, const char *path, mode_t mode, + dev_t rdev) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.mknod) { + if (fs->debug) + fprintf(stderr, "mknod %s 0%o 0x%llx umask=0%03o\n", + path, mode, (unsigned long long) rdev, + fuse_get_context()->umask); + + return fs->op.mknod(path, mode, rdev); + } else { + return -ENOSYS; + } +} + +int fuse_fs_mkdir(struct fuse_fs *fs, const char *path, mode_t mode) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.mkdir) { + if (fs->debug) + fprintf(stderr, "mkdir %s 0%o umask=0%03o\n", + path, mode, fuse_get_context()->umask); + + return fs->op.mkdir(path, mode); + } else { + return -ENOSYS; + } +} + +int fuse_fs_setxattr(struct fuse_fs *fs, const char *path, const char *name, + const char *value, size_t size, int flags) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.setxattr) { + if (fs->debug) + fprintf(stderr, "setxattr %s %s %lu 0x%x\n", + path, name, (unsigned long) size, flags); + + return fs->op.setxattr(path, name, value, size, flags); + } else { + return -ENOSYS; + } +} + +int fuse_fs_getxattr(struct fuse_fs *fs, const char *path, const char *name, + char *value, size_t size) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.getxattr) { + if (fs->debug) + fprintf(stderr, "getxattr %s %s %lu\n", + path, name, (unsigned long) size); + + return fs->op.getxattr(path, name, value, size); + } else { + return -ENOSYS; + } +} + +int fuse_fs_listxattr(struct fuse_fs *fs, const char *path, char *list, + size_t size) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.listxattr) { + if (fs->debug) + fprintf(stderr, "listxattr %s %lu\n", + path, (unsigned long) size); + + return fs->op.listxattr(path, list, size); + } else { + return -ENOSYS; + } +} + +int fuse_fs_bmap(struct fuse_fs *fs, const char *path, size_t blocksize, + uint64_t *idx) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.bmap) { + if (fs->debug) + fprintf(stderr, "bmap %s blocksize: %lu index: %llu\n", + path, (unsigned long) blocksize, + (unsigned long long) *idx); + + return fs->op.bmap(path, blocksize, idx); + } else { + return -ENOSYS; + } +} + +int fuse_fs_removexattr(struct fuse_fs *fs, const char *path, const char *name) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.removexattr) { + if (fs->debug) + fprintf(stderr, "removexattr %s %s\n", path, name); + + return fs->op.removexattr(path, name); + } else { + return -ENOSYS; + } +} + +int fuse_fs_ioctl(struct fuse_fs *fs, const char *path, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, void *data) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.ioctl) { + if (fs->debug) + fprintf(stderr, "ioctl[%llu] 0x%x flags: 0x%x\n", + (unsigned long long) fi->fh, cmd, flags); + + return fs->op.ioctl(path, cmd, arg, fi, flags, data); + } else + return -ENOSYS; +} + +int fuse_fs_poll(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, struct fuse_pollhandle *ph, + unsigned *reventsp) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.poll) { + int res; + + if (fs->debug) + fprintf(stderr, "poll[%llu] ph: %p\n", + (unsigned long long) fi->fh, ph); + + res = fs->op.poll(path, fi, ph, reventsp); + + if (fs->debug && !res) + fprintf(stderr, " poll[%llu] revents: 0x%x\n", + (unsigned long long) fi->fh, *reventsp); + + return res; + } else + return -ENOSYS; +} + +static int is_open(struct fuse *f, fuse_ino_t dir, const char *name) +{ + struct node *node; + int isopen = 0; + pthread_mutex_lock(&f->lock); + node = lookup_node(f, dir, name); + if (node && node->open_count > 0) + isopen = 1; + pthread_mutex_unlock(&f->lock); + return isopen; +} + +static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname, + char *newname, size_t bufsize) +{ + struct stat buf; + struct node *node; + struct node *newnode; + char *newpath; + int res; + int failctr = 10; + + do { + pthread_mutex_lock(&f->lock); + node = lookup_node(f, dir, oldname); + if (node == NULL) { + pthread_mutex_unlock(&f->lock); + return NULL; + } + do { + f->hidectr ++; + snprintf(newname, bufsize, ".fuse_hidden%08x%08x", + (unsigned int) node->nodeid, f->hidectr); + newnode = lookup_node(f, dir, newname); + } while(newnode); + + try_get_path(f, dir, newname, &newpath, NULL, 0); + pthread_mutex_unlock(&f->lock); + + if (!newpath) + break; + + res = fuse_fs_getattr(f->fs, newpath, &buf); + if (res == -ENOENT) + break; + free(newpath); + newpath = NULL; + } while(res == 0 && --failctr); + + return newpath; +} + +static int hide_node(struct fuse *f, const char *oldpath, + fuse_ino_t dir, const char *oldname) +{ + char newname[64]; + char *newpath; + int err = -EBUSY; + + newpath = hidden_name(f, dir, oldname, newname, sizeof(newname)); + if (newpath) { + err = fuse_fs_rename(f->fs, oldpath, newpath); + if (!err) + err = rename_node(f, dir, oldname, dir, newname, 1); + free(newpath); + } + return err; +} + +static int mtime_eq(const struct stat *stbuf, const struct timespec *ts) +{ + return stbuf->st_mtime == ts->tv_sec && + ST_MTIM_NSEC(stbuf) == ts->tv_nsec; +} + +#ifndef CLOCK_MONOTONIC +#define CLOCK_MONOTONIC CLOCK_REALTIME +#endif + +static void curr_time(struct timespec *now) +{ + static clockid_t clockid = CLOCK_MONOTONIC; + int res = clock_gettime(clockid, now); + if (res == -1 && errno == EINVAL) { + clockid = CLOCK_REALTIME; + res = clock_gettime(clockid, now); + } + if (res == -1) { + perror("fuse: clock_gettime"); + abort(); + } +} + +static void update_stat(struct node *node, const struct stat *stbuf) +{ + if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) || + stbuf->st_size != node->size)) + node->cache_valid = 0; + node->mtime.tv_sec = stbuf->st_mtime; + node->mtime.tv_nsec = ST_MTIM_NSEC(stbuf); + node->size = stbuf->st_size; + curr_time(&node->stat_updated); +} + +static int lookup_path(struct fuse *f, fuse_ino_t nodeid, + const char *name, const char *path, + struct fuse_entry_param *e, struct fuse_file_info *fi) +{ + int res; + + memset(e, 0, sizeof(struct fuse_entry_param)); + if (fi) + res = fuse_fs_fgetattr(f->fs, path, &e->attr, fi); + else + res = fuse_fs_getattr(f->fs, path, &e->attr); + if (res == 0) { + struct node *node; + + node = find_node(f, nodeid, name); + if (node == NULL) + res = -ENOMEM; + else { + e->ino = node->nodeid; + e->generation = node->generation; + e->entry_timeout = f->conf.entry_timeout; + e->attr_timeout = f->conf.attr_timeout; + if (f->conf.auto_cache) { + pthread_mutex_lock(&f->lock); + update_stat(node, &e->attr); + pthread_mutex_unlock(&f->lock); + } + set_stat(f, e->ino, &e->attr); + if (f->conf.debug) + fprintf(stderr, " NODEID: %lu\n", + (unsigned long) e->ino); + } + } + return res; +} + +static struct fuse_context_i *fuse_get_context_internal(void) +{ + struct fuse_context_i *c; + + c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key); + if (c == NULL) { + c = (struct fuse_context_i *) + malloc(sizeof(struct fuse_context_i)); + if (c == NULL) { + /* This is hard to deal with properly, so just + abort. If memory is so low that the + context cannot be allocated, there's not + much hope for the filesystem anyway */ + fprintf(stderr, "fuse: failed to allocate thread specific data\n"); + abort(); + } + pthread_setspecific(fuse_context_key, c); + } + return c; +} + +static void fuse_freecontext(void *data) +{ + free(data); +} + +static int fuse_create_context_key(void) +{ + int err = 0; + pthread_mutex_lock(&fuse_context_lock); + if (!fuse_context_ref) { + err = pthread_key_create(&fuse_context_key, fuse_freecontext); + if (err) { + fprintf(stderr, "fuse: failed to create thread specific key: %s\n", + strerror(err)); + pthread_mutex_unlock(&fuse_context_lock); + return -1; + } + } + fuse_context_ref++; + pthread_mutex_unlock(&fuse_context_lock); + return 0; +} + +static void fuse_delete_context_key(void) +{ + pthread_mutex_lock(&fuse_context_lock); + fuse_context_ref--; + if (!fuse_context_ref) { + free(pthread_getspecific(fuse_context_key)); + pthread_key_delete(fuse_context_key); + } + pthread_mutex_unlock(&fuse_context_lock); +} + +static struct fuse *req_fuse_prepare(fuse_req_t req) +{ + struct fuse_context_i *c = fuse_get_context_internal(); + const struct fuse_ctx *ctx = fuse_req_ctx(req); + c->req = req; + c->ctx.fuse = req_fuse(req); + c->ctx.uid = ctx->uid; + c->ctx.gid = ctx->gid; + c->ctx.pid = ctx->pid; + c->ctx.umask = ctx->umask; + return c->ctx.fuse; +} + +static inline void reply_err(fuse_req_t req, int err) +{ + /* fuse_reply_err() uses non-negated errno values */ + fuse_reply_err(req, -err); +} + +static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e, + int err) +{ + if (!err) { + struct fuse *f = req_fuse(req); + if (fuse_reply_entry(req, e) == -ENOENT) { + /* Skip forget for negative result */ + if (e->ino != 0) + forget_node(f, e->ino, 1); + } + } else + reply_err(req, err); +} + +void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.init) + fs->user_data = fs->op.init(conn); +} + +static void fuse_lib_init(void *data, struct fuse_conn_info *conn) +{ + struct fuse *f = (struct fuse *) data; + struct fuse_context_i *c = fuse_get_context_internal(); + + memset(c, 0, sizeof(*c)); + c->ctx.fuse = f; + conn->want |= FUSE_CAP_EXPORT_SUPPORT; + fuse_fs_init(f->fs, conn); +} + +void fuse_fs_destroy(struct fuse_fs *fs) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.destroy) + fs->op.destroy(fs->user_data); + if (fs->m) + fuse_put_module(fs->m); + free(fs); +} + +static void fuse_lib_destroy(void *data) +{ + struct fuse *f = (struct fuse *) data; + struct fuse_context_i *c = fuse_get_context_internal(); + + memset(c, 0, sizeof(*c)); + c->ctx.fuse = f; + fuse_fs_destroy(f->fs); + f->fs = NULL; +} + +static void fuse_lib_lookup(fuse_req_t req, fuse_ino_t parent, + const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + struct node *dot = NULL; + + if (name[0] == '.') { + int len = strlen(name); + + if (len == 1 || (name[1] == '.' && len == 2)) { + pthread_mutex_lock(&f->lock); + if (len == 1) { + if (f->conf.debug) + fprintf(stderr, "LOOKUP-DOT\n"); + dot = get_node_nocheck(f, parent); + if (dot == NULL) { + pthread_mutex_unlock(&f->lock); + reply_entry(req, &e, -ESTALE); + return; + } + dot->refctr++; + } else { + if (f->conf.debug) + fprintf(stderr, "LOOKUP-DOTDOT\n"); + parent = get_node(f, parent)->parent->nodeid; + } + pthread_mutex_unlock(&f->lock); + name = NULL; + } + } + + err = get_path_name(f, parent, name, &path); + if (!err) { + struct fuse_intr_data d; + if (f->conf.debug) + fprintf(stderr, "LOOKUP %s\n", path); + fuse_prepare_interrupt(f, req, &d); + err = lookup_path(f, parent, name, path, &e, NULL); + if (err == -ENOENT && f->conf.negative_timeout != 0.0) { + e.ino = 0; + e.entry_timeout = f->conf.negative_timeout; + err = 0; + } + fuse_finish_interrupt(f, req, &d); + free_path(f, parent, path); + } + if (dot) { + pthread_mutex_lock(&f->lock); + unref_node(f, dot); + pthread_mutex_unlock(&f->lock); + } + reply_entry(req, &e, err); +} + +static void fuse_lib_forget(fuse_req_t req, fuse_ino_t ino, + unsigned long nlookup) +{ + struct fuse *f = req_fuse(req); + if (f->conf.debug) + fprintf(stderr, "FORGET %llu/%lu\n", (unsigned long long)ino, + nlookup); + forget_node(f, ino, nlookup); + fuse_reply_none(req); +} + +static void fuse_lib_getattr(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct stat buf; + char *path; + int err; + + memset(&buf, 0, sizeof(buf)); + + if (fi != NULL) + err = get_path_nullok(f, ino, &path); + else + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + if (fi) + err = fuse_fs_fgetattr(f->fs, path, &buf, fi); + else + err = fuse_fs_getattr(f->fs, path, &buf); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + if (!err) { + if (f->conf.auto_cache) { + pthread_mutex_lock(&f->lock); + update_stat(get_node(f, ino), &buf); + pthread_mutex_unlock(&f->lock); + } + set_stat(f, ino, &buf); + fuse_reply_attr(req, &buf, f->conf.attr_timeout); + } else + reply_err(req, err); +} + +int fuse_fs_chmod(struct fuse_fs *fs, const char *path, mode_t mode) +{ + fuse_get_context()->private_data = fs->user_data; + if (fs->op.chmod) + return fs->op.chmod(path, mode); + else + return -ENOSYS; +} + +static void fuse_lib_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int valid, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct stat buf; + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = 0; + if (!err && (valid & FUSE_SET_ATTR_MODE)) + err = fuse_fs_chmod(f->fs, path, attr->st_mode); + if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) { + uid_t uid = (valid & FUSE_SET_ATTR_UID) ? + attr->st_uid : (uid_t) -1; + gid_t gid = (valid & FUSE_SET_ATTR_GID) ? + attr->st_gid : (gid_t) -1; + err = fuse_fs_chown(f->fs, path, uid, gid); + } + if (!err && (valid & FUSE_SET_ATTR_SIZE)) { + if (fi) + err = fuse_fs_ftruncate(f->fs, path, + attr->st_size, fi); + else + err = fuse_fs_truncate(f->fs, path, + attr->st_size); + } + if (!err && + (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == + (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) { + struct timespec tv[2]; + tv[0].tv_sec = attr->st_atime; + tv[0].tv_nsec = ST_ATIM_NSEC(attr); + tv[1].tv_sec = attr->st_mtime; + tv[1].tv_nsec = ST_MTIM_NSEC(attr); + err = fuse_fs_utimens(f->fs, path, tv); + } + if (!err) + err = fuse_fs_getattr(f->fs, path, &buf); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + if (!err) { + if (f->conf.auto_cache) { + pthread_mutex_lock(&f->lock); + update_stat(get_node(f, ino), &buf); + pthread_mutex_unlock(&f->lock); + } + set_stat(f, ino, &buf); + fuse_reply_attr(req, &buf, f->conf.attr_timeout); + } else + reply_err(req, err); +} + +static void fuse_lib_access(fuse_req_t req, fuse_ino_t ino, int mask) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_access(f->fs, path, mask); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + +static void fuse_lib_readlink(fuse_req_t req, fuse_ino_t ino) +{ + struct fuse *f = req_fuse_prepare(req); + char linkname[PATH_MAX + 1]; + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_readlink(f->fs, path, linkname, sizeof(linkname)); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + if (!err) { + linkname[PATH_MAX] = '\0'; + fuse_reply_readlink(req, linkname); + } else + reply_err(req, err); +} + +static void fuse_lib_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = get_path_name(f, parent, name, &path); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = -ENOSYS; + if (S_ISREG(mode)) { + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = O_CREAT | O_EXCL | O_WRONLY; + err = fuse_fs_create(f->fs, path, mode, &fi); + if (!err) { + err = lookup_path(f, parent, name, path, &e, + &fi); + fuse_fs_release(f->fs, path, &fi); + } + } + if (err == -ENOSYS) { + err = fuse_fs_mknod(f->fs, path, mode, rdev); + if (!err) + err = lookup_path(f, parent, name, path, &e, + NULL); + } + fuse_finish_interrupt(f, req, &d); + free_path(f, parent, path); + } + reply_entry(req, &e, err); +} + +static void fuse_lib_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = get_path_name(f, parent, name, &path); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_mkdir(f->fs, path, mode); + if (!err) + err = lookup_path(f, parent, name, path, &e, NULL); + fuse_finish_interrupt(f, req, &d); + free_path(f, parent, path); + } + reply_entry(req, &e, err); +} + +static void fuse_lib_unlink(fuse_req_t req, fuse_ino_t parent, + const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + struct node *wnode; + char *path; + int err; + + err = get_path_wrlock(f, parent, name, &path, &wnode); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + if (!f->conf.hard_remove && is_open(f, parent, name)) { + err = hide_node(f, path, parent, name); + } else { + err = fuse_fs_unlink(f->fs, path); + if (!err) + remove_node(f, parent, name); + } + fuse_finish_interrupt(f, req, &d); + free_path_wrlock(f, parent, wnode, path); + } + reply_err(req, err); +} + +static void fuse_lib_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + struct node *wnode; + char *path; + int err; + + err = get_path_wrlock(f, parent, name, &path, &wnode); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_rmdir(f->fs, path); + fuse_finish_interrupt(f, req, &d); + if (!err) + remove_node(f, parent, name); + free_path_wrlock(f, parent, wnode, path); + } + reply_err(req, err); +} + +static void fuse_lib_symlink(fuse_req_t req, const char *linkname, + fuse_ino_t parent, const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *path; + int err; + + err = get_path_name(f, parent, name, &path); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_symlink(f->fs, linkname, path); + if (!err) + err = lookup_path(f, parent, name, path, &e, NULL); + fuse_finish_interrupt(f, req, &d); + free_path(f, parent, path); + } + reply_entry(req, &e, err); +} + +static void fuse_lib_rename(fuse_req_t req, fuse_ino_t olddir, + const char *oldname, fuse_ino_t newdir, + const char *newname) +{ + struct fuse *f = req_fuse_prepare(req); + char *oldpath; + char *newpath; + struct node *wnode1; + struct node *wnode2; + int err; + + err = get_path2(f, olddir, oldname, newdir, newname, + &oldpath, &newpath, &wnode1, &wnode2); + if (!err) { + struct fuse_intr_data d; + err = 0; + fuse_prepare_interrupt(f, req, &d); + if (!f->conf.hard_remove && is_open(f, newdir, newname)) + err = hide_node(f, newpath, newdir, newname); + if (!err) { + err = fuse_fs_rename(f->fs, oldpath, newpath); + if (!err) + err = rename_node(f, olddir, oldname, newdir, + newname, 0); + } + fuse_finish_interrupt(f, req, &d); + free_path2(f, olddir, newdir, wnode1, wnode2, oldpath, newpath); + } + reply_err(req, err); +} + +static void fuse_lib_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, + const char *newname) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_entry_param e; + char *oldpath; + char *newpath; + int err; + + err = get_path2(f, ino, NULL, newparent, newname, + &oldpath, &newpath, NULL, NULL); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_link(f->fs, oldpath, newpath); + if (!err) + err = lookup_path(f, newparent, newname, newpath, + &e, NULL); + fuse_finish_interrupt(f, req, &d); + free_path2(f, ino, newparent, NULL, NULL, oldpath, newpath); + } + reply_entry(req, &e, err); +} + +static void fuse_do_release(struct fuse *f, fuse_ino_t ino, const char *path, + struct fuse_file_info *fi) +{ + struct node *node; + int unlink_hidden = 0; + + fuse_fs_release(f->fs, (path || f->nullpath_ok) ? path : "-", fi); + + pthread_mutex_lock(&f->lock); + node = get_node(f, ino); + assert(node->open_count > 0); + --node->open_count; + if (node->is_hidden && !node->open_count) { + unlink_hidden = 1; + node->is_hidden = 0; + } + pthread_mutex_unlock(&f->lock); + + if(unlink_hidden && path) + fuse_fs_unlink(f->fs, path); +} + +static void fuse_lib_create(fuse_req_t req, fuse_ino_t parent, + const char *name, mode_t mode, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_entry_param e; + char *path; + int err; + + err = get_path_name(f, parent, name, &path); + if (!err) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_create(f->fs, path, mode, fi); + if (!err) { + err = lookup_path(f, parent, name, path, &e, fi); + if (err) + fuse_fs_release(f->fs, path, fi); + else if (!S_ISREG(e.attr.st_mode)) { + err = -EIO; + fuse_fs_release(f->fs, path, fi); + forget_node(f, e.ino, 1); + } else { + if (f->conf.direct_io) + fi->direct_io = 1; + if (f->conf.kernel_cache) + fi->keep_cache = 1; + + } + } + fuse_finish_interrupt(f, req, &d); + } + if (!err) { + pthread_mutex_lock(&f->lock); + get_node(f, e.ino)->open_count++; + pthread_mutex_unlock(&f->lock); + if (fuse_reply_create(req, &e, fi) == -ENOENT) { + /* The open syscall was interrupted, so it + must be cancelled */ + fuse_prepare_interrupt(f, req, &d); + fuse_do_release(f, e.ino, path, fi); + fuse_finish_interrupt(f, req, &d); + forget_node(f, e.ino, 1); + } + } else { + reply_err(req, err); + } + + free_path(f, parent, path); +} + +static double diff_timespec(const struct timespec *t1, + const struct timespec *t2) +{ + return (t1->tv_sec - t2->tv_sec) + + ((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0; +} + +static void open_auto_cache(struct fuse *f, fuse_ino_t ino, const char *path, + struct fuse_file_info *fi) +{ + struct node *node; + + pthread_mutex_lock(&f->lock); + node = get_node(f, ino); + if (node->cache_valid) { + struct timespec now; + + curr_time(&now); + if (diff_timespec(&now, &node->stat_updated) > + f->conf.ac_attr_timeout) { + struct stat stbuf; + int err; + pthread_mutex_unlock(&f->lock); + err = fuse_fs_fgetattr(f->fs, path, &stbuf, fi); + pthread_mutex_lock(&f->lock); + if (!err) + update_stat(node, &stbuf); + else + node->cache_valid = 0; + } + } + if (node->cache_valid) + fi->keep_cache = 1; + + node->cache_valid = 1; + pthread_mutex_unlock(&f->lock); +} + +static void fuse_lib_open(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_open(f->fs, path, fi); + if (!err) { + if (f->conf.direct_io) + fi->direct_io = 1; + if (f->conf.kernel_cache) + fi->keep_cache = 1; + + if (f->conf.auto_cache) + open_auto_cache(f, ino, path, fi); + } + fuse_finish_interrupt(f, req, &d); + } + if (!err) { + pthread_mutex_lock(&f->lock); + get_node(f, ino)->open_count++; + pthread_mutex_unlock(&f->lock); + if (fuse_reply_open(req, fi) == -ENOENT) { + /* The open syscall was interrupted, so it + must be cancelled */ + fuse_prepare_interrupt(f, req, &d); + fuse_do_release(f, ino, path, fi); + fuse_finish_interrupt(f, req, &d); + } + } else + reply_err(req, err); + + free_path(f, ino, path); +} + +static void fuse_lib_read(fuse_req_t req, fuse_ino_t ino, size_t size, + off64_t off, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + char *buf; + int res; + + buf = (char *) malloc(size); + if (buf == NULL) { + reply_err(req, -ENOMEM); + return; + } + + res = get_path_nullok(f, ino, &path); + if (res == 0) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + res = fuse_fs_read(f->fs, path, buf, size, off, fi); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + + if (res >= 0) + fuse_reply_buf(req, buf, res); + else + reply_err(req, res); + + free(buf); +} + +static void fuse_lib_write(fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off64_t off, struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int res; + + res = get_path_nullok(f, ino, &path); + if (res == 0) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + res = fuse_fs_write(f->fs, path, buf, size, off, fi); + printf("died\n"); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + + if (res >= 0) + fuse_reply_write(req, res); + else + reply_err(req, res); +} + +static void fuse_lib_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = get_path_nullok(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_fsync(f->fs, path, datasync, fi); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + +static struct fuse_dh *get_dirhandle(const struct fuse_file_info *llfi, + struct fuse_file_info *fi) +{ + struct fuse_dh *dh = (struct fuse_dh *) (uintptr_t) llfi->fh; + memset(fi, 0, sizeof(struct fuse_file_info)); + fi->fh = dh->fh; + fi->fh_old = dh->fh; + return dh; +} + +static void fuse_lib_opendir(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_dh *dh; + struct fuse_file_info fi; + char *path; + int err; + + dh = (struct fuse_dh *) malloc(sizeof(struct fuse_dh)); + if (dh == NULL) { + reply_err(req, -ENOMEM); + return; + } + memset(dh, 0, sizeof(struct fuse_dh)); + dh->fuse = f; + dh->contents = NULL; + dh->len = 0; + dh->filled = 0; + dh->nodeid = ino; + fuse_mutex_init(&dh->lock); + + llfi->fh = (uintptr_t) dh; + + memset(&fi, 0, sizeof(fi)); + fi.flags = llfi->flags; + + err = get_path(f, ino, &path); + if (!err) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_opendir(f->fs, path, &fi); + fuse_finish_interrupt(f, req, &d); + dh->fh = fi.fh; + } + if (!err) { + if (fuse_reply_open(req, llfi) == -ENOENT) { + /* The opendir syscall was interrupted, so it + must be cancelled */ + fuse_prepare_interrupt(f, req, &d); + fuse_fs_releasedir(f->fs, path, &fi); + fuse_finish_interrupt(f, req, &d); + pthread_mutex_destroy(&dh->lock); + free(dh); + } + } else { + reply_err(req, err); + pthread_mutex_destroy(&dh->lock); + free(dh); + } + free_path(f, ino, path); +} + +static int extend_contents(struct fuse_dh *dh, unsigned minsize) +{ + if (minsize > dh->size) { + char *newptr; + unsigned newsize = dh->size; + if (!newsize) + newsize = 1024; + while (newsize < minsize) { + if (newsize >= 0x80000000) + newsize = 0xffffffff; + else + newsize *= 2; + } + + newptr = (char *) realloc(dh->contents, newsize); + if (!newptr) { + dh->error = -ENOMEM; + return -1; + } + dh->contents = newptr; + dh->size = newsize; + } + return 0; +} + +static int fill_dir(void *dh_, const char *name, const struct stat *statp, + off64_t off) +{ + struct fuse_dh *dh = (struct fuse_dh *) dh_; + struct stat stbuf; + size_t newlen; + + if (statp) + stbuf = *statp; + else { + memset(&stbuf, 0, sizeof(stbuf)); + stbuf.st_ino = FUSE_UNKNOWN_INO; + } + + if (!dh->fuse->conf.use_ino) { + stbuf.st_ino = FUSE_UNKNOWN_INO; + if (dh->fuse->conf.readdir_ino) { + struct node *node; + pthread_mutex_lock(&dh->fuse->lock); + node = lookup_node(dh->fuse, dh->nodeid, name); + if (node) + stbuf.st_ino = (ino_t) node->nodeid; + pthread_mutex_unlock(&dh->fuse->lock); + } + } + + if (off) { + if (extend_contents(dh, dh->needlen) == -1) + return 1; + + dh->filled = 0; + newlen = dh->len + + fuse_add_direntry(dh->req, dh->contents + dh->len, + dh->needlen - dh->len, name, + &stbuf, off); + if (newlen > dh->needlen) + return 1; + } else { + newlen = dh->len + + fuse_add_direntry(dh->req, NULL, 0, name, NULL, 0); + if (extend_contents(dh, newlen) == -1) + return 1; + + fuse_add_direntry(dh->req, dh->contents + dh->len, + dh->size - dh->len, name, &stbuf, newlen); + } + dh->len = newlen; + return 0; +} + +static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + size_t size, off64_t off, struct fuse_dh *dh, + struct fuse_file_info *fi) +{ + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + + dh->len = 0; + dh->error = 0; + dh->needlen = size; + dh->filled = 1; + dh->req = req; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_readdir(f->fs, path, dh, fill_dir, off, fi); + fuse_finish_interrupt(f, req, &d); + dh->req = NULL; + if (!err) + err = dh->error; + if (err) + dh->filled = 0; + free_path(f, ino, path); + } + return err; +} + +static void fuse_lib_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, + off64_t off, struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_file_info fi; + struct fuse_dh *dh = get_dirhandle(llfi, &fi); + + pthread_mutex_lock(&dh->lock); + /* According to SUS, directory contents need to be refreshed on + rewinddir() */ + if (!off) + dh->filled = 0; + + if (!dh->filled) { + int err = readdir_fill(f, req, ino, size, off, dh, &fi); + if (err) { + reply_err(req, err); + goto out; + } + } + if (dh->filled) { + if (off < dh->len) { + if (off + size > dh->len) + size = dh->len - off; + } else + size = 0; + } else { + size = dh->len; + off = 0; + } + fuse_reply_buf(req, dh->contents + off, size); +out: + pthread_mutex_unlock(&dh->lock); +} + +static void fuse_lib_releasedir(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + struct fuse_file_info fi; + struct fuse_dh *dh = get_dirhandle(llfi, &fi); + char *path; + + get_path(f, ino, &path); + fuse_prepare_interrupt(f, req, &d); + fuse_fs_releasedir(f->fs, (path || f->nullpath_ok) ? path : "-", &fi); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + + pthread_mutex_lock(&dh->lock); + pthread_mutex_unlock(&dh->lock); + pthread_mutex_destroy(&dh->lock); + free(dh->contents); + free(dh); + reply_err(req, 0); +} + +static void fuse_lib_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *llfi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_file_info fi; + char *path; + int err; + + get_dirhandle(llfi, &fi); + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_fsyncdir(f->fs, path, datasync, &fi); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + +static void fuse_lib_statfs(fuse_req_t req, fuse_ino_t ino) +{ + struct fuse *f = req_fuse_prepare(req); + struct statvfs buf; + char *path = NULL; + int err = 0; + + memset(&buf, 0, sizeof(buf)); + if (ino) + err = get_path(f, ino, &path); + + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_statfs(f->fs, path ? path : "/", &buf); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + + if (!err) + fuse_reply_statfs(req, &buf); + else + reply_err(req, err); +} + +static void fuse_lib_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_setxattr(f->fs, path, name, value, size, flags); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + +static int common_getxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + const char *name, char *value, size_t size) +{ + int err; + char *path; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_getxattr(f->fs, path, name, value, size); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + return err; +} + +static void fuse_lib_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size) +{ + struct fuse *f = req_fuse_prepare(req); + int res; + + if (size) { + char *value = (char *) malloc(size); + if (value == NULL) { + reply_err(req, -ENOMEM); + return; + } + res = common_getxattr(f, req, ino, name, value, size); + if (res > 0) + fuse_reply_buf(req, value, res); + else + reply_err(req, res); + free(value); + } else { + res = common_getxattr(f, req, ino, name, NULL, 0); + if (res >= 0) + fuse_reply_xattr(req, res); + else + reply_err(req, res); + } +} + +static int common_listxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + char *list, size_t size) +{ + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_listxattr(f->fs, path, list, size); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + return err; +} + +static void fuse_lib_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) +{ + struct fuse *f = req_fuse_prepare(req); + int res; + + if (size) { + char *list = (char *) malloc(size); + if (list == NULL) { + reply_err(req, -ENOMEM); + return; + } + res = common_listxattr(f, req, ino, list, size); + if (res > 0) + fuse_reply_buf(req, list, res); + else + reply_err(req, res); + free(list); + } else { + res = common_listxattr(f, req, ino, NULL, 0); + if (res >= 0) + fuse_reply_xattr(req, res); + else + reply_err(req, res); + } +} + +static void fuse_lib_removexattr(fuse_req_t req, fuse_ino_t ino, + const char *name) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_removexattr(f->fs, path, name); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + reply_err(req, err); +} + +static struct lock *locks_conflict(struct node *node, const struct lock *lock) +{ + struct lock *l; + + for (l = node->locks; l; l = l->next) + if (l->owner != lock->owner && + lock->start <= l->end && l->start <= lock->end && + (l->type == F_WRLCK || lock->type == F_WRLCK)) + break; + + return l; +} + +static void delete_lock(struct lock **lockp) +{ + struct lock *l = *lockp; + *lockp = l->next; + free(l); +} + +static void insert_lock(struct lock **pos, struct lock *lock) +{ + lock->next = *pos; + *pos = lock; +} + +static int locks_insert(struct node *node, struct lock *lock) +{ + struct lock **lp; + struct lock *newl1 = NULL; + struct lock *newl2 = NULL; + + if (lock->type != F_UNLCK || lock->start != 0 || + lock->end != OFFSET_MAX) { + newl1 = malloc(sizeof(struct lock)); + newl2 = malloc(sizeof(struct lock)); + + if (!newl1 || !newl2) { + free(newl1); + free(newl2); + return -ENOLCK; + } + } + + for (lp = &node->locks; *lp;) { + struct lock *l = *lp; + if (l->owner != lock->owner) + goto skip; + + if (lock->type == l->type) { + if (l->end < lock->start - 1) + goto skip; + if (lock->end < l->start - 1) + break; + if (l->start <= lock->start && lock->end <= l->end) + goto out; + if (l->start < lock->start) + lock->start = l->start; + if (lock->end < l->end) + lock->end = l->end; + goto delete; + } else { + if (l->end < lock->start) + goto skip; + if (lock->end < l->start) + break; + if (lock->start <= l->start && l->end <= lock->end) + goto delete; + if (l->end <= lock->end) { + l->end = lock->start - 1; + goto skip; + } + if (lock->start <= l->start) { + l->start = lock->end + 1; + break; + } + *newl2 = *l; + newl2->start = lock->end + 1; + l->end = lock->start - 1; + insert_lock(&l->next, newl2); + newl2 = NULL; + } + skip: + lp = &l->next; + continue; + + delete: + delete_lock(lp); + } + if (lock->type != F_UNLCK) { + *newl1 = *lock; + insert_lock(lp, newl1); + newl1 = NULL; + } +out: + free(newl1); + free(newl2); + return 0; +} + +static void flock_to_lock(struct flock *flock, struct lock *lock) +{ + memset(lock, 0, sizeof(struct lock)); + lock->type = flock->l_type; + lock->start = flock->l_start; + lock->end = + flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX; + lock->pid = flock->l_pid; +} + +static void lock_to_flock(struct lock *lock, struct flock *flock) +{ + flock->l_type = lock->type; + flock->l_start = lock->start; + flock->l_len = + (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1; + flock->l_pid = lock->pid; +} + +static int fuse_flush_common(struct fuse *f, fuse_req_t req, fuse_ino_t ino, + const char *path, struct fuse_file_info *fi) +{ + struct fuse_intr_data d; + struct flock lock; + struct lock l; + int err; + int errlock; + + fuse_prepare_interrupt(f, req, &d); + memset(&lock, 0, sizeof(lock)); + lock.l_type = F_UNLCK; + lock.l_whence = SEEK_SET; + err = fuse_fs_flush(f->fs, path, fi); + errlock = fuse_fs_lock(f->fs, path, fi, F_SETLK, &lock); + fuse_finish_interrupt(f, req, &d); + + if (errlock != -ENOSYS) { + flock_to_lock(&lock, &l); + l.owner = fi->lock_owner; + pthread_mutex_lock(&f->lock); + locks_insert(get_node(f, ino), &l); + pthread_mutex_unlock(&f->lock); + + /* if op.lock() is defined FLUSH is needed regardless + of op.flush() */ + if (err == -ENOSYS) + err = 0; + } + return err; +} + +static void fuse_lib_release(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int err = 0; + + get_path(f, ino, &path); + if (fi->flush) { + err = fuse_flush_common(f, req, ino, path, fi); + if (err == -ENOSYS) + err = 0; + } + + fuse_prepare_interrupt(f, req, &d); + fuse_do_release(f, ino, path, fi); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + + reply_err(req, err); +} + +static void fuse_lib_flush(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + get_path(f, ino, &path); + err = fuse_flush_common(f, req, ino, path, fi); + free_path(f, ino, path); + + reply_err(req, err); +} + +static int fuse_lock_common(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock, + int cmd) +{ + struct fuse *f = req_fuse_prepare(req); + char *path; + int err; + + err = get_path_nullok(f, ino, &path); + if (!err) { + struct fuse_intr_data d; + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_lock(f->fs, path, fi, cmd, lock); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + return err; +} + +static void fuse_lib_getlk(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock) +{ + int err; + struct lock l; + struct lock *conflict; + struct fuse *f = req_fuse(req); + + flock_to_lock(lock, &l); + l.owner = fi->lock_owner; + pthread_mutex_lock(&f->lock); + conflict = locks_conflict(get_node(f, ino), &l); + if (conflict) + lock_to_flock(conflict, lock); + pthread_mutex_unlock(&f->lock); + if (!conflict) + err = fuse_lock_common(req, ino, fi, lock, F_GETLK); + else + err = 0; + + if (!err) + fuse_reply_lock(req, lock); + else + reply_err(req, err); +} + +static void fuse_lib_setlk(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock, + int sleep) +{ + int err = fuse_lock_common(req, ino, fi, lock, + sleep ? F_SETLKW : F_SETLK); + if (!err) { + struct fuse *f = req_fuse(req); + struct lock l; + flock_to_lock(lock, &l); + l.owner = fi->lock_owner; + pthread_mutex_lock(&f->lock); + locks_insert(get_node(f, ino), &l); + pthread_mutex_unlock(&f->lock); + } + reply_err(req, err); +} + +static void fuse_lib_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, + uint64_t idx) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int err; + + err = get_path(f, ino, &path); + if (!err) { + fuse_prepare_interrupt(f, req, &d); + err = fuse_fs_bmap(f->fs, path, blocksize, &idx); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + if (!err) + fuse_reply_bmap(req, idx); + else + reply_err(req, err); +} + +static void fuse_lib_ioctl(fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, + const void *in_buf, size_t in_bufsz, + size_t out_bufsz) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path, *out_buf = NULL; + int err; + + err = -EPERM; + if (flags & FUSE_IOCTL_UNRESTRICTED) + goto err; + + if (out_bufsz) { + err = -ENOMEM; + out_buf = malloc(out_bufsz); + if (!out_buf) + goto err; + } + + assert(!in_bufsz || !out_bufsz || in_bufsz == out_bufsz); + if (out_buf) + memcpy(out_buf, in_buf, in_bufsz); + + err = get_path(f, ino, &path); + if (err) + goto err; + + fuse_prepare_interrupt(f, req, &d); + + err = fuse_fs_ioctl(f->fs, path, cmd, arg, fi, flags, + out_buf ?: (void *)in_buf); + + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + + fuse_reply_ioctl(req, err, out_buf, out_bufsz); + goto out; +err: + reply_err(req, err); +out: + free(out_buf); +} + +static void fuse_lib_poll(fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct fuse_pollhandle *ph) +{ + struct fuse *f = req_fuse_prepare(req); + struct fuse_intr_data d; + char *path; + int ret; + unsigned revents = 0; + + ret = get_path(f, ino, &path); + if (!ret) { + fuse_prepare_interrupt(f, req, &d); + ret = fuse_fs_poll(f->fs, path, fi, ph, &revents); + fuse_finish_interrupt(f, req, &d); + free_path(f, ino, path); + } + if (!ret) + fuse_reply_poll(req, revents); + else + reply_err(req, ret); +} + +static struct fuse_lowlevel_ops fuse_path_ops = { + .init = fuse_lib_init, + .destroy = fuse_lib_destroy, + .lookup = fuse_lib_lookup, + .forget = fuse_lib_forget, + .getattr = fuse_lib_getattr, + .setattr = fuse_lib_setattr, + .access = fuse_lib_access, + .readlink = fuse_lib_readlink, + .mknod = fuse_lib_mknod, + .mkdir = fuse_lib_mkdir, + .unlink = fuse_lib_unlink, + .rmdir = fuse_lib_rmdir, + .symlink = fuse_lib_symlink, + .rename = fuse_lib_rename, + .link = fuse_lib_link, + .create = fuse_lib_create, + .open = fuse_lib_open, + .read = fuse_lib_read, + .write = fuse_lib_write, + .flush = fuse_lib_flush, + .release = fuse_lib_release, + .fsync = fuse_lib_fsync, + .opendir = fuse_lib_opendir, + .readdir = fuse_lib_readdir, + .releasedir = fuse_lib_releasedir, + .fsyncdir = fuse_lib_fsyncdir, + .statfs = fuse_lib_statfs, + .setxattr = fuse_lib_setxattr, + .getxattr = fuse_lib_getxattr, + .listxattr = fuse_lib_listxattr, + .removexattr = fuse_lib_removexattr, + .getlk = fuse_lib_getlk, + .setlk = fuse_lib_setlk, + .bmap = fuse_lib_bmap, + .ioctl = fuse_lib_ioctl, + .poll = fuse_lib_poll, +}; + +int fuse_notify_poll(struct fuse_pollhandle *ph) +{ + return fuse_lowlevel_notify_poll(ph); +} + +static void free_cmd(struct fuse_cmd *cmd) +{ + free(cmd->buf); + free(cmd); +} + +void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) +{ + fuse_session_process(f->se, cmd->buf, cmd->buflen, cmd->ch); + free_cmd(cmd); +} + +int fuse_exited(struct fuse *f) +{ + return fuse_session_exited(f->se); +} + +struct fuse_session *fuse_get_session(struct fuse *f) +{ + return f->se; +} + +static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize) +{ + struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd)); + if (cmd == NULL) { + fprintf(stderr, "fuse: failed to allocate cmd\n"); + return NULL; + } + cmd->buf = (char *) malloc(bufsize); + if (cmd->buf == NULL) { + fprintf(stderr, "fuse: failed to allocate read buffer\n"); + free(cmd); + return NULL; + } + return cmd; +} + +struct fuse_cmd *fuse_read_cmd(struct fuse *f) +{ + struct fuse_chan *ch = fuse_session_next_chan(f->se, NULL); + size_t bufsize = fuse_chan_bufsize(ch); + struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize); + if (cmd != NULL) { + int res = fuse_chan_recv(&ch, cmd->buf, bufsize); + if (res <= 0) { + free_cmd(cmd); + if (res < 0 && res != -EINTR && res != -EAGAIN) + fuse_exit(f); + return NULL; + } + cmd->buflen = res; + cmd->ch = ch; + } + return cmd; +} + +int fuse_loop(struct fuse *f) +{ + if (f) + return fuse_session_loop(f->se); + else + return -1; +} + +int fuse_invalidate(struct fuse *f, const char *path) +{ + (void) f; + (void) path; + return -EINVAL; +} + +void fuse_exit(struct fuse *f) +{ + fuse_session_exit(f->se); +} + +struct fuse_context *fuse_get_context(void) +{ + return &fuse_get_context_internal()->ctx; +} + +/* + * The size of fuse_context got extended, so need to be careful about + * incompatibility (i.e. a new binary cannot work with an old + * library). + */ +struct fuse_context *fuse_get_context_compat22(void); +struct fuse_context *fuse_get_context_compat22(void) +{ + return &fuse_get_context_internal()->ctx; +} +FUSE_SYMVER(".symver fuse_get_context_compat22,fuse_get_context@FUSE_2.2"); + +int fuse_getgroups(int size, gid_t list[]) +{ + fuse_req_t req = fuse_get_context_internal()->req; + return fuse_req_getgroups(req, size, list); +} + +int fuse_interrupted(void) +{ + return fuse_req_interrupted(fuse_get_context_internal()->req); +} + +void fuse_set_getcontext_func(struct fuse_context *(*func)(void)) +{ + (void) func; + /* no-op */ +} + +enum { + KEY_HELP, +}; + +#define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v } + +static const struct fuse_opt fuse_lib_opts[] = { + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP), + FUSE_LIB_OPT("debug", debug, 1), + FUSE_LIB_OPT("-d", debug, 1), + FUSE_LIB_OPT("hard_remove", hard_remove, 1), + FUSE_LIB_OPT("use_ino", use_ino, 1), + FUSE_LIB_OPT("readdir_ino", readdir_ino, 1), + FUSE_LIB_OPT("direct_io", direct_io, 1), + FUSE_LIB_OPT("kernel_cache", kernel_cache, 1), + FUSE_LIB_OPT("auto_cache", auto_cache, 1), + FUSE_LIB_OPT("noauto_cache", auto_cache, 0), + FUSE_LIB_OPT("umask=", set_mode, 1), + FUSE_LIB_OPT("umask=%o", umask, 0), + FUSE_LIB_OPT("uid=", set_uid, 1), + FUSE_LIB_OPT("uid=%d", uid, 0), + FUSE_LIB_OPT("gid=", set_gid, 1), + FUSE_LIB_OPT("gid=%d", gid, 0), + FUSE_LIB_OPT("entry_timeout=%lf", entry_timeout, 0), + FUSE_LIB_OPT("attr_timeout=%lf", attr_timeout, 0), + FUSE_LIB_OPT("ac_attr_timeout=%lf", ac_attr_timeout, 0), + FUSE_LIB_OPT("ac_attr_timeout=", ac_attr_timeout_set, 1), + FUSE_LIB_OPT("negative_timeout=%lf", negative_timeout, 0), + FUSE_LIB_OPT("noforget", noforget, 1), + FUSE_LIB_OPT("intr", intr, 1), + FUSE_LIB_OPT("intr_signal=%d", intr_signal, 0), + FUSE_LIB_OPT("modules=%s", modules, 0), + FUSE_OPT_END +}; + +static void fuse_lib_help(void) +{ + fprintf(stderr, +" -o hard_remove immediate removal (don't hide files)\n" +" -o use_ino let filesystem set inode numbers\n" +" -o readdir_ino try to fill in d_ino in readdir\n" +" -o direct_io use direct I/O\n" +" -o kernel_cache cache files in kernel\n" +" -o [no]auto_cache enable caching based on modification times (off)\n" +" -o umask=M set file permissions (octal)\n" +" -o uid=N set file owner\n" +" -o gid=N set file group\n" +" -o entry_timeout=T cache timeout for names (1.0s)\n" +" -o negative_timeout=T cache timeout for deleted names (0.0s)\n" +" -o attr_timeout=T cache timeout for attributes (1.0s)\n" +" -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n" +" -o intr allow requests to be interrupted\n" +" -o intr_signal=NUM signal to send on interrupt (%i)\n" +" -o modules=M1[:M2...] names of modules to push onto filesystem stack\n" +"\n", FUSE_DEFAULT_INTR_SIGNAL); +} + +static void fuse_lib_help_modules(void) +{ + struct fuse_module *m; + fprintf(stderr, "\nModule options:\n"); + pthread_mutex_lock(&fuse_context_lock); + for (m = fuse_modules; m; m = m->next) { + struct fuse_fs *fs = NULL; + struct fuse_fs *newfs; + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); + if (fuse_opt_add_arg(&args, "") != -1 && + fuse_opt_add_arg(&args, "-h") != -1) { + fprintf(stderr, "\n[%s]\n", m->name); + newfs = m->factory(&args, &fs); + assert(newfs == NULL); + } + fuse_opt_free_args(&args); + } + pthread_mutex_unlock(&fuse_context_lock); +} + +static int fuse_lib_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + (void) arg; (void) outargs; + + if (key == KEY_HELP) { + struct fuse_config *conf = (struct fuse_config *) data; + fuse_lib_help(); + conf->help = 1; + } + + return 1; +} + +int fuse_is_lib_option(const char *opt) +{ + return fuse_lowlevel_is_lib_option(opt) || + fuse_opt_match(fuse_lib_opts, opt); +} + +static int fuse_init_intr_signal(int signum, int *installed) +{ + struct sigaction old_sa; + + if (sigaction(signum, NULL, &old_sa) == -1) { + perror("fuse: cannot get old signal handler"); + return -1; + } + + if (old_sa.sa_handler == SIG_DFL) { + struct sigaction sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = fuse_intr_sighandler; + sigemptyset(&sa.sa_mask); + + if (sigaction(signum, &sa, NULL) == -1) { + perror("fuse: cannot set interrupt signal handler"); + return -1; + } + *installed = 1; + } + return 0; +} + +static void fuse_restore_intr_signal(int signum) +{ + struct sigaction sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = SIG_DFL; + sigaction(signum, &sa, NULL); +} + + +static int fuse_push_module(struct fuse *f, const char *module, + struct fuse_args *args) +{ + struct fuse_fs *fs[2] = { f->fs, NULL }; + struct fuse_fs *newfs; + struct fuse_module *m = fuse_get_module(module); + + if (!m) + return -1; + + newfs = m->factory(args, fs); + if (!newfs) { + fuse_put_module(m); + return -1; + } + newfs->m = m; + f->fs = newfs; + f->nullpath_ok = newfs->op.flag_nullpath_ok && f->nullpath_ok; + return 0; +} + +struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, + void *user_data) +{ + struct fuse_fs *fs; + + if (sizeof(struct fuse_operations) < op_size) { + fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n"); + op_size = sizeof(struct fuse_operations); + } + + fs = (struct fuse_fs *) calloc(1, sizeof(struct fuse_fs)); + if (!fs) { + fprintf(stderr, "fuse: failed to allocate fuse_fs object\n"); + return NULL; + } + + fs->user_data = user_data; + if (op) + memcpy(&fs->op, op, op_size); + return fs; +} + +struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, + const struct fuse_operations *op, + size_t op_size, void *user_data, int compat) +{ + struct fuse *f; + struct node *root; + struct fuse_fs *fs; + struct fuse_lowlevel_ops llop = fuse_path_ops; + + if (fuse_create_context_key() == -1) + goto out; + + f = (struct fuse *) calloc(1, sizeof(struct fuse)); + if (f == NULL) { + fprintf(stderr, "fuse: failed to allocate fuse object\n"); + goto out_delete_context_key; + } + + fs = fuse_fs_new(op, op_size, user_data); + if (!fs) + goto out_free; + + fs->compat = compat; + f->fs = fs; + f->nullpath_ok = fs->op.flag_nullpath_ok; + + /* Oh f**k, this is ugly! */ + if (!fs->op.lock) { + llop.getlk = NULL; + llop.setlk = NULL; + } + + f->conf.entry_timeout = 1.0; + f->conf.attr_timeout = 1.0; + f->conf.negative_timeout = 0.0; + f->conf.intr_signal = FUSE_DEFAULT_INTR_SIGNAL; + + if (fuse_opt_parse(args, &f->conf, fuse_lib_opts, + fuse_lib_opt_proc) == -1) + goto out_free_fs; + + if (f->conf.modules) { + char *module; + char *next; + + for (module = f->conf.modules; module; module = next) { + char *p; + for (p = module; *p && *p != ':'; p++); + next = *p ? p + 1 : NULL; + *p = '\0'; + if (module[0] && + fuse_push_module(f, module, args) == -1) + goto out_free_fs; + } + } + + if (!f->conf.ac_attr_timeout_set) + f->conf.ac_attr_timeout = f->conf.attr_timeout; + +#ifdef __FreeBSD__ + /* + * In FreeBSD, we always use these settings as inode numbers + * are needed to make getcwd(3) work. + */ + f->conf.readdir_ino = 1; +#endif + + if (compat && compat <= 25) { + if (fuse_sync_compat_args(args) == -1) + goto out_free_fs; + } + + f->se = fuse_lowlevel_new_common(args, &llop, sizeof(llop), f); + if (f->se == NULL) { + if (f->conf.help) + fuse_lib_help_modules(); + goto out_free_fs; + } + + fuse_session_add_chan(f->se, ch); + + if (f->conf.debug) + fprintf(stderr, "nullpath_ok: %i\n", f->nullpath_ok); + + /* Trace topmost layer by default */ + f->fs->debug = f->conf.debug; + f->ctr = 0; + f->generation = 0; + /* FIXME: Dynamic hash table */ + f->name_table_size = 14057; + f->name_table = (struct node **) + calloc(1, sizeof(struct node *) * f->name_table_size); + if (f->name_table == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_session; + } + + f->id_table_size = 14057; + f->id_table = (struct node **) + calloc(1, sizeof(struct node *) * f->id_table_size); + if (f->id_table == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_name_table; + } + + fuse_mutex_init(&f->lock); + + root = (struct node *) calloc(1, sizeof(struct node)); + if (root == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_id_table; + } + + root->name = strdup("/"); + if (root->name == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + goto out_free_root; + } + + if (f->conf.intr && + fuse_init_intr_signal(f->conf.intr_signal, + &f->intr_installed) == -1) + goto out_free_root_name; + + root->parent = NULL; + root->nodeid = FUSE_ROOT_ID; + root->generation = 0; + root->refctr = 1; + root->nlookup = 1; + hash_id(f, root); + + return f; + +out_free_root_name: + free(root->name); +out_free_root: + free(root); +out_free_id_table: + free(f->id_table); +out_free_name_table: + free(f->name_table); +out_free_session: + fuse_session_destroy(f->se); +out_free_fs: + /* Horrible compatibility hack to stop the destructor from being + called on the filesystem without init being called first */ + fs->op.destroy = NULL; + fuse_fs_destroy(f->fs); + free(f->conf.modules); +out_free: + free(f); +out_delete_context_key: + fuse_delete_context_key(); +out: + return NULL; +} + +struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args, + const struct fuse_operations *op, size_t op_size, + void *user_data) +{ + return fuse_new_common(ch, args, op, op_size, user_data, 0); +} + +void fuse_destroy(struct fuse *f) +{ + size_t i; + + if (f->conf.intr && f->intr_installed) + fuse_restore_intr_signal(f->conf.intr_signal); + + if (f->fs) { + struct fuse_context_i *c = fuse_get_context_internal(); + + memset(c, 0, sizeof(*c)); + c->ctx.fuse = f; + + for (i = 0; i < f->id_table_size; i++) { + struct node *node; + + for (node = f->id_table[i]; node != NULL; + node = node->id_next) { + if (node->is_hidden) { + char *path; + if (try_get_path(f, node->nodeid, NULL, &path, NULL, 0) == 0) { + fuse_fs_unlink(f->fs, path); + free(path); + } + } + } + } + } + for (i = 0; i < f->id_table_size; i++) { + struct node *node; + struct node *next; + + for (node = f->id_table[i]; node != NULL; node = next) { + next = node->id_next; + free_node(node); + } + } + free(f->id_table); + free(f->name_table); + pthread_mutex_destroy(&f->lock); + fuse_session_destroy(f->se); + free(f->conf.modules); + free(f); + fuse_delete_context_key(); +} + +static struct fuse *fuse_new_common_compat25(int fd, struct fuse_args *args, + const struct fuse_operations *op, + size_t op_size, int compat) +{ + struct fuse *f = NULL; + struct fuse_chan *ch = fuse_kern_chan_new(fd); + + if (ch) + f = fuse_new_common(ch, args, op, op_size, NULL, compat); + + return f; +} + +/* called with fuse_context_lock held or during initialization (before + main() has been called) */ +void fuse_register_module(struct fuse_module *mod) +{ + mod->ctr = 0; + mod->so = fuse_current_so; + if (mod->so) + mod->so->ctr++; + mod->next = fuse_modules; + fuse_modules = mod; +} + +#ifndef __FreeBSD__ + +static struct fuse *fuse_new_common_compat(int fd, const char *opts, + const struct fuse_operations *op, + size_t op_size, int compat) +{ + struct fuse *f; + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); + + if (fuse_opt_add_arg(&args, "") == -1) + return NULL; + if (opts && + (fuse_opt_add_arg(&args, "-o") == -1 || + fuse_opt_add_arg(&args, opts) == -1)) { + fuse_opt_free_args(&args); + return NULL; + } + f = fuse_new_common_compat25(fd, &args, op, op_size, compat); + fuse_opt_free_args(&args); + + return f; +} + +struct fuse *fuse_new_compat22(int fd, const char *opts, + const struct fuse_operations_compat22 *op, + size_t op_size) +{ + return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op, + op_size, 22); +} + +struct fuse *fuse_new_compat2(int fd, const char *opts, + const struct fuse_operations_compat2 *op) +{ + return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op, + sizeof(struct fuse_operations_compat2), + 21); +} + +struct fuse *fuse_new_compat1(int fd, int flags, + const struct fuse_operations_compat1 *op) +{ + const char *opts = NULL; + if (flags & FUSE_DEBUG_COMPAT1) + opts = "debug"; + return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op, + sizeof(struct fuse_operations_compat1), + 11); +} + +FUSE_SYMVER(".symver fuse_exited,__fuse_exited@"); +FUSE_SYMVER(".symver fuse_process_cmd,__fuse_process_cmd@"); +FUSE_SYMVER(".symver fuse_read_cmd,__fuse_read_cmd@"); +FUSE_SYMVER(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@"); +FUSE_SYMVER(".symver fuse_new_compat2,fuse_new@"); +FUSE_SYMVER(".symver fuse_new_compat22,fuse_new@FUSE_2.2"); + +#endif /* __FreeBSD__ */ + +struct fuse *fuse_new_compat25(int fd, struct fuse_args *args, + const struct fuse_operations_compat25 *op, + size_t op_size) +{ + return fuse_new_common_compat25(fd, args, (struct fuse_operations *) op, + op_size, 25); +} + +FUSE_SYMVER(".symver fuse_new_compat25,fuse_new@FUSE_2.5"); diff --git a/fuse/fuse_i.h b/fuse/fuse_i.h new file mode 100644 index 000000000..6285c952e --- /dev/null +++ b/fuse/fuse_i.h @@ -0,0 +1,101 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse.h" +#include "fuse_lowlevel.h" +#include + +struct fuse_chan; +struct fuse_ll; + +struct fuse_session { + struct fuse_session_ops op; + + void *data; + + volatile int exited; + + struct fuse_chan *ch; +}; + +struct fuse_req { + struct fuse_ll *f; + uint64_t unique; + int ctr; + pthread_mutex_t lock; + struct fuse_ctx ctx; + struct fuse_chan *ch; + int interrupted; + union { + struct { + uint64_t unique; + } i; + struct { + fuse_interrupt_func_t func; + void *data; + } ni; + } u; + struct fuse_req *next; + struct fuse_req *prev; +}; + +struct fuse_ll { + int debug; + int allow_root; + int atomic_o_trunc; + int no_remote_lock; + int big_writes; + struct fuse_lowlevel_ops op; + int got_init; + struct cuse_data *cuse_data; + void *userdata; + uid_t owner; + struct fuse_conn_info conn; + struct fuse_req list; + struct fuse_req interrupts; + pthread_mutex_t lock; + int got_destroy; +}; + +struct fuse_cmd { + char *buf; + size_t buflen; + struct fuse_chan *ch; +}; + +struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, + const struct fuse_operations *op, + size_t op_size, void *user_data, int compat); + +int fuse_sync_compat_args(struct fuse_args *args); + +struct fuse_chan *fuse_kern_chan_new(int fd); + +struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args, + const struct fuse_lowlevel_ops *op, + size_t op_size, void *userdata); + +void fuse_kern_unmount_compat22(const char *mountpoint); +void fuse_kern_unmount(const char *mountpoint, int fd); +int fuse_kern_mount(const char *mountpoint, struct fuse_args *args); + +int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov, + int count); +void fuse_free_req(fuse_req_t req); + + +struct fuse *fuse_setup_common(int argc, char *argv[], + const struct fuse_operations *op, + size_t op_size, + char **mountpoint, + int *multithreaded, + int *fd, + void *user_data, + int compat); + +void cuse_lowlevel_init(fuse_req_t req, fuse_ino_t nodeide, const void *inarg); diff --git a/fuse/fuse_kern_chan.c b/fuse/fuse_kern_chan.c new file mode 100644 index 000000000..03291c361 --- /dev/null +++ b/fuse/fuse_kern_chan.c @@ -0,0 +1,95 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_lowlevel.h" +#include "fuse_kernel.h" +#include "fuse_i.h" + +#include +#include +#include +#include + +static int fuse_kern_chan_receive(struct fuse_chan **chp, char *buf, + size_t size) +{ + struct fuse_chan *ch = *chp; + int err; + ssize_t res; + struct fuse_session *se = fuse_chan_session(ch); + assert(se != NULL); + +restart: + res = read(fuse_chan_fd(ch), buf, size); + err = errno; + + if (fuse_session_exited(se)) + return 0; + if (res == -1) { + /* ENOENT means the operation was interrupted, it's safe + to restart */ + if (err == ENOENT) + goto restart; + + if (err == ENODEV) { + fuse_session_exit(se); + return 0; + } + /* Errors occuring during normal operation: EINTR (read + interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem + umounted) */ + if (err != EINTR && err != EAGAIN) + perror("fuse: reading device"); + return -err; + } + if ((size_t) res < sizeof(struct fuse_in_header)) { + fprintf(stderr, "short read on fuse device\n"); + return -EIO; + } + return res; +} + +static int fuse_kern_chan_send(struct fuse_chan *ch, const struct iovec iov[], + size_t count) +{ + if (iov) { + ssize_t res = writev(fuse_chan_fd(ch), iov, count); + int err = errno; + + if (res == -1) { + struct fuse_session *se = fuse_chan_session(ch); + + assert(se != NULL); + + /* ENOENT means the operation was interrupted */ + if (!fuse_session_exited(se) && err != ENOENT) + perror("fuse: writing device"); + return -err; + } + } + return 0; +} + +static void fuse_kern_chan_destroy(struct fuse_chan *ch) +{ + close(fuse_chan_fd(ch)); +} + +#define MIN_BUFSIZE 0x21000 + +struct fuse_chan *fuse_kern_chan_new(int fd) +{ + struct fuse_chan_ops op = { + .receive = fuse_kern_chan_receive, + .send = fuse_kern_chan_send, + .destroy = fuse_kern_chan_destroy, + }; + size_t bufsize = getpagesize() + 0x1000; + bufsize = bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : bufsize; + return fuse_chan_new(&op, fd, bufsize, NULL); +} diff --git a/fuse/fuse_loop.c b/fuse/fuse_loop.c new file mode 100644 index 000000000..104c5d431 --- /dev/null +++ b/fuse/fuse_loop.c @@ -0,0 +1,39 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_lowlevel.h" + +#include +#include +#include + +int fuse_session_loop(struct fuse_session *se) +{ + int res = 0; + struct fuse_chan *ch = fuse_session_next_chan(se, NULL); + size_t bufsize = fuse_chan_bufsize(ch); + char *buf = (char *) malloc(bufsize); + if (!buf) { + fprintf(stderr, "fuse: failed to allocate read buffer\n"); + return -1; + } + + while (!fuse_session_exited(se)) { + struct fuse_chan *tmpch = ch; + res = fuse_chan_recv(&tmpch, buf, bufsize); + if (res == -EINTR) + continue; + if (res <= 0) + break; + fuse_session_process(se, buf, res, tmpch); + } + + free(buf); + fuse_session_reset(se); + return res < 0 ? -1 : 0; +} diff --git a/fuse/fuse_loop_mt.c b/fuse/fuse_loop_mt.c new file mode 100644 index 000000000..a73c399b7 --- /dev/null +++ b/fuse/fuse_loop_mt.c @@ -0,0 +1,246 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "fuse_lowlevel.h" +#include "fuse_misc.h" +#include "fuse_kernel.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __MULTI_THREAD + +/* Environment var controlling the thread stack size */ +#define ENVNAME_THREAD_STACK "FUSE_THREAD_STACK" + +struct fuse_worker { + struct fuse_worker *prev; + struct fuse_worker *next; + pthread_t thread_id; + size_t bufsize; + char *buf; + struct fuse_mt *mt; +}; + +struct fuse_mt { + pthread_mutex_t lock; + int numworker; + int numavail; + struct fuse_session *se; + struct fuse_chan *prevch; + struct fuse_worker main; + sem_t finish; + int exit; + int error; +}; + +static void list_add_worker(struct fuse_worker *w, struct fuse_worker *next) +{ + struct fuse_worker *prev = next->prev; + w->next = next; + w->prev = prev; + prev->next = w; + next->prev = w; +} + +static void list_del_worker(struct fuse_worker *w) +{ + struct fuse_worker *prev = w->prev; + struct fuse_worker *next = w->next; + prev->next = next; + next->prev = prev; +} + +#define PTHREAD_CANCEL_ENABLE 0 +#define PTHREAD_CANCEL_DISABLE 1 + +static int fuse_start_thread(struct fuse_mt *mt); + +static void *fuse_do_work(void *data) +{ + struct fuse_worker *w = (struct fuse_worker *) data; + struct fuse_mt *mt = w->mt; + + while (!fuse_session_exited(mt->se)) { + int isforget = 0; + struct fuse_chan *ch = mt->prevch; + int res; + + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + res = fuse_chan_recv(&ch, w->buf, w->bufsize); + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + if (res == -EINTR) + continue; + if (res <= 0) { + if (res < 0) { + fuse_session_exit(mt->se); + mt->error = -1; + } + break; + } + + pthread_mutex_lock(&mt->lock); + if (mt->exit) { + pthread_mutex_unlock(&mt->lock); + return NULL; + } + + /* + * This disgusting hack is needed so that zillions of threads + * are not created on a burst of FORGET messages + */ + if (((struct fuse_in_header *) w->buf)->opcode == FUSE_FORGET) + isforget = 1; + + if (!isforget) + mt->numavail--; + if (mt->numavail == 0) + fuse_start_thread(mt); + pthread_mutex_unlock(&mt->lock); + + fuse_session_process(mt->se, w->buf, res, ch); + + pthread_mutex_lock(&mt->lock); + if (!isforget) + mt->numavail++; + if (mt->numavail > 10) { + if (mt->exit) { + pthread_mutex_unlock(&mt->lock); + return NULL; + } + list_del_worker(w); + mt->numavail--; + mt->numworker--; + pthread_mutex_unlock(&mt->lock); + + pthread_detach(w->thread_id); + free(w->buf); + free(w); + return NULL; + } + pthread_mutex_unlock(&mt->lock); + } + + sem_post(&mt->finish); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + pause(); + + return NULL; +} + +static int fuse_start_thread(struct fuse_mt *mt) +{ + sigset_t oldset; + sigset_t newset; + int res; + pthread_attr_t attr; + char *stack_size; + struct fuse_worker *w = malloc(sizeof(struct fuse_worker)); + if (!w) { + fprintf(stderr, "fuse: failed to allocate worker structure\n"); + return -1; + } + memset(w, 0, sizeof(struct fuse_worker)); + w->bufsize = fuse_chan_bufsize(mt->prevch); + w->buf = malloc(w->bufsize); + w->mt = mt; + if (!w->buf) { + fprintf(stderr, "fuse: failed to allocate read buffer\n"); + free(w); + return -1; + } + + /* Override default stack size */ + pthread_attr_init(&attr); + stack_size = getenv(ENVNAME_THREAD_STACK); + if (stack_size && pthread_attr_setstacksize(&attr, atoi(stack_size))) + fprintf(stderr, "fuse: invalid stack size: %s\n", stack_size); + + /* Disallow signal reception in worker threads */ + sigemptyset(&newset); + sigaddset(&newset, SIGTERM); + sigaddset(&newset, SIGINT); + sigaddset(&newset, SIGHUP); + sigaddset(&newset, SIGQUIT); + pthread_sigmask(SIG_BLOCK, &newset, &oldset); + res = pthread_create(&w->thread_id, &attr, fuse_do_work, w); + pthread_sigmask(SIG_SETMASK, &oldset, NULL); + pthread_attr_destroy(&attr); + if (res != 0) { + fprintf(stderr, "fuse: error creating thread: %s\n", + strerror(res)); + free(w->buf); + free(w); + return -1; + } + list_add_worker(w, &mt->main); + mt->numavail ++; + mt->numworker ++; + + return 0; +} + +static void fuse_join_worker(struct fuse_mt *mt, struct fuse_worker *w) +{ + pthread_join(w->thread_id, NULL); + pthread_mutex_lock(&mt->lock); + list_del_worker(w); + pthread_mutex_unlock(&mt->lock); + free(w->buf); + free(w); +} + +int fuse_session_loop_mt(struct fuse_session *se) +{ + int err; + struct fuse_mt mt; + struct fuse_worker *w; + + memset(&mt, 0, sizeof(struct fuse_mt)); + mt.se = se; + mt.prevch = fuse_session_next_chan(se, NULL); + mt.error = 0; + mt.numworker = 0; + mt.numavail = 0; + mt.main.thread_id = pthread_self(); + mt.main.prev = mt.main.next = &mt.main; + sem_init(&mt.finish, 0, 0); + fuse_mutex_init(&mt.lock); + + pthread_mutex_lock(&mt.lock); + err = fuse_start_thread(&mt); + pthread_mutex_unlock(&mt.lock); + if (!err) { + /* sem_wait() is interruptible */ + while (!fuse_session_exited(se)) + sem_wait(&mt.finish); + + for (w = mt.main.next; w != &mt.main; w = w->next) + pthread_cancel(w->thread_id); + mt.exit = 1; + pthread_mutex_unlock(&mt.lock); + + while (mt.main.next != &mt.main) + fuse_join_worker(&mt, mt.main.next); + + err = mt.error; + } + + pthread_mutex_destroy(&mt.lock); + sem_destroy(&mt.finish); + fuse_session_reset(se); + return err; +} + +#endif diff --git a/fuse/fuse_lowlevel.c b/fuse/fuse_lowlevel.c new file mode 100644 index 000000000..90f756e54 --- /dev/null +++ b/fuse/fuse_lowlevel.c @@ -0,0 +1,1862 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_i.h" +#include "fuse_kernel.h" +#include "fuse_opt.h" +#include "fuse_misc.h" +#include "fuse_common_compat.h" +#include "fuse_lowlevel_compat.h" + +#define linux +#include +#include +#include +#include +#include +#include +#include + +#define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg))) +#define OFFSET_MAX 0x7fffffffffffffffLL + +struct fuse_pollhandle { + uint64_t kh; + struct fuse_chan *ch; + struct fuse_ll *f; +}; + +static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr) +{ + attr->ino = stbuf->st_ino; + attr->mode = stbuf->st_mode; + attr->nlink = stbuf->st_nlink; + attr->uid = stbuf->st_uid; + attr->gid = stbuf->st_gid; + attr->rdev = stbuf->st_rdev; + attr->size = stbuf->st_size; + attr->blksize = stbuf->st_blksize; + attr->blocks = stbuf->st_blocks; + attr->atime = stbuf->st_atime; + attr->mtime = stbuf->st_mtime; + attr->ctime = stbuf->st_ctime; + attr->atimensec = ST_ATIM_NSEC(stbuf); + attr->mtimensec = ST_MTIM_NSEC(stbuf); + attr->ctimensec = ST_CTIM_NSEC(stbuf); +} + +static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf) +{ + stbuf->st_mode = attr->mode; + stbuf->st_uid = attr->uid; + stbuf->st_gid = attr->gid; + stbuf->st_size = attr->size; + stbuf->st_atime = attr->atime; + stbuf->st_mtime = attr->mtime; + ST_ATIM_NSEC_SET(stbuf, attr->atimensec); + ST_MTIM_NSEC_SET(stbuf, attr->mtimensec); +} + +static size_t iov_length(const struct iovec *iov, size_t count) +{ + size_t seg; + size_t ret = 0; + + for (seg = 0; seg < count; seg++) + ret += iov[seg].iov_len; + return ret; +} + +static void list_init_req(struct fuse_req *req) +{ + req->next = req; + req->prev = req; +} + +static void list_del_req(struct fuse_req *req) +{ + struct fuse_req *prev = req->prev; + struct fuse_req *next = req->next; + prev->next = next; + next->prev = prev; +} + +static void list_add_req(struct fuse_req *req, struct fuse_req *next) +{ + struct fuse_req *prev = next->prev; + req->next = next; + req->prev = prev; + prev->next = req; + next->prev = req; +} + +static void destroy_req(fuse_req_t req) +{ + pthread_mutex_destroy(&req->lock); + free(req); +} + +void fuse_free_req(fuse_req_t req) +{ + int ctr; + struct fuse_ll *f = req->f; + + pthread_mutex_lock(&f->lock); + req->u.ni.func = NULL; + req->u.ni.data = NULL; + list_del_req(req); + ctr = --req->ctr; + pthread_mutex_unlock(&f->lock); + if (!ctr) + destroy_req(req); +} + +int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov, + int count) +{ + struct fuse_out_header out; + + if (error <= -1000 || error > 0) { + fprintf(stderr, "fuse: bad error value: %i\n", error); + error = -ERANGE; + } + + out.unique = req->unique; + out.error = error; + iov[0].iov_base = &out; + iov[0].iov_len = sizeof(struct fuse_out_header); + out.len = iov_length(iov, count); + + if (req->f->debug) { + if (out.error) { + fprintf(stderr, + " unique: %llu, error: %i (%s), outsize: %i\n", + (unsigned long long) out.unique, out.error, + strerror(-out.error), out.len); + } else { + fprintf(stderr, + " unique: %llu, success, outsize: %i\n", + (unsigned long long) out.unique, out.len); + } + } + + return fuse_chan_send(req->ch, iov, count); +} + +static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov, + int count) +{ + int res; + + res = fuse_send_reply_iov_nofree(req, error, iov, count); + fuse_free_req(req); + return res; +} + +static int send_reply(fuse_req_t req, int error, const void *arg, + size_t argsize) +{ + struct iovec iov[2]; + int count = 1; + if (argsize) { + iov[1].iov_base = (void *) arg; + iov[1].iov_len = argsize; + count++; + } + return send_reply_iov(req, error, iov, count); +} + +int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count) +{ + int res; + struct iovec *padded_iov; + + padded_iov = malloc((count + 1) * sizeof(struct iovec)); + if (padded_iov == NULL) + return fuse_reply_err(req, -ENOMEM); + + memcpy(padded_iov + 1, iov, count * sizeof(struct iovec)); + count++; + + res = send_reply_iov(req, 0, padded_iov, count); + free(padded_iov); + + return res; +} + +size_t fuse_dirent_size(size_t namelen) +{ + return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen); +} + +char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf, + off64_t off) +{ + unsigned namelen = strlen(name); + unsigned entlen = FUSE_NAME_OFFSET + namelen; + unsigned entsize = fuse_dirent_size(namelen); + unsigned padlen = entsize - entlen; + struct fuse_dirent *dirent = (struct fuse_dirent *) buf; + + dirent->ino = stbuf->st_ino; + dirent->off = off; + dirent->namelen = namelen; + dirent->type = (stbuf->st_mode & 0170000) >> 12; + strncpy(dirent->name, name, namelen); + if (padlen) + memset(buf + entlen, 0, padlen); + + return buf + entsize; +} + +size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, + const char *name, const struct stat *stbuf, off64_t off) +{ + size_t entsize; + + (void) req; + entsize = fuse_dirent_size(strlen(name)); + if (entsize <= bufsize && buf) + fuse_add_dirent(buf, name, stbuf, off); + return entsize; +} + +static void convert_statfs(const struct statvfs *stbuf, + struct fuse_kstatfs *kstatfs) +{ + kstatfs->bsize = stbuf->f_bsize; + kstatfs->frsize = stbuf->f_frsize; + kstatfs->blocks = stbuf->f_blocks; + kstatfs->bfree = stbuf->f_bfree; + kstatfs->bavail = stbuf->f_bavail; + kstatfs->files = stbuf->f_files; + kstatfs->ffree = stbuf->f_ffree; + kstatfs->namelen = stbuf->f_namemax; +} + +static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize) +{ + return send_reply(req, 0, arg, argsize); +} + +int fuse_reply_err(fuse_req_t req, int err) +{ + return send_reply(req, -err, NULL, 0); +} + +void fuse_reply_none(fuse_req_t req) +{ + fuse_chan_send(req->ch, NULL, 0); + fuse_free_req(req); +} + +static unsigned long calc_timeout_sec(double t) +{ + if (t > (double) ULONG_MAX) + return ULONG_MAX; + else if (t < 0.0) + return 0; + else + return (unsigned long) t; +} + +static unsigned int calc_timeout_nsec(double t) +{ + double f = t - (double) calc_timeout_sec(t); + if (f < 0.0) + return 0; + else if (f >= 0.999999999) + return 999999999; + else + return (unsigned int) (f * 1.0e9); +} + +static void fill_entry(struct fuse_entry_out *arg, + const struct fuse_entry_param *e) +{ + arg->nodeid = e->ino; + arg->generation = e->generation; + arg->entry_valid = calc_timeout_sec(e->entry_timeout); + arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout); + arg->attr_valid = calc_timeout_sec(e->attr_timeout); + arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout); + convert_stat(&e->attr, &arg->attr); +} + +static void fill_open(struct fuse_open_out *arg, + const struct fuse_file_info *f) +{ + arg->fh = f->fh; + if (f->direct_io) + arg->open_flags |= FOPEN_DIRECT_IO; + if (f->keep_cache) + arg->open_flags |= FOPEN_KEEP_CACHE; + if (f->nonseekable) + arg->open_flags |= FOPEN_NONSEEKABLE; +} + +int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e) +{ + struct fuse_entry_out arg; + size_t size = req->f->conn.proto_minor < 9 ? + FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg); + + /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant + negative entry */ + if (!e->ino && req->f->conn.proto_minor < 4) + return fuse_reply_err(req, ENOENT); + + memset(&arg, 0, sizeof(arg)); + fill_entry(&arg, e); + return send_reply_ok(req, &arg, size); +} + +int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, + const struct fuse_file_info *f) +{ + char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)]; + size_t entrysize = req->f->conn.proto_minor < 9 ? + FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out); + struct fuse_entry_out *earg = (struct fuse_entry_out *) buf; + struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize); + + memset(buf, 0, sizeof(buf)); + fill_entry(earg, e); + fill_open(oarg, f); + return send_reply_ok(req, buf, + entrysize + sizeof(struct fuse_open_out)); +} + +int fuse_reply_attr(fuse_req_t req, const struct stat *attr, + double attr_timeout) +{ + struct fuse_attr_out arg; + size_t size = req->f->conn.proto_minor < 9 ? + FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg); + + memset(&arg, 0, sizeof(arg)); + arg.attr_valid = calc_timeout_sec(attr_timeout); + arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout); + convert_stat(attr, &arg.attr); + + return send_reply_ok(req, &arg, size); +} + +int fuse_reply_readlink(fuse_req_t req, const char *linkname) +{ + return send_reply_ok(req, linkname, strlen(linkname)); +} + +int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f) +{ + struct fuse_open_out arg; + + memset(&arg, 0, sizeof(arg)); + fill_open(&arg, f); + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_write(fuse_req_t req, size_t count) +{ + struct fuse_write_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.size = count; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size) +{ + return send_reply_ok(req, buf, size); +} + +int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf) +{ + struct fuse_statfs_out arg; + size_t size = req->f->conn.proto_minor < 4 ? + FUSE_COMPAT_STATFS_SIZE : sizeof(arg); + + memset(&arg, 0, sizeof(arg)); + convert_statfs(stbuf, &arg.st); + + return send_reply_ok(req, &arg, size); +} + +int fuse_reply_xattr(fuse_req_t req, size_t count) +{ + struct fuse_getxattr_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.size = count; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_lock(fuse_req_t req, struct flock *lock) +{ + struct fuse_lk_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.lk.type = lock->l_type; + if (lock->l_type != F_UNLCK) { + arg.lk.start = lock->l_start; + if (lock->l_len == 0) + arg.lk.end = OFFSET_MAX; + else + arg.lk.end = lock->l_start + lock->l_len - 1; + } + arg.lk.pid = lock->l_pid; + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_bmap(fuse_req_t req, uint64_t idx) +{ + struct fuse_bmap_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.block = idx; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_ioctl_retry(fuse_req_t req, + const struct iovec *in_iov, size_t in_count, + const struct iovec *out_iov, size_t out_count) +{ + struct fuse_ioctl_out arg; + struct iovec iov[4]; + size_t count = 1; + + memset(&arg, 0, sizeof(arg)); + arg.flags |= FUSE_IOCTL_RETRY; + arg.in_iovs = in_count; + arg.out_iovs = out_count; + iov[count].iov_base = &arg; + iov[count].iov_len = sizeof(arg); + count++; + + if (in_count) { + iov[count].iov_base = (void *)in_iov; + iov[count].iov_len = sizeof(in_iov[0]) * in_count; + count++; + } + + if (out_count) { + iov[count].iov_base = (void *)out_iov; + iov[count].iov_len = sizeof(out_iov[0]) * out_count; + count++; + } + + return send_reply_iov(req, 0, iov, count); +} + +int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size) +{ + struct fuse_ioctl_out arg; + struct iovec iov[3]; + size_t count = 1; + + memset(&arg, 0, sizeof(arg)); + arg.result = result; + iov[count].iov_base = &arg; + iov[count].iov_len = sizeof(arg); + count++; + + if (size) { + iov[count].iov_base = (char *) buf; + iov[count].iov_len = size; + count++; + } + + return send_reply_iov(req, 0, iov, count); +} + +int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, + int count) +{ + struct iovec *padded_iov; + struct fuse_ioctl_out arg; + int res; + + padded_iov = malloc((count + 2) * sizeof(struct iovec)); + if (padded_iov == NULL) + return fuse_reply_err(req, -ENOMEM); + + memset(&arg, 0, sizeof(arg)); + arg.result = result; + padded_iov[1].iov_base = &arg; + padded_iov[1].iov_len = sizeof(arg); + + memcpy(&padded_iov[2], iov, count * sizeof(struct iovec)); + + res = send_reply_iov(req, 0, padded_iov, count + 2); + free(padded_iov); + + return res; +} + +int fuse_reply_poll(fuse_req_t req, unsigned revents) +{ + struct fuse_poll_out arg; + + memset(&arg, 0, sizeof(arg)); + arg.revents = revents; + + return send_reply_ok(req, &arg, sizeof(arg)); +} + +static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + char *name = (char *) inarg; + + if (req->f->op.lookup) + req->f->op.lookup(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg; + + if (req->f->op.forget) + req->f->op.forget(req, nodeid, arg->nlookup); + else + fuse_reply_none(req); +} + +static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_file_info *fip = NULL; + struct fuse_file_info fi; + + if (req->f->conn.proto_minor >= 9) { + struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg; + + if (arg->getattr_flags & FUSE_GETATTR_FH) { + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + fip = &fi; + } + } + + if (req->f->op.getattr) + req->f->op.getattr(req, nodeid, fip); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg; + + if (req->f->op.setattr) { + struct fuse_file_info *fi = NULL; + struct fuse_file_info fi_store; + struct stat stbuf; + memset(&stbuf, 0, sizeof(stbuf)); + convert_attr(arg, &stbuf); + if (arg->valid & FATTR_FH) { + arg->valid &= ~FATTR_FH; + memset(&fi_store, 0, sizeof(fi_store)); + fi = &fi_store; + fi->fh = arg->fh; + fi->fh_old = fi->fh; + } + arg->valid &= + FUSE_SET_ATTR_MODE | + FUSE_SET_ATTR_UID | + FUSE_SET_ATTR_GID | + FUSE_SET_ATTR_SIZE | + FUSE_SET_ATTR_ATIME | + FUSE_SET_ATTR_MTIME | + FUSE_SET_ATTR_ATIME_NOW | + FUSE_SET_ATTR_MTIME_NOW; + + req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_access_in *arg = (struct fuse_access_in *) inarg; + + if (req->f->op.access) + req->f->op.access(req, nodeid, arg->mask); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + (void) inarg; + + if (req->f->op.readlink) + req->f->op.readlink(req, nodeid); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg; + char *name = PARAM(arg); + + if (req->f->conn.proto_minor >= 12) + req->ctx.umask = arg->umask; + else + name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE; + + if (req->f->op.mknod) + req->f->op.mknod(req, nodeid, name, arg->mode, arg->rdev); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg; + + if (req->f->conn.proto_minor >= 12) + req->ctx.umask = arg->umask; + + if (req->f->op.mkdir) + req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + char *name = (char *) inarg; + + if (req->f->op.unlink) + req->f->op.unlink(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + char *name = (char *) inarg; + + if (req->f->op.rmdir) + req->f->op.rmdir(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + char *name = (char *) inarg; + char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1; + + if (req->f->op.symlink) + req->f->op.symlink(req, linkname, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg; + char *oldname = PARAM(arg); + char *newname = oldname + strlen(oldname) + 1; + + if (req->f->op.rename) + req->f->op.rename(req, nodeid, oldname, arg->newdir, newname); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_link_in *arg = (struct fuse_link_in *) inarg; + + if (req->f->op.link) + req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg)); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_create_in *arg = (struct fuse_create_in *) inarg; + + if (req->f->op.create) { + struct fuse_file_info fi; + char *name = PARAM(arg); + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + + if (req->f->conn.proto_minor >= 12) + req->ctx.umask = arg->umask; + else + name = (char *) inarg + sizeof(struct fuse_open_in); + + req->f->op.create(req, nodeid, name, arg->mode, &fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_open_in *arg = (struct fuse_open_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + + if (req->f->op.open) + req->f->op.open(req, nodeid, &fi); + else + fuse_reply_open(req, &fi); +} + +static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_read_in *arg = (struct fuse_read_in *) inarg; + + if (req->f->op.read) { + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + if (req->f->conn.proto_minor >= 9) { + fi.lock_owner = arg->lock_owner; + fi.flags = arg->flags; + } + req->f->op.read(req, nodeid, arg->size, arg->offset, &fi); + } else + fuse_reply_err(req, ENOSYS); +} + +static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_write_in *arg = (struct fuse_write_in *) inarg; + struct fuse_file_info fi; + char *param; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + fi.writepage = arg->write_flags & 1; + + if (req->f->conn.proto_minor < 9) { + param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE; + } else { + fi.lock_owner = arg->lock_owner; + fi.flags = arg->flags; + param = PARAM(arg); + } + + if (req->f->op.write) + req->f->op.write(req, nodeid, param, arg->size, + arg->offset, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + fi.flush = 1; + if (req->f->conn.proto_minor >= 7) + fi.lock_owner = arg->lock_owner; + + if (req->f->op.flush) + req->f->op.flush(req, nodeid, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_release_in *arg = (struct fuse_release_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + fi.fh = arg->fh; + fi.fh_old = fi.fh; + if (req->f->conn.proto_minor >= 8) { + fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0; + fi.lock_owner = arg->lock_owner; + } + + if (req->f->op.release) + req->f->op.release(req, nodeid, &fi); + else + fuse_reply_err(req, 0); +} + +static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.fsync) + req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_open_in *arg = (struct fuse_open_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + + if (req->f->op.opendir) + req->f->op.opendir(req, nodeid, &fi); + else + fuse_reply_open(req, &fi); +} + +static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_read_in *arg = (struct fuse_read_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.readdir) + req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_release_in *arg = (struct fuse_release_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.flags = arg->flags; + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.releasedir) + req->f->op.releasedir(req, nodeid, &fi); + else + fuse_reply_err(req, 0); +} + +static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.fsyncdir) + req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + (void) nodeid; + (void) inarg; + + if (req->f->op.statfs) + req->f->op.statfs(req, nodeid); + else { + struct statvfs buf = { + .f_namemax = 255, + .f_bsize = 512, + }; + fuse_reply_statfs(req, &buf); + } +} + +static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg; + char *name = PARAM(arg); + char *value = name + strlen(name) + 1; + + if (req->f->op.setxattr) + req->f->op.setxattr(req, nodeid, name, value, arg->size, + arg->flags); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg; + + if (req->f->op.getxattr) + req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg; + + if (req->f->op.listxattr) + req->f->op.listxattr(req, nodeid, arg->size); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + char *name = (char *) inarg; + + if (req->f->op.removexattr) + req->f->op.removexattr(req, nodeid, name); + else + fuse_reply_err(req, ENOSYS); +} + +static void convert_fuse_file_lock(struct fuse_file_lock *fl, + struct flock *flock) +{ + memset(flock, 0, sizeof(struct flock)); + flock->l_type = fl->type; + flock->l_whence = SEEK_SET; + flock->l_start = fl->start; + if (fl->end == OFFSET_MAX) + flock->l_len = 0; + else + flock->l_len = fl->end - fl->start + 1; + flock->l_pid = fl->pid; +} + +static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg; + struct fuse_file_info fi; + struct flock flock; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.lock_owner = arg->owner; + + convert_fuse_file_lock(&arg->lk, &flock); + if (req->f->op.getlk) + req->f->op.getlk(req, nodeid, &fi, &flock); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid, + const void *inarg, int sleep) +{ + struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg; + struct fuse_file_info fi; + struct flock flock; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.lock_owner = arg->owner; + + convert_fuse_file_lock(&arg->lk, &flock); + if (req->f->op.setlk) + req->f->op.setlk(req, nodeid, &fi, &flock, sleep); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + do_setlk_common(req, nodeid, inarg, 0); +} + +static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + do_setlk_common(req, nodeid, inarg, 1); +} + +static int find_interrupted(struct fuse_ll *f, struct fuse_req *req) +{ + struct fuse_req *curr; + + for (curr = f->list.next; curr != &f->list; curr = curr->next) { + if (curr->unique == req->u.i.unique) { + fuse_interrupt_func_t func; + void *data; + + curr->ctr++; + pthread_mutex_unlock(&f->lock); + + /* Ugh, ugly locking */ + pthread_mutex_lock(&curr->lock); + pthread_mutex_lock(&f->lock); + curr->interrupted = 1; + func = curr->u.ni.func; + data = curr->u.ni.data; + pthread_mutex_unlock(&f->lock); + if (func) + func(curr, data); + pthread_mutex_unlock(&curr->lock); + + pthread_mutex_lock(&f->lock); + curr->ctr--; + if (!curr->ctr) + destroy_req(curr); + + return 1; + } + } + for (curr = f->interrupts.next; curr != &f->interrupts; + curr = curr->next) { + if (curr->u.i.unique == req->u.i.unique) + return 1; + } + return 0; +} + +static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg; + struct fuse_ll *f = req->f; + + (void) nodeid; + if (f->debug) + fprintf(stderr, "INTERRUPT: %llu\n", + (unsigned long long) arg->unique); + + req->u.i.unique = arg->unique; + + pthread_mutex_lock(&f->lock); + if (find_interrupted(f, req)) + destroy_req(req); + else + list_add_req(req, &f->interrupts); + pthread_mutex_unlock(&f->lock); +} + +static struct fuse_req *check_interrupt(struct fuse_ll *f, struct fuse_req *req) +{ + struct fuse_req *curr; + + for (curr = f->interrupts.next; curr != &f->interrupts; + curr = curr->next) { + if (curr->u.i.unique == req->unique) { + req->interrupted = 1; + list_del_req(curr); + free(curr); + return NULL; + } + } + curr = f->interrupts.next; + if (curr != &f->interrupts) { + list_del_req(curr); + list_init_req(curr); + return curr; + } else + return NULL; +} + +static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg; + + if (req->f->op.bmap) + req->f->op.bmap(req, nodeid, arg->blocksize, arg->block); + else + fuse_reply_err(req, ENOSYS); +} + +static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg; + unsigned int flags = arg->flags; + void *in_buf = arg->in_size ? PARAM(arg) : NULL; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.ioctl) + req->f->op.ioctl(req, nodeid, arg->cmd, + (void *)(uintptr_t)arg->arg, &fi, flags, + in_buf, arg->in_size, arg->out_size); + else + fuse_reply_err(req, ENOSYS); +} + +void fuse_pollhandle_destroy(struct fuse_pollhandle *ph) +{ + free(ph); +} + +static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg; + struct fuse_file_info fi; + + memset(&fi, 0, sizeof(fi)); + fi.fh = arg->fh; + fi.fh_old = fi.fh; + + if (req->f->op.poll) { + struct fuse_pollhandle *ph = NULL; + + if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) { + ph = malloc(sizeof(struct fuse_pollhandle)); + if (ph == NULL) { + fuse_reply_err(req, ENOMEM); + return; + } + ph->kh = arg->kh; + ph->ch = req->ch; + ph->f = req->f; + } + + req->f->op.poll(req, nodeid, &fi, ph); + } else { + fuse_reply_err(req, ENOSYS); + } +} + +static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_init_in *arg = (struct fuse_init_in *) inarg; + struct fuse_init_out outarg; + struct fuse_ll *f = req->f; + size_t bufsize = fuse_chan_bufsize(req->ch); + + (void) nodeid; + if (f->debug) { + fprintf(stderr, "INIT: %u.%u\n", arg->major, arg->minor); + if (arg->major == 7 && arg->minor >= 6) { + fprintf(stderr, "flags=0x%08x\n", arg->flags); + fprintf(stderr, "max_readahead=0x%08x\n", + arg->max_readahead); + } + } + f->conn.proto_major = arg->major; + f->conn.proto_minor = arg->minor; + f->conn.capable = 0; + f->conn.want = 0; + + memset(&outarg, 0, sizeof(outarg)); + outarg.major = FUSE_KERNEL_VERSION; + outarg.minor = FUSE_KERNEL_MINOR_VERSION; + + if (arg->major < 7) { + fprintf(stderr, "fuse: unsupported protocol version: %u.%u\n", + arg->major, arg->minor); + fuse_reply_err(req, EPROTO); + return; + } + + if (arg->major > 7) { + /* Wait for a second INIT request with a 7.X version */ + send_reply_ok(req, &outarg, sizeof(outarg)); + return; + } + + if (arg->minor >= 6) { + if (f->conn.async_read) + f->conn.async_read = arg->flags & FUSE_ASYNC_READ; + if (arg->max_readahead < f->conn.max_readahead) + f->conn.max_readahead = arg->max_readahead; + if (arg->flags & FUSE_ASYNC_READ) + f->conn.capable |= FUSE_CAP_ASYNC_READ; + if (arg->flags & FUSE_POSIX_LOCKS) + f->conn.capable |= FUSE_CAP_POSIX_LOCKS; + if (arg->flags & FUSE_ATOMIC_O_TRUNC) + f->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC; + if (arg->flags & FUSE_EXPORT_SUPPORT) + f->conn.capable |= FUSE_CAP_EXPORT_SUPPORT; + if (arg->flags & FUSE_BIG_WRITES) + f->conn.capable |= FUSE_CAP_BIG_WRITES; + if (arg->flags & FUSE_DONT_MASK) + f->conn.capable |= FUSE_CAP_DONT_MASK; + } else { + f->conn.async_read = 0; + f->conn.max_readahead = 0; + } + + if (f->atomic_o_trunc) + f->conn.want |= FUSE_CAP_ATOMIC_O_TRUNC; + if (f->op.getlk && f->op.setlk && !f->no_remote_lock) + f->conn.want |= FUSE_CAP_POSIX_LOCKS; + if (f->big_writes) + f->conn.want |= FUSE_CAP_BIG_WRITES; + + if (bufsize < FUSE_MIN_READ_BUFFER) { + fprintf(stderr, "fuse: warning: buffer size too small: %zu\n", + bufsize); + bufsize = FUSE_MIN_READ_BUFFER; + } + + bufsize -= 4096; + if (bufsize < f->conn.max_write) + f->conn.max_write = bufsize; + + f->got_init = 1; + if (f->op.init) + f->op.init(f->userdata, &f->conn); + + if (f->conn.async_read || (f->conn.want & FUSE_CAP_ASYNC_READ)) + outarg.flags |= FUSE_ASYNC_READ; + if (f->conn.want & FUSE_CAP_POSIX_LOCKS) + outarg.flags |= FUSE_POSIX_LOCKS; + if (f->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) + outarg.flags |= FUSE_ATOMIC_O_TRUNC; + if (f->conn.want & FUSE_CAP_EXPORT_SUPPORT) + outarg.flags |= FUSE_EXPORT_SUPPORT; + if (f->conn.want & FUSE_CAP_BIG_WRITES) + outarg.flags |= FUSE_BIG_WRITES; + if (f->conn.want & FUSE_CAP_DONT_MASK) + outarg.flags |= FUSE_DONT_MASK; + outarg.max_readahead = f->conn.max_readahead; + outarg.max_write = f->conn.max_write; + + if (f->debug) { + fprintf(stderr, " INIT: %u.%u\n", outarg.major, outarg.minor); + fprintf(stderr, " flags=0x%08x\n", outarg.flags); + fprintf(stderr, " max_readahead=0x%08x\n", + outarg.max_readahead); + fprintf(stderr, " max_write=0x%08x\n", outarg.max_write); + } + + send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg)); +} + +static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) +{ + struct fuse_ll *f = req->f; + + (void) nodeid; + (void) inarg; + + f->got_destroy = 1; + if (f->op.destroy) + f->op.destroy(f->userdata); + + send_reply_ok(req, NULL, 0); +} + +static int send_notify_iov(struct fuse_ll *f, struct fuse_chan *ch, + int notify_code, struct iovec *iov, int count) +{ + struct fuse_out_header out; + + out.unique = 0; + out.error = notify_code; + iov[0].iov_base = &out; + iov[0].iov_len = sizeof(struct fuse_out_header); + out.len = iov_length(iov, count); + + if (f->debug) + fprintf(stderr, "NOTIFY: code=%d count=%d length=%u\n", + notify_code, count, out.len); + + return fuse_chan_send(ch, iov, count); +} + +int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph) +{ + if (ph != NULL) { + struct fuse_notify_poll_wakeup_out outarg; + struct iovec iov[2]; + + outarg.kh = ph->kh; + + iov[1].iov_base = &outarg; + iov[1].iov_len = sizeof(outarg); + + return send_notify_iov(ph->f, ph->ch, FUSE_NOTIFY_POLL, iov, 2); + } else { + return 0; + } +} + +int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino, + off64_t off, off64_t len) +{ + struct fuse_notify_inval_inode_out outarg; + struct fuse_ll *f; + struct iovec iov[2]; + + if (!ch) + return -EINVAL; + + f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch)); + if (!f) + return -ENODEV; + + outarg.ino = ino; + outarg.off = off; + outarg.len = len; + + iov[1].iov_base = &outarg; + iov[1].iov_len = sizeof(outarg); + + return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_INODE, iov, 2); +} + +int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent, + const char *name, size_t namelen) +{ + struct fuse_notify_inval_entry_out outarg; + struct fuse_ll *f; + struct iovec iov[3]; + + if (!ch) + return -EINVAL; + + f = (struct fuse_ll *)fuse_session_data(fuse_chan_session(ch)); + if (!f) + return -ENODEV; + + outarg.parent = parent; + outarg.namelen = namelen; + outarg.padding = 0; + + iov[1].iov_base = &outarg; + iov[1].iov_len = sizeof(outarg); + iov[2].iov_base = (void *)name; + iov[2].iov_len = namelen + 1; + + return send_notify_iov(f, ch, FUSE_NOTIFY_INVAL_ENTRY, iov, 3); +} + +void *fuse_req_userdata(fuse_req_t req) +{ + return req->f->userdata; +} + +const struct fuse_ctx *fuse_req_ctx(fuse_req_t req) +{ + return &req->ctx; +} + +/* + * The size of fuse_ctx got extended, so need to be careful about + * incompatibility (i.e. a new binary cannot work with an old + * library). + */ +const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req); +const struct fuse_ctx *fuse_req_ctx_compat24(fuse_req_t req) +{ + return fuse_req_ctx(req); +} +FUSE_SYMVER(".symver fuse_req_ctx_compat24,fuse_req_ctx@FUSE_2.4"); + + +void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, + void *data) +{ + pthread_mutex_lock(&req->lock); + pthread_mutex_lock(&req->f->lock); + req->u.ni.func = func; + req->u.ni.data = data; + pthread_mutex_unlock(&req->f->lock); + if (req->interrupted && func) + func(req, data); + pthread_mutex_unlock(&req->lock); +} + +int fuse_req_interrupted(fuse_req_t req) +{ + int interrupted; + + pthread_mutex_lock(&req->f->lock); + interrupted = req->interrupted; + pthread_mutex_unlock(&req->f->lock); + + return interrupted; +} + +static struct { + void (*func)(fuse_req_t, fuse_ino_t, const void *); + const char *name; +} fuse_ll_ops[] = { + [FUSE_LOOKUP] = { do_lookup, "LOOKUP" }, + [FUSE_FORGET] = { do_forget, "FORGET" }, + [FUSE_GETATTR] = { do_getattr, "GETATTR" }, + [FUSE_SETATTR] = { do_setattr, "SETATTR" }, + [FUSE_READLINK] = { do_readlink, "READLINK" }, + [FUSE_SYMLINK] = { do_symlink, "SYMLINK" }, + [FUSE_MKNOD] = { do_mknod, "MKNOD" }, + [FUSE_MKDIR] = { do_mkdir, "MKDIR" }, + [FUSE_UNLINK] = { do_unlink, "UNLINK" }, + [FUSE_RMDIR] = { do_rmdir, "RMDIR" }, + [FUSE_RENAME] = { do_rename, "RENAME" }, + [FUSE_LINK] = { do_link, "LINK" }, + [FUSE_OPEN] = { do_open, "OPEN" }, + [FUSE_READ] = { do_read, "READ" }, + [FUSE_WRITE] = { do_write, "WRITE" }, + [FUSE_STATFS] = { do_statfs, "STATFS" }, + [FUSE_RELEASE] = { do_release, "RELEASE" }, + [FUSE_FSYNC] = { do_fsync, "FSYNC" }, + [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" }, + [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" }, + [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" }, + [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" }, + [FUSE_FLUSH] = { do_flush, "FLUSH" }, + [FUSE_INIT] = { do_init, "INIT" }, + [FUSE_OPENDIR] = { do_opendir, "OPENDIR" }, + [FUSE_READDIR] = { do_readdir, "READDIR" }, + [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" }, + [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" }, + [FUSE_GETLK] = { do_getlk, "GETLK" }, + [FUSE_SETLK] = { do_setlk, "SETLK" }, + [FUSE_SETLKW] = { do_setlkw, "SETLKW" }, + [FUSE_ACCESS] = { do_access, "ACCESS" }, + [FUSE_CREATE] = { do_create, "CREATE" }, + [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" }, + [FUSE_BMAP] = { do_bmap, "BMAP" }, + [FUSE_IOCTL] = { do_ioctl, "IOCTL" }, + [FUSE_POLL] = { do_poll, "POLL" }, + [FUSE_DESTROY] = { do_destroy, "DESTROY" }, + [CUSE_INIT] = { cuse_lowlevel_init, "CUSE_INIT" }, +}; + +#define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) + +static const char *opname(enum fuse_opcode opcode) +{ + if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) + return "???"; + else + return fuse_ll_ops[opcode].name; +} + +static void fuse_ll_process(void *data, const char *buf, size_t len, + struct fuse_chan *ch) +{ + struct fuse_ll *f = (struct fuse_ll *) data; + struct fuse_in_header *in = (struct fuse_in_header *) buf; + const void *inarg = buf + sizeof(struct fuse_in_header); + struct fuse_req *req; + int err; + + if (f->debug) + fprintf(stderr, + "unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %zu\n", + (unsigned long long) in->unique, + opname((enum fuse_opcode) in->opcode), in->opcode, + (unsigned long) in->nodeid, len); + + req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req)); + if (req == NULL) { + fprintf(stderr, "fuse: failed to allocate request\n"); + return; + } + + req->f = f; + req->unique = in->unique; + req->ctx.uid = in->uid; + req->ctx.gid = in->gid; + req->ctx.pid = in->pid; + req->ch = ch; + req->ctr = 1; + list_init_req(req); + fuse_mutex_init(&req->lock); + + err = EIO; + if (!f->got_init) { + enum fuse_opcode expected; + + expected = f->cuse_data ? CUSE_INIT : FUSE_INIT; + if (in->opcode != expected) + goto reply_err; + } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) + goto reply_err; + + err = EACCES; + if (f->allow_root && in->uid != f->owner && in->uid != 0 && + in->opcode != FUSE_INIT && in->opcode != FUSE_READ && + in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && + in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && + in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) + goto reply_err; + + err = ENOSYS; + if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) + goto reply_err; + if (in->opcode != FUSE_INTERRUPT) { + struct fuse_req *intr; + pthread_mutex_lock(&f->lock); + intr = check_interrupt(f, req); + list_add_req(req, &f->list); + pthread_mutex_unlock(&f->lock); + if (intr) + fuse_reply_err(intr, EAGAIN); + } + fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg); + return; + + reply_err: + fuse_reply_err(req, err); +} + +enum { + KEY_HELP, + KEY_VERSION, +}; + +static struct fuse_opt fuse_ll_opts[] = { + { "debug", offsetof(struct fuse_ll, debug), 1 }, + { "-d", offsetof(struct fuse_ll, debug), 1 }, + { "allow_root", offsetof(struct fuse_ll, allow_root), 1 }, + { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 }, + { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 }, + { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 }, + { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 }, + { "atomic_o_trunc", offsetof(struct fuse_ll, atomic_o_trunc), 1}, + { "no_remote_lock", offsetof(struct fuse_ll, no_remote_lock), 1}, + { "big_writes", offsetof(struct fuse_ll, big_writes), 1}, + FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD), + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + FUSE_OPT_END +}; + +static void fuse_ll_version(void) +{ + fprintf(stderr, "using FUSE kernel interface version %i.%i\n", + FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); +} + +static void fuse_ll_help(void) +{ + fprintf(stderr, +" -o max_write=N set maximum size of write requests\n" +" -o max_readahead=N set maximum readahead\n" +" -o async_read perform reads asynchronously (default)\n" +" -o sync_read perform reads synchronously\n" +" -o atomic_o_trunc enable atomic open+truncate support\n" +" -o big_writes enable larger than 4kB writes\n" +" -o no_remote_lock disable remote file locking\n"); +} + +static int fuse_ll_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + (void) data; (void) outargs; + + switch (key) { + case KEY_HELP: + fuse_ll_help(); + break; + + case KEY_VERSION: + fuse_ll_version(); + break; + + default: + fprintf(stderr, "fuse: unknown option `%s'\n", arg); + } + + return -1; +} + +int fuse_lowlevel_is_lib_option(const char *opt) +{ + return fuse_opt_match(fuse_ll_opts, opt); +} + +static void fuse_ll_destroy(void *data) +{ + struct fuse_ll *f = (struct fuse_ll *) data; + + if (f->got_init && !f->got_destroy) { + if (f->op.destroy) + f->op.destroy(f->userdata); + } + + pthread_mutex_destroy(&f->lock); + free(f->cuse_data); + free(f); +} + +/* + * always call fuse_lowlevel_new_common() internally, to work around a + * misfeature in the FreeBSD runtime linker, which links the old + * version of a symbol to internal references. + */ +struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args, + const struct fuse_lowlevel_ops *op, + size_t op_size, void *userdata) +{ + struct fuse_ll *f; + struct fuse_session *se; + struct fuse_session_ops sop = { + .process = fuse_ll_process, + .destroy = fuse_ll_destroy, + }; + + if (sizeof(struct fuse_lowlevel_ops) < op_size) { + fprintf(stderr, "fuse: warning: library too old, some operations may not work\n"); + op_size = sizeof(struct fuse_lowlevel_ops); + } + + f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll)); + if (f == NULL) { + fprintf(stderr, "fuse: failed to allocate fuse object\n"); + goto out; + } + + f->conn.async_read = 1; + f->conn.max_write = UINT_MAX; + f->conn.max_readahead = UINT_MAX; + f->atomic_o_trunc = 0; + list_init_req(&f->list); + list_init_req(&f->interrupts); + fuse_mutex_init(&f->lock); + + if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1) + goto out_free; + + if (f->debug) + fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION); + + memcpy(&f->op, op, op_size); + f->owner = getuid(); + f->userdata = userdata; + + se = fuse_session_new(&sop, f); + if (!se) + goto out_free; + + return se; + +out_free: + free(f); +out: + return NULL; +} + + +struct fuse_session *fuse_lowlevel_new(struct fuse_args *args, + const struct fuse_lowlevel_ops *op, + size_t op_size, void *userdata) +{ + return fuse_lowlevel_new_common(args, op, op_size, userdata); +} + +#ifdef linux +int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]) +{ + char *buf; + size_t bufsize = 1024; + char path[128]; + int ret; + int fd; + unsigned long pid = req->ctx.pid; + char *s; + + sprintf(path, "/proc/%lu/task/%lu/status", pid, pid); + +retry: + buf = malloc(bufsize); + if (buf == NULL) + return -ENOMEM; + + ret = -EIO; + fd = open(path, O_RDONLY); + if (fd == -1) + goto out_free; + + ret = read(fd, buf, bufsize); + close(fd); + if (ret == -1) { + ret = -EIO; + goto out_free; + } + + if (ret == bufsize) { + free(buf); + bufsize *= 4; + goto retry; + } + + ret = -EIO; + s = strstr(buf, "\nGroups:"); + if (s == NULL) + goto out_free; + + s += 8; + ret = 0; + while (1) { + char *end; + unsigned long val = strtoul(s, &end, 0); + if (end == s) + break; + + s = end; + if (ret < size) + list[ret] = val; + ret++; + } + +out_free: + free(buf); + return ret; +} +#else /* linux */ +/* + * This is currently not implemented on other than Linux... + */ +int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]) +{ + return -ENOSYS; +} +#endif + +#ifndef __FreeBSD__ + +static void fill_open_compat(struct fuse_open_out *arg, + const struct fuse_file_info_compat *f) +{ + arg->fh = f->fh; + if (f->direct_io) + arg->open_flags |= FOPEN_DIRECT_IO; + if (f->keep_cache) + arg->open_flags |= FOPEN_KEEP_CACHE; +} + +static void convert_statfs_compat(const struct statfs *compatbuf, + struct statvfs *buf) +{ + buf->f_bsize = compatbuf->f_bsize; + buf->f_blocks = compatbuf->f_blocks; + buf->f_bfree = compatbuf->f_bfree; + buf->f_bavail = compatbuf->f_bavail; + buf->f_files = compatbuf->f_files; + buf->f_ffree = compatbuf->f_ffree; + buf->f_namemax = compatbuf->f_namelen; +} + +int fuse_reply_open_compat(fuse_req_t req, + const struct fuse_file_info_compat *f) +{ + struct fuse_open_out arg; + + memset(&arg, 0, sizeof(arg)); + fill_open_compat(&arg, f); + return send_reply_ok(req, &arg, sizeof(arg)); +} + +int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf) +{ + struct statvfs newbuf; + + memset(&newbuf, 0, sizeof(newbuf)); + convert_statfs_compat(stbuf, &newbuf); + + return fuse_reply_statfs(req, &newbuf); +} + +struct fuse_session *fuse_lowlevel_new_compat(const char *opts, + const struct fuse_lowlevel_ops_compat *op, + size_t op_size, void *userdata) +{ + struct fuse_session *se; + struct fuse_args args = FUSE_ARGS_INIT(0, NULL); + + if (opts && + (fuse_opt_add_arg(&args, "") == -1 || + fuse_opt_add_arg(&args, "-o") == -1 || + fuse_opt_add_arg(&args, opts) == -1)) { + fuse_opt_free_args(&args); + return NULL; + } + se = fuse_lowlevel_new(&args, (const struct fuse_lowlevel_ops *) op, + op_size, userdata); + fuse_opt_free_args(&args); + + return se; +} + +struct fuse_ll_compat_conf { + unsigned max_read; + int set_max_read; +}; + +static const struct fuse_opt fuse_ll_opts_compat[] = { + { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 }, + { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 }, + FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP), + FUSE_OPT_END +}; + +int fuse_sync_compat_args(struct fuse_args *args) +{ + struct fuse_ll_compat_conf conf; + + memset(&conf, 0, sizeof(conf)); + if (fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1) + return -1; + + if (fuse_opt_insert_arg(args, 1, "-osync_read")) + return -1; + + if (conf.set_max_read) { + char tmpbuf[64]; + + sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read); + if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1) + return -1; + } + return 0; +} + +FUSE_SYMVER(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4"); +FUSE_SYMVER(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4"); +FUSE_SYMVER(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4"); + +#else /* __FreeBSD__ */ + +int fuse_sync_compat_args(struct fuse_args *args) +{ + (void) args; + return 0; +} + +#endif /* __FreeBSD__ */ + +struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args, + const struct fuse_lowlevel_ops_compat25 *op, + size_t op_size, void *userdata) +{ + if (fuse_sync_compat_args(args) == -1) + return NULL; + + return fuse_lowlevel_new_common(args, + (const struct fuse_lowlevel_ops *) op, + op_size, userdata); +} + +FUSE_SYMVER(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5"); diff --git a/fuse/fuse_misc.h b/fuse/fuse_misc.h new file mode 100644 index 000000000..c2cfee17a --- /dev/null +++ b/fuse/fuse_misc.h @@ -0,0 +1,53 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include + +/* Versioned symbols confuse the dynamic linker in uClibc */ +#ifndef __UCLIBC__ +#define FUSE_SYMVER(x) __asm__(x) +#else +#define FUSE_SYMVER(x) +#endif + +#ifndef USE_UCLIBC +#define fuse_mutex_init(mut) pthread_mutex_init(mut, NULL) +#else +/* Is this hack still needed? */ +static inline void fuse_mutex_init(pthread_mutex_t *mut) +{ + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); + pthread_mutex_init(mut, &attr); + pthread_mutexattr_destroy(&attr); +} +#endif + +#ifdef HAVE_STRUCT_STAT_ST_ATIM +/* Linux */ +#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atim.tv_nsec) +#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctim.tv_nsec) +#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtim.tv_nsec) +#define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atim.tv_nsec = (val) +#define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtim.tv_nsec = (val) +#elif defined(HAVE_STRUCT_STAT_ST_ATIMESPEC) +/* FreeBSD */ +#define ST_ATIM_NSEC(stbuf) ((stbuf)->st_atimespec.tv_nsec) +#define ST_CTIM_NSEC(stbuf) ((stbuf)->st_ctimespec.tv_nsec) +#define ST_MTIM_NSEC(stbuf) ((stbuf)->st_mtimespec.tv_nsec) +#define ST_ATIM_NSEC_SET(stbuf, val) (stbuf)->st_atimespec.tv_nsec = (val) +#define ST_MTIM_NSEC_SET(stbuf, val) (stbuf)->st_mtimespec.tv_nsec = (val) +#else +#define ST_ATIM_NSEC(stbuf) 0 +#define ST_CTIM_NSEC(stbuf) 0 +#define ST_MTIM_NSEC(stbuf) 0 +#define ST_ATIM_NSEC_SET(stbuf, val) do { } while (0) +#define ST_MTIM_NSEC_SET(stbuf, val) do { } while (0) +#endif diff --git a/fuse/fuse_mt.c b/fuse/fuse_mt.c new file mode 100644 index 000000000..7f940006d --- /dev/null +++ b/fuse/fuse_mt.c @@ -0,0 +1,120 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_lowlevel.h" + +#include +#include +#include +#include +#include + +struct procdata { + struct fuse *f; + struct fuse_chan *prevch; + struct fuse_session *prevse; + fuse_processor_t proc; + void *data; +}; + +#ifdef __MULTI_THREAD + +static void mt_session_proc(void *data, const char *buf, size_t len, + struct fuse_chan *ch) +{ + struct procdata *pd = (struct procdata *) data; + struct fuse_cmd *cmd = *(struct fuse_cmd **) buf; + + (void) len; + (void) ch; + pd->proc(pd->f, cmd, pd->data); +} + +static void mt_session_exit(void *data, int val) +{ + struct procdata *pd = (struct procdata *) data; + if (val) + fuse_session_exit(pd->prevse); + else + fuse_session_reset(pd->prevse); +} + +static int mt_session_exited(void *data) +{ + struct procdata *pd = (struct procdata *) data; + return fuse_session_exited(pd->prevse); +} + +static int mt_chan_receive(struct fuse_chan **chp, char *buf, size_t size) +{ + struct fuse_cmd *cmd; + struct procdata *pd = (struct procdata *) fuse_chan_data(*chp); + + assert(size >= sizeof(cmd)); + + cmd = fuse_read_cmd(pd->f); + if (cmd == NULL) + return 0; + + *(struct fuse_cmd **) buf = cmd; + + return sizeof(cmd); +} + +int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data) +{ + int res; + struct procdata pd; + struct fuse_session *prevse = fuse_get_session(f); + struct fuse_session *se; + struct fuse_chan *prevch = fuse_session_next_chan(prevse, NULL); + struct fuse_chan *ch; + struct fuse_session_ops sop = { + .exit = mt_session_exit, + .exited = mt_session_exited, + .process = mt_session_proc, + }; + struct fuse_chan_ops cop = { + .receive = mt_chan_receive, + }; + + pd.f = f; + pd.prevch = prevch; + pd.prevse = prevse; + pd.proc = proc; + pd.data = data; + + se = fuse_session_new(&sop, &pd); + if (se == NULL) + return -1; + + ch = fuse_chan_new(&cop, fuse_chan_fd(prevch), + sizeof(struct fuse_cmd *), &pd); + if (ch == NULL) { + fuse_session_destroy(se); + return -1; + } + fuse_session_add_chan(se, ch); + res = fuse_session_loop_mt(se); + fuse_session_destroy(se); + return res; +} + +int fuse_loop_mt(struct fuse *f) +{ + if (f == NULL) + return -1; + + return fuse_session_loop_mt(fuse_get_session(f)); +} + +FUSE_SYMVER(".symver fuse_loop_mt_proc,__fuse_loop_mt@"); + +#endif diff --git a/fuse/fuse_opt.c b/fuse/fuse_opt.c new file mode 100644 index 000000000..b15e7db11 --- /dev/null +++ b/fuse/fuse_opt.c @@ -0,0 +1,409 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_opt.h" +#include "fuse_misc.h" + +#include +#include +#include +#include + +struct fuse_opt_context { + void *data; + const struct fuse_opt *opt; + fuse_opt_proc_t proc; + int argctr; + int argc; + char **argv; + struct fuse_args outargs; + char *opts; + int nonopt; +}; + +void fuse_opt_free_args(struct fuse_args *args) +{ + if (args) { + if (args->argv && args->allocated) { + int i; + for (i = 0; i < args->argc; i++) + free(args->argv[i]); + free(args->argv); + } + args->argc = 0; + args->argv = NULL; + args->allocated = 0; + } +} + +static int alloc_failed(void) +{ + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; +} + +int fuse_opt_add_arg(struct fuse_args *args, const char *arg) +{ + char **newargv; + char *newarg; + + assert(!args->argv || args->allocated); + + newargv = realloc(args->argv, (args->argc + 2) * sizeof(char *)); + newarg = newargv ? strdup(arg) : NULL; + if (!newargv || !newarg) + return alloc_failed(); + + args->argv = newargv; + args->allocated = 1; + args->argv[args->argc++] = newarg; + args->argv[args->argc] = NULL; + return 0; +} + +static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos, + const char *arg) +{ + assert(pos <= args->argc); + if (fuse_opt_add_arg(args, arg) == -1) + return -1; + + if (pos != args->argc - 1) { + char *newarg = args->argv[args->argc - 1]; + memmove(&args->argv[pos + 1], &args->argv[pos], + sizeof(char *) * (args->argc - pos - 1)); + args->argv[pos] = newarg; + } + return 0; +} + +int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg) +{ + return fuse_opt_insert_arg_common(args, pos, arg); +} + +int fuse_opt_insert_arg_compat(struct fuse_args *args, int pos, + const char *arg); +int fuse_opt_insert_arg_compat(struct fuse_args *args, int pos, const char *arg) +{ + return fuse_opt_insert_arg_common(args, pos, arg); +} + +static int next_arg(struct fuse_opt_context *ctx, const char *opt) +{ + if (ctx->argctr + 1 >= ctx->argc) { + fprintf(stderr, "fuse: missing argument after `%s'\n", opt); + return -1; + } + ctx->argctr++; + return 0; +} + +static int add_arg(struct fuse_opt_context *ctx, const char *arg) +{ + return fuse_opt_add_arg(&ctx->outargs, arg); +} + +static int add_opt_common(char **opts, const char *opt, int esc) +{ + unsigned oldlen = *opts ? strlen(*opts) : 0; + char *d = realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1); + + if (!d) + return alloc_failed(); + + *opts = d; + if (oldlen) { + d += oldlen; + *d++ = ','; + } + + for (; *opt; opt++) { + if (esc && (*opt == ',' || *opt == '\\')) + *d++ = '\\'; + *d++ = *opt; + } + *d = '\0'; + + return 0; +} + +int fuse_opt_add_opt(char **opts, const char *opt) +{ + return add_opt_common(opts, opt, 0); +} + +int fuse_opt_add_opt_escaped(char **opts, const char *opt) +{ + return add_opt_common(opts, opt, 1); +} + +static int add_opt(struct fuse_opt_context *ctx, const char *opt) +{ + return add_opt_common(&ctx->opts, opt, 1); +} + +static int call_proc(struct fuse_opt_context *ctx, const char *arg, int key, + int iso) +{ + if (key == FUSE_OPT_KEY_DISCARD) + return 0; + + if (key != FUSE_OPT_KEY_KEEP && ctx->proc) { + int res = ctx->proc(ctx->data, arg, key, &ctx->outargs); + if (res == -1 || !res) + return res; + } + if (iso) + return add_opt(ctx, arg); + else + return add_arg(ctx, arg); +} + +static int match_template(const char *t, const char *arg, unsigned *sepp) +{ + int arglen = strlen(arg); + const char *sep = strchr(t, '='); + sep = sep ? sep : strchr(t, ' '); + if (sep && (!sep[1] || sep[1] == '%')) { + int tlen = sep - t; + if (sep[0] == '=') + tlen ++; + if (arglen >= tlen && strncmp(arg, t, tlen) == 0) { + *sepp = sep - t; + return 1; + } + } + if (strcmp(t, arg) == 0) { + *sepp = 0; + return 1; + } + return 0; +} + +static const struct fuse_opt *find_opt(const struct fuse_opt *opt, + const char *arg, unsigned *sepp) +{ + for (; opt && opt->templ; opt++) + if (match_template(opt->templ, arg, sepp)) + return opt; + return NULL; +} + +int fuse_opt_match(const struct fuse_opt *opts, const char *opt) +{ + unsigned dummy; + return find_opt(opts, opt, &dummy) ? 1 : 0; +} + +static int process_opt_param(void *var, const char *format, const char *param, + const char *arg) +{ + assert(format[0] == '%'); + if (format[1] == 's') { + char *copy = strdup(param); + if (!copy) + return alloc_failed(); + + *(char **) var = copy; + } else { + if (sscanf(param, format, var) != 1) { + fprintf(stderr, "fuse: invalid parameter in option `%s'\n", arg); + return -1; + } + } + return 0; +} + +static int process_opt(struct fuse_opt_context *ctx, + const struct fuse_opt *opt, unsigned sep, + const char *arg, int iso) +{ + if (opt->offset == -1U) { + if (call_proc(ctx, arg, opt->value, iso) == -1) + return -1; + } else { + void *var = ctx->data + opt->offset; + if (sep && opt->templ[sep + 1]) { + const char *param = arg + sep; + if (opt->templ[sep] == '=') + param ++; + if (process_opt_param(var, opt->templ + sep + 1, + param, arg) == -1) + return -1; + } else + *(int *)var = opt->value; + } + return 0; +} + +static int process_opt_sep_arg(struct fuse_opt_context *ctx, + const struct fuse_opt *opt, unsigned sep, + const char *arg, int iso) +{ + int res; + char *newarg; + char *param; + + if (next_arg(ctx, arg) == -1) + return -1; + + param = ctx->argv[ctx->argctr]; + newarg = malloc(sep + strlen(param) + 1); + if (!newarg) + return alloc_failed(); + + memcpy(newarg, arg, sep); + strcpy(newarg + sep, param); + res = process_opt(ctx, opt, sep, newarg, iso); + free(newarg); + + return res; +} + +static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int iso) +{ + unsigned sep; + const struct fuse_opt *opt = find_opt(ctx->opt, arg, &sep); + if (opt) { + for (; opt; opt = find_opt(opt + 1, arg, &sep)) { + int res; + if (sep && opt->templ[sep] == ' ' && !arg[sep]) + res = process_opt_sep_arg(ctx, opt, sep, arg, + iso); + else + res = process_opt(ctx, opt, sep, arg, iso); + if (res == -1) + return -1; + } + return 0; + } else + return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso); +} + +static int process_real_option_group(struct fuse_opt_context *ctx, char *opts) +{ + char *s = opts; + char *d = s; + int end = 0; + + while (!end) { + if (*s == '\0') + end = 1; + if (*s == ',' || end) { + int res; + + *d = '\0'; + res = process_gopt(ctx, opts, 1); + if (res == -1) + return -1; + d = opts; + } else { + if (s[0] == '\\' && s[1] != '\0') + s++; + *d++ = *s; + } + s++; + } + + return 0; +} + +static int process_option_group(struct fuse_opt_context *ctx, const char *opts) +{ + int res; + char *copy = strdup(opts); + + if (!copy) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + res = process_real_option_group(ctx, copy); + free(copy); + return res; +} + +static int process_one(struct fuse_opt_context *ctx, const char *arg) +{ + if (ctx->nonopt || arg[0] != '-') + return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0); + else if (arg[1] == 'o') { + if (arg[2]) + return process_option_group(ctx, arg + 2); + else { + if (next_arg(ctx, arg) == -1) + return -1; + + return process_option_group(ctx, + ctx->argv[ctx->argctr]); + } + } else if (arg[1] == '-' && !arg[2]) { + if (add_arg(ctx, arg) == -1) + return -1; + ctx->nonopt = ctx->outargs.argc; + return 0; + } else + return process_gopt(ctx, arg, 0); +} + +static int opt_parse(struct fuse_opt_context *ctx) +{ + if (ctx->argc) { + if (add_arg(ctx, ctx->argv[0]) == -1) + return -1; + } + + for (ctx->argctr = 1; ctx->argctr < ctx->argc; ctx->argctr++) + if (process_one(ctx, ctx->argv[ctx->argctr]) == -1) + return -1; + + if (ctx->opts) { + if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") == -1 || + fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) == -1) + return -1; + } + + /* If option separator ("--") is the last argument, remove it */ + if (ctx->nonopt && ctx->nonopt == ctx->outargs.argc && + strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") == 0) { + free(ctx->outargs.argv[ctx->outargs.argc - 1]); + ctx->outargs.argv[--ctx->outargs.argc] = NULL; + } + + return 0; +} + +int fuse_opt_parse(struct fuse_args *args, void *data, + const struct fuse_opt opts[], fuse_opt_proc_t proc) +{ + int res; + struct fuse_opt_context ctx = { + .data = data, + .opt = opts, + .proc = proc, + }; + + if (!args || !args->argv || !args->argc) + return 0; + + ctx.argc = args->argc; + ctx.argv = args->argv; + + res = opt_parse(&ctx); + if (res != -1) { + struct fuse_args tmp = *args; + *args = ctx.outargs; + ctx.outargs = tmp; + } + free(ctx.opts); + fuse_opt_free_args(&ctx.outargs); + return res; +} + +/* This symbol version was mistakenly added to the version script */ +FUSE_SYMVER(".symver fuse_opt_insert_arg_compat,fuse_opt_insert_arg@FUSE_2.5"); diff --git a/fuse/fuse_session.c b/fuse/fuse_session.c new file mode 100644 index 000000000..3758627de --- /dev/null +++ b/fuse/fuse_session.c @@ -0,0 +1,205 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_common_compat.h" +#include "fuse_lowlevel_compat.h" + +#include +#include +#include +#include +#include + +struct fuse_chan { + struct fuse_chan_ops op; + + struct fuse_session *se; + + int fd; + + size_t bufsize; + + void *data; + + int compat; +}; + +struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data) +{ + struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se)); + if (se == NULL) { + fprintf(stderr, "fuse: failed to allocate session\n"); + return NULL; + } + + memset(se, 0, sizeof(*se)); + se->op = *op; + se->data = data; + + return se; +} + +void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch) +{ + assert(se->ch == NULL); + assert(ch->se == NULL); + se->ch = ch; + ch->se = se; +} + +void fuse_session_remove_chan(struct fuse_chan *ch) +{ + struct fuse_session *se = ch->se; + if (se) { + assert(se->ch == ch); + se->ch = NULL; + ch->se = NULL; + } +} + +struct fuse_chan *fuse_session_next_chan(struct fuse_session *se, + struct fuse_chan *ch) +{ + assert(ch == NULL || ch == se->ch); + if (ch == NULL) + return se->ch; + else + return NULL; +} + +void fuse_session_process(struct fuse_session *se, const char *buf, size_t len, + struct fuse_chan *ch) +{ + se->op.process(se->data, buf, len, ch); +} + +void fuse_session_destroy(struct fuse_session *se) +{ + if (se->op.destroy) + se->op.destroy(se->data); + if (se->ch != NULL) + fuse_chan_destroy(se->ch); + free(se); +} + +void fuse_session_exit(struct fuse_session *se) +{ + if (se->op.exit) + se->op.exit(se->data, 1); + se->exited = 1; +} + +void fuse_session_reset(struct fuse_session *se) +{ + if (se->op.exit) + se->op.exit(se->data, 0); + se->exited = 0; +} + +int fuse_session_exited(struct fuse_session *se) +{ + if (se->op.exited) + return se->op.exited(se->data); + else + return se->exited; +} + +void *fuse_session_data(struct fuse_session *se) +{ + return se->data; +} + +static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd, + size_t bufsize, void *data, + int compat) +{ + struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch)); + if (ch == NULL) { + fprintf(stderr, "fuse: failed to allocate channel\n"); + return NULL; + } + + memset(ch, 0, sizeof(*ch)); + ch->op = *op; + ch->fd = fd; + ch->bufsize = bufsize; + ch->data = data; + ch->compat = compat; + + return ch; +} + +struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd, + size_t bufsize, void *data) +{ + return fuse_chan_new_common(op, fd, bufsize, data, 0); +} + +struct fuse_chan *fuse_chan_new_compat24(struct fuse_chan_ops_compat24 *op, + int fd, size_t bufsize, void *data) +{ + return fuse_chan_new_common((struct fuse_chan_ops *) op, fd, bufsize, + data, 24); +} + +int fuse_chan_fd(struct fuse_chan *ch) +{ + return ch->fd; +} + +size_t fuse_chan_bufsize(struct fuse_chan *ch) +{ + return ch->bufsize; +} + +void *fuse_chan_data(struct fuse_chan *ch) +{ + return ch->data; +} + +struct fuse_session *fuse_chan_session(struct fuse_chan *ch) +{ + return ch->se; +} + +int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size) +{ + struct fuse_chan *ch = *chp; + if (ch->compat) + return ((struct fuse_chan_ops_compat24 *) &ch->op) + ->receive(ch, buf, size); + else + return ch->op.receive(chp, buf, size); +} + +int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size) +{ + int res; + + res = fuse_chan_recv(&ch, buf, size); + return res >= 0 ? res : (res != -EINTR && res != -EAGAIN) ? -1 : 0; +} + +int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], size_t count) +{ + return ch->op.send(ch, iov, count); +} + +void fuse_chan_destroy(struct fuse_chan *ch) +{ + fuse_session_remove_chan(ch); + if (ch->op.destroy) + ch->op.destroy(ch); + free(ch); +} + +#ifndef __FreeBSD__ +FUSE_SYMVER(".symver fuse_chan_new_compat24,fuse_chan_new@FUSE_2.4"); +#endif diff --git a/fuse/fuse_signals.c b/fuse/fuse_signals.c new file mode 100644 index 000000000..88ac39e18 --- /dev/null +++ b/fuse/fuse_signals.c @@ -0,0 +1,72 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_lowlevel.h" + +#include +#include +#include + +static struct fuse_session *fuse_instance; + +static void exit_handler(int sig) +{ + (void) sig; + if (fuse_instance) + fuse_session_exit(fuse_instance); +} + +static int set_one_signal_handler(int sig, void (*handler)(int)) +{ + struct sigaction sa; + struct sigaction old_sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler = handler; + sigemptyset(&(sa.sa_mask)); + sa.sa_flags = 0; + + if (sigaction(sig, NULL, &old_sa) == -1) { + perror("fuse: cannot get old signal handler"); + return -1; + } + + if (old_sa.sa_handler == SIG_DFL && + sigaction(sig, &sa, NULL) == -1) { + perror("fuse: cannot set signal handler"); + return -1; + } + return 0; +} + +int fuse_set_signal_handlers(struct fuse_session *se) +{ + if (set_one_signal_handler(SIGHUP, exit_handler) == -1 || + set_one_signal_handler(SIGINT, exit_handler) == -1 || + set_one_signal_handler(SIGTERM, exit_handler) == -1 || + set_one_signal_handler(SIGPIPE, SIG_IGN) == -1) + return -1; + + fuse_instance = se; + return 0; +} + +void fuse_remove_signal_handlers(struct fuse_session *se) +{ + if (fuse_instance != se) + fprintf(stderr, + "fuse: fuse_remove_signal_handlers: unknown session\n"); + else + fuse_instance = NULL; + + set_one_signal_handler(SIGHUP, SIG_DFL); + set_one_signal_handler(SIGINT, SIG_DFL); + set_one_signal_handler(SIGTERM, SIG_DFL); + set_one_signal_handler(SIGPIPE, SIG_DFL); +} + diff --git a/fuse/fusexmp.c b/fuse/fusexmp.c new file mode 100644 index 000000000..d4edbc9ae --- /dev/null +++ b/fuse/fusexmp.c @@ -0,0 +1,385 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. + + gcc -Wall `pkg-config fuse --cflags --libs` fusexmp.c -o fusexmp +*/ + +#define FUSE_USE_VERSION 26 + +#ifdef HAVE_CONFIG_H +#include +#endif + +#ifdef linux +/* For pread()/pwrite() */ +#define _XOPEN_SOURCE 500 +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef HAVE_SETXATTR +#include +#endif + +static int xmp_getattr(const char *path, struct stat *stbuf) +{ + int res; + + res = lstat(path, stbuf); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_access(const char *path, int mask) +{ + int res; + + res = access(path, mask); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_readlink(const char *path, char *buf, size_t size) +{ + int res; + + res = readlink(path, buf, size - 1); + if (res == -1) + return -errno; + + buf[res] = '\0'; + return 0; +} + + +static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off64_t offset, struct fuse_file_info *fi) +{ + DIR *dp; + struct dirent *de; + + (void) offset; + (void) fi; + + dp = opendir(path); + if (dp == NULL) + return -errno; + + while ((de = readdir(dp)) != NULL) { + struct stat st; + memset(&st, 0, sizeof(st)); + st.st_ino = de->d_ino; + st.st_mode = de->d_type << 12; + if (filler(buf, de->d_name, &st, 0)) + break; + } + + closedir(dp); + return 0; +} + +static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) +{ + int res; + + /* On Linux this could just be 'mknod(path, mode, rdev)' but this + is more portable */ + if (S_ISREG(mode)) { + res = open(path, O_CREAT | O_EXCL | O_WRONLY, mode); + if (res >= 0) + res = close(res); + } else if (S_ISFIFO(mode)) + res = mkfifo(path, mode); + else + res = mknod(path, mode, rdev); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_mkdir(const char *path, mode_t mode) +{ + int res; + + res = mkdir(path, mode); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_unlink(const char *path) +{ + int res; + + res = unlink(path); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_rmdir(const char *path) +{ + int res; + + res = rmdir(path); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_symlink(const char *from, const char *to) +{ + int res; + + res = symlink(from, to); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_rename(const char *from, const char *to) +{ + int res; + + res = rename(from, to); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_link(const char *from, const char *to) +{ + int res; + + res = link(from, to); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_chmod(const char *path, mode_t mode) +{ + int res; + + res = chmod(path, mode); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_chown(const char *path, uid_t uid, gid_t gid) +{ + int res; + + res = lchown(path, uid, gid); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_truncate(const char *path, off64_t size) +{ + int res; + + res = truncate(path, size); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_utimens(const char *path, const struct timespec ts[2]) +{ + int res; + struct timeval tv[2]; + + tv[0].tv_sec = ts[0].tv_sec; + tv[0].tv_usec = ts[0].tv_nsec / 1000; + tv[1].tv_sec = ts[1].tv_sec; + tv[1].tv_usec = ts[1].tv_nsec / 1000; + + res = utimes(path, tv); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_open(const char *path, struct fuse_file_info *fi) +{ + int res; + + res = open(path, fi->flags); + if (res == -1) + return -errno; + + close(res); + return 0; +} + +static int xmp_read(const char *path, char *buf, size_t size, off64_t offset, + struct fuse_file_info *fi) +{ + int fd; + int res; + + (void) fi; + fd = open(path, O_RDONLY); + if (fd == -1) + return -errno; + + res = pread(fd, buf, size, offset); + if (res == -1) + res = -errno; + + close(fd); + return res; +} + +static int xmp_write(const char *path, const char *buf, size_t size, + off64_t offset, struct fuse_file_info *fi) +{ + int fd; + int res; + + (void) fi; + fd = open(path, O_WRONLY); + if (fd == -1) + return -errno; + + res = pwrite(fd, buf, size, offset); + if (res == -1) + res = -errno; + + close(fd); + return res; +} + +static int xmp_statfs(const char *path, struct statvfs *stbuf) +{ + int res; + + //res = statvfs(path, stbuf); + if (res == -1) + return -errno; + + return 0; +} + +static int xmp_release(const char *path, struct fuse_file_info *fi) +{ + /* Just a stub. This method is optional and can safely be left + unimplemented */ + + (void) path; + (void) fi; + return 0; +} + +static int xmp_fsync(const char *path, int isdatasync, + struct fuse_file_info *fi) +{ + /* Just a stub. This method is optional and can safely be left + unimplemented */ + + (void) path; + (void) isdatasync; + (void) fi; + return 0; +} + +#ifdef HAVE_SETXATTR +/* xattr operations are optional and can safely be left unimplemented */ +static int xmp_setxattr(const char *path, const char *name, const char *value, + size_t size, int flags) +{ + int res = lsetxattr(path, name, value, size, flags); + if (res == -1) + return -errno; + return 0; +} + +static int xmp_getxattr(const char *path, const char *name, char *value, + size_t size) +{ + int res = lgetxattr(path, name, value, size); + if (res == -1) + return -errno; + return res; +} + +static int xmp_listxattr(const char *path, char *list, size_t size) +{ + int res = llistxattr(path, list, size); + if (res == -1) + return -errno; + return res; +} + +static int xmp_removexattr(const char *path, const char *name) +{ + int res = lremovexattr(path, name); + if (res == -1) + return -errno; + return 0; +} +#endif /* HAVE_SETXATTR */ + +static struct fuse_operations xmp_oper = { + .getattr = xmp_getattr, + .access = xmp_access, + .readlink = xmp_readlink, + .readdir = xmp_readdir, + .mknod = xmp_mknod, + .mkdir = xmp_mkdir, + .symlink = xmp_symlink, + .unlink = xmp_unlink, + .rmdir = xmp_rmdir, + .rename = xmp_rename, + .link = xmp_link, + .chmod = xmp_chmod, + .chown = xmp_chown, + .truncate = xmp_truncate, + .utimens = xmp_utimens, + .open = xmp_open, + .read = xmp_read, + .write = xmp_write, + .statfs = xmp_statfs, + .release = xmp_release, + .fsync = xmp_fsync, +#ifdef HAVE_SETXATTR + .setxattr = xmp_setxattr, + .getxattr = xmp_getxattr, + .listxattr = xmp_listxattr, + .removexattr = xmp_removexattr, +#endif +}; + +int main(int argc, char *argv[]) +{ + umask(0); + return fuse_main(argc, argv, &xmp_oper, NULL); +} diff --git a/fuse/helper.c b/fuse/helper.c new file mode 100644 index 000000000..7b994fd3b --- /dev/null +++ b/fuse/helper.c @@ -0,0 +1,455 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_opt.h" +#include "fuse_lowlevel.h" +#include "fuse_common_compat.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +enum { + KEY_HELP, + KEY_HELP_NOHEADER, + KEY_VERSION, +}; + +struct helper_opts { + int singlethread; + int foreground; + int nodefault_subtype; + char *mountpoint; +}; + +#define FUSE_HELPER_OPT(t, p) { t, offsetof(struct helper_opts, p), 1 } + +static const struct fuse_opt fuse_helper_opts[] = { + FUSE_HELPER_OPT("-d", foreground), + FUSE_HELPER_OPT("debug", foreground), + FUSE_HELPER_OPT("-f", foreground), + FUSE_HELPER_OPT("-s", singlethread), + FUSE_HELPER_OPT("fsname=", nodefault_subtype), + FUSE_HELPER_OPT("subtype=", nodefault_subtype), + + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("-ho", KEY_HELP_NOHEADER), + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("fsname=", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_KEEP), + FUSE_OPT_END +}; + +static void usage(const char *progname) +{ + fprintf(stderr, + "usage: %s mountpoint [options]\n\n", progname); + fprintf(stderr, + "general options:\n" + " -o opt,[opt...] mount options\n" + " -h --help print help\n" + " -V --version print version\n" + "\n"); +} + +static void helper_help(void) +{ + fprintf(stderr, + "FUSE options:\n" + " -d -o debug enable debug output (implies -f)\n" + " -f foreground operation\n" + " -s disable multi-threaded operation\n" + "\n" + ); +} + +static void helper_version(void) +{ + fprintf(stderr, "FUSE library version: %s\n", PACKAGE_VERSION); +} + +static int fuse_helper_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + struct helper_opts *hopts = data; + + switch (key) { + case KEY_HELP: + usage(outargs->argv[0]); + /* fall through */ + + case KEY_HELP_NOHEADER: + helper_help(); + return fuse_opt_add_arg(outargs, "-h"); + + case KEY_VERSION: + helper_version(); + return 1; + + case FUSE_OPT_KEY_NONOPT: + if (!hopts->mountpoint) { + char mountpoint[PATH_MAX]; + if (realpath(arg, mountpoint) == NULL) { + fprintf(stderr, + "fuse: bad mount point `%s': %s\n", + arg, strerror(errno)); + return -1; + } + return fuse_opt_add_opt(&hopts->mountpoint, mountpoint); + } else { + fprintf(stderr, "fuse: invalid argument `%s'\n", arg); + return -1; + } + + default: + return 1; + } +} + +static int add_default_subtype(const char *progname, struct fuse_args *args) +{ + int res; + char *subtype_opt; + const char *basename = strrchr(progname, '/'); + if (basename == NULL) + basename = progname; + else if (basename[1] != '\0') + basename++; + + subtype_opt = (char *) malloc(strlen(basename) + 64); + if (subtype_opt == NULL) { + fprintf(stderr, "fuse: memory allocation failed\n"); + return -1; + } + sprintf(subtype_opt, "-osubtype=%s", basename); + res = fuse_opt_add_arg(args, subtype_opt); + free(subtype_opt); + return res; +} + +int fuse_parse_cmdline(struct fuse_args *args, char **mountpoint, + int *multithreaded, int *foreground) +{ + int res; + struct helper_opts hopts; + + memset(&hopts, 0, sizeof(hopts)); + res = fuse_opt_parse(args, &hopts, fuse_helper_opts, + fuse_helper_opt_proc); + if (res == -1) + return -1; + + if (!hopts.nodefault_subtype) { + res = add_default_subtype(args->argv[0], args); + if (res == -1) + goto err; + } + if (mountpoint) + *mountpoint = hopts.mountpoint; + else + free(hopts.mountpoint); + + if (multithreaded) + *multithreaded = !hopts.singlethread; + if (foreground) + *foreground = hopts.foreground; + return 0; + +err: + free(hopts.mountpoint); + return -1; +} + +int fuse_daemonize(int foreground) +{ + int res; + + if (!foreground) { + res = daemon(0, 0); + if (res == -1) { + perror("fuse: failed to daemonize program\n"); + return -1; + } + } + return 0; +} + +static struct fuse_chan *fuse_mount_common(const char *mountpoint, + struct fuse_args *args) +{ + struct fuse_chan *ch; + int fd; + + /* + * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos + * would ensue. + */ + do { + fd = open("/dev/null", O_RDWR); + if (fd > 2) + close(fd); + } while (fd >= 0 && fd <= 2); + + fd = fuse_mount_compat25(mountpoint, args); + if (fd == -1) + return NULL; + + ch = fuse_kern_chan_new(fd); + if (!ch) + fuse_kern_unmount(mountpoint, fd); + + return ch; +} + +struct fuse_chan *fuse_mount(const char *mountpoint, struct fuse_args *args) +{ + return fuse_mount_common(mountpoint, args); +} + +static void fuse_unmount_common(const char *mountpoint, struct fuse_chan *ch) +{ + int fd = ch ? fuse_chan_fd(ch) : -1; + fuse_kern_unmount(mountpoint, fd); + fuse_chan_destroy(ch); +} + +void fuse_unmount(const char *mountpoint, struct fuse_chan *ch) +{ + fuse_unmount_common(mountpoint, ch); +} + +struct fuse *fuse_setup_common(int argc, char *argv[], + const struct fuse_operations *op, + size_t op_size, + char **mountpoint, + int *multithreaded, + int *fd, + void *user_data, + int compat) +{ + struct fuse_args args = FUSE_ARGS_INIT(argc, argv); + struct fuse_chan *ch; + struct fuse *fuse; + int foreground; + int res; + + res = fuse_parse_cmdline(&args, mountpoint, multithreaded, &foreground); + if (res == -1) + return NULL; + + ch = fuse_mount_common(*mountpoint, &args); + if (!ch) { + fuse_opt_free_args(&args); + goto err_free; + } + + fuse = fuse_new_common(ch, &args, op, op_size, user_data, compat); + fuse_opt_free_args(&args); + if (fuse == NULL) + goto err_unmount; + + res = fuse_daemonize(foreground); + if (res == -1) + goto err_unmount; + + res = fuse_set_signal_handlers(fuse_get_session(fuse)); + if (res == -1) + goto err_unmount; + + if (fd) + *fd = fuse_chan_fd(ch); + + return fuse; + +err_unmount: + fuse_unmount_common(*mountpoint, ch); + if (fuse) + fuse_destroy(fuse); +err_free: + free(*mountpoint); + return NULL; +} + +struct fuse *fuse_setup(int argc, char *argv[], + const struct fuse_operations *op, size_t op_size, + char **mountpoint, int *multithreaded, void *user_data) +{ + return fuse_setup_common(argc, argv, op, op_size, mountpoint, + multithreaded, NULL, user_data, 0); +} + +static void fuse_teardown_common(struct fuse *fuse, char *mountpoint) +{ + struct fuse_session *se = fuse_get_session(fuse); + struct fuse_chan *ch = fuse_session_next_chan(se, NULL); + fuse_remove_signal_handlers(se); + fuse_unmount_common(mountpoint, ch); + fuse_destroy(fuse); + free(mountpoint); +} + +void fuse_teardown(struct fuse *fuse, char *mountpoint) +{ + fuse_teardown_common(fuse, mountpoint); +} + +static int fuse_main_common(int argc, char *argv[], + const struct fuse_operations *op, size_t op_size, + void *user_data, int compat) +{ + struct fuse *fuse; + char *mountpoint; + int multithreaded; + int res; + + fuse = fuse_setup_common(argc, argv, op, op_size, &mountpoint, + &multithreaded, NULL, user_data, compat); + if (fuse == NULL) + return 1; + +#ifdef __MULTI_THREAD + if (multithreaded) + res = fuse_loop_mt(fuse); + else +#endif + res = fuse_loop(fuse); + + fuse_teardown_common(fuse, mountpoint); + if (res == -1) + return 1; + + return 0; +} + +int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, + size_t op_size, void *user_data) +{ + return fuse_main_common(argc, argv, op, op_size, user_data, 0); +} + +#undef fuse_main +int fuse_main(void); +int fuse_main(void) +{ + fprintf(stderr, "fuse_main(): This function does not exist\n"); + return -1; +} + +int fuse_version(void) +{ + return FUSE_VERSION; +} + +#include "fuse_compat.h" + +#ifndef __FreeBSD__ + +struct fuse *fuse_setup_compat22(int argc, char *argv[], + const struct fuse_operations_compat22 *op, + size_t op_size, char **mountpoint, + int *multithreaded, int *fd) +{ + return fuse_setup_common(argc, argv, (struct fuse_operations *) op, + op_size, mountpoint, multithreaded, fd, NULL, + 22); +} + +struct fuse *fuse_setup_compat2(int argc, char *argv[], + const struct fuse_operations_compat2 *op, + char **mountpoint, int *multithreaded, + int *fd) +{ + return fuse_setup_common(argc, argv, (struct fuse_operations *) op, + sizeof(struct fuse_operations_compat2), + mountpoint, multithreaded, fd, NULL, 21); +} + +int fuse_main_real_compat22(int argc, char *argv[], + const struct fuse_operations_compat22 *op, + size_t op_size) +{ + return fuse_main_common(argc, argv, (struct fuse_operations *) op, + op_size, NULL, 22); +} + +void fuse_main_compat1(int argc, char *argv[], + const struct fuse_operations_compat1 *op) +{ + fuse_main_common(argc, argv, (struct fuse_operations *) op, + sizeof(struct fuse_operations_compat1), NULL, 11); +} + +int fuse_main_compat2(int argc, char *argv[], + const struct fuse_operations_compat2 *op) +{ + return fuse_main_common(argc, argv, (struct fuse_operations *) op, + sizeof(struct fuse_operations_compat2), NULL, + 21); +} + +int fuse_mount_compat1(const char *mountpoint, const char *args[]) +{ + /* just ignore mount args for now */ + (void) args; + return fuse_mount_compat22(mountpoint, NULL); +} + +FUSE_SYMVER(".symver fuse_setup_compat2,__fuse_setup@"); +FUSE_SYMVER(".symver fuse_setup_compat22,fuse_setup@FUSE_2.2"); +FUSE_SYMVER(".symver fuse_teardown,__fuse_teardown@"); +FUSE_SYMVER(".symver fuse_main_compat2,fuse_main@"); +FUSE_SYMVER(".symver fuse_main_real_compat22,fuse_main_real@FUSE_2.2"); + +#endif /* __FreeBSD__ */ + + +struct fuse *fuse_setup_compat25(int argc, char *argv[], + const struct fuse_operations_compat25 *op, + size_t op_size, char **mountpoint, + int *multithreaded, int *fd) +{ + return fuse_setup_common(argc, argv, (struct fuse_operations *) op, + op_size, mountpoint, multithreaded, fd, NULL, + 25); +} + +int fuse_main_real_compat25(int argc, char *argv[], + const struct fuse_operations_compat25 *op, + size_t op_size) +{ + return fuse_main_common(argc, argv, (struct fuse_operations *) op, + op_size, NULL, 25); +} + +void fuse_teardown_compat22(struct fuse *fuse, int fd, char *mountpoint) +{ + (void) fd; + fuse_teardown_common(fuse, mountpoint); +} + +int fuse_mount_compat25(const char *mountpoint, struct fuse_args *args) +{ + return fuse_kern_mount(mountpoint, args); +} + +FUSE_SYMVER(".symver fuse_setup_compat25,fuse_setup@FUSE_2.5"); +FUSE_SYMVER(".symver fuse_teardown_compat22,fuse_teardown@FUSE_2.2"); +FUSE_SYMVER(".symver fuse_main_real_compat25,fuse_main_real@FUSE_2.5"); +FUSE_SYMVER(".symver fuse_mount_compat25,fuse_mount@FUSE_2.5"); diff --git a/fuse/include/Makefile b/fuse/include/Makefile new file mode 100644 index 000000000..43fd00aa8 --- /dev/null +++ b/fuse/include/Makefile @@ -0,0 +1,515 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# include/Makefile. Generated from Makefile.in by configure. + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, +# Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + + + + +pkgdatadir = $(datadir)/fuse +pkgincludedir = $(includedir)/fuse +pkglibdir = $(libdir)/fuse +pkglibexecdir = $(libexecdir)/fuse +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = i686-pc-linux-gnu +host_triplet = i686-pc-linux-gnu +target_triplet = i686-pc-linux-gnu +subdir = include +DIST_COMMON = $(fuseinclude_HEADERS) $(include_HEADERS) \ + $(noinst_HEADERS) $(srcdir)/Makefile.am $(srcdir)/Makefile.in \ + $(srcdir)/config.h.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/configure.in +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +SOURCES = +DIST_SOURCES = +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`; +am__install_max = 40 +am__nobase_strip_setup = \ + srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'` +am__nobase_strip = \ + for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||" +am__nobase_list = $(am__nobase_strip_setup); \ + for p in $$list; do echo "$$p $$p"; done | \ + sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \ + $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \ + if (++n[$$2] == $(am__install_max)) \ + { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \ + END { for (dir in files) print dir, files[dir] }' +am__base_list = \ + sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \ + sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g' +am__installdirs = "$(DESTDIR)$(fuseincludedir)" \ + "$(DESTDIR)$(includedir)" +HEADERS = $(fuseinclude_HEADERS) $(include_HEADERS) $(noinst_HEADERS) +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = ${SHELL} /root/fuse-2.8.5/missing --run aclocal-1.11 +AMTAR = ${SHELL} /root/fuse-2.8.5/missing --run tar +AR = ar +AUTOCONF = ${SHELL} /root/fuse-2.8.5/missing --run autoconf +AUTOHEADER = ${SHELL} /root/fuse-2.8.5/missing --run autoheader +AUTOMAKE = ${SHELL} /root/fuse-2.8.5/missing --run automake-1.11 +AWK = mawk +CC = gcc +CCDEPMODE = depmode=gcc3 +CFLAGS = -Wall -W -Wno-sign-compare -Wstrict-prototypes -Wmissing-declarations -Wwrite-strings -g -O2 -fno-strict-aliasing +CPP = gcc -E +CPPFLAGS = +CYGPATH_W = echo +DEFS = -DHAVE_CONFIG_H +DEPDIR = .deps +DSYMUTIL = +DUMPBIN = +ECHO_C = +ECHO_N = -n +ECHO_T = +EGREP = /bin/grep -E +EXEEXT = +FGREP = /bin/grep -F +GREP = /bin/grep +INIT_D_PATH = /etc/init.d +INSTALL = /usr/bin/install -c +INSTALL_DATA = ${INSTALL} -m 644 +INSTALL_PROGRAM = ${INSTALL} +INSTALL_SCRIPT = ${INSTALL} +INSTALL_STRIP_PROGRAM = $(install_sh) -c -s +LD = /usr/bin/ld +LDFLAGS = +LIBICONV = +LIBOBJS = +LIBS = +LIBTOOL = $(SHELL) $(top_builddir)/libtool +LIPO = +LN_S = ln -s +LTLIBICONV = +LTLIBOBJS = +MAKEINFO = ${SHELL} /root/fuse-2.8.5/missing --run makeinfo +MKDIR_P = /bin/mkdir -p +MOUNT_FUSE_PATH = /sbin +NM = /usr/bin/nm -B +NMEDIT = +OBJDUMP = objdump +OBJEXT = o +OTOOL = +OTOOL64 = +PACKAGE = fuse +PACKAGE_BUGREPORT = +PACKAGE_NAME = fuse +PACKAGE_STRING = fuse 2.8.5 +PACKAGE_TARNAME = fuse +PACKAGE_VERSION = 2.8.5 +PATH_SEPARATOR = : +RANLIB = ranlib +SED = /bin/sed +SET_MAKE = +SHELL = /bin/bash +STRIP = strip +UDEV_RULES_PATH = /etc/udev/rules.d +VERSION = 2.8.5 +abs_builddir = /root/fuse-2.8.5/include +abs_srcdir = /root/fuse-2.8.5/include +abs_top_builddir = /root/fuse-2.8.5 +abs_top_srcdir = /root/fuse-2.8.5 +ac_ct_CC = gcc +ac_ct_DUMPBIN = +am__include = include +am__leading_dot = . +am__quote = +am__tar = ${AMTAR} chof - "$$tardir" +am__untar = ${AMTAR} xf - +bindir = ${exec_prefix}/bin +build = i686-pc-linux-gnu +build_alias = +build_cpu = i686 +build_os = linux-gnu +build_vendor = pc +builddir = . +datadir = ${datarootdir} +datarootdir = ${prefix}/share +docdir = ${datarootdir}/doc/${PACKAGE_TARNAME} +dvidir = ${docdir} +exec_prefix = ${prefix} +host = i686-pc-linux-gnu +host_alias = +host_cpu = i686 +host_os = linux-gnu +host_vendor = pc +htmldir = ${docdir} +includedir = ${prefix}/include +infodir = ${datarootdir}/info +install_sh = ${SHELL} /root/fuse-2.8.5/install-sh +libdir = ${exec_prefix}/lib +libexecdir = ${exec_prefix}/libexec +libfuse_libs = -pthread -lrt -ldl +localedir = ${datarootdir}/locale +localstatedir = ${prefix}/var +lt_ECHO = echo +mandir = ${datarootdir}/man +mkdir_p = /bin/mkdir -p +oldincludedir = /usr/include +pdfdir = ${docdir} +pkgconfigdir = ${libdir}/pkgconfig +prefix = /usr/local +program_transform_name = s,x,x, +psdir = ${docdir} +sbindir = ${exec_prefix}/sbin +sharedstatedir = ${prefix}/com +srcdir = . +subdirs2 = include lib util example +sysconfdir = ${prefix}/etc +target = i686-pc-linux-gnu +target_alias = +target_cpu = i686 +target_os = linux-gnu +target_vendor = pc +top_build_prefix = ../ +top_builddir = .. +top_srcdir = .. +fuseincludedir = $(includedir)/fuse +fuseinclude_HEADERS = \ + fuse.h \ + fuse_compat.h \ + fuse_common.h \ + fuse_common_compat.h \ + fuse_lowlevel.h \ + fuse_lowlevel_compat.h \ + fuse_opt.h \ + cuse_lowlevel.h + +include_HEADERS = old/fuse.h ulockmgr.h +noinst_HEADERS = fuse_kernel.h +all: config.h + $(MAKE) $(AM_MAKEFLAGS) all-am + +.SUFFIXES: +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu include/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --gnu include/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +config.h: stamp-h1 + @if test ! -f $@; then \ + rm -f stamp-h1; \ + $(MAKE) $(AM_MAKEFLAGS) stamp-h1; \ + else :; fi + +stamp-h1: $(srcdir)/config.h.in $(top_builddir)/config.status + @rm -f stamp-h1 + cd $(top_builddir) && $(SHELL) ./config.status include/config.h +$(srcdir)/config.h.in: $(am__configure_deps) + ($(am__cd) $(top_srcdir) && $(AUTOHEADER)) + rm -f stamp-h1 + touch $@ + +distclean-hdr: + -rm -f config.h stamp-h1 + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs +install-fuseincludeHEADERS: $(fuseinclude_HEADERS) + @$(NORMAL_INSTALL) + test -z "$(fuseincludedir)" || $(MKDIR_P) "$(DESTDIR)$(fuseincludedir)" + @list='$(fuseinclude_HEADERS)'; test -n "$(fuseincludedir)" || list=; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(fuseincludedir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(fuseincludedir)" || exit $$?; \ + done + +uninstall-fuseincludeHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(fuseinclude_HEADERS)'; test -n "$(fuseincludedir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + test -n "$$files" || exit 0; \ + echo " ( cd '$(DESTDIR)$(fuseincludedir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(fuseincludedir)" && rm -f $$files +install-includeHEADERS: $(include_HEADERS) + @$(NORMAL_INSTALL) + test -z "$(includedir)" || $(MKDIR_P) "$(DESTDIR)$(includedir)" + @list='$(include_HEADERS)'; test -n "$(includedir)" || list=; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(includedir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(includedir)" || exit $$?; \ + done + +uninstall-includeHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(include_HEADERS)'; test -n "$(includedir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + test -n "$$files" || exit 0; \ + echo " ( cd '$(DESTDIR)$(includedir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(includedir)" && rm -f $$files + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(HEADERS) config.h +installdirs: + for dir in "$(DESTDIR)$(fuseincludedir)" "$(DESTDIR)$(includedir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + -rm -f Makefile +distclean-am: clean-am distclean-generic distclean-hdr distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: install-fuseincludeHEADERS install-includeHEADERS + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-fuseincludeHEADERS uninstall-includeHEADERS + +.MAKE: all install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ + clean-libtool ctags distclean distclean-generic distclean-hdr \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-fuseincludeHEADERS install-html \ + install-html-am install-includeHEADERS install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic \ + mostlyclean-libtool pdf pdf-am ps ps-am tags uninstall \ + uninstall-am uninstall-fuseincludeHEADERS \ + uninstall-includeHEADERS + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/fuse/include/Makefile.am b/fuse/include/Makefile.am new file mode 100644 index 000000000..663e164cc --- /dev/null +++ b/fuse/include/Makefile.am @@ -0,0 +1,17 @@ +## Process this file with automake to produce Makefile.in + +fuseincludedir=$(includedir)/fuse + +fuseinclude_HEADERS = \ + fuse.h \ + fuse_compat.h \ + fuse_common.h \ + fuse_common_compat.h \ + fuse_lowlevel.h \ + fuse_lowlevel_compat.h \ + fuse_opt.h \ + cuse_lowlevel.h + +include_HEADERS = old/fuse.h ulockmgr.h + +noinst_HEADERS = fuse_kernel.h diff --git a/fuse/include/Makefile.in b/fuse/include/Makefile.in new file mode 100644 index 000000000..1ae6d8589 --- /dev/null +++ b/fuse/include/Makefile.in @@ -0,0 +1,515 @@ +# Makefile.in generated by automake 1.11.1 from Makefile.am. +# @configure_input@ + +# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, +# 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, +# Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +VPATH = @srcdir@ +pkgdatadir = $(datadir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkglibexecdir = $(libexecdir)/@PACKAGE@ +am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd +install_sh_DATA = $(install_sh) -c -m 644 +install_sh_PROGRAM = $(install_sh) -c +install_sh_SCRIPT = $(install_sh) -c +INSTALL_HEADER = $(INSTALL_DATA) +transform = $(program_transform_name) +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_triplet = @build@ +host_triplet = @host@ +target_triplet = @target@ +subdir = include +DIST_COMMON = $(fuseinclude_HEADERS) $(include_HEADERS) \ + $(noinst_HEADERS) $(srcdir)/Makefile.am $(srcdir)/Makefile.in \ + $(srcdir)/config.h.in +ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 +am__aclocal_m4_deps = $(top_srcdir)/m4/libtool.m4 \ + $(top_srcdir)/m4/ltoptions.m4 $(top_srcdir)/m4/ltsugar.m4 \ + $(top_srcdir)/m4/ltversion.m4 $(top_srcdir)/m4/lt~obsolete.m4 \ + $(top_srcdir)/configure.in +am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ + $(ACLOCAL_M4) +mkinstalldirs = $(install_sh) -d +CONFIG_HEADER = config.h +CONFIG_CLEAN_FILES = +CONFIG_CLEAN_VPATH_FILES = +SOURCES = +DIST_SOURCES = +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = f=`echo $$p | sed -e 's|^.*/||'`; +am__install_max = 40 +am__nobase_strip_setup = \ + srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*|]/\\\\&/g'` +am__nobase_strip = \ + for p in $$list; do echo "$$p"; done | sed -e "s|$$srcdirstrip/||" +am__nobase_list = $(am__nobase_strip_setup); \ + for p in $$list; do echo "$$p $$p"; done | \ + sed "s| $$srcdirstrip/| |;"' / .*\//!s/ .*/ ./; s,\( .*\)/[^/]*$$,\1,' | \ + $(AWK) 'BEGIN { files["."] = "" } { files[$$2] = files[$$2] " " $$1; \ + if (++n[$$2] == $(am__install_max)) \ + { print $$2, files[$$2]; n[$$2] = 0; files[$$2] = "" } } \ + END { for (dir in files) print dir, files[dir] }' +am__base_list = \ + sed '$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;$$!N;s/\n/ /g' | \ + sed '$$!N;$$!N;$$!N;$$!N;s/\n/ /g' +am__installdirs = "$(DESTDIR)$(fuseincludedir)" \ + "$(DESTDIR)$(includedir)" +HEADERS = $(fuseinclude_HEADERS) $(include_HEADERS) $(noinst_HEADERS) +ETAGS = etags +CTAGS = ctags +DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +ACLOCAL = @ACLOCAL@ +AMTAR = @AMTAR@ +AR = @AR@ +AUTOCONF = @AUTOCONF@ +AUTOHEADER = @AUTOHEADER@ +AUTOMAKE = @AUTOMAKE@ +AWK = @AWK@ +CC = @CC@ +CCDEPMODE = @CCDEPMODE@ +CFLAGS = @CFLAGS@ +CPP = @CPP@ +CPPFLAGS = @CPPFLAGS@ +CYGPATH_W = @CYGPATH_W@ +DEFS = @DEFS@ +DEPDIR = @DEPDIR@ +DSYMUTIL = @DSYMUTIL@ +DUMPBIN = @DUMPBIN@ +ECHO_C = @ECHO_C@ +ECHO_N = @ECHO_N@ +ECHO_T = @ECHO_T@ +EGREP = @EGREP@ +EXEEXT = @EXEEXT@ +FGREP = @FGREP@ +GREP = @GREP@ +INIT_D_PATH = @INIT_D_PATH@ +INSTALL = @INSTALL@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +LD = @LD@ +LDFLAGS = @LDFLAGS@ +LIBICONV = @LIBICONV@ +LIBOBJS = @LIBOBJS@ +LIBS = @LIBS@ +LIBTOOL = @LIBTOOL@ +LIPO = @LIPO@ +LN_S = @LN_S@ +LTLIBICONV = @LTLIBICONV@ +LTLIBOBJS = @LTLIBOBJS@ +MAKEINFO = @MAKEINFO@ +MKDIR_P = @MKDIR_P@ +MOUNT_FUSE_PATH = @MOUNT_FUSE_PATH@ +NM = @NM@ +NMEDIT = @NMEDIT@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +OTOOL = @OTOOL@ +OTOOL64 = @OTOOL64@ +PACKAGE = @PACKAGE@ +PACKAGE_BUGREPORT = @PACKAGE_BUGREPORT@ +PACKAGE_NAME = @PACKAGE_NAME@ +PACKAGE_STRING = @PACKAGE_STRING@ +PACKAGE_TARNAME = @PACKAGE_TARNAME@ +PACKAGE_VERSION = @PACKAGE_VERSION@ +PATH_SEPARATOR = @PATH_SEPARATOR@ +RANLIB = @RANLIB@ +SED = @SED@ +SET_MAKE = @SET_MAKE@ +SHELL = @SHELL@ +STRIP = @STRIP@ +UDEV_RULES_PATH = @UDEV_RULES_PATH@ +VERSION = @VERSION@ +abs_builddir = @abs_builddir@ +abs_srcdir = @abs_srcdir@ +abs_top_builddir = @abs_top_builddir@ +abs_top_srcdir = @abs_top_srcdir@ +ac_ct_CC = @ac_ct_CC@ +ac_ct_DUMPBIN = @ac_ct_DUMPBIN@ +am__include = @am__include@ +am__leading_dot = @am__leading_dot@ +am__quote = @am__quote@ +am__tar = @am__tar@ +am__untar = @am__untar@ +bindir = @bindir@ +build = @build@ +build_alias = @build_alias@ +build_cpu = @build_cpu@ +build_os = @build_os@ +build_vendor = @build_vendor@ +builddir = @builddir@ +datadir = @datadir@ +datarootdir = @datarootdir@ +docdir = @docdir@ +dvidir = @dvidir@ +exec_prefix = @exec_prefix@ +host = @host@ +host_alias = @host_alias@ +host_cpu = @host_cpu@ +host_os = @host_os@ +host_vendor = @host_vendor@ +htmldir = @htmldir@ +includedir = @includedir@ +infodir = @infodir@ +install_sh = @install_sh@ +libdir = @libdir@ +libexecdir = @libexecdir@ +libfuse_libs = @libfuse_libs@ +localedir = @localedir@ +localstatedir = @localstatedir@ +lt_ECHO = @lt_ECHO@ +mandir = @mandir@ +mkdir_p = @mkdir_p@ +oldincludedir = @oldincludedir@ +pdfdir = @pdfdir@ +pkgconfigdir = @pkgconfigdir@ +prefix = @prefix@ +program_transform_name = @program_transform_name@ +psdir = @psdir@ +sbindir = @sbindir@ +sharedstatedir = @sharedstatedir@ +srcdir = @srcdir@ +subdirs2 = @subdirs2@ +sysconfdir = @sysconfdir@ +target = @target@ +target_alias = @target_alias@ +target_cpu = @target_cpu@ +target_os = @target_os@ +target_vendor = @target_vendor@ +top_build_prefix = @top_build_prefix@ +top_builddir = @top_builddir@ +top_srcdir = @top_srcdir@ +fuseincludedir = $(includedir)/fuse +fuseinclude_HEADERS = \ + fuse.h \ + fuse_compat.h \ + fuse_common.h \ + fuse_common_compat.h \ + fuse_lowlevel.h \ + fuse_lowlevel_compat.h \ + fuse_opt.h \ + cuse_lowlevel.h + +include_HEADERS = old/fuse.h ulockmgr.h +noinst_HEADERS = fuse_kernel.h +all: config.h + $(MAKE) $(AM_MAKEFLAGS) all-am + +.SUFFIXES: +$(srcdir)/Makefile.in: $(srcdir)/Makefile.am $(am__configure_deps) + @for dep in $?; do \ + case '$(am__configure_deps)' in \ + *$$dep*) \ + ( cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh ) \ + && { if test -f $@; then exit 0; else break; fi; }; \ + exit 1;; \ + esac; \ + done; \ + echo ' cd $(top_srcdir) && $(AUTOMAKE) --gnu include/Makefile'; \ + $(am__cd) $(top_srcdir) && \ + $(AUTOMAKE) --gnu include/Makefile +.PRECIOUS: Makefile +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + @case '$?' in \ + *config.status*) \ + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh;; \ + *) \ + echo ' cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe)'; \ + cd $(top_builddir) && $(SHELL) ./config.status $(subdir)/$@ $(am__depfiles_maybe);; \ + esac; + +$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh + +$(top_srcdir)/configure: $(am__configure_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(ACLOCAL_M4): $(am__aclocal_m4_deps) + cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +$(am__aclocal_m4_deps): + +config.h: stamp-h1 + @if test ! -f $@; then \ + rm -f stamp-h1; \ + $(MAKE) $(AM_MAKEFLAGS) stamp-h1; \ + else :; fi + +stamp-h1: $(srcdir)/config.h.in $(top_builddir)/config.status + @rm -f stamp-h1 + cd $(top_builddir) && $(SHELL) ./config.status include/config.h +$(srcdir)/config.h.in: $(am__configure_deps) + ($(am__cd) $(top_srcdir) && $(AUTOHEADER)) + rm -f stamp-h1 + touch $@ + +distclean-hdr: + -rm -f config.h stamp-h1 + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs +install-fuseincludeHEADERS: $(fuseinclude_HEADERS) + @$(NORMAL_INSTALL) + test -z "$(fuseincludedir)" || $(MKDIR_P) "$(DESTDIR)$(fuseincludedir)" + @list='$(fuseinclude_HEADERS)'; test -n "$(fuseincludedir)" || list=; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(fuseincludedir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(fuseincludedir)" || exit $$?; \ + done + +uninstall-fuseincludeHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(fuseinclude_HEADERS)'; test -n "$(fuseincludedir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + test -n "$$files" || exit 0; \ + echo " ( cd '$(DESTDIR)$(fuseincludedir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(fuseincludedir)" && rm -f $$files +install-includeHEADERS: $(include_HEADERS) + @$(NORMAL_INSTALL) + test -z "$(includedir)" || $(MKDIR_P) "$(DESTDIR)$(includedir)" + @list='$(include_HEADERS)'; test -n "$(includedir)" || list=; \ + for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + echo "$$d$$p"; \ + done | $(am__base_list) | \ + while read files; do \ + echo " $(INSTALL_HEADER) $$files '$(DESTDIR)$(includedir)'"; \ + $(INSTALL_HEADER) $$files "$(DESTDIR)$(includedir)" || exit $$?; \ + done + +uninstall-includeHEADERS: + @$(NORMAL_UNINSTALL) + @list='$(include_HEADERS)'; test -n "$(includedir)" || list=; \ + files=`for p in $$list; do echo $$p; done | sed -e 's|^.*/||'`; \ + test -n "$$files" || exit 0; \ + echo " ( cd '$(DESTDIR)$(includedir)' && rm -f" $$files ")"; \ + cd "$(DESTDIR)$(includedir)" && rm -f $$files + +ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES) + list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + mkid -fID $$unique +tags: TAGS + +TAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + set x; \ + here=`pwd`; \ + list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + shift; \ + if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else \ + test -n "$$unique" || unique=$$empty_fix; \ + if test $$# -gt 0; then \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + "$$@" $$unique; \ + else \ + $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) \ + $$unique; \ + fi; \ + fi +ctags: CTAGS +CTAGS: $(HEADERS) $(SOURCES) config.h.in $(TAGS_DEPENDENCIES) \ + $(TAGS_FILES) $(LISP) + list='$(SOURCES) $(HEADERS) config.h.in $(LISP) $(TAGS_FILES)'; \ + unique=`for i in $$list; do \ + if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; \ + done | \ + $(AWK) '{ files[$$0] = 1; nonempty = 1; } \ + END { if (nonempty) { for (i in files) print i; }; }'`; \ + test -z "$(CTAGS_ARGS)$$unique" \ + || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) \ + $$unique + +GTAGS: + here=`$(am__cd) $(top_builddir) && pwd` \ + && $(am__cd) $(top_srcdir) \ + && gtags -i $(GTAGS_ARGS) "$$here" + +distclean-tags: + -rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags + +distdir: $(DISTFILES) + @srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; \ + list='$(DISTFILES)'; \ + dist_files=`for file in $$list; do echo $$file; done | \ + sed -e "s|^$$srcdirstrip/||;t" \ + -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; \ + case $$dist_files in \ + */*) $(MKDIR_P) `echo "$$dist_files" | \ + sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | \ + sort -u` ;; \ + esac; \ + for file in $$dist_files; do \ + if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; \ + if test -d $$d/$$file; then \ + dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test -d "$(distdir)/$$file"; then \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + if test -d $(srcdir)/$$file && test $$d != $(srcdir); then \ + cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; \ + find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; \ + fi; \ + cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; \ + else \ + test -f "$(distdir)/$$file" \ + || cp -p $$d/$$file "$(distdir)/$$file" \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(HEADERS) config.h +installdirs: + for dir in "$(DESTDIR)$(fuseincludedir)" "$(DESTDIR)$(includedir)"; do \ + test -z "$$dir" || $(MKDIR_P) "$$dir"; \ + done +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) + -test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES) + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +clean: clean-am + +clean-am: clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + -rm -f Makefile +distclean-am: clean-am distclean-generic distclean-hdr distclean-tags + +dvi: dvi-am + +dvi-am: + +html: html-am + +html-am: + +info: info-am + +info-am: + +install-data-am: install-fuseincludeHEADERS install-includeHEADERS + +install-dvi: install-dvi-am + +install-dvi-am: + +install-exec-am: + +install-html: install-html-am + +install-html-am: + +install-info: install-info-am + +install-info-am: + +install-man: + +install-pdf: install-pdf-am + +install-pdf-am: + +install-ps: install-ps-am + +install-ps-am: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + -rm -f Makefile +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic mostlyclean-libtool + +pdf: pdf-am + +pdf-am: + +ps: ps-am + +ps-am: + +uninstall-am: uninstall-fuseincludeHEADERS uninstall-includeHEADERS + +.MAKE: all install-am install-strip + +.PHONY: CTAGS GTAGS all all-am check check-am clean clean-generic \ + clean-libtool ctags distclean distclean-generic distclean-hdr \ + distclean-libtool distclean-tags distdir dvi dvi-am html \ + html-am info info-am install install-am install-data \ + install-data-am install-dvi install-dvi-am install-exec \ + install-exec-am install-fuseincludeHEADERS install-html \ + install-html-am install-includeHEADERS install-info \ + install-info-am install-man install-pdf install-pdf-am \ + install-ps install-ps-am install-strip installcheck \ + installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic \ + mostlyclean-libtool pdf pdf-am ps ps-am tags uninstall \ + uninstall-am uninstall-fuseincludeHEADERS \ + uninstall-includeHEADERS + + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: diff --git a/fuse/include/config.h b/fuse/include/config.h new file mode 100644 index 000000000..a0c603ab6 --- /dev/null +++ b/fuse/include/config.h @@ -0,0 +1,87 @@ +/* include/config.h. Generated from config.h.in by configure. */ +/* include/config.h.in. Generated from configure.in by autoheader. */ + +/* Define to 1 if you have the header file. */ +#define HAVE_DLFCN_H 1 + +/* Define to 1 if you have the `fdatasync' function. */ +#define HAVE_FDATASYNC 1 + +/* Define to 1 if you have the `fork' function. */ +#define HAVE_FORK 1 + +/* Define if you have the iconv() function and it works. */ +#define HAVE_ICONV 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if you have the `setxattr' function. */ +#define HAVE_SETXATTR 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define to 1 if `st_atim' is member of `struct stat'. */ +//#define HAVE_STRUCT_STAT_ST_ATIM 0 + +/* Define to 1 if `st_atimespec' is member of `struct stat'. */ +/* #undef HAVE_STRUCT_STAT_ST_ATIMESPEC */ + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define as const if the declaration of iconv() needs const. */ +#define ICONV_CONST + +/* Don't update /etc/mtab */ +/* #undef IGNORE_MTAB */ + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#define LT_OBJDIR ".libs/" + +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ +/* #undef NO_MINUS_C_MINUS_O */ + +/* Name of package */ +#define PACKAGE "fuse" + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "fuse" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "fuse 2.8.5" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "fuse" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "2.8.5" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Version number of package */ +#define VERSION "2.8.5" diff --git a/fuse/include/config.h.in b/fuse/include/config.h.in new file mode 100644 index 000000000..3e7c14ccb --- /dev/null +++ b/fuse/include/config.h.in @@ -0,0 +1,86 @@ +/* include/config.h.in. Generated from configure.in by autoheader. */ + +/* Define to 1 if you have the header file. */ +#undef HAVE_DLFCN_H + +/* Define to 1 if you have the `fdatasync' function. */ +#undef HAVE_FDATASYNC + +/* Define to 1 if you have the `fork' function. */ +#undef HAVE_FORK + +/* Define if you have the iconv() function and it works. */ +#undef HAVE_ICONV + +/* Define to 1 if you have the header file. */ +#undef HAVE_INTTYPES_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_MEMORY_H + +/* Define to 1 if you have the `setxattr' function. */ +#undef HAVE_SETXATTR + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDINT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STDLIB_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRINGS_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_STRING_H + +/* Define to 1 if `st_atim' is member of `struct stat'. */ +#undef HAVE_STRUCT_STAT_ST_ATIM + +/* Define to 1 if `st_atimespec' is member of `struct stat'. */ +#undef HAVE_STRUCT_STAT_ST_ATIMESPEC + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_STAT_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_SYS_TYPES_H + +/* Define to 1 if you have the header file. */ +#undef HAVE_UNISTD_H + +/* Define as const if the declaration of iconv() needs const. */ +#undef ICONV_CONST + +/* Don't update /etc/mtab */ +#undef IGNORE_MTAB + +/* Define to the sub-directory in which libtool stores uninstalled libraries. + */ +#undef LT_OBJDIR + +/* Define to 1 if your C compiler doesn't accept -c and -o together. */ +#undef NO_MINUS_C_MINUS_O + +/* Name of package */ +#undef PACKAGE + +/* Define to the address where bug reports for this package should be sent. */ +#undef PACKAGE_BUGREPORT + +/* Define to the full name of this package. */ +#undef PACKAGE_NAME + +/* Define to the full name and version of this package. */ +#undef PACKAGE_STRING + +/* Define to the one symbol short name of this package. */ +#undef PACKAGE_TARNAME + +/* Define to the version of this package. */ +#undef PACKAGE_VERSION + +/* Define to 1 if you have the ANSI C header files. */ +#undef STDC_HEADERS + +/* Version number of package */ +#undef VERSION diff --git a/fuse/include/cuse_lowlevel.h b/fuse/include/cuse_lowlevel.h new file mode 100644 index 000000000..d628eac4e --- /dev/null +++ b/fuse/include/cuse_lowlevel.h @@ -0,0 +1,87 @@ +/* + CUSE: Character device in Userspace + Copyright (C) 2008-2009 SUSE Linux Products GmbH + Copyright (C) 2008-2009 Tejun Heo + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. + + Read example/cusexmp.c for usages. +*/ + +#ifndef _CUSE_LOWLEVEL_H_ +#define _CUSE_LOWLEVEL_H_ + +#ifndef FUSE_USE_VERSION +#define FUSE_USE_VERSION 29 +#endif + +#include "fuse_lowlevel.h" + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define CUSE_UNRESTRICTED_IOCTL (1 << 0) /* use unrestricted ioctl */ + +struct fuse_session; + +struct cuse_info { + unsigned dev_major; + unsigned dev_minor; + unsigned dev_info_argc; + const char **dev_info_argv; + unsigned flags; +}; + +/* + * Most ops behave almost identically to the matching fuse_lowlevel + * ops except that they don't take @ino. + * + * init_done : called after initialization is complete + * read/write : always direct IO, simultaneous operations allowed + * ioctl : might be in unrestricted mode depending on ci->flags + */ +struct cuse_lowlevel_ops { + void (*init) (void *userdata, struct fuse_conn_info *conn); + void (*init_done) (void *userdata); + void (*destroy) (void *userdata); + void (*open) (fuse_req_t req, struct fuse_file_info *fi); + void (*read) (fuse_req_t req, size_t size, off64_t off, + struct fuse_file_info *fi); + void (*write) (fuse_req_t req, const char *buf, size_t size, off64_t off, + struct fuse_file_info *fi); + void (*flush) (fuse_req_t req, struct fuse_file_info *fi); + void (*release) (fuse_req_t req, struct fuse_file_info *fi); + void (*fsync) (fuse_req_t req, int datasync, struct fuse_file_info *fi); + void (*ioctl) (fuse_req_t req, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, + const void *in_buf, size_t in_bufsz, size_t out_bufsz); + void (*poll) (fuse_req_t req, struct fuse_file_info *fi, + struct fuse_pollhandle *ph); +}; + +struct fuse_session *cuse_lowlevel_new(struct fuse_args *args, + const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop, + void *userdata); + +struct fuse_session *cuse_lowlevel_setup(int argc, char *argv[], + const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop, + int *multithreaded, void *userdata); + +void cuse_lowlevel_teardown(struct fuse_session *se); + +int cuse_lowlevel_main(int argc, char *argv[], const struct cuse_info *ci, + const struct cuse_lowlevel_ops *clop, void *userdata); + +#ifdef __cplusplus +} +#endif + +#endif /* _CUSE_LOWLEVEL_H_ */ diff --git a/fuse/include/fuse.h b/fuse/include/fuse.h new file mode 100644 index 000000000..899830fb8 --- /dev/null +++ b/fuse/include/fuse.h @@ -0,0 +1,928 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#ifndef _FUSE_H_ +#define _FUSE_H_ + +/** @file + * + * This file defines the library interface of FUSE + * + * IMPORTANT: you should define FUSE_USE_VERSION before including this + * header. To use the newest API define it to 26 (recommended for any + * new application), to use the old API define it to 21 (default) 22 + * or 25, to use the even older 1.X API define it to 11. + */ + +#ifndef FUSE_USE_VERSION +#define FUSE_USE_VERSION 28 +#endif + +#include "fuse_common.h" + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* ----------------------------------------------------------- * + * Basic FUSE API * + * ----------------------------------------------------------- */ + +/** Handle for a FUSE filesystem */ +struct fuse; + +/** Structure containing a raw command */ +struct fuse_cmd; + +/** Function to add an entry in a readdir() operation + * + * @param buf the buffer passed to the readdir() operation + * @param name the file name of the directory entry + * @param stat file attributes, can be NULL + * @param off offset of the next entry or zero + * @return 1 if buffer is full, zero otherwise + */ +typedef int (*fuse_fill_dir_t) (void *buf, const char *name, + const struct stat *stbuf, off64_t off); + +/* Used by deprecated getdir() method */ +typedef struct fuse_dirhandle *fuse_dirh_t; +typedef int (*fuse_dirfil_t) (fuse_dirh_t h, const char *name, int type, + ino_t ino); + +/** + * The file system operations: + * + * Most of these should work very similarly to the well known UNIX + * file system operations. A major exception is that instead of + * returning an error in 'errno', the operation should return the + * negated error value (-errno) directly. + * + * All methods are optional, but some are essential for a useful + * filesystem (e.g. getattr). Open, flush, release, fsync, opendir, + * releasedir, fsyncdir, access, create, ftruncate, fgetattr, lock, + * init and destroy are special purpose methods, without which a full + * featured filesystem can still be implemented. + * + * Almost all operations take a path which can be of any length. + * + * Changed in fuse 2.8.0 (regardless of API version) + * Previously, paths were limited to a length of PATH_MAX. + * + * See http://fuse.sourceforge.net/wiki/ for more information. There + * is also a snapshot of the relevant wiki pages in the doc/ folder. + */ +struct fuse_operations { + /** Get file attributes. + * + * Similar to stat(). The 'st_dev' and 'st_blksize' fields are + * ignored. The 'st_ino' field is ignored except if the 'use_ino' + * mount option is given. + */ + int (*getattr) (const char *, struct stat *); + + /** Read the target of a symbolic link + * + * The buffer should be filled with a null terminated string. The + * buffer size argument includes the space for the terminating + * null character. If the linkname is too long to fit in the + * buffer, it should be truncated. The return value should be 0 + * for success. + */ + int (*readlink) (const char *, char *, size_t); + + /* Deprecated, use readdir() instead */ + int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); + + /** Create a file node + * + * This is called for creation of all non-directory, non-symlink + * nodes. If the filesystem defines a create() method, then for + * regular files that will be called instead. + */ + int (*mknod) (const char *, mode_t, dev_t); + + /** Create a directory + * + * Note that the mode argument may not have the type specification + * bits set, i.e. S_ISDIR(mode) can be false. To obtain the + * correct directory type bits use mode|S_IFDIR + * */ + int (*mkdir) (const char *, mode_t); + + /** Remove a file */ + int (*unlink) (const char *); + + /** Remove a directory */ + int (*rmdir) (const char *); + + /** Create a symbolic link */ + int (*symlink) (const char *, const char *); + + /** Rename a file */ + int (*rename) (const char *, const char *); + + /** Create a hard link to a file */ + int (*link) (const char *, const char *); + + /** Change the permission bits of a file */ + int (*chmod) (const char *, mode_t); + + /** Change the owner and group of a file */ + int (*chown) (const char *, uid_t, gid_t); + + /** Change the size of a file */ + int (*truncate) (const char *, off64_t); + + /** Change the access and/or modification times of a file + * + * Deprecated, use utimens() instead. + */ + int (*utime) (const char *, struct utimbuf *); + + /** File open operation + * + * No creation (O_CREAT, O_EXCL) and by default also no + * truncation (O_TRUNC) flags will be passed to open(). If an + * application specifies O_TRUNC, fuse first calls truncate() + * and then open(). Only if 'atomic_o_trunc' has been + * specified and kernel version is 2.6.24 or later, O_TRUNC is + * passed on to open. + * + * Unless the 'default_permissions' mount option is given, + * open should check if the operation is permitted for the + * given flags. Optionally open may also return an arbitrary + * filehandle in the fuse_file_info structure, which will be + * passed to all file operations. + * + * Changed in version 2.2 + */ + int (*open) (const char *, struct fuse_file_info *); + + /** Read data from an open file + * + * Read should return exactly the number of bytes requested except + * on EOF or error, otherwise the rest of the data will be + * substituted with zeroes. An exception to this is when the + * 'direct_io' mount option is specified, in which case the return + * value of the read system call will reflect the return value of + * this operation. + * + * Changed in version 2.2 + */ + int (*read) (const char *, char *, size_t, off64_t, + struct fuse_file_info *); + + /** Write data to an open file + * + * Write should return exactly the number of bytes requested + * except on error. An exception to this is when the 'direct_io' + * mount option is specified (see read operation). + * + * Changed in version 2.2 + */ + int (*write) (const char *, const char *, size_t, off64_t, + struct fuse_file_info *); + + /** Get file system statistics + * + * The 'f_frsize', 'f_favail', 'f_fsid' and 'f_flag' fields are ignored + * + * Replaced 'struct statfs' parameter with 'struct statvfs' in + * version 2.5 + */ + int (*statfs) (const char *, struct statvfs *); + + /** Possibly flush cached data + * + * BIG NOTE: This is not equivalent to fsync(). It's not a + * request to sync dirty data. + * + * Flush is called on each close() of a file descriptor. So if a + * filesystem wants to return write errors in close() and the file + * has cached dirty data, this is a good place to write back data + * and return any errors. Since many applications ignore close() + * errors this is not always useful. + * + * NOTE: The flush() method may be called more than once for each + * open(). This happens if more than one file descriptor refers + * to an opened file due to dup(), dup2() or fork() calls. It is + * not possible to determine if a flush is final, so each flush + * should be treated equally. Multiple write-flush sequences are + * relatively rare, so this shouldn't be a problem. + * + * Filesystems shouldn't assume that flush will always be called + * after some writes, or that if will be called at all. + * + * Changed in version 2.2 + */ + int (*flush) (const char *, struct fuse_file_info *); + + /** Release an open file + * + * Release is called when there are no more references to an open + * file: all file descriptors are closed and all memory mappings + * are unmapped. + * + * For every open() call there will be exactly one release() call + * with the same flags and file descriptor. It is possible to + * have a file opened more than once, in which case only the last + * release will mean, that no more reads/writes will happen on the + * file. The return value of release is ignored. + * + * Changed in version 2.2 + */ + int (*release) (const char *, struct fuse_file_info *); + + /** Synchronize file contents + * + * If the datasync parameter is non-zero, then only the user data + * should be flushed, not the meta data. + * + * Changed in version 2.2 + */ + int (*fsync) (const char *, int, struct fuse_file_info *); + + /** Set extended attributes */ + int (*setxattr) (const char *, const char *, const char *, size_t, int); + + /** Get extended attributes */ + int (*getxattr) (const char *, const char *, char *, size_t); + + /** List extended attributes */ + int (*listxattr) (const char *, char *, size_t); + + /** Remove extended attributes */ + int (*removexattr) (const char *, const char *); + + /** Open directory + * + * Unless the 'default_permissions' mount option is given, + * this method should check if opendir is permitted for this + * directory. Optionally opendir may also return an arbitrary + * filehandle in the fuse_file_info structure, which will be + * passed to readdir, closedir and fsyncdir. + * + * Introduced in version 2.3 + */ + int (*opendir) (const char *, struct fuse_file_info *); + + /** Read directory + * + * This supersedes the old getdir() interface. New applications + * should use this. + * + * The filesystem may choose between two modes of operation: + * + * 1) The readdir implementation ignores the offset parameter, and + * passes zero to the filler function's offset. The filler + * function will not return '1' (unless an error happens), so the + * whole directory is read in a single readdir operation. This + * works just like the old getdir() method. + * + * 2) The readdir implementation keeps track of the offsets of the + * directory entries. It uses the offset parameter and always + * passes non-zero offset to the filler function. When the buffer + * is full (or an error happens) the filler function will return + * '1'. + * + * Introduced in version 2.3 + */ + int (*readdir) (const char *, void *, fuse_fill_dir_t, off64_t, + struct fuse_file_info *); + + /** Release directory + * + * Introduced in version 2.3 + */ + int (*releasedir) (const char *, struct fuse_file_info *); + + /** Synchronize directory contents + * + * If the datasync parameter is non-zero, then only the user data + * should be flushed, not the meta data + * + * Introduced in version 2.3 + */ + int (*fsyncdir) (const char *, int, struct fuse_file_info *); + + /** + * Initialize filesystem + * + * The return value will passed in the private_data field of + * fuse_context to all file operations and as a parameter to the + * destroy() method. + * + * Introduced in version 2.3 + * Changed in version 2.6 + */ + void *(*init) (struct fuse_conn_info *conn); + + /** + * Clean up filesystem + * + * Called on filesystem exit. + * + * Introduced in version 2.3 + */ + void (*destroy) (void *); + + /** + * Check file access permissions + * + * This will be called for the access() system call. If the + * 'default_permissions' mount option is given, this method is not + * called. + * + * This method is not called under Linux kernel versions 2.4.x + * + * Introduced in version 2.5 + */ + int (*access) (const char *, int); + + /** + * Create and open a file + * + * If the file does not exist, first create it with the specified + * mode, and then open it. + * + * If this method is not implemented or under Linux kernel + * versions earlier than 2.6.15, the mknod() and open() methods + * will be called instead. + * + * Introduced in version 2.5 + */ + int (*create) (const char *, mode_t, struct fuse_file_info *); + + /** + * Change the size of an open file + * + * This method is called instead of the truncate() method if the + * truncation was invoked from an ftruncate() system call. + * + * If this method is not implemented or under Linux kernel + * versions earlier than 2.6.15, the truncate() method will be + * called instead. + * + * Introduced in version 2.5 + */ + int (*ftruncate) (const char *, off64_t, struct fuse_file_info *); + + /** + * Get attributes from an open file + * + * This method is called instead of the getattr() method if the + * file information is available. + * + * Currently this is only called after the create() method if that + * is implemented (see above). Later it may be called for + * invocations of fstat() too. + * + * Introduced in version 2.5 + */ + int (*fgetattr) (const char *, struct stat *, struct fuse_file_info *); + + /** + * Perform POSIX file locking operation + * + * The cmd argument will be either F_GETLK, F_SETLK or F_SETLKW. + * + * For the meaning of fields in 'struct flock' see the man page + * for fcntl(2). The l_whence field will always be set to + * SEEK_SET. + * + * For checking lock ownership, the 'fuse_file_info->owner' + * argument must be used. + * + * For F_GETLK operation, the library will first check currently + * held locks, and if a conflicting lock is found it will return + * information without calling this method. This ensures, that + * for local locks the l_pid field is correctly filled in. The + * results may not be accurate in case of race conditions and in + * the presence of hard links, but it's unlikly that an + * application would rely on accurate GETLK results in these + * cases. If a conflicting lock is not found, this method will be + * called, and the filesystem may fill out l_pid by a meaningful + * value, or it may leave this field zero. + * + * For F_SETLK and F_SETLKW the l_pid field will be set to the pid + * of the process performing the locking operation. + * + * Note: if this method is not implemented, the kernel will still + * allow file locking to work locally. Hence it is only + * interesting for network filesystems and similar. + * + * Introduced in version 2.6 + */ + int (*lock) (const char *, struct fuse_file_info *, int cmd, + struct flock *); + + /** + * Change the access and modification times of a file with + * nanosecond resolution + * + * Introduced in version 2.6 + */ + int (*utimens) (const char *, const struct timespec tv[2]); + + /** + * Map block index within file to block index within device + * + * Note: This makes sense only for block device backed filesystems + * mounted with the 'blkdev' option + * + * Introduced in version 2.6 + */ + int (*bmap) (const char *, size_t blocksize, uint64_t *idx); + + /** + * Flag indicating, that the filesystem can accept a NULL path + * as the first argument for the following operations: + * + * read, write, flush, release, fsync, readdir, releasedir, + * fsyncdir, ftruncate, fgetattr and lock + */ + unsigned int flag_nullpath_ok : 1; + + /** + * Reserved flags, don't set + */ + unsigned int flag_reserved : 31; + + /** + * Ioctl + * + * flags will have FUSE_IOCTL_COMPAT set for 32bit ioctls in + * 64bit environment. The size and direction of data is + * determined by _IOC_*() decoding of cmd. For _IOC_NONE, + * data will be NULL, for _IOC_WRITE data is out area, for + * _IOC_READ in area and if both are set in/out area. In all + * non-NULL cases, the area is of _IOC_SIZE(cmd) bytes. + * + * Introduced in version 2.8 + */ + int (*ioctl) (const char *, int cmd, void *arg, + struct fuse_file_info *, unsigned int flags, void *data); + + /** + * Poll for IO readiness events + * + * Note: If ph is non-NULL, the client should notify + * when IO readiness events occur by calling + * fuse_notify_poll() with the specified ph. + * + * Regardless of the number of times poll with a non-NULL ph + * is received, single notification is enough to clear all. + * Notifying more times incurs overhead but doesn't harm + * correctness. + * + * The callee is responsible for destroying ph with + * fuse_pollhandle_destroy() when no longer in use. + * + * Introduced in version 2.8 + */ + int (*poll) (const char *, struct fuse_file_info *, + struct fuse_pollhandle *ph, unsigned *reventsp); +}; + +/** Extra context that may be needed by some filesystems + * + * The uid, gid and pid fields are not filled in case of a writepage + * operation. + */ +struct fuse_context { + /** Pointer to the fuse object */ + struct fuse *fuse; + + /** User ID of the calling process */ + uid_t uid; + + /** Group ID of the calling process */ + gid_t gid; + + /** Thread ID of the calling process */ + pid_t pid; + + /** Private filesystem data */ + void *private_data; + + /** Umask of the calling process (introduced in version 2.8) */ + mode_t umask; +}; + +/** + * Main function of FUSE. + * + * This is for the lazy. This is all that has to be called from the + * main() function. + * + * This function does the following: + * - parses command line options (-d -s and -h) + * - passes relevant mount options to the fuse_mount() + * - installs signal handlers for INT, HUP, TERM and PIPE + * - registers an exit handler to unmount the filesystem on program exit + * - creates a fuse handle + * - registers the operations + * - calls either the single-threaded or the multi-threaded event loop + * + * Note: this is currently implemented as a macro. + * + * @param argc the argument counter passed to the main() function + * @param argv the argument vector passed to the main() function + * @param op the file system operation + * @param user_data user data supplied in the context during the init() method + * @return 0 on success, nonzero on failure + */ +/* + int fuse_main(int argc, char *argv[], const struct fuse_operations *op, + void *user_data); +*/ +#define fuse_main(argc, argv, op, user_data) \ + fuse_main_real(argc, argv, op, sizeof(*(op)), user_data) + +/* ----------------------------------------------------------- * + * More detailed API * + * ----------------------------------------------------------- */ + +/** + * Create a new FUSE filesystem. + * + * @param ch the communication channel + * @param args argument vector + * @param op the filesystem operations + * @param op_size the size of the fuse_operations structure + * @param user_data user data supplied in the context during the init() method + * @return the created FUSE handle + */ +struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args, + const struct fuse_operations *op, size_t op_size, + void *user_data); + +/** + * Destroy the FUSE handle. + * + * The communication channel attached to the handle is also destroyed. + * + * NOTE: This function does not unmount the filesystem. If this is + * needed, call fuse_unmount() before calling this function. + * + * @param f the FUSE handle + */ +void fuse_destroy(struct fuse *f); + +/** + * FUSE event loop. + * + * Requests from the kernel are processed, and the appropriate + * operations are called. + * + * @param f the FUSE handle + * @return 0 if no error occurred, -1 otherwise + */ +int fuse_loop(struct fuse *f); + +/** + * Exit from event loop + * + * @param f the FUSE handle + */ +void fuse_exit(struct fuse *f); + +/** + * FUSE event loop with multiple threads + * + * Requests from the kernel are processed, and the appropriate + * operations are called. Request are processed in parallel by + * distributing them between multiple threads. + * + * Calling this function requires the pthreads library to be linked to + * the application. + * + * @param f the FUSE handle + * @return 0 if no error occurred, -1 otherwise + */ +int fuse_loop_mt(struct fuse *f); + +/** + * Get the current context + * + * The context is only valid for the duration of a filesystem + * operation, and thus must not be stored and used later. + * + * @return the context + */ +struct fuse_context *fuse_get_context(void); + +/** + * Get the current supplementary group IDs for the current request + * + * Similar to the getgroups(2) system call, except the return value is + * always the total number of group IDs, even if it is larger than the + * specified size. + * + * The current fuse kernel module in linux (as of 2.6.30) doesn't pass + * the group list to userspace, hence this function needs to parse + * "/proc/$TID/task/$TID/status" to get the group IDs. + * + * This feature may not be supported on all operating systems. In + * such a case this function will return -ENOSYS. + * + * @param size size of given array + * @param list array of group IDs to be filled in + * @return the total number of supplementary group IDs or -errno on failure + */ +int fuse_getgroups(int size, gid_t list[]); + +/** + * Check if the current request has already been interrupted + * + * @return 1 if the request has been interrupted, 0 otherwise + */ +int fuse_interrupted(void); + +/** + * Obsolete, doesn't do anything + * + * @return -EINVAL + */ +int fuse_invalidate(struct fuse *f, const char *path); + +/* Deprecated, don't use */ +int fuse_is_lib_option(const char *opt); + +/** + * The real main function + * + * Do not call this directly, use fuse_main() + */ +int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op, + size_t op_size, void *user_data); + +/* + * Stacking API + */ + +/** + * Fuse filesystem object + * + * This is opaque object represents a filesystem layer + */ +struct fuse_fs; + +/* + * These functions call the relevant filesystem operation, and return + * the result. + * + * If the operation is not defined, they return -ENOSYS, with the + * exception of fuse_fs_open, fuse_fs_release, fuse_fs_opendir, + * fuse_fs_releasedir and fuse_fs_statfs, which return 0. + */ + +int fuse_fs_getattr(struct fuse_fs *fs, const char *path, struct stat *buf); +int fuse_fs_fgetattr(struct fuse_fs *fs, const char *path, struct stat *buf, + struct fuse_file_info *fi); +int fuse_fs_rename(struct fuse_fs *fs, const char *oldpath, + const char *newpath); +int fuse_fs_unlink(struct fuse_fs *fs, const char *path); +int fuse_fs_rmdir(struct fuse_fs *fs, const char *path); +int fuse_fs_symlink(struct fuse_fs *fs, const char *linkname, + const char *path); +int fuse_fs_link(struct fuse_fs *fs, const char *oldpath, const char *newpath); +int fuse_fs_release(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_open(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_read(struct fuse_fs *fs, const char *path, char *buf, size_t size, + off64_t off, struct fuse_file_info *fi); +int fuse_fs_write(struct fuse_fs *fs, const char *path, const char *buf, + size_t size, off64_t off, struct fuse_file_info *fi); +int fuse_fs_fsync(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi); +int fuse_fs_flush(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_statfs(struct fuse_fs *fs, const char *path, struct statvfs *buf); +int fuse_fs_opendir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_readdir(struct fuse_fs *fs, const char *path, void *buf, + fuse_fill_dir_t filler, off64_t off, + struct fuse_file_info *fi); +int fuse_fs_fsyncdir(struct fuse_fs *fs, const char *path, int datasync, + struct fuse_file_info *fi); +int fuse_fs_releasedir(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi); +int fuse_fs_create(struct fuse_fs *fs, const char *path, mode_t mode, + struct fuse_file_info *fi); +int fuse_fs_lock(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, int cmd, struct flock *lock); +int fuse_fs_chmod(struct fuse_fs *fs, const char *path, mode_t mode); +int fuse_fs_chown(struct fuse_fs *fs, const char *path, uid_t uid, gid_t gid); +int fuse_fs_truncate(struct fuse_fs *fs, const char *path, off64_t size); +int fuse_fs_ftruncate(struct fuse_fs *fs, const char *path, off64_t size, + struct fuse_file_info *fi); +int fuse_fs_utimens(struct fuse_fs *fs, const char *path, + const struct timespec tv[2]); +int fuse_fs_access(struct fuse_fs *fs, const char *path, int mask); +int fuse_fs_readlink(struct fuse_fs *fs, const char *path, char *buf, + size_t len); +int fuse_fs_mknod(struct fuse_fs *fs, const char *path, mode_t mode, + dev_t rdev); +int fuse_fs_mkdir(struct fuse_fs *fs, const char *path, mode_t mode); +int fuse_fs_setxattr(struct fuse_fs *fs, const char *path, const char *name, + const char *value, size_t size, int flags); +int fuse_fs_getxattr(struct fuse_fs *fs, const char *path, const char *name, + char *value, size_t size); +int fuse_fs_listxattr(struct fuse_fs *fs, const char *path, char *list, + size_t size); +int fuse_fs_removexattr(struct fuse_fs *fs, const char *path, + const char *name); +int fuse_fs_bmap(struct fuse_fs *fs, const char *path, size_t blocksize, + uint64_t *idx); +int fuse_fs_ioctl(struct fuse_fs *fs, const char *path, int cmd, void *arg, + struct fuse_file_info *fi, unsigned int flags, void *data); +int fuse_fs_poll(struct fuse_fs *fs, const char *path, + struct fuse_file_info *fi, struct fuse_pollhandle *ph, + unsigned *reventsp); +void fuse_fs_init(struct fuse_fs *fs, struct fuse_conn_info *conn); +void fuse_fs_destroy(struct fuse_fs *fs); + +int fuse_notify_poll(struct fuse_pollhandle *ph); + +/** + * Create a new fuse filesystem object + * + * This is usually called from the factory of a fuse module to create + * a new instance of a filesystem. + * + * @param op the filesystem operations + * @param op_size the size of the fuse_operations structure + * @param user_data user data supplied in the context during the init() method + * @return a new filesystem object + */ +struct fuse_fs *fuse_fs_new(const struct fuse_operations *op, size_t op_size, + void *user_data); + +/** + * Filesystem module + * + * Filesystem modules are registered with the FUSE_REGISTER_MODULE() + * macro. + * + * If the "-omodules=modname:..." option is present, filesystem + * objects are created and pushed onto the stack with the 'factory' + * function. + */ +struct fuse_module { + /** + * Name of filesystem + */ + const char *name; + + /** + * Factory for creating filesystem objects + * + * The function may use and remove options from 'args' that belong + * to this module. + * + * For now the 'fs' vector always contains exactly one filesystem. + * This is the filesystem which will be below the newly created + * filesystem in the stack. + * + * @param args the command line arguments + * @param fs NULL terminated filesystem object vector + * @return the new filesystem object + */ + struct fuse_fs *(*factory)(struct fuse_args *args, + struct fuse_fs *fs[]); + + struct fuse_module *next; + struct fusemod_so *so; + int ctr; +}; + +/** + * Register a filesystem module + * + * This function is used by FUSE_REGISTER_MODULE and there's usually + * no need to call it directly + */ +void fuse_register_module(struct fuse_module *mod); + +/** + * Register filesystem module + * + * For the parameters, see description of the fields in 'struct + * fuse_module' + */ +#define FUSE_REGISTER_MODULE(name_, factory_) \ + static __attribute__((constructor)) void name_ ## _register(void) \ + { \ + static struct fuse_module mod = \ + { #name_, factory_, NULL, NULL, 0 }; \ + fuse_register_module(&mod); \ + } + + +/* ----------------------------------------------------------- * + * Advanced API for event handling, don't worry about this... * + * ----------------------------------------------------------- */ + +/* NOTE: the following functions are deprecated, and will be removed + from the 3.0 API. Use the lowlevel session functions instead */ + +/** Function type used to process commands */ +typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *); + +/** This is the part of fuse_main() before the event loop */ +struct fuse *fuse_setup(int argc, char *argv[], + const struct fuse_operations *op, size_t op_size, + char **mountpoint, int *multithreaded, + void *user_data); + +/** This is the part of fuse_main() after the event loop */ +void fuse_teardown(struct fuse *fuse, char *mountpoint); + +/** Read a single command. If none are read, return NULL */ +struct fuse_cmd *fuse_read_cmd(struct fuse *f); + +/** Process a single command */ +void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd); + +/** Multi threaded event loop, which calls the custom command + processor function */ +int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data); + +/** Return the exited flag, which indicates if fuse_exit() has been + called */ +int fuse_exited(struct fuse *f); + +/** This function is obsolete and implemented as a no-op */ +void fuse_set_getcontext_func(struct fuse_context *(*func)(void)); + +/** Get session from fuse object */ +struct fuse_session *fuse_get_session(struct fuse *f); + +/* ----------------------------------------------------------- * + * Compatibility stuff * + * ----------------------------------------------------------- */ + +#if FUSE_USE_VERSION < 26 +# include "fuse_compat.h" +# undef fuse_main +# if FUSE_USE_VERSION == 25 +# define fuse_main(argc, argv, op) \ + fuse_main_real_compat25(argc, argv, op, sizeof(*(op))) +# define fuse_new fuse_new_compat25 +# define fuse_setup fuse_setup_compat25 +# define fuse_teardown fuse_teardown_compat22 +# define fuse_operations fuse_operations_compat25 +# elif FUSE_USE_VERSION == 22 +# define fuse_main(argc, argv, op) \ + fuse_main_real_compat22(argc, argv, op, sizeof(*(op))) +# define fuse_new fuse_new_compat22 +# define fuse_setup fuse_setup_compat22 +# define fuse_teardown fuse_teardown_compat22 +# define fuse_operations fuse_operations_compat22 +# define fuse_file_info fuse_file_info_compat +# elif FUSE_USE_VERSION == 24 +# error Compatibility with high-level API version 24 not supported +# else +# define fuse_dirfil_t fuse_dirfil_t_compat +# define __fuse_read_cmd fuse_read_cmd +# define __fuse_process_cmd fuse_process_cmd +# define __fuse_loop_mt fuse_loop_mt_proc +# if FUSE_USE_VERSION == 21 +# define fuse_operations fuse_operations_compat2 +# define fuse_main fuse_main_compat2 +# define fuse_new fuse_new_compat2 +# define __fuse_setup fuse_setup_compat2 +# define __fuse_teardown fuse_teardown_compat22 +# define __fuse_exited fuse_exited +# define __fuse_set_getcontext_func fuse_set_getcontext_func +# else +# define fuse_statfs fuse_statfs_compat1 +# define fuse_operations fuse_operations_compat1 +# define fuse_main fuse_main_compat1 +# define fuse_new fuse_new_compat1 +# define FUSE_DEBUG FUSE_DEBUG_COMPAT1 +# endif +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_H_ */ diff --git a/fuse/include/fuse_common.h b/fuse/include/fuse_common.h new file mode 100644 index 000000000..48c07468d --- /dev/null +++ b/fuse/include/fuse_common.h @@ -0,0 +1,298 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +/** @file */ + +#if !defined(_FUSE_H_) && !defined(_FUSE_LOWLEVEL_H_) +#error "Never include directly; use or instead." +#endif + +#ifndef _FUSE_COMMON_H_ +#define _FUSE_COMMON_H_ + +#include "fuse_opt.h" +#include + +/** Major version of FUSE library interface */ +#define FUSE_MAJOR_VERSION 2 + +/** Minor version of FUSE library interface */ +#define FUSE_MINOR_VERSION 8 + +#define FUSE_MAKE_VERSION(maj, min) ((maj) * 10 + (min)) +#define FUSE_VERSION 26 +//#define FUSE_VERSION FUSE_MAKE_VERSION(FUSE_MAJOR_VERSION, FUSE_MINOR_VERSION) + +/* This interface uses 64 bit off64_t */ +#if _FILE_OFFSET_BITS != 64 +#error Please add -D_FILE_OFFSET_BITS=64 to your compile flags! +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Information about open files + * + * Changed in version 2.5 + */ +struct fuse_file_info { + /** Open flags. Available in open() and release() */ + int flags; + + /** Old file handle, don't use */ + unsigned long fh_old; + + /** In case of a write operation indicates if this was caused by a + writepage */ + int writepage; + + /** Can be filled in by open, to use direct I/O on this file. + Introduced in version 2.4 */ + unsigned int direct_io : 1; + + /** Can be filled in by open, to indicate, that cached file data + need not be invalidated. Introduced in version 2.4 */ + unsigned int keep_cache : 1; + + /** Indicates a flush operation. Set in flush operation, also + maybe set in highlevel lock operation and lowlevel release + operation. Introduced in version 2.6 */ + unsigned int flush : 1; + + /** Can be filled in by open, to indicate that the file is not + seekable. Introduced in version 2.8 */ + unsigned int nonseekable : 1; + + /** Padding. Do not use*/ + unsigned int padding : 28; + + /** File handle. May be filled in by filesystem in open(). + Available in all other file operations */ + uint64_t fh; + + /** Lock owner id. Available in locking operations and flush */ + uint64_t lock_owner; +}; + +/** + * Capability bits for 'fuse_conn_info.capable' and 'fuse_conn_info.want' + * + * FUSE_CAP_ASYNC_READ: filesystem supports asynchronous read requests + * FUSE_CAP_POSIX_LOCKS: filesystem supports "remote" locking + * FUSE_CAP_ATOMIC_O_TRUNC: filesystem handles the O_TRUNC open flag + * FUSE_CAP_EXPORT_SUPPORT: filesystem handles lookups of "." and ".." + * FUSE_CAP_BIG_WRITES: filesystem can handle write size larger than 4kB + * FUSE_CAP_DONT_MASK: don't apply umask to file mode on create operations + */ +#define FUSE_CAP_ASYNC_READ (1 << 0) +#define FUSE_CAP_POSIX_LOCKS (1 << 1) +#define FUSE_CAP_ATOMIC_O_TRUNC (1 << 3) +#define FUSE_CAP_EXPORT_SUPPORT (1 << 4) +#define FUSE_CAP_BIG_WRITES (1 << 5) +#define FUSE_CAP_DONT_MASK (1 << 6) + +/** + * Ioctl flags + * + * FUSE_IOCTL_COMPAT: 32bit compat ioctl on 64bit machine + * FUSE_IOCTL_UNRESTRICTED: not restricted to well-formed ioctls, retry allowed + * FUSE_IOCTL_RETRY: retry with new iovecs + * + * FUSE_IOCTL_MAX_IOV: maximum of in_iovecs + out_iovecs + */ +#define FUSE_IOCTL_COMPAT (1 << 0) +#define FUSE_IOCTL_UNRESTRICTED (1 << 1) +#define FUSE_IOCTL_RETRY (1 << 2) + +#define FUSE_IOCTL_MAX_IOV 256 + +/** + * Connection information, passed to the ->init() method + * + * Some of the elements are read-write, these can be changed to + * indicate the value requested by the filesystem. The requested + * value must usually be smaller than the indicated value. + */ +struct fuse_conn_info { + /** + * Major version of the protocol (read-only) + */ + unsigned proto_major; + + /** + * Minor version of the protocol (read-only) + */ + unsigned proto_minor; + + /** + * Is asynchronous read supported (read-write) + */ + unsigned async_read; + + /** + * Maximum size of the write buffer + */ + unsigned max_write; + + /** + * Maximum readahead + */ + unsigned max_readahead; + + /** + * Capability flags, that the kernel supports + */ + unsigned capable; + + /** + * Capability flags, that the filesystem wants to enable + */ + unsigned want; + + /** + * For future use. + */ + unsigned reserved[25]; +}; + +struct fuse_session; +struct fuse_chan; +struct fuse_pollhandle; + +/** + * Create a FUSE mountpoint + * + * Returns a control file descriptor suitable for passing to + * fuse_new() + * + * @param mountpoint the mount point path + * @param args argument vector + * @return the communication channel on success, NULL on failure + */ +struct fuse_chan *fuse_mount(const char *mountpoint, struct fuse_args *args); + +/** + * Umount a FUSE mountpoint + * + * @param mountpoint the mount point path + * @param ch the communication channel + */ +void fuse_unmount(const char *mountpoint, struct fuse_chan *ch); + +/** + * Parse common options + * + * The following options are parsed: + * + * '-f' foreground + * '-d' '-odebug' foreground, but keep the debug option + * '-s' single threaded + * '-h' '--help' help + * '-ho' help without header + * '-ofsname=..' file system name, if not present, then set to the program + * name + * + * All parameters may be NULL + * + * @param args argument vector + * @param mountpoint the returned mountpoint, should be freed after use + * @param multithreaded set to 1 unless the '-s' option is present + * @param foreground set to 1 if one of the relevant options is present + * @return 0 on success, -1 on failure + */ +int fuse_parse_cmdline(struct fuse_args *args, char **mountpoint, + int *multithreaded, int *foreground); + +/** + * Go into the background + * + * @param foreground if true, stay in the foreground + * @return 0 on success, -1 on failure + */ +int fuse_daemonize(int foreground); + +/** + * Get the version of the library + * + * @return the version + */ +int fuse_version(void); + +/** + * Destroy poll handle + * + * @param ph the poll handle + */ +void fuse_pollhandle_destroy(struct fuse_pollhandle *ph); + +/* ----------------------------------------------------------- * + * Signal handling * + * ----------------------------------------------------------- */ + +/** + * Exit session on HUP, TERM and INT signals and ignore PIPE signal + * + * Stores session in a global variable. May only be called once per + * process until fuse_remove_signal_handlers() is called. + * + * @param se the session to exit + * @return 0 on success, -1 on failure + */ +int fuse_set_signal_handlers(struct fuse_session *se); + +/** + * Restore default signal handlers + * + * Resets global session. After this fuse_set_signal_handlers() may + * be called again. + * + * @param se the same session as given in fuse_set_signal_handlers() + */ +void fuse_remove_signal_handlers(struct fuse_session *se); + +/* ----------------------------------------------------------- * + * Compatibility stuff * + * ----------------------------------------------------------- */ + +#if FUSE_USE_VERSION < 26 +# ifdef __FreeBSD__ +# if FUSE_USE_VERSION < 25 +# error On FreeBSD API version 25 or greater must be used +# endif +# endif +# include "fuse_common_compat.h" +# undef FUSE_MINOR_VERSION +# undef fuse_main +# define fuse_unmount fuse_unmount_compat22 +# if FUSE_USE_VERSION == 25 +# define FUSE_MINOR_VERSION 5 +# define fuse_mount fuse_mount_compat25 +# elif FUSE_USE_VERSION == 24 || FUSE_USE_VERSION == 22 +# define FUSE_MINOR_VERSION 4 +# define fuse_mount fuse_mount_compat22 +# elif FUSE_USE_VERSION == 21 +# define FUSE_MINOR_VERSION 1 +# define fuse_mount fuse_mount_compat22 +# elif FUSE_USE_VERSION == 11 +# warning Compatibility with API version 11 is deprecated +# undef FUSE_MAJOR_VERSION +# define FUSE_MAJOR_VERSION 1 +# define FUSE_MINOR_VERSION 1 +# define fuse_mount fuse_mount_compat1 +# else +# error Compatibility with API version other than 21, 22, 24, 25 and 11 not supported +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_COMMON_H_ */ diff --git a/fuse/include/fuse_common_compat.h b/fuse/include/fuse_common_compat.h new file mode 100644 index 000000000..34440ff71 --- /dev/null +++ b/fuse/include/fuse_common_compat.h @@ -0,0 +1,26 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +/* these definitions provide source compatibility to prior versions. + Do not include this file directly! */ + +struct fuse_file_info_compat { + int flags; + unsigned long fh; + int writepage; + unsigned int direct_io : 1; + unsigned int keep_cache : 1; +}; + +int fuse_mount_compat25(const char *mountpoint, struct fuse_args *args); + +int fuse_mount_compat22(const char *mountpoint, const char *opts); + +int fuse_mount_compat1(const char *mountpoint, const char *args[]); + +void fuse_unmount_compat22(const char *mountpoint); diff --git a/fuse/include/fuse_compat.h b/fuse/include/fuse_compat.h new file mode 100644 index 000000000..95b7c789f --- /dev/null +++ b/fuse/include/fuse_compat.h @@ -0,0 +1,201 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +/* these definitions provide source compatibility to prior versions. + Do not include this file directly! */ + +struct fuse_operations_compat25 { + int (*getattr) (const char *, struct stat *); + int (*readlink) (const char *, char *, size_t); + int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); + int (*mknod) (const char *, mode_t, dev_t); + int (*mkdir) (const char *, mode_t); + int (*unlink) (const char *); + int (*rmdir) (const char *); + int (*symlink) (const char *, const char *); + int (*rename) (const char *, const char *); + int (*link) (const char *, const char *); + int (*chmod) (const char *, mode_t); + int (*chown) (const char *, uid_t, gid_t); + int (*truncate) (const char *, off64_t); + int (*utime) (const char *, struct utimbuf *); + int (*open) (const char *, struct fuse_file_info *); + int (*read) (const char *, char *, size_t, off64_t, + struct fuse_file_info *); + int (*write) (const char *, const char *, size_t, off64_t, + struct fuse_file_info *); + int (*statfs) (const char *, struct statvfs *); + int (*flush) (const char *, struct fuse_file_info *); + int (*release) (const char *, struct fuse_file_info *); + int (*fsync) (const char *, int, struct fuse_file_info *); + int (*setxattr) (const char *, const char *, const char *, size_t, int); + int (*getxattr) (const char *, const char *, char *, size_t); + int (*listxattr) (const char *, char *, size_t); + int (*removexattr) (const char *, const char *); + int (*opendir) (const char *, struct fuse_file_info *); + int (*readdir) (const char *, void *, fuse_fill_dir_t, off64_t, + struct fuse_file_info *); + int (*releasedir) (const char *, struct fuse_file_info *); + int (*fsyncdir) (const char *, int, struct fuse_file_info *); + void *(*init) (void); + void (*destroy) (void *); + int (*access) (const char *, int); + int (*create) (const char *, mode_t, struct fuse_file_info *); + int (*ftruncate) (const char *, off64_t, struct fuse_file_info *); + int (*fgetattr) (const char *, struct stat *, struct fuse_file_info *); +}; + +struct fuse *fuse_new_compat25(int fd, struct fuse_args *args, + const struct fuse_operations_compat25 *op, + size_t op_size); + +int fuse_main_real_compat25(int argc, char *argv[], + const struct fuse_operations_compat25 *op, + size_t op_size); + +struct fuse *fuse_setup_compat25(int argc, char *argv[], + const struct fuse_operations_compat25 *op, + size_t op_size, char **mountpoint, + int *multithreaded, int *fd); + +void fuse_teardown_compat22(struct fuse *fuse, int fd, char *mountpoint); + +#ifndef __FreeBSD__ +#include + +struct fuse_operations_compat22 { + int (*getattr) (const char *, struct stat *); + int (*readlink) (const char *, char *, size_t); + int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t); + int (*mknod) (const char *, mode_t, dev_t); + int (*mkdir) (const char *, mode_t); + int (*unlink) (const char *); + int (*rmdir) (const char *); + int (*symlink) (const char *, const char *); + int (*rename) (const char *, const char *); + int (*link) (const char *, const char *); + int (*chmod) (const char *, mode_t); + int (*chown) (const char *, uid_t, gid_t); + int (*truncate) (const char *, off64_t); + int (*utime) (const char *, struct utimbuf *); + int (*open) (const char *, struct fuse_file_info_compat *); + int (*read) (const char *, char *, size_t, off64_t, + struct fuse_file_info_compat *); + int (*write) (const char *, const char *, size_t, off64_t, + struct fuse_file_info_compat *); + int (*statfs) (const char *, struct statfs *); + int (*flush) (const char *, struct fuse_file_info_compat *); + int (*release) (const char *, struct fuse_file_info_compat *); + int (*fsync) (const char *, int, struct fuse_file_info_compat *); + int (*setxattr) (const char *, const char *, const char *, size_t, int); + int (*getxattr) (const char *, const char *, char *, size_t); + int (*listxattr) (const char *, char *, size_t); + int (*removexattr) (const char *, const char *); + int (*opendir) (const char *, struct fuse_file_info_compat *); + int (*readdir) (const char *, void *, fuse_fill_dir_t, off64_t, + struct fuse_file_info_compat *); + int (*releasedir) (const char *, struct fuse_file_info_compat *); + int (*fsyncdir) (const char *, int, struct fuse_file_info_compat *); + void *(*init) (void); + void (*destroy) (void *); +}; + +struct fuse *fuse_new_compat22(int fd, const char *opts, + const struct fuse_operations_compat22 *op, + size_t op_size); + +struct fuse *fuse_setup_compat22(int argc, char *argv[], + const struct fuse_operations_compat22 *op, + size_t op_size, char **mountpoint, + int *multithreaded, int *fd); + +int fuse_main_real_compat22(int argc, char *argv[], + const struct fuse_operations_compat22 *op, + size_t op_size); + +typedef int (*fuse_dirfil_t_compat) (fuse_dirh_t h, const char *name, int type); +struct fuse_operations_compat2 { + int (*getattr) (const char *, struct stat *); + int (*readlink) (const char *, char *, size_t); + int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t_compat); + int (*mknod) (const char *, mode_t, dev_t); + int (*mkdir) (const char *, mode_t); + int (*unlink) (const char *); + int (*rmdir) (const char *); + int (*symlink) (const char *, const char *); + int (*rename) (const char *, const char *); + int (*link) (const char *, const char *); + int (*chmod) (const char *, mode_t); + int (*chown) (const char *, uid_t, gid_t); + int (*truncate) (const char *, off64_t); + int (*utime) (const char *, struct utimbuf *); + int (*open) (const char *, int); + int (*read) (const char *, char *, size_t, off64_t); + int (*write) (const char *, const char *, size_t, off64_t); + int (*statfs) (const char *, struct statfs *); + int (*flush) (const char *); + int (*release) (const char *, int); + int (*fsync) (const char *, int); + int (*setxattr) (const char *, const char *, const char *, + size_t, int); + int (*getxattr) (const char *, const char *, char *, size_t); + int (*listxattr) (const char *, char *, size_t); + int (*removexattr) (const char *, const char *); +}; + +int fuse_main_compat2(int argc, char *argv[], + const struct fuse_operations_compat2 *op); + +struct fuse *fuse_new_compat2(int fd, const char *opts, + const struct fuse_operations_compat2 *op); + +struct fuse *fuse_setup_compat2(int argc, char *argv[], + const struct fuse_operations_compat2 *op, + char **mountpoint, int *multithreaded, int *fd); + +struct fuse_statfs_compat1 { + long block_size; + long blocks; + long blocks_free; + long files; + long files_free; + long namelen; +}; + +struct fuse_operations_compat1 { + int (*getattr) (const char *, struct stat *); + int (*readlink) (const char *, char *, size_t); + int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t_compat); + int (*mknod) (const char *, mode_t, dev_t); + int (*mkdir) (const char *, mode_t); + int (*unlink) (const char *); + int (*rmdir) (const char *); + int (*symlink) (const char *, const char *); + int (*rename) (const char *, const char *); + int (*link) (const char *, const char *); + int (*chmod) (const char *, mode_t); + int (*chown) (const char *, uid_t, gid_t); + int (*truncate) (const char *, off64_t); + int (*utime) (const char *, struct utimbuf *); + int (*open) (const char *, int); + int (*read) (const char *, char *, size_t, off64_t); + int (*write) (const char *, const char *, size_t, off64_t); + int (*statfs) (struct fuse_statfs_compat1 *); + int (*release) (const char *, int); + int (*fsync) (const char *, int); +}; + +#define FUSE_DEBUG_COMPAT1 (1 << 1) + +struct fuse *fuse_new_compat1(int fd, int flags, + const struct fuse_operations_compat1 *op); + +void fuse_main_compat1(int argc, char *argv[], + const struct fuse_operations_compat1 *op); + +#endif /* __FreeBSD__ */ diff --git a/fuse/include/fuse_kernel.h b/fuse/include/fuse_kernel.h new file mode 100644 index 000000000..bd736307d --- /dev/null +++ b/fuse/include/fuse_kernel.h @@ -0,0 +1,593 @@ +/* + This file defines the kernel interface of FUSE + Copyright (C) 2001-2008 Miklos Szeredi + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. + + This -- and only this -- header file may also be distributed under + the terms of the BSD Licence as follows: + + Copyright (C) 2001-2007 Miklos Szeredi. All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. +*/ + +/* + * This file defines the kernel interface of FUSE + * + * Protocol changelog: + * + * 7.9: + * - new fuse_getattr_in input argument of GETATTR + * - add lk_flags in fuse_lk_in + * - add lock_owner field to fuse_setattr_in, fuse_read_in and fuse_write_in + * - add blksize field to fuse_attr + * - add file flags field to fuse_read_in and fuse_write_in + * + * 7.10 + * - add nonseekable open flag + * + * 7.11 + * - add IOCTL message + * - add unsolicited notification support + * - add POLL message and NOTIFY_POLL notification + * + * 7.12 + * - add umask flag to input argument of open, mknod and mkdir + * - add notification messages for invalidation of inodes and + * directory entries + */ + +#ifndef _LINUX_FUSE_H +#define _LINUX_FUSE_H + +#include +#define __u64 uint64_t +#define __s64 int64_t +#define __u32 uint32_t +#define __s32 int32_t + +/* + * Version negotiation: + * + * Both the kernel and userspace send the version they support in the + * INIT request and reply respectively. + * + * If the major versions match then both shall use the smallest + * of the two minor versions for communication. + * + * If the kernel supports a larger major version, then userspace shall + * reply with the major version it supports, ignore the rest of the + * INIT message and expect a new INIT message from the kernel with a + * matching major version. + * + * If the library supports a larger major version, then it shall fall + * back to the major protocol version sent by the kernel for + * communication and reply with that major version (and an arbitrary + * supported minor version). + */ + +/** Version number of this interface */ +#define FUSE_KERNEL_VERSION 7 + +/** Minor version number of this interface */ +#define FUSE_KERNEL_MINOR_VERSION 12 + +/** The node ID of the root inode */ +#define FUSE_ROOT_ID 1 + +/* Make sure all structures are padded to 64bit boundary, so 32bit + userspace works under 64bit kernels */ + +struct fuse_attr { + __u64 ino; + __u64 size; + __u64 blocks; + __u64 atime; + __u64 mtime; + __u64 ctime; + __u32 atimensec; + __u32 mtimensec; + __u32 ctimensec; + __u32 mode; + __u32 nlink; + __u32 uid; + __u32 gid; + __u32 rdev; + __u32 blksize; + __u32 padding; +}; + +struct fuse_kstatfs { + __u64 blocks; + __u64 bfree; + __u64 bavail; + __u64 files; + __u64 ffree; + __u32 bsize; + __u32 namelen; + __u32 frsize; + __u32 padding; + __u32 spare[6]; +}; + +struct fuse_file_lock { + __u64 start; + __u64 end; + __u32 type; + __u32 pid; /* tgid */ +}; + +/** + * Bitmasks for fuse_setattr_in.valid + */ +#define FATTR_MODE (1 << 0) +#define FATTR_UID (1 << 1) +#define FATTR_GID (1 << 2) +#define FATTR_SIZE (1 << 3) +#define FATTR_ATIME (1 << 4) +#define FATTR_MTIME (1 << 5) +#define FATTR_FH (1 << 6) +#define FATTR_ATIME_NOW (1 << 7) +#define FATTR_MTIME_NOW (1 << 8) +#define FATTR_LOCKOWNER (1 << 9) + +/** + * Flags returned by the OPEN request + * + * FOPEN_DIRECT_IO: bypass page cache for this open file + * FOPEN_KEEP_CACHE: don't invalidate the data cache on open + * FOPEN_NONSEEKABLE: the file is not seekable + */ +#define FOPEN_DIRECT_IO (1 << 0) +#define FOPEN_KEEP_CACHE (1 << 1) +#define FOPEN_NONSEEKABLE (1 << 2) + +/** + * INIT request/reply flags + * + * FUSE_EXPORT_SUPPORT: filesystem handles lookups of "." and ".." + * FUSE_DONT_MASK: don't apply umask to file mode on create operations + */ +#define FUSE_ASYNC_READ (1 << 0) +#define FUSE_POSIX_LOCKS (1 << 1) +#define FUSE_FILE_OPS (1 << 2) +#define FUSE_ATOMIC_O_TRUNC (1 << 3) +#define FUSE_EXPORT_SUPPORT (1 << 4) +#define FUSE_BIG_WRITES (1 << 5) +#define FUSE_DONT_MASK (1 << 6) + +/** + * CUSE INIT request/reply flags + * + * CUSE_UNRESTRICTED_IOCTL: use unrestricted ioctl + */ +#define CUSE_UNRESTRICTED_IOCTL (1 << 0) + +/** + * Release flags + */ +#define FUSE_RELEASE_FLUSH (1 << 0) + +/** + * Getattr flags + */ +#define FUSE_GETATTR_FH (1 << 0) + +/** + * Lock flags + */ +#define FUSE_LK_FLOCK (1 << 0) + +/** + * WRITE flags + * + * FUSE_WRITE_CACHE: delayed write from page cache, file handle is guessed + * FUSE_WRITE_LOCKOWNER: lock_owner field is valid + */ +#define FUSE_WRITE_CACHE (1 << 0) +#define FUSE_WRITE_LOCKOWNER (1 << 1) + +/** + * Read flags + */ +#define FUSE_READ_LOCKOWNER (1 << 1) + +/** + * Ioctl flags + * + * FUSE_IOCTL_COMPAT: 32bit compat ioctl on 64bit machine + * FUSE_IOCTL_UNRESTRICTED: not restricted to well-formed ioctls, retry allowed + * FUSE_IOCTL_RETRY: retry with new iovecs + * + * FUSE_IOCTL_MAX_IOV: maximum of in_iovecs + out_iovecs + */ +#define FUSE_IOCTL_COMPAT (1 << 0) +#define FUSE_IOCTL_UNRESTRICTED (1 << 1) +#define FUSE_IOCTL_RETRY (1 << 2) + +#define FUSE_IOCTL_MAX_IOV 256 + +/** + * Poll flags + * + * FUSE_POLL_SCHEDULE_NOTIFY: request poll notify + */ +#define FUSE_POLL_SCHEDULE_NOTIFY (1 << 0) + +enum fuse_opcode { + FUSE_LOOKUP = 1, + FUSE_FORGET = 2, /* no reply */ + FUSE_GETATTR = 3, + FUSE_SETATTR = 4, + FUSE_READLINK = 5, + FUSE_SYMLINK = 6, + FUSE_MKNOD = 8, + FUSE_MKDIR = 9, + FUSE_UNLINK = 10, + FUSE_RMDIR = 11, + FUSE_RENAME = 12, + FUSE_LINK = 13, + FUSE_OPEN = 14, + FUSE_READ = 15, + FUSE_WRITE = 16, + FUSE_STATFS = 17, + FUSE_RELEASE = 18, + FUSE_FSYNC = 20, + FUSE_SETXATTR = 21, + FUSE_GETXATTR = 22, + FUSE_LISTXATTR = 23, + FUSE_REMOVEXATTR = 24, + FUSE_FLUSH = 25, + FUSE_INIT = 26, + FUSE_OPENDIR = 27, + FUSE_READDIR = 28, + FUSE_RELEASEDIR = 29, + FUSE_FSYNCDIR = 30, + FUSE_GETLK = 31, + FUSE_SETLK = 32, + FUSE_SETLKW = 33, + FUSE_ACCESS = 34, + FUSE_CREATE = 35, + FUSE_INTERRUPT = 36, + FUSE_BMAP = 37, + FUSE_DESTROY = 38, + FUSE_IOCTL = 39, + FUSE_POLL = 40, + + /* CUSE specific operations */ + CUSE_INIT = 4096, +}; + +enum fuse_notify_code { + FUSE_NOTIFY_POLL = 1, + FUSE_NOTIFY_INVAL_INODE = 2, + FUSE_NOTIFY_INVAL_ENTRY = 3, + FUSE_NOTIFY_CODE_MAX, +}; + +/* The read buffer is required to be at least 8k, but may be much larger */ +#define FUSE_MIN_READ_BUFFER 8192 + +#define FUSE_COMPAT_ENTRY_OUT_SIZE 120 + +struct fuse_entry_out { + __u64 nodeid; /* Inode ID */ + __u64 generation; /* Inode generation: nodeid:gen must + be unique for the fs's lifetime */ + __u64 entry_valid; /* Cache timeout for the name */ + __u64 attr_valid; /* Cache timeout for the attributes */ + __u32 entry_valid_nsec; + __u32 attr_valid_nsec; + struct fuse_attr attr; +}; + +struct fuse_forget_in { + __u64 nlookup; +}; + +struct fuse_getattr_in { + __u32 getattr_flags; + __u32 dummy; + __u64 fh; +}; + +#define FUSE_COMPAT_ATTR_OUT_SIZE 96 + +struct fuse_attr_out { + __u64 attr_valid; /* Cache timeout for the attributes */ + __u32 attr_valid_nsec; + __u32 dummy; + struct fuse_attr attr; +}; + +#define FUSE_COMPAT_MKNOD_IN_SIZE 8 + +struct fuse_mknod_in { + __u32 mode; + __u32 rdev; + __u32 umask; + __u32 padding; +}; + +struct fuse_mkdir_in { + __u32 mode; + __u32 umask; +}; + +struct fuse_rename_in { + __u64 newdir; +}; + +struct fuse_link_in { + __u64 oldnodeid; +}; + +struct fuse_setattr_in { + __u32 valid; + __u32 padding; + __u64 fh; + __u64 size; + __u64 lock_owner; + __u64 atime; + __u64 mtime; + __u64 unused2; + __u32 atimensec; + __u32 mtimensec; + __u32 unused3; + __u32 mode; + __u32 unused4; + __u32 uid; + __u32 gid; + __u32 unused5; +}; + +struct fuse_open_in { + __u32 flags; + __u32 unused; +}; + +struct fuse_create_in { + __u32 flags; + __u32 mode; + __u32 umask; + __u32 padding; +}; + +struct fuse_open_out { + __u64 fh; + __u32 open_flags; + __u32 padding; +}; + +struct fuse_release_in { + __u64 fh; + __u32 flags; + __u32 release_flags; + __u64 lock_owner; +}; + +struct fuse_flush_in { + __u64 fh; + __u32 unused; + __u32 padding; + __u64 lock_owner; +}; + +struct fuse_read_in { + __u64 fh; + __u64 offset; + __u32 size; + __u32 read_flags; + __u64 lock_owner; + __u32 flags; + __u32 padding; +}; + +#define FUSE_COMPAT_WRITE_IN_SIZE 24 + +struct fuse_write_in { + __u64 fh; + __u64 offset; + __u32 size; + __u32 write_flags; + __u64 lock_owner; + __u32 flags; + __u32 padding; +}; + +struct fuse_write_out { + __u32 size; + __u32 padding; +}; + +#define FUSE_COMPAT_STATFS_SIZE 48 + +struct fuse_statfs_out { + struct fuse_kstatfs st; +}; + +struct fuse_fsync_in { + __u64 fh; + __u32 fsync_flags; + __u32 padding; +}; + +struct fuse_setxattr_in { + __u32 size; + __u32 flags; +}; + +struct fuse_getxattr_in { + __u32 size; + __u32 padding; +}; + +struct fuse_getxattr_out { + __u32 size; + __u32 padding; +}; + +struct fuse_lk_in { + __u64 fh; + __u64 owner; + struct fuse_file_lock lk; + __u32 lk_flags; + __u32 padding; +}; + +struct fuse_lk_out { + struct fuse_file_lock lk; +}; + +struct fuse_access_in { + __u32 mask; + __u32 padding; +}; + +struct fuse_init_in { + __u32 major; + __u32 minor; + __u32 max_readahead; + __u32 flags; +}; + +struct fuse_init_out { + __u32 major; + __u32 minor; + __u32 max_readahead; + __u32 flags; + __u32 unused; + __u32 max_write; +}; + +#define CUSE_INIT_INFO_MAX 4096 + +struct cuse_init_in { + __u32 major; + __u32 minor; + __u32 unused; + __u32 flags; +}; + +struct cuse_init_out { + __u32 major; + __u32 minor; + __u32 unused; + __u32 flags; + __u32 max_read; + __u32 max_write; + __u32 dev_major; /* chardev major */ + __u32 dev_minor; /* chardev minor */ + __u32 spare[10]; +}; + +struct fuse_interrupt_in { + __u64 unique; +}; + +struct fuse_bmap_in { + __u64 block; + __u32 blocksize; + __u32 padding; +}; + +struct fuse_bmap_out { + __u64 block; +}; + +struct fuse_ioctl_in { + __u64 fh; + __u32 flags; + __u32 cmd; + __u64 arg; + __u32 in_size; + __u32 out_size; +}; + +struct fuse_ioctl_out { + __s32 result; + __u32 flags; + __u32 in_iovs; + __u32 out_iovs; +}; + +struct fuse_poll_in { + __u64 fh; + __u64 kh; + __u32 flags; + __u32 padding; +}; + +struct fuse_poll_out { + __u32 revents; + __u32 padding; +}; + +struct fuse_notify_poll_wakeup_out { + __u64 kh; +}; + +struct fuse_in_header { + __u32 len; + __u32 opcode; + __u64 unique; + __u64 nodeid; + __u32 uid; + __u32 gid; + __u32 pid; + __u32 padding; +}; + +struct fuse_out_header { + __u32 len; + __s32 error; + __u64 unique; +}; + +struct fuse_dirent { + __u64 ino; + __u64 off; + __u32 namelen; + __u32 type; + char name[0]; +}; + +#define FUSE_NAME_OFFSET offsetof(struct fuse_dirent, name) +#define FUSE_DIRENT_ALIGN(x) (((x) + sizeof(__u64) - 1) & ~(sizeof(__u64) - 1)) +#define FUSE_DIRENT_SIZE(d) \ + FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + (d)->namelen) + +struct fuse_notify_inval_inode_out { + __u64 ino; + __s64 off; + __s64 len; +}; + +struct fuse_notify_inval_entry_out { + __u64 parent; + __u32 namelen; + __u32 padding; +}; + +#endif /* _LINUX_FUSE_H */ diff --git a/fuse/include/fuse_lowlevel.h b/fuse/include/fuse_lowlevel.h new file mode 100644 index 000000000..f1cfeb954 --- /dev/null +++ b/fuse/include/fuse_lowlevel.h @@ -0,0 +1,1577 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#ifndef _FUSE_LOWLEVEL_H_ +#define _FUSE_LOWLEVEL_H_ + +/** @file + * + * Low level API + * + * IMPORTANT: you should define FUSE_USE_VERSION before including this + * header. To use the newest API define it to 26 (recommended for any + * new application), to use the old API define it to 24 (default) or + * 25 + */ + +#ifndef FUSE_USE_VERSION +#define FUSE_USE_VERSION 24 +#endif + +#include "fuse_common.h" + +#include +#include +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* ----------------------------------------------------------- * + * Miscellaneous definitions * + * ----------------------------------------------------------- */ + +/** The node ID of the root inode */ +#define FUSE_ROOT_ID 1 + +/** Inode number type */ +typedef unsigned long fuse_ino_t; + +/** Request pointer type */ +typedef struct fuse_req *fuse_req_t; + +/** + * Session + * + * This provides hooks for processing requests, and exiting + */ +struct fuse_session; + +/** + * Channel + * + * A communication channel, providing hooks for sending and receiving + * messages + */ +struct fuse_chan; + +/** Directory entry parameters supplied to fuse_reply_entry() */ +struct fuse_entry_param { + /** Unique inode number + * + * In lookup, zero means negative entry (from version 2.5) + * Returning ENOENT also means negative entry, but by setting zero + * ino the kernel may cache negative entries for entry_timeout + * seconds. + */ + fuse_ino_t ino; + + /** Generation number for this entry. + * + * The ino/generation pair should be unique for the filesystem's + * lifetime. It must be non-zero, otherwise FUSE will treat it as an + * error. + */ + unsigned long generation; + + /** Inode attributes. + * + * Even if attr_timeout == 0, attr must be correct. For example, + * for open(), FUSE uses attr.st_size from lookup() to determine + * how many bytes to request. If this value is not correct, + * incorrect data will be returned. + */ + struct stat attr; + + /** Validity timeout (in seconds) for the attributes */ + double attr_timeout; + + /** Validity timeout (in seconds) for the name */ + double entry_timeout; +}; + +/** Additional context associated with requests */ +struct fuse_ctx { + /** User ID of the calling process */ + uid_t uid; + + /** Group ID of the calling process */ + gid_t gid; + + /** Thread ID of the calling process */ + pid_t pid; + + /** Umask of the calling process (introduced in version 2.8) */ + mode_t umask; +}; + +/* 'to_set' flags in setattr */ +#define FUSE_SET_ATTR_MODE (1 << 0) +#define FUSE_SET_ATTR_UID (1 << 1) +#define FUSE_SET_ATTR_GID (1 << 2) +#define FUSE_SET_ATTR_SIZE (1 << 3) +#define FUSE_SET_ATTR_ATIME (1 << 4) +#define FUSE_SET_ATTR_MTIME (1 << 5) +#define FUSE_SET_ATTR_ATIME_NOW (1 << 7) +#define FUSE_SET_ATTR_MTIME_NOW (1 << 8) + +/* ----------------------------------------------------------- * + * Request methods and replies * + * ----------------------------------------------------------- */ + +/** + * Low level filesystem operations + * + * Most of the methods (with the exception of init and destroy) + * receive a request handle (fuse_req_t) as their first argument. + * This handle must be passed to one of the specified reply functions. + * + * This may be done inside the method invocation, or after the call + * has returned. The request handle is valid until one of the reply + * functions is called. + * + * Other pointer arguments (name, fuse_file_info, etc) are not valid + * after the call has returned, so if they are needed later, their + * contents have to be copied. + * + * The filesystem sometimes needs to handle a return value of -ENOENT + * from the reply function, which means, that the request was + * interrupted, and the reply discarded. For example if + * fuse_reply_open() return -ENOENT means, that the release method for + * this file will not be called. + */ +struct fuse_lowlevel_ops { + /** + * Initialize filesystem + * + * Called before any other filesystem method + * + * There's no reply to this function + * + * @param userdata the user data passed to fuse_lowlevel_new() + */ + void (*init) (void *userdata, struct fuse_conn_info *conn); + + /** + * Clean up filesystem + * + * Called on filesystem exit + * + * There's no reply to this function + * + * @param userdata the user data passed to fuse_lowlevel_new() + */ + void (*destroy) (void *userdata); + + /** + * Look up a directory entry by name and get its attributes. + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name the name to look up + */ + void (*lookup) (fuse_req_t req, fuse_ino_t parent, const char *name); + + /** + * Forget about an inode + * + * The nlookup parameter indicates the number of lookups + * previously performed on this inode. + * + * If the filesystem implements inode lifetimes, it is recommended + * that inodes acquire a single reference on each lookup, and lose + * nlookup references on each forget. + * + * The filesystem may ignore forget calls, if the inodes don't + * need to have a limited lifetime. + * + * On unmount it is not guaranteed, that all referenced inodes + * will receive a forget message. + * + * Valid replies: + * fuse_reply_none + * + * @param req request handle + * @param ino the inode number + * @param nlookup the number of lookups to forget + */ + void (*forget) (fuse_req_t req, fuse_ino_t ino, unsigned long nlookup); + + /** + * Get file attributes + * + * Valid replies: + * fuse_reply_attr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi for future use, currently always NULL + */ + void (*getattr) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Set file attributes + * + * In the 'attr' argument only members indicated by the 'to_set' + * bitmask contain valid values. Other members contain undefined + * values. + * + * If the setattr was invoked from the ftruncate() system call + * under Linux kernel versions 2.6.15 or later, the fi->fh will + * contain the value set by the open method or will be undefined + * if the open method didn't set any value. Otherwise (not + * ftruncate call, or kernel version earlier than 2.6.15) the fi + * parameter will be NULL. + * + * Valid replies: + * fuse_reply_attr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param attr the attributes + * @param to_set bit mask of attributes which should be set + * @param fi file information, or NULL + * + * Changed in version 2.5: + * file information filled in for ftruncate + */ + void (*setattr) (fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int to_set, struct fuse_file_info *fi); + + /** + * Read symbolic link + * + * Valid replies: + * fuse_reply_readlink + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + */ + void (*readlink) (fuse_req_t req, fuse_ino_t ino); + + /** + * Create file node + * + * Create a regular file, character device, block device, fifo or + * socket node. + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to create + * @param mode file type and mode with which to create the new file + * @param rdev the device number (only valid if created file is a device) + */ + void (*mknod) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev); + + /** + * Create a directory + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to create + * @param mode with which to create the new file + */ + void (*mkdir) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode); + + /** + * Remove a file + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to remove + */ + void (*unlink) (fuse_req_t req, fuse_ino_t parent, const char *name); + + /** + * Remove a directory + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to remove + */ + void (*rmdir) (fuse_req_t req, fuse_ino_t parent, const char *name); + + /** + * Create a symbolic link + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param link the contents of the symbolic link + * @param parent inode number of the parent directory + * @param name to create + */ + void (*symlink) (fuse_req_t req, const char *link, fuse_ino_t parent, + const char *name); + + /** Rename a file + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the old parent directory + * @param name old name + * @param newparent inode number of the new parent directory + * @param newname new name + */ + void (*rename) (fuse_req_t req, fuse_ino_t parent, const char *name, + fuse_ino_t newparent, const char *newname); + + /** + * Create a hard link + * + * Valid replies: + * fuse_reply_entry + * fuse_reply_err + * + * @param req request handle + * @param ino the old inode number + * @param newparent inode number of the new parent directory + * @param newname new name to create + */ + void (*link) (fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, + const char *newname); + + /** + * Open a file + * + * Open flags (with the exception of O_CREAT, O_EXCL, O_NOCTTY and + * O_TRUNC) are available in fi->flags. + * + * Filesystem may store an arbitrary file handle (pointer, index, + * etc) in fi->fh, and use this in other all other file operations + * (read, write, flush, release, fsync). + * + * Filesystem may also implement stateless file I/O and not store + * anything in fi->fh. + * + * There are also some flags (direct_io, keep_cache) which the + * filesystem may set in fi, to change the way the file is opened. + * See fuse_file_info structure in for more details. + * + * Valid replies: + * fuse_reply_open + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*open) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Read data + * + * Read should send exactly the number of bytes requested except + * on EOF or error, otherwise the rest of the data will be + * substituted with zeroes. An exception to this is when the file + * has been opened in 'direct_io' mode, in which case the return + * value of the read system call will reflect the return value of + * this operation. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_iov + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param size number of bytes to read + * @param off offset to read from + * @param fi file information + */ + void (*read) (fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, + struct fuse_file_info *fi); + + /** + * Write data + * + * Write should return exactly the number of bytes requested + * except on error. An exception to this is when the file has + * been opened in 'direct_io' mode, in which case the return value + * of the write system call will reflect the return value of this + * operation. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * + * Valid replies: + * fuse_reply_write + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param buf data to write + * @param size number of bytes to write + * @param off offset to write to + * @param fi file information + */ + void (*write) (fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off64_t off, struct fuse_file_info *fi); + + /** + * Flush method + * + * This is called on each close() of the opened file. + * + * Since file descriptors can be duplicated (dup, dup2, fork), for + * one open call there may be many flush calls. + * + * Filesystems shouldn't assume that flush will always be called + * after some writes, or that if will be called at all. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * + * NOTE: the name of the method is misleading, since (unlike + * fsync) the filesystem is not forced to flush pending writes. + * One reason to flush data, is if the filesystem wants to return + * write errors. + * + * If the filesystem supports file locking operations (setlk, + * getlk) it should remove all locks belonging to 'fi->owner'. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*flush) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Release an open file + * + * Release is called when there are no more references to an open + * file: all file descriptors are closed and all memory mappings + * are unmapped. + * + * For every open call there will be exactly one release call. + * + * The filesystem may reply with an error, but error values are + * not returned to close() or munmap() which triggered the + * release. + * + * fi->fh will contain the value set by the open method, or will + * be undefined if the open method didn't set any value. + * fi->flags will contain the same flags as for open. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*release) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Synchronize file contents + * + * If the datasync parameter is non-zero, then only the user data + * should be flushed, not the meta data. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param datasync flag indicating if only data should be flushed + * @param fi file information + */ + void (*fsync) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi); + + /** + * Open a directory + * + * Filesystem may store an arbitrary file handle (pointer, index, + * etc) in fi->fh, and use this in other all other directory + * stream operations (readdir, releasedir, fsyncdir). + * + * Filesystem may also implement stateless directory I/O and not + * store anything in fi->fh, though that makes it impossible to + * implement standard conforming directory stream operations in + * case the contents of the directory can change between opendir + * and releasedir. + * + * Valid replies: + * fuse_reply_open + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*opendir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Read directory + * + * Send a buffer filled using fuse_add_direntry(), with size not + * exceeding the requested size. Send an empty buffer on end of + * stream. + * + * fi->fh will contain the value set by the opendir method, or + * will be undefined if the opendir method didn't set any value. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param size maximum number of bytes to send + * @param off offset to continue reading the directory stream + * @param fi file information + */ + void (*readdir) (fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, + struct fuse_file_info *fi); + + /** + * Release an open directory + * + * For every opendir call there will be exactly one releasedir + * call. + * + * fi->fh will contain the value set by the opendir method, or + * will be undefined if the opendir method didn't set any value. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + */ + void (*releasedir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + + /** + * Synchronize directory contents + * + * If the datasync parameter is non-zero, then only the directory + * contents should be flushed, not the meta data. + * + * fi->fh will contain the value set by the opendir method, or + * will be undefined if the opendir method didn't set any value. + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param datasync flag indicating if only data should be flushed + * @param fi file information + */ + void (*fsyncdir) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi); + + /** + * Get file system statistics + * + * Valid replies: + * fuse_reply_statfs + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number, zero means "undefined" + */ + void (*statfs) (fuse_req_t req, fuse_ino_t ino); + + /** + * Set an extended attribute + * + * Valid replies: + * fuse_reply_err + */ + void (*setxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags); + + /** + * Get an extended attribute + * + * If size is zero, the size of the value should be sent with + * fuse_reply_xattr. + * + * If the size is non-zero, and the value fits in the buffer, the + * value should be sent with fuse_reply_buf. + * + * If the size is too small for the value, the ERANGE error should + * be sent. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_xattr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param name of the extended attribute + * @param size maximum size of the value to send + */ + void (*getxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size); + + /** + * List extended attribute names + * + * If size is zero, the total size of the attribute list should be + * sent with fuse_reply_xattr. + * + * If the size is non-zero, and the null character separated + * attribute list fits in the buffer, the list should be sent with + * fuse_reply_buf. + * + * If the size is too small for the list, the ERANGE error should + * be sent. + * + * Valid replies: + * fuse_reply_buf + * fuse_reply_xattr + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param size maximum size of the list to send + */ + void (*listxattr) (fuse_req_t req, fuse_ino_t ino, size_t size); + + /** + * Remove an extended attribute + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param name of the extended attribute + */ + void (*removexattr) (fuse_req_t req, fuse_ino_t ino, const char *name); + + /** + * Check file access permissions + * + * This will be called for the access() system call. If the + * 'default_permissions' mount option is given, this method is not + * called. + * + * This method is not called under Linux kernel versions 2.4.x + * + * Introduced in version 2.5 + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param mask requested access mode + */ + void (*access) (fuse_req_t req, fuse_ino_t ino, int mask); + + /** + * Create and open a file + * + * If the file does not exist, first create it with the specified + * mode, and then open it. + * + * Open flags (with the exception of O_NOCTTY) are available in + * fi->flags. + * + * Filesystem may store an arbitrary file handle (pointer, index, + * etc) in fi->fh, and use this in other all other file operations + * (read, write, flush, release, fsync). + * + * There are also some flags (direct_io, keep_cache) which the + * filesystem may set in fi, to change the way the file is opened. + * See fuse_file_info structure in for more details. + * + * If this method is not implemented or under Linux kernel + * versions earlier than 2.6.15, the mknod() and open() methods + * will be called instead. + * + * Introduced in version 2.5 + * + * Valid replies: + * fuse_reply_create + * fuse_reply_err + * + * @param req request handle + * @param parent inode number of the parent directory + * @param name to create + * @param mode file type and mode with which to create the new file + * @param fi file information + */ + void (*create) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, struct fuse_file_info *fi); + + /** + * Test for a POSIX file lock + * + * Introduced in version 2.6 + * + * Valid replies: + * fuse_reply_lock + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + * @param lock the region/type to test + */ + void (*getlk) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, struct flock *lock); + + /** + * Acquire, modify or release a POSIX file lock + * + * For POSIX threads (NPTL) there's a 1-1 relation between pid and + * owner, but otherwise this is not always the case. For checking + * lock ownership, 'fi->owner' must be used. The l_pid field in + * 'struct flock' should only be used to fill in this field in + * getlk(). + * + * Note: if the locking methods are not implemented, the kernel + * will still allow file locking to work locally. Hence these are + * only interesting for network filesystems and similar. + * + * Introduced in version 2.6 + * + * Valid replies: + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + * @param lock the region/type to test + * @param sleep locking operation may sleep + */ + void (*setlk) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi, + struct flock *lock, int sleep); + + /** + * Map block index within file to block index within device + * + * Note: This makes sense only for block device backed filesystems + * mounted with the 'blkdev' option + * + * Introduced in version 2.6 + * + * Valid replies: + * fuse_reply_bmap + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param blocksize unit of block index + * @param idx block index within file + */ + void (*bmap) (fuse_req_t req, fuse_ino_t ino, size_t blocksize, + uint64_t idx); + + /** + * Ioctl + * + * Note: For unrestricted ioctls (not allowed for FUSE + * servers), data in and out areas can be discovered by giving + * iovs and setting FUSE_IOCTL_RETRY in @flags. For + * restricted ioctls, kernel prepares in/out data area + * according to the information encoded in cmd. + * + * Introduced in version 2.8 + * + * Valid replies: + * fuse_reply_ioctl_retry + * fuse_reply_ioctl + * fuse_reply_ioctl_iov + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param cmd ioctl command + * @param arg ioctl argument + * @param fi file information + * @param flags for FUSE_IOCTL_* flags + * @param in_buf data fetched from the caller + * @param in_bufsz number of fetched bytes + * @param out_bufsz maximum size of output data + */ + void (*ioctl) (fuse_req_t req, fuse_ino_t ino, int cmd, void *arg, + struct fuse_file_info *fi, unsigned flags, + const void *in_buf, size_t in_bufsz, size_t out_bufsz); + + /** + * Poll for IO readiness + * + * Introduced in version 2.8 + * + * Note: If ph is non-NULL, the client should notify + * when IO readiness events occur by calling + * fuse_lowelevel_notify_poll() with the specified ph. + * + * Regardless of the number of times poll with a non-NULL ph + * is received, single notification is enough to clear all. + * Notifying more times incurs overhead but doesn't harm + * correctness. + * + * The callee is responsible for destroying ph with + * fuse_pollhandle_destroy() when no longer in use. + * + * Valid replies: + * fuse_reply_poll + * fuse_reply_err + * + * @param req request handle + * @param ino the inode number + * @param fi file information + * @param ph poll handle to be used for notification + */ + void (*poll) (fuse_req_t req, fuse_ino_t ino, struct fuse_file_info *fi, + struct fuse_pollhandle *ph); +}; + +/** + * Reply with an error code or success + * + * Possible requests: + * all except forget + * + * unlink, rmdir, rename, flush, release, fsync, fsyncdir, setxattr, + * removexattr and setlk may send a zero code + * + * @param req request handle + * @param err the positive error value, or zero for success + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_err(fuse_req_t req, int err); + +/** + * Don't send reply + * + * Possible requests: + * forget + * + * @param req request handle + */ +void fuse_reply_none(fuse_req_t req); + +/** + * Reply with a directory entry + * + * Possible requests: + * lookup, mknod, mkdir, symlink, link + * + * @param req request handle + * @param e the entry parameters + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e); + +/** + * Reply with a directory entry and open parameters + * + * currently the following members of 'fi' are used: + * fh, direct_io, keep_cache + * + * Possible requests: + * create + * + * @param req request handle + * @param e the entry parameters + * @param fi file information + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, + const struct fuse_file_info *fi); + +/** + * Reply with attributes + * + * Possible requests: + * getattr, setattr + * + * @param req request handle + * @param attr the attributes + * @param attr_timeout validity timeout (in seconds) for the attributes + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_attr(fuse_req_t req, const struct stat *attr, + double attr_timeout); + +/** + * Reply with the contents of a symbolic link + * + * Possible requests: + * readlink + * + * @param req request handle + * @param link symbolic link contents + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_readlink(fuse_req_t req, const char *link); + +/** + * Reply with open parameters + * + * currently the following members of 'fi' are used: + * fh, direct_io, keep_cache + * + * Possible requests: + * open, opendir + * + * @param req request handle + * @param fi file information + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *fi); + +/** + * Reply with number of bytes written + * + * Possible requests: + * write + * + * @param req request handle + * @param count the number of bytes written + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_write(fuse_req_t req, size_t count); + +/** + * Reply with data + * + * Possible requests: + * read, readdir, getxattr, listxattr + * + * @param req request handle + * @param buf buffer containing data + * @param size the size of data in bytes + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size); + +/** + * Reply with data vector + * + * Possible requests: + * read, readdir, getxattr, listxattr + * + * @param req request handle + * @param iov the vector containing the data + * @param count the size of vector + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count); + +/** + * Reply with filesystem statistics + * + * Possible requests: + * statfs + * + * @param req request handle + * @param stbuf filesystem statistics + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf); + +/** + * Reply with needed buffer size + * + * Possible requests: + * getxattr, listxattr + * + * @param req request handle + * @param count the buffer size needed in bytes + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_xattr(fuse_req_t req, size_t count); + +/** + * Reply with file lock information + * + * Possible requests: + * getlk + * + * @param req request handle + * @param lock the lock information + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_lock(fuse_req_t req, struct flock *lock); + +/** + * Reply with block index + * + * Possible requests: + * bmap + * + * @param req request handle + * @param idx block index within device + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_bmap(fuse_req_t req, uint64_t idx); + +/* ----------------------------------------------------------- * + * Filling a buffer in readdir * + * ----------------------------------------------------------- */ + +/** + * Add a directory entry to the buffer + * + * Buffer needs to be large enough to hold the entry. If it's not, + * then the entry is not filled in but the size of the entry is still + * returned. The caller can check this by comparing the bufsize + * parameter with the returned entry size. If the entry size is + * larger than the buffer size, the operation failed. + * + * From the 'stbuf' argument the st_ino field and bits 12-15 of the + * st_mode field are used. The other fields are ignored. + * + * Note: offsets do not necessarily represent physical offsets, and + * could be any marker, that enables the implementation to find a + * specific point in the directory stream. + * + * @param req request handle + * @param buf the point where the new entry will be added to the buffer + * @param bufsize remaining size of the buffer + * @param name the name of the entry + * @param stbuf the file attributes + * @param off the offset of the next entry + * @return the space needed for the entry + */ +size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, + const char *name, const struct stat *stbuf, + off64_t off); + +/** + * Reply to ask for data fetch and output buffer preparation. ioctl + * will be retried with the specified input data fetched and output + * buffer prepared. + * + * Possible requests: + * ioctl + * + * @param req request handle + * @param in_iov iovec specifying data to fetch from the caller + * @param in_count number of entries in in_iov + * @param out_iov iovec specifying addresses to write output to + * @param out_count number of entries in out_iov + * @return zero for success, -errno for failure to send reply + */ +int fuse_reply_ioctl_retry(fuse_req_t req, + const struct iovec *in_iov, size_t in_count, + const struct iovec *out_iov, size_t out_count); + +/** + * Reply to finish ioctl + * + * Possible requests: + * ioctl + * + * @param req request handle + * @param result result to be passed to the caller + * @param buf buffer containing output data + * @param size length of output data + */ +int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size); + +/** + * Reply to finish ioctl with iov buffer + * + * Possible requests: + * ioctl + * + * @param req request handle + * @param result result to be passed to the caller + * @param iov the vector containing the data + * @param count the size of vector + */ +int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, + int count); + +/** + * Reply with poll result event mask + * + * @param req request handle + * @param revents poll result event mask + */ +int fuse_reply_poll(fuse_req_t req, unsigned revents); + +/* ----------------------------------------------------------- * + * Notification * + * ----------------------------------------------------------- */ + +/** + * Notify IO readiness event + * + * For more information, please read comment for poll operation. + * + * @param ph poll handle to notify IO readiness event for + */ +int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph); + +/** + * Notify to invalidate cache for an inode + * + * @param ch the channel through which to send the invalidation + * @param ino the inode number + * @param off the offset in the inode where to start invalidating + * or negative to invalidate attributes only + * @param len the amount of cache to invalidate or 0 for all + * @return zero for success, -errno for failure + */ +int fuse_lowlevel_notify_inval_inode(struct fuse_chan *ch, fuse_ino_t ino, + off64_t off, off64_t len); + +/** + * Notify to invalidate parent attributes and the dentry matching + * parent/name + * + * @param ch the channel through which to send the invalidation + * @param parent inode number + * @param name file name + * @param namelen strlen() of file name + * @return zero for success, -errno for failure + */ +int fuse_lowlevel_notify_inval_entry(struct fuse_chan *ch, fuse_ino_t parent, + const char *name, size_t namelen); + +/* ----------------------------------------------------------- * + * Utility functions * + * ----------------------------------------------------------- */ + +/** + * Get the userdata from the request + * + * @param req request handle + * @return the user data passed to fuse_lowlevel_new() + */ +void *fuse_req_userdata(fuse_req_t req); + +/** + * Get the context from the request + * + * The pointer returned by this function will only be valid for the + * request's lifetime + * + * @param req request handle + * @return the context structure + */ +const struct fuse_ctx *fuse_req_ctx(fuse_req_t req); + +/** + * Get the current supplementary group IDs for the specified request + * + * Similar to the getgroups(2) system call, except the return value is + * always the total number of group IDs, even if it is larger than the + * specified size. + * + * The current fuse kernel module in linux (as of 2.6.30) doesn't pass + * the group list to userspace, hence this function needs to parse + * "/proc/$TID/task/$TID/status" to get the group IDs. + * + * This feature may not be supported on all operating systems. In + * such a case this function will return -ENOSYS. + * + * @param req request handle + * @param size size of given array + * @param list array of group IDs to be filled in + * @return the total number of supplementary group IDs or -errno on failure + */ +int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]); + +/** + * Callback function for an interrupt + * + * @param req interrupted request + * @param data user data + */ +typedef void (*fuse_interrupt_func_t)(fuse_req_t req, void *data); + +/** + * Register/unregister callback for an interrupt + * + * If an interrupt has already happened, then the callback function is + * called from within this function, hence it's not possible for + * interrupts to be lost. + * + * @param req request handle + * @param func the callback function or NULL for unregister + * @param data user data passed to the callback function + */ +void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, + void *data); + +/** + * Check if a request has already been interrupted + * + * @param req request handle + * @return 1 if the request has been interrupted, 0 otherwise + */ +int fuse_req_interrupted(fuse_req_t req); + +/* ----------------------------------------------------------- * + * Filesystem setup * + * ----------------------------------------------------------- */ + +/* Deprecated, don't use */ +int fuse_lowlevel_is_lib_option(const char *opt); + +/** + * Create a low level session + * + * @param args argument vector + * @param op the low level filesystem operations + * @param op_size sizeof(struct fuse_lowlevel_ops) + * @param userdata user data + * @return the created session object, or NULL on failure + */ +struct fuse_session *fuse_lowlevel_new(struct fuse_args *args, + const struct fuse_lowlevel_ops *op, + size_t op_size, void *userdata); + +/* ----------------------------------------------------------- * + * Session interface * + * ----------------------------------------------------------- */ + +/** + * Session operations + * + * This is used in session creation + */ +struct fuse_session_ops { + /** + * Hook to process a request (mandatory) + * + * @param data user data passed to fuse_session_new() + * @param buf buffer containing the raw request + * @param len request length + * @param ch channel on which the request was received + */ + void (*process) (void *data, const char *buf, size_t len, + struct fuse_chan *ch); + + /** + * Hook for session exit and reset (optional) + * + * @param data user data passed to fuse_session_new() + * @param val exited status (1 - exited, 0 - not exited) + */ + void (*exit) (void *data, int val); + + /** + * Hook for querying the current exited status (optional) + * + * @param data user data passed to fuse_session_new() + * @return 1 if exited, 0 if not exited + */ + int (*exited) (void *data); + + /** + * Hook for cleaning up the channel on destroy (optional) + * + * @param data user data passed to fuse_session_new() + */ + void (*destroy) (void *data); +}; + +/** + * Create a new session + * + * @param op session operations + * @param data user data + * @return new session object, or NULL on failure + */ +struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data); + +/** + * Assign a channel to a session + * + * Note: currently only a single channel may be assigned. This may + * change in the future + * + * If a session is destroyed, the assigned channel is also destroyed + * + * @param se the session + * @param ch the channel + */ +void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch); + +/** + * Remove a channel from a session + * + * If the channel is not assigned to a session, then this is a no-op + * + * @param ch the channel to remove + */ +void fuse_session_remove_chan(struct fuse_chan *ch); + +/** + * Iterate over the channels assigned to a session + * + * The iterating function needs to start with a NULL channel, and + * after that needs to pass the previously returned channel to the + * function. + * + * @param se the session + * @param ch the previous channel, or NULL + * @return the next channel, or NULL if no more channels exist + */ +struct fuse_chan *fuse_session_next_chan(struct fuse_session *se, + struct fuse_chan *ch); + +/** + * Process a raw request + * + * @param se the session + * @param buf buffer containing the raw request + * @param len request length + * @param ch channel on which the request was received + */ +void fuse_session_process(struct fuse_session *se, const char *buf, size_t len, + struct fuse_chan *ch); + +/** + * Destroy a session + * + * @param se the session + */ +void fuse_session_destroy(struct fuse_session *se); + +/** + * Exit a session + * + * @param se the session + */ +void fuse_session_exit(struct fuse_session *se); + +/** + * Reset the exited status of a session + * + * @param se the session + */ +void fuse_session_reset(struct fuse_session *se); + +/** + * Query the exited status of a session + * + * @param se the session + * @return 1 if exited, 0 if not exited + */ +int fuse_session_exited(struct fuse_session *se); + +/** + * Get the user data provided to the session + * + * @param se the session + * @return the user data + */ +void *fuse_session_data(struct fuse_session *se); + +/** + * Enter a single threaded event loop + * + * @param se the session + * @return 0 on success, -1 on error + */ +int fuse_session_loop(struct fuse_session *se); + +/** + * Enter a multi-threaded event loop + * + * @param se the session + * @return 0 on success, -1 on error + */ +int fuse_session_loop_mt(struct fuse_session *se); + +/* ----------------------------------------------------------- * + * Channel interface * + * ----------------------------------------------------------- */ + +/** + * Channel operations + * + * This is used in channel creation + */ +struct fuse_chan_ops { + /** + * Hook for receiving a raw request + * + * @param ch pointer to the channel + * @param buf the buffer to store the request in + * @param size the size of the buffer + * @return the actual size of the raw request, or -1 on error + */ + int (*receive)(struct fuse_chan **chp, char *buf, size_t size); + + /** + * Hook for sending a raw reply + * + * A return value of -ENOENT means, that the request was + * interrupted, and the reply was discarded + * + * @param ch the channel + * @param iov vector of blocks + * @param count the number of blocks in vector + * @return zero on success, -errno on failure + */ + int (*send)(struct fuse_chan *ch, const struct iovec iov[], + size_t count); + + /** + * Destroy the channel + * + * @param ch the channel + */ + void (*destroy)(struct fuse_chan *ch); +}; + +/** + * Create a new channel + * + * @param op channel operations + * @param fd file descriptor of the channel + * @param bufsize the minimal receive buffer size + * @param data user data + * @return the new channel object, or NULL on failure + */ +struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd, + size_t bufsize, void *data); + +/** + * Query the file descriptor of the channel + * + * @param ch the channel + * @return the file descriptor passed to fuse_chan_new() + */ +int fuse_chan_fd(struct fuse_chan *ch); + +/** + * Query the minimal receive buffer size + * + * @param ch the channel + * @return the buffer size passed to fuse_chan_new() + */ +size_t fuse_chan_bufsize(struct fuse_chan *ch); + +/** + * Query the user data + * + * @param ch the channel + * @return the user data passed to fuse_chan_new() + */ +void *fuse_chan_data(struct fuse_chan *ch); + +/** + * Query the session to which this channel is assigned + * + * @param ch the channel + * @return the session, or NULL if the channel is not assigned + */ +struct fuse_session *fuse_chan_session(struct fuse_chan *ch); + +/** + * Receive a raw request + * + * A return value of -ENODEV means, that the filesystem was unmounted + * + * @param ch pointer to the channel + * @param buf the buffer to store the request in + * @param size the size of the buffer + * @return the actual size of the raw request, or -errno on error + */ +int fuse_chan_recv(struct fuse_chan **ch, char *buf, size_t size); + +/** + * Send a raw reply + * + * A return value of -ENOENT means, that the request was + * interrupted, and the reply was discarded + * + * @param ch the channel + * @param iov vector of blocks + * @param count the number of blocks in vector + * @return zero on success, -errno on failure + */ +int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], + size_t count); + +/** + * Destroy a channel + * + * @param ch the channel + */ +void fuse_chan_destroy(struct fuse_chan *ch); + +/* ----------------------------------------------------------- * + * Compatibility stuff * + * ----------------------------------------------------------- */ + +#if FUSE_USE_VERSION < 26 +# include "fuse_lowlevel_compat.h" +# define fuse_chan_ops fuse_chan_ops_compat24 +# define fuse_chan_new fuse_chan_new_compat24 +# if FUSE_USE_VERSION == 25 +# define fuse_lowlevel_ops fuse_lowlevel_ops_compat25 +# define fuse_lowlevel_new fuse_lowlevel_new_compat25 +# elif FUSE_USE_VERSION == 24 +# define fuse_lowlevel_ops fuse_lowlevel_ops_compat +# define fuse_lowlevel_new fuse_lowlevel_new_compat +# define fuse_file_info fuse_file_info_compat +# define fuse_reply_statfs fuse_reply_statfs_compat +# define fuse_reply_open fuse_reply_open_compat +# else +# error Compatibility with low-level API version < 24 not supported +# endif +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_LOWLEVEL_H_ */ diff --git a/fuse/include/fuse_lowlevel_compat.h b/fuse/include/fuse_lowlevel_compat.h new file mode 100644 index 000000000..3d2902d7e --- /dev/null +++ b/fuse/include/fuse_lowlevel_compat.h @@ -0,0 +1,155 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +/* these definitions provide source compatibility to prior versions. + Do not include this file directly! */ + +struct fuse_lowlevel_ops_compat25 { + void (*init) (void *userdata); + void (*destroy) (void *userdata); + void (*lookup) (fuse_req_t req, fuse_ino_t parent, const char *name); + void (*forget) (fuse_req_t req, fuse_ino_t ino, unsigned long nlookup); + void (*getattr) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + void (*setattr) (fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int to_set, struct fuse_file_info *fi); + void (*readlink) (fuse_req_t req, fuse_ino_t ino); + void (*mknod) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev); + void (*mkdir) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode); + void (*unlink) (fuse_req_t req, fuse_ino_t parent, const char *name); + void (*rmdir) (fuse_req_t req, fuse_ino_t parent, const char *name); + void (*symlink) (fuse_req_t req, const char *link, fuse_ino_t parent, + const char *name); + void (*rename) (fuse_req_t req, fuse_ino_t parent, const char *name, + fuse_ino_t newparent, const char *newname); + void (*link) (fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, + const char *newname); + void (*open) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + void (*read) (fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, + struct fuse_file_info *fi); + void (*write) (fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off64_t off, struct fuse_file_info *fi); + void (*flush) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + void (*release) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + void (*fsync) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi); + void (*opendir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + void (*readdir) (fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, + struct fuse_file_info *fi); + void (*releasedir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info *fi); + void (*fsyncdir) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info *fi); + void (*statfs) (fuse_req_t req); + void (*setxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags); + void (*getxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size); + void (*listxattr) (fuse_req_t req, fuse_ino_t ino, size_t size); + void (*removexattr) (fuse_req_t req, fuse_ino_t ino, const char *name); + void (*access) (fuse_req_t req, fuse_ino_t ino, int mask); + void (*create) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, struct fuse_file_info *fi); +}; + +struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args, + const struct fuse_lowlevel_ops_compat25 *op, + size_t op_size, void *userdata); + +size_t fuse_dirent_size(size_t namelen); + +char *fuse_add_dirent(char *buf, const char *name, const struct stat *stbuf, + off64_t off); + +#ifndef __FreeBSD__ + +#include + +struct fuse_lowlevel_ops_compat { + void (*init) (void *userdata); + void (*destroy) (void *userdata); + void (*lookup) (fuse_req_t req, fuse_ino_t parent, const char *name); + void (*forget) (fuse_req_t req, fuse_ino_t ino, unsigned long nlookup); + void (*getattr) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info_compat *fi); + void (*setattr) (fuse_req_t req, fuse_ino_t ino, struct stat *attr, + int to_set, struct fuse_file_info_compat *fi); + void (*readlink) (fuse_req_t req, fuse_ino_t ino); + void (*mknod) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, dev_t rdev); + void (*mkdir) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode); + void (*unlink) (fuse_req_t req, fuse_ino_t parent, const char *name); + void (*rmdir) (fuse_req_t req, fuse_ino_t parent, const char *name); + void (*symlink) (fuse_req_t req, const char *link, fuse_ino_t parent, + const char *name); + void (*rename) (fuse_req_t req, fuse_ino_t parent, const char *name, + fuse_ino_t newparent, const char *newname); + void (*link) (fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, + const char *newname); + void (*open) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info_compat *fi); + void (*read) (fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, + struct fuse_file_info_compat *fi); + void (*write) (fuse_req_t req, fuse_ino_t ino, const char *buf, + size_t size, off64_t off, struct fuse_file_info_compat *fi); + void (*flush) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info_compat *fi); + void (*release) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info_compat *fi); + void (*fsync) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info_compat *fi); + void (*opendir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info_compat *fi); + void (*readdir) (fuse_req_t req, fuse_ino_t ino, size_t size, off64_t off, + struct fuse_file_info_compat *fi); + void (*releasedir) (fuse_req_t req, fuse_ino_t ino, + struct fuse_file_info_compat *fi); + void (*fsyncdir) (fuse_req_t req, fuse_ino_t ino, int datasync, + struct fuse_file_info_compat *fi); + void (*statfs) (fuse_req_t req); + void (*setxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + const char *value, size_t size, int flags); + void (*getxattr) (fuse_req_t req, fuse_ino_t ino, const char *name, + size_t size); + void (*listxattr) (fuse_req_t req, fuse_ino_t ino, size_t size); + void (*removexattr) (fuse_req_t req, fuse_ino_t ino, const char *name); + void (*access) (fuse_req_t req, fuse_ino_t ino, int mask); + void (*create) (fuse_req_t req, fuse_ino_t parent, const char *name, + mode_t mode, struct fuse_file_info_compat *fi); +}; + +int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf); + +int fuse_reply_open_compat(fuse_req_t req, + const struct fuse_file_info_compat *fi); + +struct fuse_session *fuse_lowlevel_new_compat(const char *opts, + const struct fuse_lowlevel_ops_compat *op, + size_t op_size, void *userdata); + +#endif /* __FreeBSD__ */ + +struct fuse_chan_ops_compat24 { + int (*receive)(struct fuse_chan *ch, char *buf, size_t size); + int (*send)(struct fuse_chan *ch, const struct iovec iov[], + size_t count); + void (*destroy)(struct fuse_chan *ch); +}; + +struct fuse_chan *fuse_chan_new_compat24(struct fuse_chan_ops_compat24 *op, + int fd, size_t bufsize, void *data); + +int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size); +struct fuse_chan *fuse_kern_chan_new(int fd); diff --git a/fuse/include/fuse_opt.h b/fuse/include/fuse_opt.h new file mode 100644 index 000000000..8c08d779a --- /dev/null +++ b/fuse/include/fuse_opt.h @@ -0,0 +1,270 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#ifndef _FUSE_OPT_H_ +#define _FUSE_OPT_H_ + +/** @file + * + * This file defines the option parsing interface of FUSE + */ + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Option description + * + * This structure describes a single option, and and action associated + * with it, in case it matches. + * + * More than one such match may occur, in which case the action for + * each match is executed. + * + * There are three possible actions in case of a match: + * + * i) An integer (int or unsigned) variable determined by 'offset' is + * set to 'value' + * + * ii) The processing function is called, with 'value' as the key + * + * iii) An integer (any) or string (char *) variable determined by + * 'offset' is set to the value of an option parameter + * + * 'offset' should normally be either set to + * + * - 'offsetof(struct foo, member)' actions i) and iii) + * + * - -1 action ii) + * + * The 'offsetof()' macro is defined in the header. + * + * The template determines which options match, and also have an + * effect on the action. Normally the action is either i) or ii), but + * if a format is present in the template, then action iii) is + * performed. + * + * The types of templates are: + * + * 1) "-x", "-foo", "--foo", "--foo-bar", etc. These match only + * themselves. Invalid values are "--" and anything beginning + * with "-o" + * + * 2) "foo", "foo-bar", etc. These match "-ofoo", "-ofoo-bar" or + * the relevant option in a comma separated option list + * + * 3) "bar=", "--foo=", etc. These are variations of 1) and 2) + * which have a parameter + * + * 4) "bar=%s", "--foo=%lu", etc. Same matching as above but perform + * action iii). + * + * 5) "-x ", etc. Matches either "-xparam" or "-x param" as + * two separate arguments + * + * 6) "-x %s", etc. Combination of 4) and 5) + * + * If the format is "%s", memory is allocated for the string unlike + * with scanf(). + */ +struct fuse_opt { + /** Matching template and optional parameter formatting */ + const char *templ; + + /** + * Offset of variable within 'data' parameter of fuse_opt_parse() + * or -1 + */ + unsigned long offset; + + /** + * Value to set the variable to, or to be passed as 'key' to the + * processing function. Ignored if template has a format + */ + int value; +}; + +/** + * Key option. In case of a match, the processing function will be + * called with the specified key. + */ +#define FUSE_OPT_KEY(templ, key) { templ, -1U, key } + +/** + * Last option. An array of 'struct fuse_opt' must end with a NULL + * template value + */ +#define FUSE_OPT_END { NULL, 0, 0 } + +/** + * Argument list + */ +struct fuse_args { + /** Argument count */ + int argc; + + /** Argument vector. NULL terminated */ + char **argv; + + /** Is 'argv' allocated? */ + int allocated; +}; + +/** + * Initializer for 'struct fuse_args' + */ +#define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 } + +/** + * Key value passed to the processing function if an option did not + * match any template + */ +#define FUSE_OPT_KEY_OPT -1 + +/** + * Key value passed to the processing function for all non-options + * + * Non-options are the arguments beginning with a charater other than + * '-' or all arguments after the special '--' option + */ +#define FUSE_OPT_KEY_NONOPT -2 + +/** + * Special key value for options to keep + * + * Argument is not passed to processing function, but behave as if the + * processing function returned 1 + */ +#define FUSE_OPT_KEY_KEEP -3 + +/** + * Special key value for options to discard + * + * Argument is not passed to processing function, but behave as if the + * processing function returned zero + */ +#define FUSE_OPT_KEY_DISCARD -4 + +/** + * Processing function + * + * This function is called if + * - option did not match any 'struct fuse_opt' + * - argument is a non-option + * - option did match and offset was set to -1 + * + * The 'arg' parameter will always contain the whole argument or + * option including the parameter if exists. A two-argument option + * ("-x foo") is always converted to single arguemnt option of the + * form "-xfoo" before this function is called. + * + * Options of the form '-ofoo' are passed to this function without the + * '-o' prefix. + * + * The return value of this function determines whether this argument + * is to be inserted into the output argument vector, or discarded. + * + * @param data is the user data passed to the fuse_opt_parse() function + * @param arg is the whole argument or option + * @param key determines why the processing function was called + * @param outargs the current output argument list + * @return -1 on error, 0 if arg is to be discarded, 1 if arg should be kept + */ +typedef int (*fuse_opt_proc_t)(void *data, const char *arg, int key, + struct fuse_args *outargs); + +/** + * Option parsing function + * + * If 'args' was returned from a previous call to fuse_opt_parse() or + * it was constructed from + * + * A NULL 'args' is equivalent to an empty argument vector + * + * A NULL 'opts' is equivalent to an 'opts' array containing a single + * end marker + * + * A NULL 'proc' is equivalent to a processing function always + * returning '1' + * + * @param args is the input and output argument list + * @param data is the user data + * @param opts is the option description array + * @param proc is the processing function + * @return -1 on error, 0 on success + */ +int fuse_opt_parse(struct fuse_args *args, void *data, + const struct fuse_opt opts[], fuse_opt_proc_t proc); + +/** + * Add an option to a comma separated option list + * + * @param opts is a pointer to an option list, may point to a NULL value + * @param opt is the option to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_add_opt(char **opts, const char *opt); + +/** + * Add an option, escaping commas, to a comma separated option list + * + * @param opts is a pointer to an option list, may point to a NULL value + * @param opt is the option to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_add_opt_escaped(char **opts, const char *opt); + +/** + * Add an argument to a NULL terminated argument vector + * + * @param args is the structure containing the current argument list + * @param arg is the new argument to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_add_arg(struct fuse_args *args, const char *arg); + +/** + * Add an argument at the specified position in a NULL terminated + * argument vector + * + * Adds the argument to the N-th position. This is useful for adding + * options at the beggining of the array which must not come after the + * special '--' option. + * + * @param args is the structure containing the current argument list + * @param pos is the position at which to add the argument + * @param arg is the new argument to add + * @return -1 on allocation error, 0 on success + */ +int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg); + +/** + * Free the contents of argument list + * + * The structure itself is not freed + * + * @param args is the structure containing the argument list + */ +void fuse_opt_free_args(struct fuse_args *args); + + +/** + * Check if an option matches + * + * @param opts is the option description array + * @param opt is the option to match + * @return 1 if a match is found, 0 if not + */ +int fuse_opt_match(const struct fuse_opt opts[], const char *opt); + +#ifdef __cplusplus +} +#endif + +#endif /* _FUSE_OPT_H_ */ diff --git a/fuse/include/old/fuse.h b/fuse/include/old/fuse.h new file mode 100644 index 000000000..3db0945a9 --- /dev/null +++ b/fuse/include/old/fuse.h @@ -0,0 +1,9 @@ +/* + This header is for compatibility with older software using FUSE. + + Please use 'pkg-config --cflags fuse' to set include path. The + correct usage is still '#include ', not '#include + '. +*/ + +#include "fuse/fuse.h" diff --git a/fuse/include/stamp-h1 b/fuse/include/stamp-h1 new file mode 100644 index 000000000..b330768e9 --- /dev/null +++ b/fuse/include/stamp-h1 @@ -0,0 +1 @@ +timestamp for include/config.h diff --git a/fuse/include/sys/statvfs.h b/fuse/include/sys/statvfs.h new file mode 100644 index 000000000..6e3e39fd5 --- /dev/null +++ b/fuse/include/sys/statvfs.h @@ -0,0 +1,18 @@ +#ifndef __STATVFS_H +#define __STATVFS_H + +struct statvfs { + unsigned long f_bsize; /* file system block size */ + unsigned long f_frsize; /* fragment size */ + fsblkcnt_t f_blocks; /* size of fs in f_frsize units */ + fsblkcnt_t f_bfree; /* # free blocks */ + fsblkcnt_t f_bavail; /* # free blocks for non-root */ + fsfilcnt_t f_files; /* # inodes */ + fsfilcnt_t f_ffree; /* # free inodes */ + fsfilcnt_t f_favail; /* # free inodes for non-root */ + unsigned long f_fsid; /* file system ID */ + unsigned long f_flag; /* mount flags */ + unsigned long f_namemax; /* maximum filename length */ +}; + +#endif diff --git a/fuse/include/ulockmgr.h b/fuse/include/ulockmgr.h new file mode 100644 index 000000000..ad555799f --- /dev/null +++ b/fuse/include/ulockmgr.h @@ -0,0 +1,24 @@ +/* + libulockmgr: Userspace Lock Manager Library + Copyright (C) 2006 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include +#include +#include + +/** + * Perform POSIX locking operation + * + * @param fd the file descriptor + * @param cmd the locking command (F_GETFL, F_SETLK or F_SETLKW) + * @param lock the lock parameters + * @param owner the lock owner ID cookie + * @param owner_len length of the lock owner ID cookie + * @return 0 on success -errno on error + */ +int ulockmgr_op(int fd, int cmd, struct flock *lock, const void *owner, + size_t owner_len); diff --git a/fuse/mount.c b/fuse/mount.c new file mode 100644 index 000000000..88ab59721 --- /dev/null +++ b/fuse/mount.c @@ -0,0 +1,596 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_opt.h" +#include "fuse_common_compat.h" +#include "mount_util.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FUSERMOUNT_PROG "fusermount" +#define FUSE_COMMFD_ENV "_FUSE_COMMFD" + +#ifndef HAVE_FORK +#define fork() vfork() +#endif + +#ifndef MS_DIRSYNC +#define MS_DIRSYNC 128 +#endif + +enum { + KEY_KERN_FLAG, + KEY_KERN_OPT, + KEY_FUSERMOUNT_OPT, + KEY_SUBTYPE_OPT, + KEY_MTAB_OPT, + KEY_ALLOW_ROOT, + KEY_RO, + KEY_HELP, + KEY_VERSION, +}; + +struct mount_opts { + int allow_other; + int allow_root; + int ishelp; + int flags; + int nonempty; + int blkdev; + char *fsname; + char *subtype; + char *subtype_opt; + char *mtab_opts; + char *fusermount_opts; + char *kernel_opts; +}; + +#define FUSE_MOUNT_OPT(t, p) { t, offsetof(struct mount_opts, p), 1 } + +static const struct fuse_opt fuse_mount_opts[] = { + FUSE_MOUNT_OPT("allow_other", allow_other), + FUSE_MOUNT_OPT("allow_root", allow_root), + FUSE_MOUNT_OPT("nonempty", nonempty), + FUSE_MOUNT_OPT("blkdev", blkdev), + FUSE_MOUNT_OPT("fsname=%s", fsname), + FUSE_MOUNT_OPT("subtype=%s", subtype), + FUSE_OPT_KEY("allow_other", KEY_KERN_OPT), + FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT), + FUSE_OPT_KEY("nonempty", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("blkdev", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("fsname=", KEY_FUSERMOUNT_OPT), + FUSE_OPT_KEY("subtype=", KEY_SUBTYPE_OPT), + FUSE_OPT_KEY("large_read", KEY_KERN_OPT), + FUSE_OPT_KEY("blksize=", KEY_KERN_OPT), + FUSE_OPT_KEY("default_permissions", KEY_KERN_OPT), + FUSE_OPT_KEY("max_read=", KEY_KERN_OPT), + FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("user=", KEY_MTAB_OPT), + FUSE_OPT_KEY("-r", KEY_RO), + FUSE_OPT_KEY("ro", KEY_KERN_FLAG), + FUSE_OPT_KEY("rw", KEY_KERN_FLAG), + FUSE_OPT_KEY("suid", KEY_KERN_FLAG), + FUSE_OPT_KEY("nosuid", KEY_KERN_FLAG), + FUSE_OPT_KEY("dev", KEY_KERN_FLAG), + FUSE_OPT_KEY("nodev", KEY_KERN_FLAG), + FUSE_OPT_KEY("exec", KEY_KERN_FLAG), + FUSE_OPT_KEY("noexec", KEY_KERN_FLAG), + FUSE_OPT_KEY("async", KEY_KERN_FLAG), + FUSE_OPT_KEY("sync", KEY_KERN_FLAG), + FUSE_OPT_KEY("dirsync", KEY_KERN_FLAG), + FUSE_OPT_KEY("atime", KEY_KERN_FLAG), + FUSE_OPT_KEY("noatime", KEY_KERN_FLAG), + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + FUSE_OPT_END +}; + +static void mount_help(void) +{ + fprintf(stderr, +" -o allow_other allow access to other users\n" +" -o allow_root allow access to root\n" +" -o nonempty allow mounts over non-empty file/dir\n" +" -o default_permissions enable permission checking by kernel\n" +" -o fsname=NAME set filesystem name\n" +" -o subtype=NAME set filesystem type\n" +" -o large_read issue large read requests (2.4 only)\n" +" -o max_read=N set maximum size of read requests\n" +"\n"); +} + +#define FUSERMOUNT_DIR "/usr/bin" + +static void exec_fusermount(const char *argv[]) +{ + execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv); + execvp(FUSERMOUNT_PROG, (char **) argv); +} + +static void mount_version(void) +{ + int pid = fork(); + if (!pid) { + const char *argv[] = { FUSERMOUNT_PROG, "--version", NULL }; + exec_fusermount(argv); + _exit(1); + } else if (pid != -1) + waitpid(pid, NULL, 0); +} + +struct mount_flags { + const char *opt; + unsigned long flag; + int on; +}; + +static struct mount_flags mount_flags[] = { + {"rw", MS_RDONLY, 0}, + {"ro", MS_RDONLY, 1}, + {"suid", MS_NOSUID, 0}, + {"nosuid", MS_NOSUID, 1}, + {"dev", MS_NODEV, 0}, + {"nodev", MS_NODEV, 1}, + {"exec", MS_NOEXEC, 0}, + {"noexec", MS_NOEXEC, 1}, + {"async", MS_SYNCHRONOUS, 0}, + {"sync", MS_SYNCHRONOUS, 1}, + {"atime", MS_NOATIME, 0}, + {"noatime", MS_NOATIME, 1}, + {"dirsync", MS_DIRSYNC, 1}, + {NULL, 0, 0} +}; + +static void set_mount_flag(const char *s, int *flags) +{ + int i; + + for (i = 0; mount_flags[i].opt != NULL; i++) { + const char *opt = mount_flags[i].opt; + if (strcmp(opt, s) == 0) { + if (mount_flags[i].on) + *flags |= mount_flags[i].flag; + else + *flags &= ~mount_flags[i].flag; + return; + } + } + fprintf(stderr, "fuse: internal error, can't find mount flag\n"); + abort(); +} + +static int fuse_mount_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + struct mount_opts *mo = data; + + switch (key) { + case KEY_ALLOW_ROOT: + if (fuse_opt_add_opt(&mo->kernel_opts, "allow_other") == -1 || + fuse_opt_add_arg(outargs, "-oallow_root") == -1) + return -1; + return 0; + + case KEY_RO: + arg = "ro"; + /* fall through */ + case KEY_KERN_FLAG: + set_mount_flag(arg, &mo->flags); + return 0; + + case KEY_KERN_OPT: + return fuse_opt_add_opt(&mo->kernel_opts, arg); + + case KEY_FUSERMOUNT_OPT: + return fuse_opt_add_opt_escaped(&mo->fusermount_opts, arg); + + case KEY_SUBTYPE_OPT: + return fuse_opt_add_opt(&mo->subtype_opt, arg); + + case KEY_MTAB_OPT: + return fuse_opt_add_opt(&mo->mtab_opts, arg); + + case KEY_HELP: + mount_help(); + mo->ishelp = 1; + break; + + case KEY_VERSION: + mount_version(); + mo->ishelp = 1; + break; + } + return 1; +} + +/* return value: + * >= 0 => fd + * -1 => error + */ +static int receive_fd(int fd) +{ + struct msghdr msg; + struct iovec iov; + char buf[1]; + int rv; + size_t ccmsg[CMSG_SPACE(sizeof(int)) / sizeof(size_t)]; + struct cmsghdr *cmsg; + + iov.iov_base = buf; + iov.iov_len = 1; + + msg.msg_name = 0; + msg.msg_namelen = 0; + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + /* old BSD implementations should use msg_accrights instead of + * msg_control; the interface is different. */ + msg.msg_control = ccmsg; + msg.msg_controllen = sizeof(ccmsg); + + while(((rv = recvmsg(fd, &msg, 0)) == -1) && errno == EINTR); + if (rv == -1) { + perror("recvmsg"); + return -1; + } + if(!rv) { + /* EOF */ + return -1; + } + + cmsg = CMSG_FIRSTHDR(&msg); + if (!cmsg->cmsg_type == SCM_RIGHTS) { + fprintf(stderr, "got control message of unknown type %d\n", + cmsg->cmsg_type); + return -1; + } + return *(int*)CMSG_DATA(cmsg); +} + +void fuse_kern_unmount(const char *mountpoint, int fd) +{ + int res; + int pid; + + if (!mountpoint) + return; + + if (fd != -1) { + struct pollfd pfd; + + pfd.fd = fd; + pfd.events = 0; + res = poll(&pfd, 1, 0); + /* If file poll returns POLLERR on the device file descriptor, + then the filesystem is already unmounted */ + if (res == 1 && (pfd.revents & POLLERR)) + return; + + /* Need to close file descriptor, otherwise synchronous umount + would recurse into filesystem, and deadlock */ + close(fd); + } + + if (geteuid() == 0) { + fuse_mnt_umount("fuse", mountpoint, mountpoint, 1); + return; + } + + res = umount2(mountpoint, 2); + if (res == 0) + return; + + pid = fork(); + if(pid == -1) + return; + + if(pid == 0) { + const char *argv[] = { FUSERMOUNT_PROG, "-u", "-q", "-z", + "--", mountpoint, NULL }; + + exec_fusermount(argv); + _exit(1); + } + waitpid(pid, NULL, 0); +} + +void fuse_unmount_compat22(const char *mountpoint) +{ + fuse_kern_unmount(mountpoint, -1); +} + +static int fuse_mount_fusermount(const char *mountpoint, const char *opts, + int quiet) +{ + int fds[2], pid; + int res; + int rv; + + if (!mountpoint) { + fprintf(stderr, "fuse: missing mountpoint parameter\n"); + return -1; + } + + res = socketpair(PF_UNIX, SOCK_STREAM, 0, fds); + if(res == -1) { + perror("fuse: socketpair() failed"); + return -1; + } + + pid = fork(); + if(pid == -1) { + perror("fuse: fork() failed"); + close(fds[0]); + close(fds[1]); + return -1; + } + + if(pid == 0) { + char env[10]; + const char *argv[32]; + int a = 0; + + if (quiet) { + int fd = open("/dev/null", O_RDONLY); + dup2(fd, 1); + dup2(fd, 2); + } + + argv[a++] = FUSERMOUNT_PROG; + if (opts) { + argv[a++] = "-o"; + argv[a++] = opts; + } + argv[a++] = "--"; + argv[a++] = mountpoint; + argv[a++] = NULL; + + close(fds[1]); + fcntl(fds[0], F_SETFD, 0); + snprintf(env, sizeof(env), "%i", fds[0]); + setenv(FUSE_COMMFD_ENV, env, 1); + exec_fusermount(argv); + perror("fuse: failed to exec fusermount"); + _exit(1); + } + + close(fds[0]); + rv = receive_fd(fds[1]); + close(fds[1]); + waitpid(pid, NULL, 0); /* bury zombie */ + + return rv; +} + +int fuse_mount_compat22(const char *mountpoint, const char *opts) +{ + return fuse_mount_fusermount(mountpoint, opts, 0); +} + +static int fuse_mount_sys(const char *mnt, struct mount_opts *mo, + const char *mnt_opts) +{ + char tmp[128]; + const char *devname = "/dev/fuse"; + char *source = NULL; + char *type = NULL; + struct stat stbuf; + int fd; + int res; + + if (!mnt) { + fprintf(stderr, "fuse: missing mountpoint parameter\n"); + return -1; + } + + res = stat(mnt, &stbuf); + if (res == -1) { + fprintf(stderr ,"fuse: failed to access mountpoint %s: %s\n", + mnt, strerror(errno)); + return -1; + } + + if (!mo->nonempty) { + res = fuse_mnt_check_empty("fuse", mnt, stbuf.st_mode, + stbuf.st_size); + if (res == -1) + return -1; + } + + fd = open(devname, O_RDWR); + if (fd == -1) { + if (errno == ENODEV || errno == ENOENT) + fprintf(stderr, "fuse: device not found, try 'modprobe fuse' first\n"); + else + fprintf(stderr, "fuse: failed to open %s: %s\n", + devname, strerror(errno)); + return -1; + } + + snprintf(tmp, sizeof(tmp), "fd=%i,rootmode=%o,user_id=%i,group_id=%i", + fd, stbuf.st_mode & S_IFMT, getuid(), getgid()); + + res = fuse_opt_add_opt(&mo->kernel_opts, tmp); + if (res == -1) + goto out_close; + + source = malloc((mo->fsname ? strlen(mo->fsname) : 0) + + (mo->subtype ? strlen(mo->subtype) : 0) + + strlen(devname) + 32); + + type = malloc((mo->subtype ? strlen(mo->subtype) : 0) + 32); + if (!type || !source) { + fprintf(stderr, "fuse: failed to allocate memory\n"); + goto out_close; + } + + strcpy(type, mo->blkdev ? "fuseblk" : "fuse"); + if (mo->subtype) { + strcat(type, "."); + strcat(type, mo->subtype); + } + strcpy(source, + mo->fsname ? mo->fsname : (mo->subtype ? mo->subtype : devname)); + + res = mount(source, mnt, type, mo->flags, mo->kernel_opts); + if (res == -1 && errno == ENODEV && mo->subtype) { + /* Probably missing subtype support */ + strcpy(type, mo->blkdev ? "fuseblk" : "fuse"); + if (mo->fsname) { + if (!mo->blkdev) + sprintf(source, "%s#%s", mo->subtype, + mo->fsname); + } else { + strcpy(source, type); + } + res = mount(source, mnt, type, mo->flags, mo->kernel_opts); + } + if (res == -1) { + /* + * Maybe kernel doesn't support unprivileged mounts, in this + * case try falling back to fusermount + */ + if (errno == EPERM) { + res = -2; + } else { + int errno_save = errno; + if (mo->blkdev && errno == ENODEV && + !fuse_mnt_check_fuseblk()) + fprintf(stderr, + "fuse: 'fuseblk' support missing\n"); + else + fprintf(stderr, "fuse: mount failed: %s\n", + strerror(errno_save)); + } + + goto out_close; + } + + if (geteuid() == 0) { + char *newmnt = fuse_mnt_resolve_path("fuse", mnt); + res = -1; + if (!newmnt) + goto out_umount; + + res = fuse_mnt_add_mount("fuse", source, newmnt, type, + mnt_opts); + free(newmnt); + if (res == -1) + goto out_umount; + } + free(type); + free(source); + + return fd; + +out_umount: + umount2(mnt, 2); /* lazy umount */ +out_close: + free(type); + free(source); + close(fd); + return res; +} + +static int get_mnt_flag_opts(char **mnt_optsp, int flags) +{ + int i; + + if (!(flags & MS_RDONLY) && fuse_opt_add_opt(mnt_optsp, "rw") == -1) + return -1; + + for (i = 0; mount_flags[i].opt != NULL; i++) { + if (mount_flags[i].on && (flags & mount_flags[i].flag) && + fuse_opt_add_opt(mnt_optsp, mount_flags[i].opt) == -1) + return -1; + } + return 0; +} + +int fuse_kern_mount(const char *mountpoint, struct fuse_args *args) +{ + struct mount_opts mo; + int res = -1; + char *mnt_opts = NULL; + + memset(&mo, 0, sizeof(mo)); + mo.flags = MS_NOSUID | MS_NODEV; + + if (args && + fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1) + return -1; + + if (mo.allow_other && mo.allow_root) { + fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n"); + goto out; + } + res = 0; + if (mo.ishelp) + goto out; + + res = -1; + if (get_mnt_flag_opts(&mnt_opts, mo.flags) == -1) + goto out; + if (mo.kernel_opts && fuse_opt_add_opt(&mnt_opts, mo.kernel_opts) == -1) + goto out; + if (mo.mtab_opts && fuse_opt_add_opt(&mnt_opts, mo.mtab_opts) == -1) + goto out; + + res = fuse_mount_sys(mountpoint, &mo, mnt_opts); + if (res == -2) { + if (mo.fusermount_opts && + fuse_opt_add_opt(&mnt_opts, mo.fusermount_opts) == -1) + goto out; + + if (mo.subtype) { + char *tmp_opts = NULL; + + res = -1; + if (fuse_opt_add_opt(&tmp_opts, mnt_opts) == -1 || + fuse_opt_add_opt(&tmp_opts, mo.subtype_opt) == -1) { + free(tmp_opts); + goto out; + } + + res = fuse_mount_fusermount(mountpoint, tmp_opts, 1); + free(tmp_opts); + if (res == -1) + res = fuse_mount_fusermount(mountpoint, + mnt_opts, 0); + } else { + res = fuse_mount_fusermount(mountpoint, mnt_opts, 0); + } + } +out: + free(mnt_opts); + free(mo.fsname); + free(mo.subtype); + free(mo.fusermount_opts); + free(mo.subtype_opt); + free(mo.kernel_opts); + free(mo.mtab_opts); + return res; +} + +FUSE_SYMVER(".symver fuse_mount_compat22,fuse_mount@FUSE_2.2"); +FUSE_SYMVER(".symver fuse_unmount_compat22,fuse_unmount@FUSE_2.2"); diff --git a/fuse/mount_bsd.c b/fuse/mount_bsd.c new file mode 100644 index 000000000..62443ac25 --- /dev/null +++ b/fuse/mount_bsd.c @@ -0,0 +1,388 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2005-2008 Csaba Henk + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_opt.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define FUSERMOUNT_PROG "mount_fusefs" +#define FUSE_DEV_TRUNK "/dev/fuse" + +enum { + KEY_ALLOW_ROOT, + KEY_RO, + KEY_HELP, + KEY_VERSION, + KEY_KERN +}; + +struct mount_opts { + int allow_other; + int allow_root; + int ishelp; + char *kernel_opts; +}; + +#define FUSE_DUAL_OPT_KEY(templ, key) \ + FUSE_OPT_KEY(templ, key), FUSE_OPT_KEY("no" templ, key) + +static const struct fuse_opt fuse_mount_opts[] = { + { "allow_other", offsetof(struct mount_opts, allow_other), 1 }, + { "allow_root", offsetof(struct mount_opts, allow_root), 1 }, + FUSE_OPT_KEY("allow_root", KEY_ALLOW_ROOT), + FUSE_OPT_KEY("-r", KEY_RO), + FUSE_OPT_KEY("-h", KEY_HELP), + FUSE_OPT_KEY("--help", KEY_HELP), + FUSE_OPT_KEY("-V", KEY_VERSION), + FUSE_OPT_KEY("--version", KEY_VERSION), + /* standard FreeBSD mount options */ + FUSE_DUAL_OPT_KEY("dev", KEY_KERN), + FUSE_DUAL_OPT_KEY("async", KEY_KERN), + FUSE_DUAL_OPT_KEY("atime", KEY_KERN), + FUSE_DUAL_OPT_KEY("dev", KEY_KERN), + FUSE_DUAL_OPT_KEY("exec", KEY_KERN), + FUSE_DUAL_OPT_KEY("suid", KEY_KERN), + FUSE_DUAL_OPT_KEY("symfollow", KEY_KERN), + FUSE_DUAL_OPT_KEY("rdonly", KEY_KERN), + FUSE_DUAL_OPT_KEY("sync", KEY_KERN), + FUSE_DUAL_OPT_KEY("union", KEY_KERN), + FUSE_DUAL_OPT_KEY("userquota", KEY_KERN), + FUSE_DUAL_OPT_KEY("groupquota", KEY_KERN), + FUSE_DUAL_OPT_KEY("clusterr", KEY_KERN), + FUSE_DUAL_OPT_KEY("clusterw", KEY_KERN), + FUSE_DUAL_OPT_KEY("suiddir", KEY_KERN), + FUSE_DUAL_OPT_KEY("snapshot", KEY_KERN), + FUSE_DUAL_OPT_KEY("multilabel", KEY_KERN), + FUSE_DUAL_OPT_KEY("acls", KEY_KERN), + FUSE_DUAL_OPT_KEY("force", KEY_KERN), + FUSE_DUAL_OPT_KEY("update", KEY_KERN), + FUSE_DUAL_OPT_KEY("ro", KEY_KERN), + FUSE_DUAL_OPT_KEY("rw", KEY_KERN), + FUSE_DUAL_OPT_KEY("auto", KEY_KERN), + /* options supported under both Linux and FBSD */ + FUSE_DUAL_OPT_KEY("allow_other", KEY_KERN), + FUSE_DUAL_OPT_KEY("default_permissions",KEY_KERN), + FUSE_OPT_KEY("max_read=", KEY_KERN), + FUSE_OPT_KEY("subtype=", KEY_KERN), + /* FBSD FUSE specific mount options */ + FUSE_DUAL_OPT_KEY("private", KEY_KERN), + FUSE_DUAL_OPT_KEY("neglect_shares", KEY_KERN), + FUSE_DUAL_OPT_KEY("push_symlinks_in", KEY_KERN), + FUSE_OPT_KEY("nosync_unmount", KEY_KERN), + /* stock FBSD mountopt parsing routine lets anything be negated... */ + /* + * Linux specific mount options, but let just the mount util + * handle them + */ + FUSE_OPT_KEY("fsname=", KEY_KERN), + FUSE_OPT_KEY("nonempty", KEY_KERN), + FUSE_OPT_KEY("large_read", KEY_KERN), + FUSE_OPT_END +}; + +static void mount_help(void) +{ + fprintf(stderr, + " -o allow_root allow access to root\n" + ); + system(FUSERMOUNT_PROG " --help"); + fputc('\n', stderr); +} + +static void mount_version(void) +{ + system(FUSERMOUNT_PROG " --version"); +} + +static int fuse_mount_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + struct mount_opts *mo = data; + + switch (key) { + case KEY_ALLOW_ROOT: + if (fuse_opt_add_opt(&mo->kernel_opts, "allow_other") == -1 || + fuse_opt_add_arg(outargs, "-oallow_root") == -1) + return -1; + return 0; + + case KEY_RO: + arg = "ro"; + /* fall through */ + + case KEY_KERN: + return fuse_opt_add_opt(&mo->kernel_opts, arg); + + case KEY_HELP: + mount_help(); + mo->ishelp = 1; + break; + + case KEY_VERSION: + mount_version(); + mo->ishelp = 1; + break; + } + return 1; +} + +void fuse_unmount_compat22(const char *mountpoint) +{ + char dev[128]; + char *ssc, *umount_cmd; + FILE *sf; + int rv; + char seekscript[] = + /* error message is annoying in help output */ + "exec 2>/dev/null; " + "/usr/bin/fstat " FUSE_DEV_TRUNK "* | " + "/usr/bin/awk 'BEGIN{ getline; if (! ($3 == \"PID\" && $10 == \"NAME\")) exit 1; }; " + " { if ($3 == %d) print $10; }' | " + "/usr/bin/sort | " + "/usr/bin/uniq | " + "/usr/bin/awk '{ i += 1; if (i > 1){ exit 1; }; printf; }; END{ if (i == 0) exit 1; }'"; + + (void) mountpoint; + + /* + * If we don't know the fd, we have to resort to the scripted + * solution -- iterating over the fd-s is unpractical, as we + * don't know how many of open files we have. (This could be + * looked up in procfs -- however, that's optional on FBSD; or + * read out from the kmem -- however, that's bound to + * privileges (in fact, that's what happens when we call the + * setgid kmem fstat(1) utility). + */ + if (asprintf(&ssc, seekscript, getpid()) == -1) + return; + + errno = 0; + sf = popen(ssc, "r"); + free(ssc); + if (! sf) + return; + + fgets(dev, sizeof(dev), sf); + rv = pclose(sf); + if (rv) + return; + + if (asprintf(&umount_cmd, "/sbin/umount %s", dev) == -1) + return; + system(umount_cmd); + free(umount_cmd); +} + +static void do_unmount(char *dev, int fd) +{ + char device_path[SPECNAMELEN + 12]; + const char *argv[4]; + const char umount_cmd[] = "/sbin/umount"; + pid_t pid; + + snprintf(device_path, SPECNAMELEN + 12, _PATH_DEV "%s", dev); + + argv[0] = umount_cmd; + argv[1] = "-f"; + argv[2] = device_path; + argv[3] = NULL; + + pid = fork(); + + if (pid == -1) + return; + + if (pid == 0) { + close(fd); + execvp(umount_cmd, (char **)argv); + exit(1); + } + + waitpid(pid, NULL, 0); +} + +void fuse_kern_unmount(const char *mountpoint, int fd) +{ + char *ep, dev[128]; + struct stat sbuf; + + (void)mountpoint; + + if (fstat(fd, &sbuf) == -1) + return; + + devname_r(sbuf.st_rdev, S_IFCHR, dev, 128); + + if (strncmp(dev, "fuse", 4)) + return; + + strtol(dev + 4, &ep, 10); + if (*ep != '\0') + return; + + do_unmount(dev, fd); +} + +/* Check if kernel is doing init in background */ +static int init_backgrounded(void) +{ + unsigned ibg, len; + + len = sizeof(ibg); + + if (sysctlbyname("vfs.fuse.init_backgrounded", &ibg, &len, NULL, 0)) + return 0; + + return ibg; +} + + +static int fuse_mount_core(const char *mountpoint, const char *opts) +{ + const char *mountprog = FUSERMOUNT_PROG; + int fd; + char *fdnam, *dev; + pid_t pid, cpid; + int status; + + fdnam = getenv("FUSE_DEV_FD"); + + if (fdnam) { + char *ep; + + fd = strtol(fdnam, &ep, 10); + + if (*ep != '\0') { + fprintf(stderr, "invalid value given in FUSE_DEV_FD\n"); + return -1; + } + + if (fd < 0) + return -1; + + goto mount; + } + + dev = getenv("FUSE_DEV_NAME"); + + if (! dev) + dev = (char *)FUSE_DEV_TRUNK; + + if ((fd = open(dev, O_RDWR)) < 0) { + perror("fuse: failed to open fuse device"); + return -1; + } + +mount: + if (getenv("FUSE_NO_MOUNT") || ! mountpoint) + goto out; + + pid = fork(); + cpid = pid; + + if (pid == -1) { + perror("fuse: fork() failed"); + close(fd); + return -1; + } + + if (pid == 0) { + if (! init_backgrounded()) { + /* + * If init is not backgrounded, we have to + * call the mount util backgrounded, to avoid + * deadlock. + */ + + pid = fork(); + + if (pid == -1) { + perror("fuse: fork() failed"); + close(fd); + exit(1); + } + } + + if (pid == 0) { + const char *argv[32]; + int a = 0; + + if (! fdnam && asprintf(&fdnam, "%d", fd) == -1) { + perror("fuse: failed to assemble mount arguments"); + exit(1); + } + + argv[a++] = mountprog; + if (opts) { + argv[a++] = "-o"; + argv[a++] = opts; + } + argv[a++] = fdnam; + argv[a++] = mountpoint; + argv[a++] = NULL; + execvp(mountprog, (char **) argv); + perror("fuse: failed to exec mount program"); + exit(1); + } + + exit(0); + } + + if (waitpid(cpid, &status, 0) == -1 || WEXITSTATUS(status) != 0) { + perror("fuse: failed to mount file system"); + close(fd); + return -1; + } + +out: + return fd; +} + +int fuse_kern_mount(const char *mountpoint, struct fuse_args *args) +{ + struct mount_opts mo; + int res = -1; + + memset(&mo, 0, sizeof(mo)); + /* mount util should not try to spawn the daemon */ + setenv("MOUNT_FUSEFS_SAFE", "1", 1); + /* to notify the mount util it's called from lib */ + setenv("MOUNT_FUSEFS_CALL_BY_LIB", "1", 1); + + if (args && + fuse_opt_parse(args, &mo, fuse_mount_opts, fuse_mount_opt_proc) == -1) + return -1; + + if (mo.allow_other && mo.allow_root) { + fprintf(stderr, "fuse: 'allow_other' and 'allow_root' options are mutually exclusive\n"); + goto out; + } + if (mo.ishelp) + return 0; + + res = fuse_mount_core(mountpoint, mo.kernel_opts); +out: + free(mo.kernel_opts); + return res; +} + +FUSE_SYMVER(".symver fuse_unmount_compat22,fuse_unmount@FUSE_2.2"); diff --git a/fuse/mount_util.c b/fuse/mount_util.c new file mode 100644 index 000000000..de1bd6714 --- /dev/null +++ b/fuse/mount_util.c @@ -0,0 +1,363 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "mount_util.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +static int mtab_needs_update(const char *mnt) +{ + int res; + struct stat stbuf; + + /* If mtab is within new mount, don't touch it */ + if (strncmp(mnt, _PATH_MOUNTED, strlen(mnt)) == 0 && + _PATH_MOUNTED[strlen(mnt)] == '/') + return 0; + + /* + * Skip mtab update if /etc/mtab: + * + * - doesn't exist, + * - is a symlink, + * - is on a read-only filesystem. + */ + res = lstat(_PATH_MOUNTED, &stbuf); + if (res == -1) { + if (errno == ENOENT) + return 0; + } else { + if (S_ISLNK(stbuf.st_mode)) + return 0; + + res = access(_PATH_MOUNTED, W_OK); + if (res == -1 && errno == EROFS) + return 0; + } + + return 1; +} + +static int add_mount_legacy(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts) +{ + int res; + int status; + sigset_t blockmask; + sigset_t oldmask; + + sigemptyset(&blockmask); + sigaddset(&blockmask, SIGCHLD); + res = sigprocmask(SIG_BLOCK, &blockmask, &oldmask); + if (res == -1) { + fprintf(stderr, "%s: sigprocmask: %s\n", progname, strerror(errno)); + return -1; + } + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + goto out_restore; + } + if (res == 0) { + char templ[] = "/tmp/fusermountXXXXXX"; + char *tmp; + + sigprocmask(SIG_SETMASK, &oldmask, NULL); + setuid(geteuid()); + + /* + * hide in a directory, where mount isn't able to resolve + * fsname as a valid path + */ + tmp = mkdtemp(templ); + if (!tmp) { + fprintf(stderr, + "%s: failed to create temporary directory\n", + progname); + exit(1); + } + if (chdir(tmp)) { + fprintf(stderr, "%s: failed to chdir to %s: %s\n", + progname, tmp, strerror(errno)); + exit(1); + } + rmdir(tmp); + execl("/bin/mount", "/bin/mount", "-i", "-f", "-t", type, + "-o", opts, fsname, mnt, NULL); + fprintf(stderr, "%s: failed to execute /bin/mount: %s\n", + progname, strerror(errno)); + exit(1); + } + res = waitpid(res, &status, 0); + if (res == -1) + fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno)); + + if (status != 0) + res = -1; + + out_restore: + sigprocmask(SIG_SETMASK, &oldmask, NULL); + + return res; +} + +static int add_mount(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts) +{ + int res; + int status; + sigset_t blockmask; + sigset_t oldmask; + + sigemptyset(&blockmask); + sigaddset(&blockmask, SIGCHLD); + res = sigprocmask(SIG_BLOCK, &blockmask, &oldmask); + if (res == -1) { + fprintf(stderr, "%s: sigprocmask: %s\n", progname, strerror(errno)); + return -1; + } + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + goto out_restore; + } + if (res == 0) { + /* + * Hide output, because old versions don't support + * --no-canonicalize + */ + int fd = open("/dev/null", O_RDONLY); + dup2(fd, 1); + dup2(fd, 2); + + sigprocmask(SIG_SETMASK, &oldmask, NULL); + setuid(geteuid()); + execl("/bin/mount", "/bin/mount", "--no-canonicalize", "-i", + "-f", "-t", type, "-o", opts, fsname, mnt, NULL); + fprintf(stderr, "%s: failed to execute /bin/mount: %s\n", + progname, strerror(errno)); + exit(1); + } + res = waitpid(res, &status, 0); + if (res == -1) + fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno)); + + if (status != 0) + res = -1; + + out_restore: + sigprocmask(SIG_SETMASK, &oldmask, NULL); + + return res; +} + +int fuse_mnt_add_mount(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts) +{ + int res; + + if (!mtab_needs_update(mnt)) + return 0; + + res = add_mount(progname, fsname, mnt, type, opts); + if (res == -1) + res = add_mount_legacy(progname, fsname, mnt, type, opts); + + return res; +} + +static int exec_umount(const char *progname, const char *rel_mnt, int lazy) +{ + int res; + int status; + sigset_t blockmask; + sigset_t oldmask; + + sigemptyset(&blockmask); + sigaddset(&blockmask, SIGCHLD); + res = sigprocmask(SIG_BLOCK, &blockmask, &oldmask); + if (res == -1) { + fprintf(stderr, "%s: sigprocmask: %s\n", progname, strerror(errno)); + return -1; + } + + res = fork(); + if (res == -1) { + fprintf(stderr, "%s: fork: %s\n", progname, strerror(errno)); + goto out_restore; + } + if (res == 0) { + sigprocmask(SIG_SETMASK, &oldmask, NULL); + setuid(geteuid()); + execl("/bin/umount", "/bin/umount", "-i", rel_mnt, + lazy ? "-l" : NULL, NULL); + fprintf(stderr, "%s: failed to execute /bin/umount: %s\n", + progname, strerror(errno)); + exit(1); + } + res = waitpid(res, &status, 0); + if (res == -1) + fprintf(stderr, "%s: waitpid: %s\n", progname, strerror(errno)); + + if (status != 0) { + res = -1; + } + + out_restore: + sigprocmask(SIG_SETMASK, &oldmask, NULL); + return res; + +} + +int fuse_mnt_umount(const char *progname, const char *abs_mnt, + const char *rel_mnt, int lazy) +{ + int res; + + if (!mtab_needs_update(abs_mnt)) { + res = umount2(rel_mnt, lazy ? 2 : 0); + if (res == -1) + fprintf(stderr, "%s: failed to unmount %s: %s\n", + progname, abs_mnt, strerror(errno)); + return res; + } + + return exec_umount(progname, rel_mnt, lazy); +} + +char *fuse_mnt_resolve_path(const char *progname, const char *orig) +{ + char buf[PATH_MAX]; + char *copy; + char *dst; + char *end; + char *lastcomp; + const char *toresolv; + + if (!orig[0]) { + fprintf(stderr, "%s: invalid mountpoint '%s'\n", progname, + orig); + return NULL; + } + + copy = strdup(orig); + if (copy == NULL) { + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return NULL; + } + + toresolv = copy; + lastcomp = NULL; + for (end = copy + strlen(copy) - 1; end > copy && *end == '/'; end --); + if (end[0] != '/') { + char *tmp; + end[1] = '\0'; + tmp = strrchr(copy, '/'); + if (tmp == NULL) { + lastcomp = copy; + toresolv = "."; + } else { + lastcomp = tmp + 1; + if (tmp == copy) + toresolv = "/"; + } + if (strcmp(lastcomp, ".") == 0 || strcmp(lastcomp, "..") == 0) { + lastcomp = NULL; + toresolv = copy; + } + else if (tmp) + tmp[0] = '\0'; + } + if (realpath(toresolv, buf) == NULL) { + fprintf(stderr, "%s: bad mount point %s: %s\n", progname, orig, + strerror(errno)); + free(copy); + return NULL; + } + if (lastcomp == NULL) + dst = strdup(buf); + else { + dst = (char *) malloc(strlen(buf) + 1 + strlen(lastcomp) + 1); + if (dst) { + unsigned buflen = strlen(buf); + if (buflen && buf[buflen-1] == '/') + sprintf(dst, "%s%s", buf, lastcomp); + else + sprintf(dst, "%s/%s", buf, lastcomp); + } + } + free(copy); + if (dst == NULL) + fprintf(stderr, "%s: failed to allocate memory\n", progname); + return dst; +} + +int fuse_mnt_check_empty(const char *progname, const char *mnt, + mode_t rootmode, off64_t rootsize) +{ + int isempty = 1; + + if (S_ISDIR(rootmode)) { + struct dirent *ent; + DIR *dp = opendir(mnt); + if (dp == NULL) { + fprintf(stderr, + "%s: failed to open mountpoint for reading: %s\n", + progname, strerror(errno)); + return -1; + } + while ((ent = readdir(dp)) != NULL) { + if (strcmp(ent->d_name, ".") != 0 && + strcmp(ent->d_name, "..") != 0) { + isempty = 0; + break; + } + } + closedir(dp); + } else if (rootsize) + isempty = 0; + + if (!isempty) { + fprintf(stderr, "%s: mountpoint is not empty\n", progname); + fprintf(stderr, "%s: if you are sure this is safe, use the 'nonempty' mount option\n", progname); + return -1; + } + return 0; +} + +int fuse_mnt_check_fuseblk(void) +{ + char buf[256]; + FILE *f = fopen("/proc/filesystems", "r"); + if (!f) + return 1; + + while (fgets(buf, sizeof(buf), f)) + if (strstr(buf, "fuseblk\n")) { + fclose(f); + return 1; + } + + fclose(f); + return 0; +} diff --git a/fuse/mount_util.h b/fuse/mount_util.h new file mode 100644 index 000000000..5c2fb8e6e --- /dev/null +++ b/fuse/mount_util.h @@ -0,0 +1,18 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include + +int fuse_mnt_add_mount(const char *progname, const char *fsname, + const char *mnt, const char *type, const char *opts); +int fuse_mnt_umount(const char *progname, const char *abs_mnt, + const char *rel_mnt, int lazy); +char *fuse_mnt_resolve_path(const char *progname, const char *orig); +int fuse_mnt_check_empty(const char *progname, const char *mnt, + mode_t rootmode, off64_t rootsize); +int fuse_mnt_check_fuseblk(void); diff --git a/fuse/ulockmgr.c b/fuse/ulockmgr.c new file mode 100644 index 000000000..4a049d5b2 --- /dev/null +++ b/fuse/ulockmgr.c @@ -0,0 +1,440 @@ +/* + libulockmgr: Userspace Lock Manager Library + Copyright (C) 2006 Miklos Szeredi + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +/* #define DEBUG 1 */ + +#include "ulockmgr.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct message { + unsigned intr : 1; + unsigned nofd : 1; + pthread_t thr; + int cmd; + int fd; + struct flock lock; + int error; +}; + +struct fd_store { + struct fd_store *next; + int fd; + int inuse; +}; + +struct owner { + struct owner *next; + struct owner *prev; + struct fd_store *fds; + void *id; + size_t id_len; + int cfd; +}; + +static pthread_mutex_t ulockmgr_lock; +static int ulockmgr_cfd = -1; +static struct owner owner_list = { .next = &owner_list, .prev = &owner_list }; + +#define MAX_SEND_FDS 2 + +static void list_del_owner(struct owner *owner) +{ + struct owner *prev = owner->prev; + struct owner *next = owner->next; + prev->next = next; + next->prev = prev; +} + +static void list_add_owner(struct owner *owner, struct owner *next) +{ + struct owner *prev = next->prev; + owner->next = next; + owner->prev = prev; + prev->next = owner; + next->prev = owner; +} + +/* + * There's a bug in the linux kernel (< 2.6.22) recv() implementation + * on AF_UNIX, SOCK_STREAM sockets, that could cause it to return + * zero, even if data was available. Retrying the recv will return + * the data in this case. + */ +static int do_recv(int sock, void *buf, size_t len, int flags) +{ + int res = recv(sock, buf, len, flags); + if (res == 0) + res = recv(sock, buf, len, flags); + + return res; +} + +static int ulockmgr_send_message(int sock, void *buf, size_t buflen, + int *fdp, int numfds) +{ + struct msghdr msg; + struct cmsghdr *p_cmsg; + struct iovec vec; + size_t cmsgbuf[CMSG_SPACE(sizeof(int) * MAX_SEND_FDS) / sizeof(size_t)]; + int res; + + assert(numfds <= MAX_SEND_FDS); + msg.msg_control = cmsgbuf; + msg.msg_controllen = sizeof(cmsgbuf); + p_cmsg = CMSG_FIRSTHDR(&msg); + p_cmsg->cmsg_level = SOL_SOCKET; + p_cmsg->cmsg_type = SCM_RIGHTS; + p_cmsg->cmsg_len = CMSG_LEN(sizeof(int) * numfds); + memcpy(CMSG_DATA(p_cmsg), fdp, sizeof(int) * numfds); + msg.msg_controllen = p_cmsg->cmsg_len; + msg.msg_name = NULL; + msg.msg_namelen = 0; + msg.msg_iov = &vec; + msg.msg_iovlen = 1; + msg.msg_flags = 0; + vec.iov_base = buf; + vec.iov_len = buflen; + res = sendmsg(sock, &msg, MSG_NOSIGNAL); + if (res == -1) { + perror("libulockmgr: sendmsg"); + return -1; + } + if ((size_t) res != buflen) { + fprintf(stderr, "libulockmgr: sendmsg short\n"); + return -1; + } + return 0; +} + +static int ulockmgr_start_daemon(void) +{ + int sv[2]; + int res; + char tmp[64]; + + res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); + if (res == -1) { + perror("libulockmgr: socketpair"); + return -1; + } + snprintf(tmp, sizeof(tmp), "exec ulockmgr_server %i", sv[0]); + res = system(tmp); + close(sv[0]); + if (res == -1 || !WIFEXITED(res) || WEXITSTATUS(res) != 0) { + close(sv[1]); + return -1; + } + ulockmgr_cfd = sv[1]; + return 0; +} + +static struct owner *ulockmgr_new_owner(const void *id, size_t id_len) +{ + int sv[2]; + int res; + char c = 'm'; + struct owner *o; + + if (ulockmgr_cfd == -1 && ulockmgr_start_daemon() == -1) + return NULL; + + o = calloc(1, sizeof(struct owner) + id_len); + if (!o) { + fprintf(stderr, "libulockmgr: failed to allocate memory\n"); + return NULL; + } + o->id = o + 1; + o->id_len = id_len; + res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); + if (res == -1) { + perror("libulockmgr: socketpair"); + goto out_free; + } + res = ulockmgr_send_message(ulockmgr_cfd, &c, sizeof(c), &sv[0], 1); + close(sv[0]); + if (res == -1) { + close(ulockmgr_cfd); + ulockmgr_cfd = -1; + goto out_close; + } + + o->cfd = sv[1]; + memcpy(o->id, id, id_len); + list_add_owner(o, &owner_list); + + return o; + +out_close: + close(sv[1]); +out_free: + free(o); + return NULL; +} + +static int ulockmgr_send_request(struct message *msg, const void *id, + size_t id_len) +{ + int sv[2]; + int cfd; + struct owner *o; + struct fd_store *f = NULL; + struct fd_store *newf = NULL; + struct fd_store **fp; + int fd = msg->fd; + int cmd = msg->cmd; + int res; + int unlockall = (cmd == F_SETLK && msg->lock.l_type == F_UNLCK && + msg->lock.l_start == 0 && msg->lock.l_len == 0); + + for (o = owner_list.next; o != &owner_list; o = o->next) + if (o->id_len == id_len && memcmp(o->id, id, id_len) == 0) + break; + + if (o == &owner_list) + o = NULL; + + if (!o && cmd != F_GETLK && msg->lock.l_type != F_UNLCK) + o = ulockmgr_new_owner(id, id_len); + + if (!o) { + if (cmd == F_GETLK) { + res = fcntl(msg->fd, F_GETLK, &msg->lock); + return (res == -1) ? -errno : 0; + } else if (msg->lock.l_type == F_UNLCK) + return 0; + else + return -ENOLCK; + } + + if (unlockall) + msg->nofd = 1; + else { + for (fp = &o->fds; *fp; fp = &(*fp)->next) { + f = *fp; + if (f->fd == fd) { + msg->nofd = 1; + break; + } + } + } + + if (!msg->nofd) { + newf = f = calloc(1, sizeof(struct fd_store)); + if (!f) { + fprintf(stderr, "libulockmgr: failed to allocate memory\n"); + return -ENOLCK; + } + } + + res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); + if (res == -1) { + perror("libulockmgr: socketpair"); + free(newf); + return -ENOLCK; + } + + cfd = sv[1]; + sv[1] = msg->fd; + res = ulockmgr_send_message(o->cfd, msg, sizeof(struct message), sv, + msg->nofd ? 1 : 2); + close(sv[0]); + if (res == -1) { + free(newf); + close(cfd); + return -EIO; + } + + if (newf) { + newf->fd = msg->fd; + newf->next = o->fds; + o->fds = newf; + } + if (f) + f->inuse++; + + res = do_recv(cfd, msg, sizeof(struct message), MSG_WAITALL); + if (res == -1) { + perror("libulockmgr: recv"); + msg->error = EIO; + } else if (res != sizeof(struct message)) { + fprintf(stderr, "libulockmgr: recv short\n"); + msg->error = EIO; + } else if (cmd == F_SETLKW && msg->error == EAGAIN) { + pthread_mutex_unlock(&ulockmgr_lock); + while (1) { + sigset_t old; + sigset_t unblock; + int errno_save; + + sigemptyset(&unblock); + sigaddset(&unblock, SIGUSR1); + pthread_sigmask(SIG_UNBLOCK, &unblock, &old); + res = do_recv(cfd, msg, sizeof(struct message), + MSG_WAITALL); + errno_save = errno; + pthread_sigmask(SIG_SETMASK, &old, NULL); + if (res == sizeof(struct message)) + break; + else if (res >= 0) { + fprintf(stderr, "libulockmgr: recv short\n"); + msg->error = EIO; + break; + } else if (errno_save != EINTR) { + errno = errno_save; + perror("libulockmgr: recv"); + msg->error = EIO; + break; + } + msg->intr = 1; + res = send(o->cfd, msg, sizeof(struct message), + MSG_NOSIGNAL); + if (res == -1) { + perror("libulockmgr: send"); + msg->error = EIO; + break; + } + if (res != sizeof(struct message)) { + fprintf(stderr, "libulockmgr: send short\n"); + msg->error = EIO; + break; + } + } + pthread_mutex_lock(&ulockmgr_lock); + + } + if (f) + f->inuse--; + close(cfd); + if (unlockall) { + for (fp = &o->fds; *fp;) { + f = *fp; + if (f->fd == fd && !f->inuse) { + *fp = f->next; + free(f); + } else + fp = &f->next; + } + if (!o->fds) { + list_del_owner(o); + close(o->cfd); + free(o); + } + /* Force OK on unlock-all, since it _will_ succeed once the + owner is deleted */ + msg->error = 0; + } + + return -msg->error; +} + +#ifdef DEBUG +static uint32_t owner_hash(const unsigned char *id, size_t id_len) +{ + uint32_t h = 0; + size_t i; + for (i = 0; i < id_len; i++) + h = ((h << 8) | (h >> 24)) ^ id[i]; + + return h; +} +#endif + +static int ulockmgr_canonicalize(int fd, struct flock *lock) +{ + off64_t offset; + if (lock->l_whence == SEEK_CUR) { + offset = lseek(fd, 0, SEEK_CUR); + if (offset == (off64_t) -1) + return -errno; + } else if (lock->l_whence == SEEK_END) { + struct stat stbuf; + int res = fstat(fd, &stbuf); + if (res == -1) + return -errno; + + offset = stbuf.st_size; + } else + offset = 0; + + lock->l_whence = SEEK_SET; + lock->l_start += offset; + + if (lock->l_start < 0) + return -EINVAL; + + if (lock->l_len < 0) { + lock->l_start += lock->l_len; + if (lock->l_start < 0) + return -EINVAL; + lock->l_len = -lock->l_len; + } + if (lock->l_len && lock->l_start + lock->l_len - 1 < 0) + return -EINVAL; + + return 0; +} + +int ulockmgr_op(int fd, int cmd, struct flock *lock, const void *owner, + size_t owner_len) +{ + int err; + struct message msg; + sigset_t old; + sigset_t block; + + if (cmd != F_GETLK && cmd != F_SETLK && cmd != F_SETLKW) + return -EINVAL; + + if (lock->l_whence != SEEK_SET && lock->l_whence != SEEK_CUR && + lock->l_whence != SEEK_END) + return -EINVAL; + +#ifdef DEBUG + fprintf(stderr, "libulockmgr: %i %i %i %lli %lli own: 0x%08x\n", + cmd, lock->l_type, lock->l_whence, lock->l_start, lock->l_len, + owner_hash(owner, owner_len)); +#endif + + /* Unlock should never block anyway */ + if (cmd == F_SETLKW && lock->l_type == F_UNLCK) + cmd = F_SETLK; + + memset(&msg, 0, sizeof(struct message)); + msg.cmd = cmd; + msg.fd = fd; + msg.lock = *lock; + err = ulockmgr_canonicalize(fd, &msg.lock); + if (err) + return err; + + sigemptyset(&block); + sigaddset(&block, SIGUSR1); + pthread_sigmask(SIG_BLOCK, &block, &old); + pthread_mutex_lock(&ulockmgr_lock); + err = ulockmgr_send_request(&msg, owner, owner_len); + pthread_mutex_unlock(&ulockmgr_lock); + pthread_sigmask(SIG_SETMASK, &old, NULL); + if (!err && cmd == F_GETLK) { + if (msg.lock.l_type == F_UNLCK) + lock->l_type = F_UNLCK; + else + *lock = msg.lock; + } + + return err; +} diff --git a/gui/action.cpp b/gui/action.cpp index dd38b3175..85ad8eceb 100644 --- a/gui/action.cpp +++ b/gui/action.cpp @@ -208,12 +208,13 @@ int GUIAction::flash_zip(std::string filename, std::string pageName, const int s // Now, check if we need to ensure TWRP remains installed... struct stat st; + string result; if (stat("/sbin/installTwrp", &st) == 0) { DataManager::SetValue("tw_operation", "Configuring TWRP"); DataManager::SetValue("tw_partition", ""); ui_print("Configuring TWRP...\n"); - if (system("/sbin/installTwrp reinstall") < 0) + if (TWFunc::Exec_Cmd("/sbin/installTwrp reinstall", result) < 0) { ui_print("Unable to configure TWRP with this kernel.\n"); } @@ -274,7 +275,6 @@ void GUIAction::operation_start(const string operation_name) void GUIAction::operation_end(const int operation_status, const int simulate) { int simulate_fail; - DataManager::SetValue("ui_progress", 100); if (simulate) { DataManager::GetValue(TW_SIMULATE_FAIL, simulate_fail); @@ -283,10 +283,12 @@ void GUIAction::operation_end(const int operation_status, const int simulate) else DataManager::SetValue("tw_operation_status", 0); } else { - if (operation_status != 0) + if (operation_status != 0) { DataManager::SetValue("tw_operation_status", 1); - else + } + else { DataManager::SetValue("tw_operation_status", 0); + } } DataManager::SetValue("tw_operation_state", 1); DataManager::SetValue(TW_ACTION_BUSY, 0); @@ -462,11 +464,10 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) operation_start("Copy Log"); if (!simulate) { - char command[255]; - + string dst; PartitionManager.Mount_Current_Storage(true); - sprintf(command, "cp /tmp/recovery.log %s", DataManager::GetCurrentStoragePath().c_str()); - system(command); + dst = DataManager::GetCurrentStoragePath() + "/recovery.log"; + TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755); sync(); ui_print("Copied recovery log to %s.\n", DataManager::GetCurrentStoragePath().c_str()); } else @@ -663,7 +664,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) if (wipe_cache) PartitionManager.Wipe_By_Path("/cache"); - + string result; if (DataManager::GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager::GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { operation_start("ReinjectTWRP"); ui_print("Injecting TWRP into boot image...\n"); @@ -672,10 +673,10 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) } else { TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); if (Boot == NULL || Boot->Current_File_System != "emmc") - system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); + TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); else { string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; - system(injectcmd.c_str()); + TWFunc::Exec_Cmd(injectcmd, result); } ui_print("TWRP injection complete.\n"); } @@ -766,8 +767,9 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) if (arg == "backup") { string Backup_Name; DataManager::GetValue(TW_BACKUP_NAME, Backup_Name); - if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) + if (Backup_Name == "(Current Date)" || Backup_Name == "0" || Backup_Name == "(" || PartitionManager.Check_Backup_Name(true) == 0) { ret = PartitionManager.Run_Backup(); + } else { operation_end(1, simulate); return -1; @@ -786,8 +788,8 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) ret = 1; // 1 for failure else ret = 0; // 0 for success - operation_end(ret, simulate); - return 0; + operation_end(ret, simulate); + return 0; } if (function == "fixpermissions") { @@ -810,9 +812,9 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) if (simulate) { simulate_progress_bar(); } else { - char cmd[512]; - sprintf(cmd, "dd %s", arg.c_str()); - system(cmd); + string result; + string cmd = "dd " + arg; + TWFunc::Exec_Cmd(cmd, result); } operation_end(0, simulate); return 0; @@ -873,13 +875,14 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) if (function == "cmd") { int op_status = 0; + string result; operation_start("Command"); LOGI("Running command: '%s'\n", arg.c_str()); if (simulate) { simulate_progress_bar(); } else { - op_status = system(arg.c_str()); + op_status = TWFunc::Exec_Cmd(arg, result); if (op_status != 0) op_status = 1; } @@ -930,13 +933,13 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) if (function == "reinjecttwrp") { int op_status = 0; - + string result; operation_start("ReinjectTWRP"); ui_print("Injecting TWRP into boot image...\n"); if (simulate) { simulate_progress_bar(); } else { - system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); + TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); ui_print("TWRP injection complete.\n"); } @@ -1031,7 +1034,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) simulate_progress_bar(); } else { int wipe_cache = 0; - string Command, Sideload_File; + string result, Sideload_File; if (!PartitionManager.Mount_Current_Storage(true)) { operation_end(1, simulate); @@ -1039,8 +1042,7 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) } Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; if (TWFunc::Path_Exists(Sideload_File)) { - Command = "rm " + Sideload_File; - system(Command.c_str()); + unlink(Sideload_File.c_str()); } ui_print("Starting ADB sideload feature...\n"); ret = apply_from_adb(ui, &wipe_cache, Sideload_File.c_str()); @@ -1056,10 +1058,10 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) } else { TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); if (Boot == NULL || Boot->Current_File_System != "emmc") - system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); + TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", result); else { string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; - system(injectcmd.c_str()); + TWFunc::Exec_Cmd(injectcmd, result); } ui_print("TWRP injection complete.\n"); } @@ -1071,10 +1073,9 @@ int GUIAction::doAction(Action action, int isThreaded /* = 0 */) if (function == "adbsideloadcancel") { int child_pid; - string Command, Sideload_File; + string Sideload_File; Sideload_File = DataManager::GetCurrentStoragePath() + "/sideload.zip"; - Command = "rm " + Sideload_File; - system(Command.c_str()); + unlink(Sideload_File.c_str()); DataManager::GetValue("tw_child_pid", child_pid); ui_print("Cancelling ADB sideload...\n"); kill(child_pid, SIGTERM); diff --git a/gui/devices/720x1280/res/ui.xml b/gui/devices/720x1280/res/ui.xml index 4e6ba9f82..eed6c58fa 100644 --- a/gui/devices/720x1280/res/ui.xml +++ b/gui/devices/720x1280/res/ui.xml @@ -1556,7 +1556,9 @@ - + + + tw_back=backup tw_complete_text1=Backup Complete diff --git a/libtar/Android.mk b/libtar/Android.mk new file mode 100644 index 000000000..b73a41f3f --- /dev/null +++ b/libtar/Android.mk @@ -0,0 +1,15 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) + +LOCAL_MODULE := libtar +LOCAL_MODULE_TAGS := eng +LOCAL_MODULES_TAGS = optional +LOCAL_CFLAGS = +LOCAL_SRC_FILES = append.c block.c decode.c encode.c extract.c handle.c output.c util.c wrapper.c basename.c strmode.c libtar_hash.c libtar_list.c dirname.c +LOCAL_C_INCLUDES += $(LOCAL_PATH) \ + external/zlib +LOCAL_SHARED_LIBRARIES += libz libc + +include $(BUILD_SHARED_LIBRARY) + diff --git a/libtar/COPYRIGHT b/libtar/COPYRIGHT new file mode 100644 index 000000000..2471ec01b --- /dev/null +++ b/libtar/COPYRIGHT @@ -0,0 +1,35 @@ +Copyright (c) 1998-2003 University of Illinois Board of Trustees +Copyright (c) 1998-2003 Mark D. Roth +All rights reserved. + +Developed by: Campus Information Technologies and Educational Services, + University of Illinois at Urbana-Champaign + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +``Software''), to deal with the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +* Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimers. + +* Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimers in the + documentation and/or other materials provided with the distribution. + +* Neither the names of Campus Information Technologies and Educational + Services, University of Illinois at Urbana-Champaign, nor the names + of its contributors may be used to endorse or promote products derived + from this Software without specific prior written permission. + +THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR +ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE. + diff --git a/libtar/ChangeLog b/libtar/ChangeLog new file mode 100644 index 000000000..cde7675e2 --- /dev/null +++ b/libtar/ChangeLog @@ -0,0 +1,15 @@ +2002-12-09 added list_empty() and hash_empty() functions + +2002-09-12 fixed list_iterate function to return -1 if it gets + an invalid argument + + include and from source files, not + from header file, since header file is sometimes + installed as part of a user-visible API + (those APIs should eventually be redesigned without the + listhash code being publicly visible, but for now we + need to accomodate this) + +2002-07-07 modified list iterate function to return int + (returns -1 if plugin function returns -1) + diff --git a/libtar/ChangeLog-1.0.x b/libtar/ChangeLog-1.0.x new file mode 100644 index 000000000..23b06b3ac --- /dev/null +++ b/libtar/ChangeLog-1.0.x @@ -0,0 +1,141 @@ +libtar 1.0.2 - 6/21/00 +------------ + +- tar_set_file_perms() now calls chown() only if the effective user ID is 0 + (workaround for IRIX and HP-UX, which allow file giveaways) + +- tar_set_file_perms() now calls chmod() or lchmod() after chown() + (this fixes a problem with extracting setuid files under Linux) + +- removed calls to fchown() and fchmod() from tar_extract_regfile() + +- fixed bugs in th_read() which didn't set errno properly + +- removed various unused variables + +---------------------------------------------------------------------- + +libtar 1.0.1 - 4/1/00 +------------ + +- removed libgen.h include from dirname and basename compat code + +- added lib/fnmatch.c compatability module from OpenBSD + +- fixed several objdirs bugs in libtar/Makefile.in + +- misc Makefile changes (added $CPPFLAGS support, added -o flag to compile + commands, use $CFLAGS on link line, etc) + +- removed "inline" keyword from all source files to prevent portability + problems + +- updated README + +---------------------------------------------------------------------- + +libtar 1.0 - 1/2/00 +---------- + +- various portability fixes + +- "make install" now runs mkencap and epkg if they're available + +- libmisc is now integrated into libtar + +---------------------------------------------------------------------- + +libtar 0.5.6 beta - 12/16/99 +----------------- + +- changed API to allow better error reporting via errno + +- added manpages to document libtar API + +- replaced symbolic_mode() call with strmode() compatibility code + +---------------------------------------------------------------------- + +libtar 0.5.5 beta - 11/16/99 +----------------- + +- fixed conditional expression in extract.c to check if we're overwriting + a pre-existing file + +- many improvements to libtar.c driver program (better error checking, + added -C and -v options, etc) + +- changed API to include list of canned file types, instead of passing + function pointers to tar_open() + +- fixed tar_set_file_perms() to not complain about chown() if not root + and not to call utime() on a symlink + +- added hash code for extracting hard links in other directory paths + +- fixed tar_extract_glob() to only print filenames if TAR_VERBOSE option + is set + +- replaced GNU basename(), dirname(), and strdup() compatibility code + with OpenBSD versions + +- configure performs super-anal checking of basename() and dirname() + +---------------------------------------------------------------------- + +libtar 0.5.4 beta - 11/13/99 +----------------- + +- portability fix: use ranlib instead of ar -s + +- misc fixes in append.c, extract.c, and wrapper.c to do error checking + +- fixed a bug in tar_append_file() in append.c which added some garbage + characters to encoded symlink names (wasn't NULL-terminating the result + of readlink()) + +- fixed a bug in symbolic_mode() in output.c concerning setuid and setgid + bit displaying + +- fixed tar_extract_all() in wrapper.c to only call print_long_ls() if + the TAR_VERBOSE option is set + +- added libtar_version constant string to handle.c for external configure + scripts to detect what version of libtar is installed + +---------------------------------------------------------------------- + +libtar 0.5.3 beta - 09/27/99 +----------------- + +- fixed mk_dirs_for_file() to avoid broken dirname() implementations + +- misc portability fixes + +- merged old "compat" and "libds" directories into new "misc" directory + and cleaned up Makefiles + +---------------------------------------------------------------------- + +libtar 0.5.2 beta - 09/10/99 +----------------- + +- use calloc() instead of malloc() in tar_open() to fix a bounds-checking + bug in tar_extract_all() + +- fix tar_extract_all() to properly honor the prefix argument + +---------------------------------------------------------------------- + +libtar 0.5.1 beta - 08/27/99 +----------------- + +- misc portability fixes + +---------------------------------------------------------------------- + +libtar 0.5 beta - 07/05/99 +--------------- + +- first public release + diff --git a/libtar/INSTALL b/libtar/INSTALL new file mode 100644 index 000000000..50dbe439d --- /dev/null +++ b/libtar/INSTALL @@ -0,0 +1,183 @@ +Basic Installation +================== + + These are generic installation instructions. + + The `configure' shell script attempts to guess correct values for +various system-dependent variables used during compilation. It uses +those values to create a `Makefile' in each directory of the package. +It may also create one or more `.h' files containing system-dependent +definitions. Finally, it creates a shell script `config.status' that +you can run in the future to recreate the current configuration, a file +`config.cache' that saves the results of its tests to speed up +reconfiguring, and a file `config.log' containing compiler output +(useful mainly for debugging `configure'). + + If you need to do unusual things to compile the package, please try +to figure out how `configure' could check whether to do them, and mail +diffs or instructions to the address given in the `README' so they can +be considered for the next release. If at some point `config.cache' +contains results you don't want to keep, you may remove or edit it. + + The file `configure.in' is used to create `configure' by a program +called `autoconf'. You only need `configure.in' if you want to change +it or regenerate `configure' using a newer version of `autoconf'. + +The simplest way to compile this package is: + + 1. `cd' to the directory containing the package's source code and type + `./configure' to configure the package for your system. If you're + using `csh' on an old version of System V, you might need to type + `sh ./configure' instead to prevent `csh' from trying to execute + `configure' itself. + + Running `configure' takes awhile. While running, it prints some + messages telling which features it is checking for. + + 2. Type `make' to compile the package. + + 3. Optionally, type `make check' to run any self-tests that come with + the package. + + 4. Type `make install' to install the programs and any data files and + documentation. + + 5. You can remove the program binaries and object files from the + source code directory by typing `make clean'. To also remove the + files that `configure' created (so you can compile the package for + a different kind of computer), type `make distclean'. There is + also a `make maintainer-clean' target, but that is intended mainly + for the package's developers. If you use it, you may have to get + all sorts of other programs in order to regenerate files that came + with the distribution. + +Compilers and Options +===================== + + Some systems require unusual options for compilation or linking that +the `configure' script does not know about. You can give `configure' +initial values for variables by setting them in the environment. Using +a Bourne-compatible shell, you can do that on the command line like +this: + CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure + +Or on systems that have the `env' program, you can do it like this: + env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure + +Compiling For Multiple Architectures +==================================== + + You can compile the package for more than one kind of computer at the +same time, by placing the object files for each architecture in their +own directory. To do this, you must use a version of `make' that +supports the `VPATH' variable, such as GNU `make'. `cd' to the +directory where you want the object files and executables to go and run +the `configure' script. `configure' automatically checks for the +source code in the directory that `configure' is in and in `..'. + + If you have to use a `make' that does not supports the `VPATH' +variable, you have to compile the package for one architecture at a time +in the source code directory. After you have installed the package for +one architecture, use `make distclean' before reconfiguring for another +architecture. + +Installation Names +================== + + By default, `make install' will install the package's files in +`/usr/local/bin', `/usr/local/man', etc. You can specify an +installation prefix other than `/usr/local' by giving `configure' the +option `--prefix=PATH'. + + You can specify separate installation prefixes for +architecture-specific files and architecture-independent files. If you +give `configure' the option `--exec-prefix=PATH', the package will use +PATH as the prefix for installing programs and libraries. +Documentation and other data files will still use the regular prefix. + + In addition, if you use an unusual directory layout you can give +options like `--bindir=PATH' to specify different values for particular +kinds of files. Run `configure --help' for a list of the directories +you can set and what kinds of files go in them. + + If the package supports it, you can cause programs to be installed +with an extra prefix or suffix on their names by giving `configure' the +option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'. + +Optional Features +================= + + Some packages pay attention to `--enable-FEATURE' options to +`configure', where FEATURE indicates an optional part of the package. +They may also pay attention to `--with-PACKAGE' options, where PACKAGE +is something like `gnu-as' or `x' (for the X Window System). The +`README' should mention any `--enable-' and `--with-' options that the +package recognizes. + + For packages that use the X Window System, `configure' can usually +find the X include and library files automatically, but if it doesn't, +you can use the `configure' options `--x-includes=DIR' and +`--x-libraries=DIR' to specify their locations. + +Specifying the System Type +========================== + + There may be some features `configure' can not figure out +automatically, but needs to determine by the type of host the package +will run on. Usually `configure' can figure that out, but if it prints +a message saying it can not guess the host type, give it the +`--host=TYPE' option. TYPE can either be a short name for the system +type, such as `sun4', or a canonical name with three fields: + CPU-COMPANY-SYSTEM + +See the file `config.sub' for the possible values of each field. If +`config.sub' isn't included in this package, then this package doesn't +need to know the host type. + + If you are building compiler tools for cross-compiling, you can also +use the `--target=TYPE' option to select the type of system they will +produce code for and the `--build=TYPE' option to select the type of +system on which you are compiling the package. + +Sharing Defaults +================ + + If you want to set default values for `configure' scripts to share, +you can create a site shell script called `config.site' that gives +default values for variables like `CC', `cache_file', and `prefix'. +`configure' looks for `PREFIX/share/config.site' if it exists, then +`PREFIX/etc/config.site' if it exists. Or, you can set the +`CONFIG_SITE' environment variable to the location of the site script. +A warning: not all `configure' scripts look for a site script. + +Operation Controls +================== + + `configure' recognizes the following options to control how it +operates. + +`--cache-file=FILE' + Use and save the results of the tests in FILE instead of + `./config.cache'. Set FILE to `/dev/null' to disable caching, for + debugging `configure'. + +`--help' + Print a summary of the options to `configure', and exit. + +`--quiet' +`--silent' +`-q' + Do not print messages saying which checks are being made. To + suppress all normal output, redirect it to `/dev/null' (any error + messages will still be shown). + +`--srcdir=DIR' + Look for the package's source code in directory DIR. Usually + `configure' can determine that directory automatically. + +`--version' + Print the version of Autoconf used to generate the `configure' + script, and exit. + +`configure' also accepts some other, not widely useful, options. + diff --git a/libtar/README b/libtar/README new file mode 100644 index 000000000..91f466cbe --- /dev/null +++ b/libtar/README @@ -0,0 +1,121 @@ +libtar - C library for manipulating tar files +====== + +libtar is a library for manipulating tar files from within C programs. +Here are some of its features: + + * Handles both POSIX tar file format and the GNU extensions. + * API provides functions for easy use, such as tar_extract_all(). + * Also provides functions for more granular use, such as + tar_append_regfile(). + + +Installation +------------ + +To build libtar, you should be able to simply run these commands: + + ./configure + make + make install + + +Encap Package Support +--------------------- + +To build this software as an Encap package, you can pass the +--enable-encap option to configure. This will be automatically +enabled if the epkg or mkencap programs are detected on the system, +but can be overridden by the --disable-encap option. + +When building an Encap package, the configure script will automatically +adjust the installation prefix to use an appropriate Encap package +directory. It does this using a heuristic algorithm which examines the +values of the ${ENCAP_SOURCE} and ${ENCAP_TARGET} environment variables +and the argument to configure's --prefix option. + +If mkencap was detected on the system, it will be automatically run during +"make install". By default, epkg will also be run, but this can be +inhibited with the --disable-epkg-install configure option. + +For information on the Encap package management system, see the WSG +Encap Archive: + + http://www.encap.org/ + + +zlib Support +------------ + +The configure script will attempt to find the zlib library on your system +for use with the libtar driver program. The zlib package is available from: + + http://www.gzip.org/zlib/ + +If zlib is installed on your system, but you do not wish to use it, +specify the --without-zlib option when you invoke configure. + + +More Information +---------------- + +For documentation of the libtar API, see the enclosed manpages. For more +information on the libtar package, see: + + http://www-dev.cites.uiuc.edu/libtar/ + +Source code for the latest version of libtar will be available there, as +well as Encap binary distributions for many common platforms. + + +Supported Platforms +------------------- + +I develop and test libtar on the following platforms: + + AIX 4.3.3 and 5.1 + HP-UX 11.00 + IRIX 6.5 + RedHat Linux 7.2 + Solaris 8 and 9 + +It should also build on the following platforms, but I do not actively +support them: + + AIX 3.2.5 + AIX 4.2.1 + Cygwin + FreeBSD + HP-UX 10.20 + Linux/libc5 + OpenBSD + Solaris 2.5 + Solaris 2.6 + Solaris 7 + +If you successfully build libtar on another platform, please email me a +patch and/or configuration information. + + +Compatibility Code +------------------ + +libtar depends on some library calls which are not available or not +usable on some platforms. To accomodate these systems, I've included +a version of these calls in the compat subdirectory. + +I've slightly modified these functions for integration into this source +tree, but the functionality has not been modified from the original +source. Please note that while this code should work for you, I didn't +write it, so please don't send me bug reports on it. + + +Author +------ + +Feedback and bug reports are welcome. + +Mark D. Roth +Campus Information Technologies and Educational Services +University of Illinois at Urbana-Champaign + diff --git a/libtar/TODO b/libtar/TODO new file mode 100644 index 000000000..f2f859f54 --- /dev/null +++ b/libtar/TODO @@ -0,0 +1,21 @@ +Functionality: +-------------- + +* add list mode to allow nodes to be inserted in any arbitrary location +* add "*_hash_iterate()" function +* add flags argument to *_list_del() that allows the listptr to be set + to the previous or next element +* add a generic pointer type to replace *_listptr_t and *_hashptr_t ??? + + +Code Cleanup: +------------- + +* rename functions: + *_list_next => *_listptr_next() + *_list_prev => *_listptr_prev() + *_hash_next => *_hashptr_next() +* start using "*_list_t" and "*_hash_t" instead of "*_list_t *" and + "*_hash_t *" ? +* add prefixes to structure member field names + diff --git a/libtar/append.c b/libtar/append.c new file mode 100644 index 000000000..05024b926 --- /dev/null +++ b/libtar/append.c @@ -0,0 +1,256 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** append.c - libtar code to append files to a tar archive +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include +#include +#include + +#ifdef STDC_HEADERS +# include +# include +#endif + +#ifdef HAVE_UNISTD_H +# include +#endif + + +struct tar_dev +{ + dev_t td_dev; + libtar_hash_t *td_h; +}; +typedef struct tar_dev tar_dev_t; + +struct tar_ino +{ + ino_t ti_ino; + char ti_name[MAXPATHLEN]; +}; +typedef struct tar_ino tar_ino_t; + + +/* free memory associated with a tar_dev_t */ +void +tar_dev_free(tar_dev_t *tdp) +{ + libtar_hash_free(tdp->td_h, free); + free(tdp); +} + + +/* appends a file to the tar archive */ +int +tar_append_file(TAR *t, char *realname, char *savename) +{ + struct stat s; + int i; + libtar_hashptr_t hp; + tar_dev_t *td = NULL; + tar_ino_t *ti = NULL; + char path[MAXPATHLEN]; + +#ifdef DEBUG + printf("==> tar_append_file(TAR=0x%lx (\"%s\"), realname=\"%s\", " + "savename=\"%s\")\n", t, t->pathname, realname, + (savename ? savename : "[NULL]")); +#endif + + if (lstat(realname, &s) != 0) + { +#ifdef DEBUG + perror("lstat()"); +#endif + return -1; + } + + /* set header block */ +#ifdef DEBUG + puts(" tar_append_file(): setting header block..."); +#endif + memset(&(t->th_buf), 0, sizeof(struct tar_header)); + th_set_from_stat(t, &s); + + /* set the header path */ +#ifdef DEBUG + puts(" tar_append_file(): setting header path..."); +#endif + th_set_path(t, (savename ? savename : realname)); + + /* check if it's a hardlink */ +#ifdef DEBUG + puts(" tar_append_file(): checking inode cache for hardlink..."); +#endif + libtar_hashptr_reset(&hp); + if (libtar_hash_getkey(t->h, &hp, &(s.st_dev), + (libtar_matchfunc_t)dev_match) != 0) + td = (tar_dev_t *)libtar_hashptr_data(&hp); + else + { +#ifdef DEBUG + printf("+++ adding hash for device (0x%lx, 0x%lx)...\n", + major(s.st_dev), minor(s.st_dev)); +#endif + td = (tar_dev_t *)calloc(1, sizeof(tar_dev_t)); + td->td_dev = s.st_dev; + td->td_h = libtar_hash_new(256, (libtar_hashfunc_t)ino_hash); + if (td->td_h == NULL) + return -1; + if (libtar_hash_add(t->h, td) == -1) + return -1; + } + libtar_hashptr_reset(&hp); + if (libtar_hash_getkey(td->td_h, &hp, &(s.st_ino), + (libtar_matchfunc_t)ino_match) != 0) + { + ti = (tar_ino_t *)libtar_hashptr_data(&hp); +#ifdef DEBUG + printf(" tar_append_file(): encoding hard link \"%s\" " + "to \"%s\"...\n", realname, ti->ti_name); +#endif + t->th_buf.typeflag = LNKTYPE; + th_set_link(t, ti->ti_name); + } + else + { +#ifdef DEBUG + printf("+++ adding entry: device (0x%lx,0x%lx), inode %ld " + "(\"%s\")...\n", major(s.st_dev), minor(s.st_dev), + s.st_ino, realname); +#endif + ti = (tar_ino_t *)calloc(1, sizeof(tar_ino_t)); + if (ti == NULL) + return -1; + ti->ti_ino = s.st_ino; + snprintf(ti->ti_name, sizeof(ti->ti_name), "%s", + savename ? savename : realname); + libtar_hash_add(td->td_h, ti); + } + + /* check if it's a symlink */ + if (TH_ISSYM(t)) + { + i = readlink(realname, path, sizeof(path)); + if (i == -1) + return -1; + if (i >= MAXPATHLEN) + i = MAXPATHLEN - 1; + path[i] = '\0'; +#ifdef DEBUG + printf(" tar_append_file(): encoding symlink \"%s\" -> " + "\"%s\"...\n", realname, path); +#endif + th_set_link(t, path); + } + + /* print file info */ + if (t->options & TAR_VERBOSE) + th_print_long_ls(t); + +#ifdef DEBUG + puts(" tar_append_file(): writing header"); +#endif + /* write header */ + if (th_write(t) != 0) + { +#ifdef DEBUG + printf("t->fd = %d\n", t->fd); +#endif + return -1; + } +#ifdef DEBUG + puts(" tar_append_file(): back from th_write()"); +#endif + + /* if it's a regular file, write the contents as well */ + if (TH_ISREG(t) && tar_append_regfile(t, realname) != 0) + return -1; + + return 0; +} + + +/* write EOF indicator */ +int +tar_append_eof(TAR *t) +{ + int i, j; + char block[T_BLOCKSIZE]; + + memset(&block, 0, T_BLOCKSIZE); + for (j = 0; j < 2; j++) + { + i = tar_block_write(t, &block); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + } + + return 0; +} + + +/* add file contents to a tarchive */ +int +tar_append_regfile(TAR *t, char *realname) +{ + char block[T_BLOCKSIZE]; + int filefd; + int i, j; + size_t size; + + filefd = open(realname, O_RDONLY); + if (filefd == -1) + { +#ifdef DEBUG + perror("open()"); +#endif + return -1; + } + + size = th_get_size(t); + for (i = size; i > T_BLOCKSIZE; i -= T_BLOCKSIZE) + { + j = read(filefd, &block, T_BLOCKSIZE); + if (j != T_BLOCKSIZE) + { + if (j != -1) + errno = EINVAL; + return -1; + } + if (tar_block_write(t, &block) == -1) + return -1; + } + + if (i > 0) + { + j = read(filefd, &block, i); + if (j == -1) + return -1; + memset(&(block[i]), 0, T_BLOCKSIZE - i); + if (tar_block_write(t, &block) == -1) + return -1; + } + + close(filefd); + + return 0; +} + + diff --git a/libtar/basename.c b/libtar/basename.c new file mode 100644 index 000000000..2ac1e139e --- /dev/null +++ b/libtar/basename.c @@ -0,0 +1,74 @@ +/* $OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */ + +/* + * Copyright (c) 1997 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef lint +static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; +#endif /* not lint */ + +#include +#include +#include + +char * +openbsd_basename(path) + const char *path; +{ + static char bname[MAXPATHLEN]; + register const char *endp, *startp; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + (void)strcpy(bname, "."); + return(bname); + } + + /* Strip trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* All slashes becomes "/" */ + if (endp == path && *endp == '/') { + (void)strcpy(bname, "/"); + return(bname); + } + + /* Find the start of the base */ + startp = endp; + while (startp > path && *(startp - 1) != '/') + startp--; + + if (endp - startp + 1 > sizeof(bname)) { + errno = ENAMETOOLONG; + return(NULL); + } + (void)strncpy(bname, startp, endp - startp + 1); + bname[endp - startp + 1] = '\0'; + return(bname); +} diff --git a/libtar/block.c b/libtar/block.c new file mode 100644 index 000000000..89e5e3d70 --- /dev/null +++ b/libtar/block.c @@ -0,0 +1,383 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** block.c - libtar code to handle tar archive header blocks +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include + +#ifdef STDC_HEADERS +# include +# include +#endif + +#define BIT_ISSET(bitmask, bit) ((bitmask) & (bit)) + + +/* read a header block */ +int +th_read_internal(TAR *t) +{ + int i; + int num_zero_blocks = 0; + +#ifdef DEBUG + printf("==> th_read_internal(TAR=\"%s\")\n", t->pathname); +#endif + + while ((i = tar_block_read(t, &(t->th_buf))) == T_BLOCKSIZE) + { + /* two all-zero blocks mark EOF */ + if (t->th_buf.name[0] == '\0') + { + num_zero_blocks++; + if (!BIT_ISSET(t->options, TAR_IGNORE_EOT) + && num_zero_blocks >= 2) + return 0; /* EOF */ + else + continue; + } + + /* verify magic and version */ + if (BIT_ISSET(t->options, TAR_CHECK_MAGIC) + && strncmp(t->th_buf.magic, TMAGIC, TMAGLEN - 1) != 0) + { +#ifdef DEBUG + puts("!!! unknown magic value in tar header"); +#endif + return -2; + } + + if (BIT_ISSET(t->options, TAR_CHECK_VERSION) + && strncmp(t->th_buf.version, TVERSION, TVERSLEN) != 0) + { +#ifdef DEBUG + puts("!!! unknown version value in tar header"); +#endif + return -2; + } + + /* check chksum */ + if (!BIT_ISSET(t->options, TAR_IGNORE_CRC) + && !th_crc_ok(t)) + { +#ifdef DEBUG + puts("!!! tar header checksum error"); +#endif + return -2; + } + + break; + } + +#ifdef DEBUG + printf("<== th_read_internal(): returning %d\n", i); +#endif + return i; +} + + +/* wrapper function for th_read_internal() to handle GNU extensions */ +int +th_read(TAR *t) +{ + int i, j; + size_t sz; + char *ptr; + +#ifdef DEBUG + printf("==> th_read(t=0x%lx)\n", t); +#endif + + if (t->th_buf.gnu_longname != NULL) + free(t->th_buf.gnu_longname); + if (t->th_buf.gnu_longlink != NULL) + free(t->th_buf.gnu_longlink); + memset(&(t->th_buf), 0, sizeof(struct tar_header)); + + i = th_read_internal(t); + if (i == 0) + return 1; + else if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + + /* check for GNU long link extention */ + if (TH_ISLONGLINK(t)) + { + sz = th_get_size(t); + j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0); +#ifdef DEBUG + printf(" th_read(): GNU long linkname detected " + "(%ld bytes, %d blocks)\n", sz, j); +#endif + t->th_buf.gnu_longlink = (char *)malloc(j * T_BLOCKSIZE); + if (t->th_buf.gnu_longlink == NULL) + return -1; + + for (ptr = t->th_buf.gnu_longlink; j > 0; + j--, ptr += T_BLOCKSIZE) + { +#ifdef DEBUG + printf(" th_read(): reading long linkname " + "(%d blocks left, ptr == %ld)\n", j, ptr); +#endif + i = tar_block_read(t, ptr); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } +#ifdef DEBUG + printf(" th_read(): read block == \"%s\"\n", ptr); +#endif + } +#ifdef DEBUG + printf(" th_read(): t->th_buf.gnu_longlink == \"%s\"\n", + t->th_buf.gnu_longlink); +#endif + + i = th_read_internal(t); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + } + + /* check for GNU long name extention */ + if (TH_ISLONGNAME(t)) + { + sz = th_get_size(t); + j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0); +#ifdef DEBUG + printf(" th_read(): GNU long filename detected " + "(%ld bytes, %d blocks)\n", sz, j); +#endif + t->th_buf.gnu_longname = (char *)malloc(j * T_BLOCKSIZE); + if (t->th_buf.gnu_longname == NULL) + return -1; + + for (ptr = t->th_buf.gnu_longname; j > 0; + j--, ptr += T_BLOCKSIZE) + { +#ifdef DEBUG + printf(" th_read(): reading long filename " + "(%d blocks left, ptr == %ld)\n", j, ptr); +#endif + i = tar_block_read(t, ptr); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } +#ifdef DEBUG + printf(" th_read(): read block == \"%s\"\n", ptr); +#endif + } +#ifdef DEBUG + printf(" th_read(): t->th_buf.gnu_longname == \"%s\"\n", + t->th_buf.gnu_longname); +#endif + + i = th_read_internal(t); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + } + +#if 0 + /* + ** work-around for old archive files with broken typeflag fields + ** NOTE: I fixed this in the TH_IS*() macros instead + */ + + /* + ** (directories are signified with a trailing '/') + */ + if (t->th_buf.typeflag == AREGTYPE + && t->th_buf.name[strlen(t->th_buf.name) - 1] == '/') + t->th_buf.typeflag = DIRTYPE; + + /* + ** fallback to using mode bits + */ + if (t->th_buf.typeflag == AREGTYPE) + { + mode = (mode_t)oct_to_int(t->th_buf.mode); + + if (S_ISREG(mode)) + t->th_buf.typeflag = REGTYPE; + else if (S_ISDIR(mode)) + t->th_buf.typeflag = DIRTYPE; + else if (S_ISFIFO(mode)) + t->th_buf.typeflag = FIFOTYPE; + else if (S_ISCHR(mode)) + t->th_buf.typeflag = CHRTYPE; + else if (S_ISBLK(mode)) + t->th_buf.typeflag = BLKTYPE; + else if (S_ISLNK(mode)) + t->th_buf.typeflag = SYMTYPE; + } +#endif + + return 0; +} + + +/* write a header block */ +int +th_write(TAR *t) +{ + int i, j; + char type2; + size_t sz, sz2; + char *ptr; + char buf[T_BLOCKSIZE]; + +#ifdef DEBUG + printf("==> th_write(TAR=\"%s\")\n", t->pathname); + th_print(t); +#endif + + if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL) + { +#ifdef DEBUG + printf("th_write(): using gnu_longlink (\"%s\")\n", + t->th_buf.gnu_longlink); +#endif + /* save old size and type */ + type2 = t->th_buf.typeflag; + sz2 = th_get_size(t); + + /* write out initial header block with fake size and type */ + t->th_buf.typeflag = GNU_LONGLINK_TYPE; + sz = strlen(t->th_buf.gnu_longlink); + th_set_size(t, sz); + th_finish(t); + i = tar_block_write(t, &(t->th_buf)); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + + /* write out extra blocks containing long name */ + for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0), + ptr = t->th_buf.gnu_longlink; j > 1; + j--, ptr += T_BLOCKSIZE) + { + i = tar_block_write(t, ptr); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + } + memset(buf, 0, T_BLOCKSIZE); + strncpy(buf, ptr, T_BLOCKSIZE); + i = tar_block_write(t, &buf); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + + /* reset type and size to original values */ + t->th_buf.typeflag = type2; + th_set_size(t, sz2); + } + + if ((t->options & TAR_GNU) && t->th_buf.gnu_longname != NULL) + { +#ifdef DEBUG + printf("th_write(): using gnu_longname (\"%s\")\n", + t->th_buf.gnu_longname); +#endif + /* save old size and type */ + type2 = t->th_buf.typeflag; + sz2 = th_get_size(t); + + /* write out initial header block with fake size and type */ + t->th_buf.typeflag = GNU_LONGNAME_TYPE; + sz = strlen(t->th_buf.gnu_longname); + th_set_size(t, sz); + th_finish(t); + i = tar_block_write(t, &(t->th_buf)); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + + /* write out extra blocks containing long name */ + for (j = (sz / T_BLOCKSIZE) + (sz % T_BLOCKSIZE ? 1 : 0), + ptr = t->th_buf.gnu_longname; j > 1; + j--, ptr += T_BLOCKSIZE) + { + i = tar_block_write(t, ptr); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + } + memset(buf, 0, T_BLOCKSIZE); + strncpy(buf, ptr, T_BLOCKSIZE); + i = tar_block_write(t, &buf); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + + /* reset type and size to original values */ + t->th_buf.typeflag = type2; + th_set_size(t, sz2); + } + + th_finish(t); + +#ifdef DEBUG + /* print tar header */ + th_print(t); +#endif + + i = tar_block_write(t, &(t->th_buf)); + if (i != T_BLOCKSIZE) + { + if (i != -1) + errno = EINVAL; + return -1; + } + +#ifdef DEBUG + puts("th_write(): returning 0"); +#endif + return 0; +} + + diff --git a/libtar/compat.h b/libtar/compat.h new file mode 100644 index 000000000..70ac2f435 --- /dev/null +++ b/libtar/compat.h @@ -0,0 +1,256 @@ +/* prototypes for borrowed "compatibility" code */ + +#include + +#include +#include + +#include +#include + +#ifdef HAVE_LIBGEN_H +# include +#endif + + +#if defined(NEED_BASENAME) && !defined(HAVE_BASENAME) + +# ifdef basename +# undef basename /* fix glibc brokenness */ +# endif + +char *openbsd_basename(const char *); +# define basename openbsd_basename + +#endif /* NEED_BASENAME && ! HAVE_BASENAME */ + + +#if defined(NEED_DIRNAME) && !defined(HAVE_DIRNAME) + +char *openbsd_dirname(const char *); +# define dirname openbsd_dirname + +#endif /* NEED_DIRNAME && ! HAVE_DIRNAME */ + + +#ifdef NEED_FNMATCH +# ifndef HAVE_FNMATCH + +# define FNM_NOMATCH 1 /* Match failed. */ + +# define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ +# define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ +# define FNM_PERIOD 0x04 /* Period must be matched by period. */ + +# define FNM_LEADING_DIR 0x08 /* Ignore / after Imatch. */ +# define FNM_CASEFOLD 0x10 /* Case insensitive search. */ +# define FNM_IGNORECASE FNM_CASEFOLD +# define FNM_FILE_NAME FNM_PATHNAME + +int openbsd_fnmatch(const char *, const char *, int); +# define fnmatch openbsd_fnmatch + +# else /* HAVE_FNMATCH */ + +# ifdef HAVE_FNMATCH_H +# include +# endif + +# endif /* ! HAVE_FNMATCH */ +#endif /* NEED_FNMATCH */ + + +#ifdef NEED_GETHOSTBYNAME_R + +# include + +# if GETHOSTBYNAME_R_NUM_ARGS != 6 + +int compat_gethostbyname_r(const char *, struct hostent *, + char *, size_t, struct hostent **, int *); + +# define gethostbyname_r compat_gethostbyname_r + +# endif /* GETHOSTBYNAME_R_NUM_ARGS != 6 */ + +#endif /* NEED_GETHOSTBYNAME_R */ + + +#if defined(NEED_GETHOSTNAME) && !defined(HAVE_GETHOSTNAME) + +int gethostname(char *, size_t); + +#endif /* NEED_GETHOSTNAME && ! HAVE_GETHOSTNAME */ + + +#ifdef NEED_GETSERVBYNAME_R + +# include + +# if GETSERVBYNAME_R_NUM_ARGS != 6 + +int compat_getservbyname_r(const char *, const char *, struct servent *, + char *, size_t, struct servent **); + +# define getservbyname_r compat_getservbyname_r + +# endif /* GETSERVBYNAME_R_NUM_ARGS != 6 */ + +#endif /* NEED_GETSERVBYNAME_R */ + + + +#ifdef NEED_GLOB +# ifndef HAVE_GLOB + +typedef struct { + int gl_pathc; /* Count of total paths so far. */ + int gl_matchc; /* Count of paths matching pattern. */ + int gl_offs; /* Reserved at beginning of gl_pathv. */ + int gl_flags; /* Copy of flags parameter to glob. */ + char **gl_pathv; /* List of paths matching pattern. */ + /* Copy of errfunc parameter to glob. */ + int (*gl_errfunc)(const char *, int); + + /* + * Alternate filesystem access methods for glob; replacement + * versions of closedir(3), readdir(3), opendir(3), stat(2) + * and lstat(2). + */ + void (*gl_closedir)(void *); + struct dirent *(*gl_readdir)(void *); + void *(*gl_opendir)(const char *); + int (*gl_lstat)(const char *, struct stat *); + int (*gl_stat)(const char *, struct stat *); +} glob_t; + +/* Flags */ +# define GLOB_APPEND 0x0001 /* Append to output from previous call. */ +# define GLOB_DOOFFS 0x0002 /* Use gl_offs. */ +# define GLOB_ERR 0x0004 /* Return on error. */ +# define GLOB_MARK 0x0008 /* Append / to matching directories. */ +# define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */ +# define GLOB_NOSORT 0x0020 /* Don't sort. */ + +# define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */ +# define GLOB_BRACE 0x0080 /* Expand braces ala csh. */ +# define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */ +# define GLOB_NOMAGIC 0x0200 /* GLOB_NOCHECK without magic chars (csh). */ +# define GLOB_QUOTE 0x0400 /* Quote special chars with \. */ +# define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */ +# define GLOB_NOESCAPE 0x1000 /* Disable backslash escaping. */ + +/* Error values returned by glob(3) */ +# define GLOB_NOSPACE (-1) /* Malloc call failed. */ +# define GLOB_ABORTED (-2) /* Unignored error. */ +# define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK not set. */ +# define GLOB_NOSYS (-4) /* Function not supported. */ +# define GLOB_ABEND GLOB_ABORTED + +int openbsd_glob(const char *, int, int (*)(const char *, int), glob_t *); +void openbsd_globfree(glob_t *); +# define glob openbsd_glob +# define globfree openbsd_globfree + +# else /* HAVE_GLOB */ + +# ifdef HAVE_GLOB_H +# include +# endif + +# endif /* ! HAVE_GLOB */ +#endif /* NEED_GLOB */ + + +#if defined(NEED_INET_ATON) && !defined(HAVE_INET_ATON) + +int inet_aton(const char *, struct in_addr *); + +#endif /* NEED_INET_ATON && ! HAVE_INET_ATON */ + + +#ifdef NEED_MAKEDEV + +# ifdef MAJOR_IN_MKDEV +# include +# else +# ifdef MAJOR_IN_SYSMACROS +# include +# endif +# endif + +/* +** On most systems makedev() has two args. +** Some weird systems, like QNX6, have makedev() functions that expect +** an extra first argument for "node", which can be 0 for a local +** machine. +*/ + +# ifdef MAKEDEV_THREE_ARGS +# define compat_makedev(maj, min) makedev(0, maj, min) +# else +# define compat_makedev makedev +# endif + +#endif /* NEED_MAKEDEV */ + + +#if defined(NEED_SNPRINTF) && !defined(HAVE_SNPRINTF) + +int mutt_snprintf(char *, size_t, const char *, ...); +int mutt_vsnprintf(char *, size_t, const char *, va_list); +#define snprintf mutt_snprintf +#define vsnprintf mutt_vsnprintf + +#endif /* NEED_SNPRINTF && ! HAVE_SNPRINTF */ + + +#if defined(NEED_STRLCAT) && !defined(HAVE_STRLCAT) + +size_t strlcat(char *, const char *, size_t); + +#endif /* NEED_STRLCAT && ! HAVE_STRLCAT */ + + +#if defined(NEED_STRLCPY) && !defined(HAVE_STRLCPY) + +size_t strlcpy(char *, const char *, size_t); + +#endif /* NEED_STRLCPY && ! HAVE_STRLCPY */ + + +#if defined(NEED_STRDUP) && !defined(HAVE_STRDUP) + +char *openbsd_strdup(const char *); +# define strdup openbsd_strdup + +#endif /* NEED_STRDUP && ! HAVE_STRDUP */ + + +#if defined(NEED_STRMODE) && !defined(HAVE_STRMODE) + +void strmode(register mode_t, register char *); + +#endif /* NEED_STRMODE && ! HAVE_STRMODE */ + + +#if defined(NEED_STRRSTR) && !defined(HAVE_STRRSTR) + +char *strrstr(char *, char *); + +#endif /* NEED_STRRSTR && ! HAVE_STRRSTR */ + + +#ifdef NEED_STRSEP + +# ifdef HAVE_STRSEP +# define _LINUX_SOURCE_COMPAT /* needed on AIX 4.3.3 */ +# else + +char *strsep(register char **, register const char *); + +# endif + +#endif /* NEED_STRSEP */ + + diff --git a/libtar/config.h b/libtar/config.h new file mode 100644 index 000000000..c658020ea --- /dev/null +++ b/libtar/config.h @@ -0,0 +1,188 @@ +/* config.h. Generated by configure. */ +/* config.h.in. Generated from configure.ac by autoheader. */ + +/* Define if your system has a working basename */ +/* #undef HAVE_BASENAME */ + +/* Define to 1 if you have the header file. */ +/* #undef HAVE_CTYPE_H */ + +/* Define to 1 if the system has the type `dev_t'. */ +#define HAVE_DEV_T 1 + +/* Define if your system has a working dirname */ +/* #undef HAVE_DIRNAME */ + +/* Define to 1 if your system has a working POSIX `fnmatch' function. */ +#define HAVE_FNMATCH 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_FNMATCH_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_INTTYPES_H 1 + +/* Define to 1 if you have the `lchown' function. */ +#define HAVE_LCHOWN 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_LIBGEN_H 1 + +/* Define to 1 if you have the `z' library (-lz). */ +#define HAVE_LIBZ 1 + +/* Define to 1 if the system has the type `major_t'. */ +/* #undef HAVE_MAJOR_T */ + +/* Define to 1 if you have the header file. */ +#define HAVE_MEMORY_H 1 + +/* Define to 1 if the system has the type `minor_t'. */ +/* #undef HAVE_MINOR_T */ + +/* Define to 1 if the system has the type `nlink_t'. */ +#define HAVE_NLINK_T 1 + +/* Define if your system has a working snprintf */ +#define HAVE_SNPRINTF 1 + +/* Define to 1 if the system has the type `socklen_t'. */ +#define HAVE_SOCKLEN_T 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDINT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STDLIB_H 1 + +/* Define if you have the strdup function */ +#define HAVE_STRDUP 1 + +/* Define to 1 if you have the `strftime' function. */ +#define HAVE_STRFTIME 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRINGS_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_STRING_H 1 + +/* Define if you have the strlcpy function */ +/* #undef HAVE_STRLCPY */ + +/* Define if you have the strmode function */ +/* #undef HAVE_STRMODE */ + +/* Define if you have the strsep function */ +#define HAVE_STRSEP 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_STAT_H 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_SYS_TYPES_H 1 + +/* Define to 1 if the system has the type `uint64_t'. */ +#define HAVE_UINT64_T 1 + +/* Define to 1 if you have the header file. */ +#define HAVE_UNISTD_H 1 + +/* Define to 1 if `major', `minor', and `makedev' are declared in . + */ +/* #undef MAJOR_IN_MKDEV */ + +/* Define to 1 if `major', `minor', and `makedev' are declared in + . */ +/* #undef MAJOR_IN_SYSMACROS */ + +/* Define as 1 if makedev expects three arguments */ +/* #undef MAKEDEV_THREE_ARGS */ + +/* Define if you want to use the basename function */ +#define NEED_BASENAME 1 + +/* Define if you want to use the dirname function */ +#define NEED_DIRNAME 1 + +/* Define if you want to use the fnmatch function */ +#define NEED_FNMATCH 1 + +/* Define if you want to use the makedev function */ +#define NEED_MAKEDEV 1 + +/* Define if you want to use the snprintf function */ +#define NEED_SNPRINTF 1 + +/* Define if you want to use the strdup function */ +#define NEED_STRDUP 1 + +/* Define if you want to use the strlcpy function */ +#define NEED_STRLCPY 1 + +/* Define if you want to use the strmode function */ +#define NEED_STRMODE 1 + +/* Define if you want to use the strsep function */ +#define NEED_STRSEP 1 + +/* Define to the address where bug reports for this package should be sent. */ +#define PACKAGE_BUGREPORT "" + +/* Define to the full name of this package. */ +#define PACKAGE_NAME "libtar" + +/* Define to the full name and version of this package. */ +#define PACKAGE_STRING "libtar 1.2.11" + +/* Define to the one symbol short name of this package. */ +#define PACKAGE_TARNAME "libtar" + +/* Define to the version of this package. */ +#define PACKAGE_VERSION "1.2.11" + +/* Define to 1 if you have the ANSI C header files. */ +#define STDC_HEADERS 1 + +/* Define to 1 if on AIX 3. + System headers sometimes define this. + We just want to avoid a redefinition error message. */ +#ifndef _ALL_SOURCE +/* # undef _ALL_SOURCE */ +#endif + +/* Define to empty if `const' does not conform to ANSI C. */ +/* #undef const */ + +/* Define to `unsigned long' if not defined in system header files. */ +/* #undef dev_t */ + +/* Define to `int' if doesn't define. */ +/* #undef gid_t */ + +/* Define to `unsigned int' if not defined in system header files. */ +#define major_t unsigned int + +/* Define to `unsigned int' if not defined in system header files. */ +#define minor_t unsigned int + +/* Define to `int' if does not define. */ +/* #undef mode_t */ + +/* Define to `unsigned short' if not defined in system header files. */ +/* #undef nlink_t */ + +/* Define to `long' if does not define. */ +/* #undef off_t */ + +/* Define to `unsigned' if does not define. */ +/* #undef size_t */ + +/* Define to `unsigned long' if not defined in system header files. */ +/* #undef socklen_t */ + +/* Define to `int' if doesn't define. */ +/* #undef uid_t */ + +/* Define to `long long' if not defined in system header files. */ +/* #undef uint64_t */ diff --git a/libtar/decode.c b/libtar/decode.c new file mode 100644 index 000000000..383306d64 --- /dev/null +++ b/libtar/decode.c @@ -0,0 +1,122 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** decode.c - libtar code to decode tar header blocks +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include +#include + +#ifdef STDC_HEADERS +# include +#endif + + +/* determine full path name */ +char * +th_get_pathname(TAR *t) +{ + char filename[MAXPATHLEN]; + + if (t->th_buf.gnu_longname) { + printf("returning gnu longname\n"); + return t->th_buf.gnu_longname; + } + + if (t->th_buf.prefix[0] != '\0') + { + snprintf(filename, sizeof(filename), "%.155s/%.100s", + t->th_buf.prefix, t->th_buf.name); + return strdup(filename); + } + + snprintf(filename, sizeof(filename), "%.100s", t->th_buf.name); + return strdup(filename); +} + + +uid_t +th_get_uid(TAR *t) +{ + int uid; + struct passwd *pw; + + pw = getpwnam(t->th_buf.uname); + if (pw != NULL) + return pw->pw_uid; + + /* if the password entry doesn't exist */ + sscanf(t->th_buf.uid, "%o", &uid); + return uid; +} + + +gid_t +th_get_gid(TAR *t) +{ + int gid; + struct group *gr; + + gr = getgrnam(t->th_buf.gname); + if (gr != NULL) + return gr->gr_gid; + + /* if the group entry doesn't exist */ + sscanf(t->th_buf.gid, "%o", &gid); + return gid; +} + + +mode_t +th_get_mode(TAR *t) +{ + mode_t mode; + + mode = (mode_t)oct_to_int(t->th_buf.mode); + if (! (mode & S_IFMT)) + { + switch (t->th_buf.typeflag) + { + case SYMTYPE: + mode |= S_IFLNK; + break; + case CHRTYPE: + mode |= S_IFCHR; + break; + case BLKTYPE: + mode |= S_IFBLK; + break; + case DIRTYPE: + mode |= S_IFDIR; + break; + case FIFOTYPE: + mode |= S_IFIFO; + break; + case AREGTYPE: + if (t->th_buf.name[strlen(t->th_buf.name) - 1] == '/') + { + mode |= S_IFDIR; + break; + } + /* FALLTHROUGH */ + case LNKTYPE: + case REGTYPE: + default: + mode |= S_IFREG; + } + } + + return mode; +} + + diff --git a/libtar/dirname.c b/libtar/dirname.c new file mode 100644 index 000000000..986db4aa1 --- /dev/null +++ b/libtar/dirname.c @@ -0,0 +1,77 @@ +/* $OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */ + +/* + * Copyright (c) 1997 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef lint +static char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; +#endif /* not lint */ + +#include +#include +#include + +char * +openbsd_dirname(path) + const char *path; +{ + static char bname[MAXPATHLEN]; + register const char *endp; + + /* Empty or NULL string gets treated as "." */ + if (path == NULL || *path == '\0') { + (void)strcpy(bname, "."); + return(bname); + } + + /* Strip trailing slashes */ + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') + endp--; + + /* Find the start of the dir */ + while (endp > path && *endp != '/') + endp--; + + /* Either the dir is "/" or there are no slashes */ + if (endp == path) { + (void)strcpy(bname, *endp == '/' ? "/" : "."); + return(bname); + } else { + do { + endp--; + } while (endp > path && *endp == '/'); + } + + if (endp - path + 1 > sizeof(bname)) { + errno = ENAMETOOLONG; + return(NULL); + } + (void)strncpy(bname, path, endp - path + 1); + bname[endp - path + 1] = '\0'; + return(bname); +} diff --git a/libtar/encode.c b/libtar/encode.c new file mode 100644 index 000000000..662eff584 --- /dev/null +++ b/libtar/encode.c @@ -0,0 +1,213 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** encode.c - libtar code to encode tar header blocks +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include +#include + +#ifdef STDC_HEADERS +# include +# include +#endif + + +/* magic, version, and checksum */ +void +th_finish(TAR *t) +{ + if (t->options & TAR_GNU) + { + /* we're aiming for this result, but must do it in + * two calls to avoid FORTIFY segfaults on some Linux + * systems: + * strncpy(t->th_buf.magic, "ustar ", 8); + */ + strncpy(t->th_buf.magic, "ustar ", 6); + strncpy(t->th_buf.version, " ", 2); + } + else + { + strncpy(t->th_buf.version, TVERSION, TVERSLEN); + strncpy(t->th_buf.magic, TMAGIC, TMAGLEN); + } + + int_to_oct(th_crc_calc(t), t->th_buf.chksum, 8); +} + + +/* map a file mode to a typeflag */ +void +th_set_type(TAR *t, mode_t mode) +{ + if (S_ISLNK(mode)) + t->th_buf.typeflag = SYMTYPE; + if (S_ISREG(mode)) + t->th_buf.typeflag = REGTYPE; + if (S_ISDIR(mode)) + t->th_buf.typeflag = DIRTYPE; + if (S_ISCHR(mode)) + t->th_buf.typeflag = CHRTYPE; + if (S_ISBLK(mode)) + t->th_buf.typeflag = BLKTYPE; + if (S_ISFIFO(mode) || S_ISSOCK(mode)) + t->th_buf.typeflag = FIFOTYPE; +} + + +/* encode file path */ +void +th_set_path(TAR *t, char *pathname) +{ + char suffix[2] = ""; + char *tmp; + +#ifdef DEBUG + printf("in th_set_path(th, pathname=\"%s\")\n", pathname); +#endif + + if (t->th_buf.gnu_longname != NULL) + free(t->th_buf.gnu_longname); + t->th_buf.gnu_longname = NULL; + + if (pathname[strlen(pathname) - 1] != '/' && TH_ISDIR(t)) + strcpy(suffix, "/"); + + if (strlen(pathname) > T_NAMELEN-1 && (t->options & TAR_GNU)) + { + /* GNU-style long name */ + t->th_buf.gnu_longname = strdup(pathname); + strncpy(t->th_buf.name, t->th_buf.gnu_longname, T_NAMELEN); + } + else if (strlen(pathname) > T_NAMELEN) + { + /* POSIX-style prefix field */ + tmp = strchr(&(pathname[strlen(pathname) - T_NAMELEN - 1]), '/'); + if (tmp == NULL) + { + printf("!!! '/' not found in \"%s\"\n", pathname); + return; + } + snprintf(t->th_buf.name, 100, "%s%s", &(tmp[1]), suffix); + snprintf(t->th_buf.prefix, + ((tmp - pathname + 1) < + 155 ? (tmp - pathname + 1) : 155), "%s", pathname); + } + else + /* classic tar format */ + snprintf(t->th_buf.name, 100, "%s%s", pathname, suffix); + +#ifdef DEBUG + puts("returning from th_set_path()..."); +#endif +} + + +/* encode link path */ +void +th_set_link(TAR *t, char *linkname) +{ +#ifdef DEBUG + printf("==> th_set_link(th, linkname=\"%s\")\n", linkname); +#endif + + if (strlen(linkname) > T_NAMELEN-1 && (t->options & TAR_GNU)) + { + /* GNU longlink format */ + t->th_buf.gnu_longlink = strdup(linkname); + strcpy(t->th_buf.linkname, "././@LongLink"); + } + else + { + /* classic tar format */ + strlcpy(t->th_buf.linkname, linkname, + sizeof(t->th_buf.linkname)); + if (t->th_buf.gnu_longlink != NULL) + free(t->th_buf.gnu_longlink); + t->th_buf.gnu_longlink = NULL; + } +} + + +/* encode device info */ +void +th_set_device(TAR *t, dev_t device) +{ +#ifdef DEBUG + printf("th_set_device(): major = %d, minor = %d\n", + major(device), minor(device)); +#endif + int_to_oct(major(device), t->th_buf.devmajor, 8); + int_to_oct(minor(device), t->th_buf.devminor, 8); +} + + +/* encode user info */ +void +th_set_user(TAR *t, uid_t uid) +{ + struct passwd *pw; + + pw = getpwuid(uid); + if (pw != NULL) + strlcpy(t->th_buf.uname, pw->pw_name, sizeof(t->th_buf.uname)); + + int_to_oct(uid, t->th_buf.uid, 8); +} + + +/* encode group info */ +void +th_set_group(TAR *t, gid_t gid) +{ + struct group *gr; + + gr = getgrgid(gid); + if (gr != NULL) + strlcpy(t->th_buf.gname, gr->gr_name, sizeof(t->th_buf.gname)); + + int_to_oct(gid, t->th_buf.gid, 8); +} + + +/* encode file mode */ +void +th_set_mode(TAR *t, mode_t fmode) +{ + if (S_ISSOCK(fmode)) + { + fmode &= ~S_IFSOCK; + fmode |= S_IFIFO; + } + int_to_oct(fmode, (t)->th_buf.mode, 8); +} + + +void +th_set_from_stat(TAR *t, struct stat *s) +{ + th_set_type(t, s->st_mode); + if (S_ISCHR(s->st_mode) || S_ISBLK(s->st_mode)) + th_set_device(t, s->st_rdev); + th_set_user(t, s->st_uid); + th_set_group(t, s->st_gid); + th_set_mode(t, s->st_mode); + th_set_mtime(t, s->st_mtime); + if (S_ISREG(s->st_mode)) + th_set_size(t, s->st_size); + else + th_set_size(t, 0); +} + + diff --git a/libtar/extract.c b/libtar/extract.c new file mode 100644 index 000000000..554428715 --- /dev/null +++ b/libtar/extract.c @@ -0,0 +1,551 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** extract.c - libtar code to extract a file from a tar archive +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include +#include +#include +#include + +#define DEBUG +#ifdef STDC_HEADERS +# include +#endif + +#ifdef HAVE_UNISTD_H +# include +#endif + +#define DEBUG + +static int +tar_set_file_perms(TAR *t, char *realname) +{ + mode_t mode; + uid_t uid; + gid_t gid; + struct utimbuf ut; + char *filename; + + filename = (realname ? realname : th_get_pathname(t)); + mode = th_get_mode(t); + uid = th_get_uid(t); + gid = th_get_gid(t); + ut.modtime = ut.actime = th_get_mtime(t); + + /* change owner/group */ + if (geteuid() == 0) +#ifdef HAVE_LCHOWN + if (lchown(filename, uid, gid) == -1) + { +# ifdef DEBUG + fprintf(stderr, "lchown(\"%s\", %d, %d): %s\n", + filename, uid, gid, strerror(errno)); +# endif +#else /* ! HAVE_LCHOWN */ + if (!TH_ISSYM(t) && chown(filename, uid, gid) == -1) + { +# ifdef DEBUG + fprintf(stderr, "chown(\"%s\", %d, %d): %s\n", + filename, uid, gid, strerror(errno)); +# endif +#endif /* HAVE_LCHOWN */ + return -1; + } + + /* change access/modification time */ + if (!TH_ISSYM(t) && utime(filename, &ut) == -1) + { +#ifdef DEBUG + perror("utime()"); +#endif + return -1; + } + + /* change permissions */ + if (!TH_ISSYM(t) && chmod(filename, mode) == -1) + { +#ifdef DEBUG + perror("chmod()"); +#endif + return -1; + } + + return 0; +} + + +/* switchboard */ +int +tar_extract_file(TAR *t, char *realname) +{ + int i; + char *lnp; + int pathname_len; + int realname_len; + + if (t->options & TAR_NOOVERWRITE) + { + struct stat s; + + if (lstat(realname, &s) == 0 || errno != ENOENT) + { + errno = EEXIST; + return -1; + } + } + + if (TH_ISDIR(t)) + { + i = tar_extract_dir(t, realname); + if (i == 1) + i = 0; + } + else if (TH_ISLNK(t)) { + printf("link\n"); + i = tar_extract_hardlink(t, realname); + } + else if (TH_ISSYM(t)) { + printf("sym\n"); + i = tar_extract_symlink(t, realname); + } + else if (TH_ISCHR(t)) { + printf("chr\n"); + i = tar_extract_chardev(t, realname); + } + else if (TH_ISBLK(t)) { + printf("blk\n"); + i = tar_extract_blockdev(t, realname); + } + else if (TH_ISFIFO(t)) { + printf("fifo\n"); + i = tar_extract_fifo(t, realname); + } + else /* if (TH_ISREG(t)) */ { + printf("reg\n"); + i = tar_extract_regfile(t, realname); + } + + if (i != 0) { + printf("here i: %d\n", i); + return i; + } + + i = tar_set_file_perms(t, realname); + if (i != 0) { + printf("i: %d\n", i); + return i; + } +/* + pathname_len = strlen(th_get_pathname(t)) + 1; + realname_len = strlen(realname) + 1; + lnp = (char *)calloc(1, pathname_len + realname_len); + if (lnp == NULL) + return -1; + strcpy(&lnp[0], th_get_pathname(t)); + strcpy(&lnp[pathname_len], realname); +#ifdef DEBUG + printf("tar_extract_file(): calling libtar_hash_add(): key=\"%s\", " + "value=\"%s\"\n", th_get_pathname(t), realname); +#endif + if (libtar_hash_add(t->h, lnp) != 0) + return -1; + free(lnp); +*/ + return 0; +} + + +/* extract regular file */ +int +tar_extract_regfile(TAR *t, char *realname) +{ + mode_t mode; + size_t size; + uid_t uid; + gid_t gid; + int fdout; + int i, k; + char buf[T_BLOCKSIZE]; + char *filename; + + fflush(NULL); +#ifdef DEBUG + printf("==> tar_extract_regfile(t=0x%lx, realname=\"%s\")\n", t, + realname); +#endif + + if (!TH_ISREG(t)) + { + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + mode = th_get_mode(t); + size = th_get_size(t); + uid = th_get_uid(t); + gid = th_get_gid(t); + + if (mkdirhier(dirname(filename)) == -1) + return -1; + +#ifdef DEBUG + printf(" ==> extracting: %s (mode %04o, uid %d, gid %d, %d bytes)\n", + filename, mode, uid, gid, size); +#endif + fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC +#ifdef O_BINARY + | O_BINARY +#endif + , 0666); + if (fdout == -1) + { +#ifdef DEBUG + perror("open()"); +#endif + return -1; + } + +#if 0 + /* change the owner. (will only work if run as root) */ + if (fchown(fdout, uid, gid) == -1 && errno != EPERM) + { +#ifdef DEBUG + perror("fchown()"); +#endif + return -1; + } + + /* make sure the mode isn't inheritted from a file we're overwriting */ + if (fchmod(fdout, mode & 07777) == -1) + { +#ifdef DEBUG + perror("fchmod()"); +#endif + return -1; + } +#endif + + /* extract the file */ + for (i = size; i > 0; i -= T_BLOCKSIZE) + { + k = tar_block_read(t, buf); + if (k != T_BLOCKSIZE) + { + if (k != -1) + errno = EINVAL; + return -1; + } + + /* write block to output file */ + if (write(fdout, buf, + ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1) + return -1; + } + + /* close output file */ + if (close(fdout) == -1) + return -1; + +#ifdef DEBUG + printf("### done extracting %s\n", filename); +#endif + + return 0; +} + + +/* skip regfile */ +int +tar_skip_regfile(TAR *t) +{ + int i, k; + size_t size; + char buf[T_BLOCKSIZE]; + + if (!TH_ISREG(t)) + { + errno = EINVAL; + return -1; + } + + size = th_get_size(t); + for (i = size; i > 0; i -= T_BLOCKSIZE) + { + k = tar_block_read(t, buf); + if (k != T_BLOCKSIZE) + { + if (k != -1) + errno = EINVAL; + return -1; + } + } + + return 0; +} + + +/* hardlink */ +int +tar_extract_hardlink(TAR * t, char *realname) +{ + char *filename; + char *linktgt = NULL; + char *lnp; + libtar_hashptr_t hp; + + if (!TH_ISLNK(t)) + { + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + if (mkdirhier(dirname(filename)) == -1) + return -1; + libtar_hashptr_reset(&hp); + if (libtar_hash_getkey(t->h, &hp, th_get_linkname(t), + (libtar_matchfunc_t)libtar_str_match) != 0) + { + lnp = (char *)libtar_hashptr_data(&hp); + linktgt = &lnp[strlen(lnp) + 1]; + } + else + linktgt = th_get_linkname(t); + +#ifdef DEBUG + printf(" ==> extracting: %s (link to %s)\n", filename, linktgt); +#endif + if (link(linktgt, filename) == -1) + { +#ifdef DEBUG + perror("link()"); +#endif + return -1; + } + + return 0; +} + + +/* symlink */ +int +tar_extract_symlink(TAR *t, char *realname) +{ + char *filename; + + if (!TH_ISSYM(t)) + { + printf("not a sym\n"); + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + printf("file: %s\n", filename); + if (mkdirhier(dirname(filename)) == -1) { + printf("mkdirhier\n"); + return -1; + } + + if (unlink(filename) == -1 && errno != ENOENT) { + printf("unlink\n"); + return -1; + } + +#ifdef DEBUG + printf(" ==> extracting: %s (symlink to %s)\n", + filename, th_get_linkname(t)); +#endif + if (symlink(th_get_linkname(t), filename) == -1) + { +#ifdef DEBUG + perror("symlink()"); +#endif + return -1; + } + + return 0; +} + + +/* character device */ +int +tar_extract_chardev(TAR *t, char *realname) +{ + mode_t mode; + unsigned long devmaj, devmin; + char *filename; + + if (!TH_ISCHR(t)) + { + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + mode = th_get_mode(t); + devmaj = th_get_devmajor(t); + devmin = th_get_devminor(t); + + if (mkdirhier(dirname(filename)) == -1) + return -1; + +#ifdef DEBUG + printf(" ==> extracting: %s (character device %ld,%ld)\n", + filename, devmaj, devmin); +#endif + if (mknod(filename, mode | S_IFCHR, + compat_makedev(devmaj, devmin)) == -1) + { +#ifdef DEBUG + perror("mknod()"); +#endif + return -1; + } + + return 0; +} + + +/* block device */ +int +tar_extract_blockdev(TAR *t, char *realname) +{ + mode_t mode; + unsigned long devmaj, devmin; + char *filename; + + if (!TH_ISBLK(t)) + { + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + mode = th_get_mode(t); + devmaj = th_get_devmajor(t); + devmin = th_get_devminor(t); + + if (mkdirhier(dirname(filename)) == -1) + return -1; + +#ifdef DEBUG + printf(" ==> extracting: %s (block device %ld,%ld)\n", + filename, devmaj, devmin); +#endif + if (mknod(filename, mode | S_IFBLK, + compat_makedev(devmaj, devmin)) == -1) + { +#ifdef DEBUG + perror("mknod()"); +#endif + return -1; + } + + return 0; +} + + +/* directory */ +int +tar_extract_dir(TAR *t, char *realname) +{ + mode_t mode; + char *filename; + printf("filename: %s\n", filename); + if (!TH_ISDIR(t)) + { + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + mode = th_get_mode(t); + + if (mkdirhier(dirname(filename)) == -1) + return -1; + +#ifdef DEBUG + printf(" ==> extracting: %s (mode %04o, directory)\n", filename, + mode); +#endif + if (mkdir(filename, mode) == -1) + { + if (errno == EEXIST) + { + if (chmod(filename, mode) == -1) + { +#ifdef DEBUG + perror("chmod()"); +#endif + return -1; + } + else + { +#ifdef DEBUG + puts(" *** using existing directory"); +#endif + return 1; + } + } + else + { +#ifdef DEBUG + perror("mkdir()"); +#endif + return -1; + } + } + + return 0; +} + + +/* FIFO */ +int +tar_extract_fifo(TAR *t, char *realname) +{ + mode_t mode; + char *filename; + + if (!TH_ISFIFO(t)) + { + errno = EINVAL; + return -1; + } + + filename = (realname ? realname : th_get_pathname(t)); + mode = th_get_mode(t); + + if (mkdirhier(dirname(filename)) == -1) + return -1; + +#ifdef DEBUG + printf(" ==> extracting: %s (fifo)\n", filename); +#endif + if (mkfifo(filename, mode) == -1) + { +#ifdef DEBUG + perror("mkfifo()"); +#endif + return -1; + } + + return 0; +} + + diff --git a/libtar/fnmatch.c b/libtar/fnmatch.c new file mode 100644 index 000000000..fe75f0eeb --- /dev/null +++ b/libtar/fnmatch.c @@ -0,0 +1,237 @@ +/* $OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $ */ + +/* + * Copyright (c) 1989, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Guido van Rossum. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)fnmatch.c 8.2 (Berkeley) 4/16/94"; +#else +static char rcsid[] = "$OpenBSD: fnmatch.c,v 1.6 1998/03/19 00:29:59 millert Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * Function fnmatch() as specified in POSIX 1003.2-1992, section B.6. + * Compares a filename or pathname to a pattern. + */ + +#include + +#include + +#ifdef STDC_HEADERS +# include +#endif + +#ifdef HAVE_CTYPE_H +# include +#endif + +#include + + +#define EOS '\0' + +#define RANGE_MATCH 1 +#define RANGE_NOMATCH 0 +#define RANGE_ERROR (-1) + +#ifdef NO_IBM_COMPILER_HORKAGE +static int rangematch (const char *, char, int, char **); +#else +static int rangematch (); +#endif + +int +fnmatch(pattern, string, flags) + const char *pattern, *string; + int flags; +{ + const char *stringstart; + char *newp; + char c, test; + + for (stringstart = string;;) + switch (c = *pattern++) { + case EOS: + if ((flags & FNM_LEADING_DIR) && *string == '/') + return (0); + return (*string == EOS ? 0 : FNM_NOMATCH); + case '?': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + ++string; + break; + case '*': + c = *pattern; + /* Collapse multiple stars. */ + while (c == '*') + c = *++pattern; + + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + /* Optimize for pattern with * at end or before /. */ + if (c == EOS) { + if (flags & FNM_PATHNAME) + return ((flags & FNM_LEADING_DIR) || + strchr(string, '/') == NULL ? + 0 : FNM_NOMATCH); + else + return (0); + } else if (c == '/' && (flags & FNM_PATHNAME)) { + if ((string = strchr(string, '/')) == NULL) + return (FNM_NOMATCH); + break; + } + + /* General case, use recursion. */ + while ((test = *string) != EOS) { + if (!fnmatch(pattern, string, flags & ~FNM_PERIOD)) + return (0); + if (test == '/' && (flags & FNM_PATHNAME)) + break; + ++string; + } + return (FNM_NOMATCH); + case '[': + if (*string == EOS) + return (FNM_NOMATCH); + if (*string == '/' && (flags & FNM_PATHNAME)) + return (FNM_NOMATCH); + if (*string == '.' && (flags & FNM_PERIOD) && + (string == stringstart || + ((flags & FNM_PATHNAME) && *(string - 1) == '/'))) + return (FNM_NOMATCH); + + switch (rangematch(pattern, *string, flags, &newp)) { + case RANGE_ERROR: + /* not a good range, treat as normal text */ + goto normal; + case RANGE_MATCH: + pattern = newp; + break; + case RANGE_NOMATCH: + return (FNM_NOMATCH); + } + ++string; + break; + case '\\': + if (!(flags & FNM_NOESCAPE)) { + if ((c = *pattern++) == EOS) { + c = '\\'; + --pattern; + } + } + /* FALLTHROUGH */ + default: + normal: + if (c != *string && !((flags & FNM_CASEFOLD) && + (tolower((unsigned char)c) == + tolower((unsigned char)*string)))) + return (FNM_NOMATCH); + ++string; + break; + } + /* NOTREACHED */ +} + +static int +rangematch(pattern, test, flags, newp) + const char *pattern; + char test; + int flags; + char **newp; +{ + int negate, ok; + char c, c2; + + /* + * A bracket expression starting with an unquoted circumflex + * character produces unspecified results (IEEE 1003.2-1992, + * 3.13.2). This implementation treats it like '!', for + * consistency with the regular expression syntax. + * J.T. Conklin (conklin@ngai.kaleida.com) + */ + if ((negate = (*pattern == '!' || *pattern == '^'))) + ++pattern; + + if (flags & FNM_CASEFOLD) + test = tolower((unsigned char)test); + + /* + * A right bracket shall lose its special meaning and represent + * itself in a bracket expression if it occurs first in the list. + * -- POSIX.2 2.8.3.2 + */ + ok = 0; + c = *pattern++; + do { + if (c == '\\' && !(flags & FNM_NOESCAPE)) + c = *pattern++; + if (c == EOS) + return (RANGE_ERROR); + if (c == '/' && (flags & FNM_PATHNAME)) + return (RANGE_NOMATCH); + if ((flags & FNM_CASEFOLD)) + c = tolower((unsigned char)c); + if (*pattern == '-' + && (c2 = *(pattern+1)) != EOS && c2 != ']') { + pattern += 2; + if (c2 == '\\' && !(flags & FNM_NOESCAPE)) + c2 = *pattern++; + if (c2 == EOS) + return (RANGE_ERROR); + if (flags & FNM_CASEFOLD) + c2 = tolower((unsigned char)c2); + if (c <= test && test <= c2) + ok = 1; + } else if (c == test) + ok = 1; + } while ((c = *pattern++) != ']'); + + *newp = (char *)pattern; + return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); +} diff --git a/libtar/gethostbyname_r.c b/libtar/gethostbyname_r.c new file mode 100644 index 000000000..5264b8444 --- /dev/null +++ b/libtar/gethostbyname_r.c @@ -0,0 +1,41 @@ +/* +** Copyright 2002 University of Illinois Board of Trustees +** Copyright 2002 Mark D. Roth +** All rights reserved. +** +** gethostbyname_r.c - gethostbyname_r() function for compatibility library +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include + + +int +compat_gethostbyname_r(const char *name, struct hostent *hp, + char *buf, size_t buflen, + struct hostent **hpp, int *herr) +{ +#if GETHOSTBYNAME_R_NUM_ARGS == 5 + *hpp = gethostbyname_r(name, hp, buf, buflen, herr); + + if (*hpp == NULL) + return -1; + return 0; +#elif GETHOSTBYNAME_R_NUM_ARGS == 3 + struct hostent_data hdata; + + if (gethostbyname_r(name, hp, &hdata) == -1) + return -1; + *hpp = hp; + return 0; +#endif /* GETHOSTBYNAME_R_NUM_ARGS == 5 */ +} + + diff --git a/libtar/gethostname.c b/libtar/gethostname.c new file mode 100644 index 000000000..1abaae124 --- /dev/null +++ b/libtar/gethostname.c @@ -0,0 +1,36 @@ +/* gethostname.c: minimal substitute for missing gethostname() function + * created 2000-Mar-02 jmk + * requires SVR4 uname() and -lc + * + * by Jim Knoble + * Copyright ? 2000 Jim Knoble + * + * Permission to use, copy, modify, distribute, and sell this software + * and its documentation for any purpose is hereby granted without fee, + * provided that the above copyright notice appear in all copies and + * that both that copyright notice and this permission notice appear in + * supporting documentation. + * + * This software is provided "as is", without warranty of any kind, + * express or implied, including but not limited to the warranties of + * merchantability, fitness for a particular purpose and + * noninfringement. In no event shall the author(s) be liable for any + * claim, damages or other liability, whether in an action of contract, + * tort or otherwise, arising from, out of or in connection with the + * software or the use or other dealings in the software. + */ + +#include +#include + +int gethostname(char *name, size_t len) +{ + struct utsname u; + int status = uname(&u); + if (-1 != status) { + strncpy(name, u.nodename, len); + name[len - 1] = '\0'; + } + return(status); +} + diff --git a/libtar/getservbyname_r.c b/libtar/getservbyname_r.c new file mode 100644 index 000000000..e386bc9f6 --- /dev/null +++ b/libtar/getservbyname_r.c @@ -0,0 +1,41 @@ +/* +** Copyright 2002 University of Illinois Board of Trustees +** Copyright 2002 Mark D. Roth +** All rights reserved. +** +** getservbyname_r.c - getservbyname_r() function for compatibility library +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include + + +int +compat_getservbyname_r(const char *name, const char *proto, + struct servent *sp, char *buf, size_t buflen, + struct servent **spp) +{ +#if GETSERVBYNAME_R_NUM_ARGS == 5 + *spp = getservbyname_r(name, proto, sp, buf, buflen); + + if (*spp == NULL) + return -1; + return 0; +#elif GETSERVBYNAME_R_NUM_ARGS == 4 + struct servent_data sdata; + + if (getservbyname_r(name, proto, sp, &sdata) == -1) + return -1; + *spp = sp; + return 0; +#endif /* GETSERVBYNAME_R_NUM_ARGS == 5 */ +} + + diff --git a/libtar/glob.c b/libtar/glob.c new file mode 100644 index 000000000..9ee235a35 --- /dev/null +++ b/libtar/glob.c @@ -0,0 +1,873 @@ +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Guido van Rossum. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)glob.c 8.3 (Berkeley) 10/13/93"; +#else +static char rcsid[] = "$OpenBSD: glob.c,v 1.8 1998/08/14 21:39:30 deraadt Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +/* + * glob(3) -- a superset of the one defined in POSIX 1003.2. + * + * The [!...] convention to negate a range is supported (SysV, Posix, ksh). + * + * Optional extra services, controlled by flags not defined by POSIX: + * + * GLOB_QUOTE: + * Escaping convention: \ inhibits any special meaning the following + * character might have (except \ at end of string is retained). + * GLOB_MAGCHAR: + * Set in gl_flags if pattern contained a globbing character. + * GLOB_NOMAGIC: + * Same as GLOB_NOCHECK, but it will only append pattern if it did + * not contain any magic characters. [Used in csh style globbing] + * GLOB_ALTDIRFUNC: + * Use alternately specified directory access functions. + * GLOB_TILDE: + * expand ~user/foo to the /home/dir/of/user/foo + * GLOB_BRACE: + * expand {1,2}{a,b} to 1a 1b 2a 2b + * gl_matchc: + * Number of matches in the current invocation of glob. + */ + +#include + +#include +#include + +#include +#include +#include +#include + +#ifdef STDC_HEADERS +# include +# include +#endif + +#ifdef HAVE_UNISTD_H +# include +#endif + +#include + + +#define DOLLAR '$' +#define DOT '.' +#define EOS '\0' +#define LBRACKET '[' +#define NOT '!' +#define QUESTION '?' +#define QUOTE '\\' +#define RANGE '-' +#define RBRACKET ']' +#define SEP '/' +#define STAR '*' +#define TILDE '~' +#define UNDERSCORE '_' +#define LBRACE '{' +#define RBRACE '}' +#define SLASH '/' +#define COMMA ',' + +#ifndef DEBUG + +#define M_QUOTE 0x8000 +#define M_PROTECT 0x4000 +#define M_MASK 0xffff +#define M_ASCII 0x00ff + +typedef u_short Char; + +#else + +#define M_QUOTE 0x80 +#define M_PROTECT 0x40 +#define M_MASK 0xff +#define M_ASCII 0x7f + +typedef char Char; + +#endif + + +#define CHAR(c) ((Char)((c)&M_ASCII)) +#define META(c) ((Char)((c)|M_QUOTE)) +#define M_ALL META('*') +#define M_END META(']') +#define M_NOT META('!') +#define M_ONE META('?') +#define M_RNG META('-') +#define M_SET META('[') +#define ismeta(c) (((c)&M_QUOTE) != 0) + + +static int compare (const void *, const void *); +static void g_Ctoc (const Char *, char *); +static int g_lstat (Char *, struct stat *, glob_t *); +static DIR *g_opendir (Char *, glob_t *); +static Char *g_strchr (Char *, int); +#ifdef notdef +static Char *g_strcat (Char *, const Char *); +#endif +static int g_stat (Char *, struct stat *, glob_t *); +static int glob0 (const Char *, glob_t *); +static int glob1 (Char *, glob_t *); +static int glob2 (Char *, Char *, Char *, glob_t *); +static int glob3 (Char *, Char *, Char *, Char *, glob_t *); +static int globextend (const Char *, glob_t *); +static const Char * globtilde (const Char *, Char *, size_t, glob_t *); +static int globexp1 (const Char *, glob_t *); +static int globexp2 (const Char *, const Char *, glob_t *, int *); +static int match (Char *, Char *, Char *); +#ifdef DEBUG +static void qprintf (const char *, Char *); +#endif + +int +openbsd_glob(pattern, flags, errfunc, pglob) + const char *pattern; + int flags, (*errfunc) __P((const char *, int)); + glob_t *pglob; +{ + const u_char *patnext; + int c; + Char *bufnext, *bufend, patbuf[MAXPATHLEN+1]; + + patnext = (u_char *) pattern; + if (!(flags & GLOB_APPEND)) { + pglob->gl_pathc = 0; + pglob->gl_pathv = NULL; + if (!(flags & GLOB_DOOFFS)) + pglob->gl_offs = 0; + } + pglob->gl_flags = flags & ~GLOB_MAGCHAR; + pglob->gl_errfunc = errfunc; + pglob->gl_matchc = 0; + + bufnext = patbuf; + bufend = bufnext + MAXPATHLEN; + if (flags & GLOB_NOESCAPE) + while (bufnext < bufend && (c = *patnext++) != EOS) + *bufnext++ = c; + else { + /* Protect the quoted characters. */ + while (bufnext < bufend && (c = *patnext++) != EOS) + if (c == QUOTE) { + if ((c = *patnext++) == EOS) { + c = QUOTE; + --patnext; + } + *bufnext++ = c | M_PROTECT; + } + else + *bufnext++ = c; + } + *bufnext = EOS; + + if (flags & GLOB_BRACE) + return globexp1(patbuf, pglob); + else + return glob0(patbuf, pglob); +} + +/* + * Expand recursively a glob {} pattern. When there is no more expansion + * invoke the standard globbing routine to glob the rest of the magic + * characters + */ +static int globexp1(pattern, pglob) + const Char *pattern; + glob_t *pglob; +{ + const Char* ptr = pattern; + int rv; + + /* Protect a single {}, for find(1), like csh */ + if (pattern[0] == LBRACE && pattern[1] == RBRACE && pattern[2] == EOS) + return glob0(pattern, pglob); + + while ((ptr = (const Char *) g_strchr((Char *) ptr, LBRACE)) != NULL) + if (!globexp2(ptr, pattern, pglob, &rv)) + return rv; + + return glob0(pattern, pglob); +} + + +/* + * Recursive brace globbing helper. Tries to expand a single brace. + * If it succeeds then it invokes globexp1 with the new pattern. + * If it fails then it tries to glob the rest of the pattern and returns. + */ +static int globexp2(ptr, pattern, pglob, rv) + const Char *ptr, *pattern; + glob_t *pglob; + int *rv; +{ + int i; + Char *lm, *ls; + const Char *pe, *pm, *pl; + Char patbuf[MAXPATHLEN + 1]; + + /* copy part up to the brace */ + for (lm = patbuf, pm = pattern; pm != ptr; *lm++ = *pm++) + continue; + ls = lm; + + /* Find the balanced brace */ + for (i = 0, pe = ++ptr; *pe; pe++) + if (*pe == LBRACKET) { + /* Ignore everything between [] */ + for (pm = pe++; *pe != RBRACKET && *pe != EOS; pe++) + continue; + if (*pe == EOS) { + /* + * We could not find a matching RBRACKET. + * Ignore and just look for RBRACE + */ + pe = pm; + } + } + else if (*pe == LBRACE) + i++; + else if (*pe == RBRACE) { + if (i == 0) + break; + i--; + } + + /* Non matching braces; just glob the pattern */ + if (i != 0 || *pe == EOS) { + *rv = glob0(patbuf, pglob); + return 0; + } + + for (i = 0, pl = pm = ptr; pm <= pe; pm++) + switch (*pm) { + case LBRACKET: + /* Ignore everything between [] */ + for (pl = pm++; *pm != RBRACKET && *pm != EOS; pm++) + continue; + if (*pm == EOS) { + /* + * We could not find a matching RBRACKET. + * Ignore and just look for RBRACE + */ + pm = pl; + } + break; + + case LBRACE: + i++; + break; + + case RBRACE: + if (i) { + i--; + break; + } + /* FALLTHROUGH */ + case COMMA: + if (i && *pm == COMMA) + break; + else { + /* Append the current string */ + for (lm = ls; (pl < pm); *lm++ = *pl++) + continue; + /* + * Append the rest of the pattern after the + * closing brace + */ + for (pl = pe + 1; (*lm++ = *pl++) != EOS;) + continue; + + /* Expand the current pattern */ +#ifdef DEBUG + qprintf("globexp2:", patbuf); +#endif + *rv = globexp1(patbuf, pglob); + + /* move after the comma, to the next string */ + pl = pm + 1; + } + break; + + default: + break; + } + *rv = 0; + return 0; +} + + + +/* + * expand tilde from the passwd file. + */ +static const Char * +globtilde(pattern, patbuf, patbuf_len, pglob) + const Char *pattern; + Char *patbuf; + size_t patbuf_len; + glob_t *pglob; +{ + struct passwd *pwd; + char *h; + const Char *p; + Char *b, *eb; + + if (*pattern != TILDE || !(pglob->gl_flags & GLOB_TILDE)) + return pattern; + + /* Copy up to the end of the string or / */ + eb = &patbuf[patbuf_len - 1]; + for (p = pattern + 1, h = (char *) patbuf; + h < (char *)eb && *p && *p != SLASH; *h++ = *p++) + continue; + + *h = EOS; + + if (((char *) patbuf)[0] == EOS) { + /* + * handle a plain ~ or ~/ by expanding $HOME + * first and then trying the password file + */ +#ifdef HAVE_ISSETUGID + if (issetugid() != 0 || (h = getenv("HOME")) == NULL) { +#endif + if ((pwd = getpwuid(getuid())) == NULL) + return pattern; + else + h = pwd->pw_dir; +#ifdef HAVE_ISSETUGID + } +#endif + } + else { + /* + * Expand a ~user + */ + if ((pwd = getpwnam((char*) patbuf)) == NULL) + return pattern; + else + h = pwd->pw_dir; + } + + /* Copy the home directory */ + for (b = patbuf; b < eb && *h; *b++ = *h++) + continue; + + /* Append the rest of the pattern */ + while (b < eb && (*b++ = *p++) != EOS) + continue; + *b = EOS; + + return patbuf; +} + + +/* + * The main glob() routine: compiles the pattern (optionally processing + * quotes), calls glob1() to do the real pattern matching, and finally + * sorts the list (unless unsorted operation is requested). Returns 0 + * if things went well, nonzero if errors occurred. It is not an error + * to find no matches. + */ +static int +glob0(pattern, pglob) + const Char *pattern; + glob_t *pglob; +{ + const Char *qpatnext; + int c, err, oldpathc; + Char *bufnext, patbuf[MAXPATHLEN+1]; + + qpatnext = globtilde(pattern, patbuf, sizeof(patbuf) / sizeof(Char), + pglob); + oldpathc = pglob->gl_pathc; + bufnext = patbuf; + + /* We don't need to check for buffer overflow any more. */ + while ((c = *qpatnext++) != EOS) { + switch (c) { + case LBRACKET: + c = *qpatnext; + if (c == NOT) + ++qpatnext; + if (*qpatnext == EOS || + g_strchr((Char *) qpatnext+1, RBRACKET) == NULL) { + *bufnext++ = LBRACKET; + if (c == NOT) + --qpatnext; + break; + } + *bufnext++ = M_SET; + if (c == NOT) + *bufnext++ = M_NOT; + c = *qpatnext++; + do { + *bufnext++ = CHAR(c); + if (*qpatnext == RANGE && + (c = qpatnext[1]) != RBRACKET) { + *bufnext++ = M_RNG; + *bufnext++ = CHAR(c); + qpatnext += 2; + } + } while ((c = *qpatnext++) != RBRACKET); + pglob->gl_flags |= GLOB_MAGCHAR; + *bufnext++ = M_END; + break; + case QUESTION: + pglob->gl_flags |= GLOB_MAGCHAR; + *bufnext++ = M_ONE; + break; + case STAR: + pglob->gl_flags |= GLOB_MAGCHAR; + /* collapse adjacent stars to one, + * to avoid exponential behavior + */ + if (bufnext == patbuf || bufnext[-1] != M_ALL) + *bufnext++ = M_ALL; + break; + default: + *bufnext++ = CHAR(c); + break; + } + } + *bufnext = EOS; +#ifdef DEBUG + qprintf("glob0:", patbuf); +#endif + + if ((err = glob1(patbuf, pglob)) != 0) + return(err); + + /* + * If there was no match we are going to append the pattern + * if GLOB_NOCHECK was specified or if GLOB_NOMAGIC was specified + * and the pattern did not contain any magic characters + * GLOB_NOMAGIC is there just for compatibility with csh. + */ + if (pglob->gl_pathc == oldpathc) { + if ((pglob->gl_flags & GLOB_NOCHECK) || + ((pglob->gl_flags & GLOB_NOMAGIC) && + !(pglob->gl_flags & GLOB_MAGCHAR))) + return(globextend(pattern, pglob)); + else + return(GLOB_NOMATCH); + } + if (!(pglob->gl_flags & GLOB_NOSORT)) + qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, + pglob->gl_pathc - oldpathc, sizeof(char *), compare); + return(0); +} + +static int +compare(p, q) + const void *p, *q; +{ + return(strcmp(*(char **)p, *(char **)q)); +} + +static int +glob1(pattern, pglob) + Char *pattern; + glob_t *pglob; +{ + Char pathbuf[MAXPATHLEN+1]; + + /* A null pathname is invalid -- POSIX 1003.1 sect. 2.4. */ + if (*pattern == EOS) + return(0); + return(glob2(pathbuf, pathbuf, pattern, pglob)); +} + +/* + * The functions glob2 and glob3 are mutually recursive; there is one level + * of recursion for each segment in the pattern that contains one or more + * meta characters. + */ +static int +glob2(pathbuf, pathend, pattern, pglob) + Char *pathbuf, *pathend, *pattern; + glob_t *pglob; +{ + struct stat sb; + Char *p, *q; + int anymeta; + + /* + * Loop over pattern segments until end of pattern or until + * segment with meta character found. + */ + for (anymeta = 0;;) { + if (*pattern == EOS) { /* End of pattern? */ + *pathend = EOS; + if (g_lstat(pathbuf, &sb, pglob)) + return(0); + + if (((pglob->gl_flags & GLOB_MARK) && + pathend[-1] != SEP) && (S_ISDIR(sb.st_mode) + || (S_ISLNK(sb.st_mode) && + (g_stat(pathbuf, &sb, pglob) == 0) && + S_ISDIR(sb.st_mode)))) { + *pathend++ = SEP; + *pathend = EOS; + } + ++pglob->gl_matchc; + return(globextend(pathbuf, pglob)); + } + + /* Find end of next segment, copy tentatively to pathend. */ + q = pathend; + p = pattern; + while (*p != EOS && *p != SEP) { + if (ismeta(*p)) + anymeta = 1; + *q++ = *p++; + } + + if (!anymeta) { /* No expansion, do next segment. */ + pathend = q; + pattern = p; + while (*pattern == SEP) + *pathend++ = *pattern++; + } else /* Need expansion, recurse. */ + return(glob3(pathbuf, pathend, pattern, p, pglob)); + } + /* NOTREACHED */ +} + +static int +glob3(pathbuf, pathend, pattern, restpattern, pglob) + Char *pathbuf, *pathend, *pattern, *restpattern; + glob_t *pglob; +{ + register struct dirent *dp; + DIR *dirp; + int err; + char buf[MAXPATHLEN]; + + /* + * The readdirfunc declaration can't be prototyped, because it is + * assigned, below, to two functions which are prototyped in glob.h + * and dirent.h as taking pointers to differently typed opaque + * structures. + */ + struct dirent *(*readdirfunc)(); + + *pathend = EOS; + errno = 0; + + if ((dirp = g_opendir(pathbuf, pglob)) == NULL) { + /* TODO: don't call for ENOENT or ENOTDIR? */ + if (pglob->gl_errfunc) { + g_Ctoc(pathbuf, buf); + if (pglob->gl_errfunc(buf, errno) || + pglob->gl_flags & GLOB_ERR) + return (GLOB_ABORTED); + } + return(0); + } + + err = 0; + + /* Search directory for matching names. */ + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + readdirfunc = pglob->gl_readdir; + else + readdirfunc = readdir; + while ((dp = (*readdirfunc)(dirp))) { + register u_char *sc; + register Char *dc; + + /* Initial DOT must be matched literally. */ + if (dp->d_name[0] == DOT && *pattern != DOT) + continue; + for (sc = (u_char *) dp->d_name, dc = pathend; + (*dc++ = *sc++) != EOS;) + continue; + if (!match(pathend, pattern, restpattern)) { + *pathend = EOS; + continue; + } + err = glob2(pathbuf, --dc, restpattern, pglob); + if (err) + break; + } + + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + (*pglob->gl_closedir)(dirp); + else + closedir(dirp); + return(err); +} + + +/* + * Extend the gl_pathv member of a glob_t structure to accomodate a new item, + * add the new item, and update gl_pathc. + * + * This assumes the BSD realloc, which only copies the block when its size + * crosses a power-of-two boundary; for v7 realloc, this would cause quadratic + * behavior. + * + * Return 0 if new item added, error code if memory couldn't be allocated. + * + * Invariant of the glob_t structure: + * Either gl_pathc is zero and gl_pathv is NULL; or gl_pathc > 0 and + * gl_pathv points to (gl_offs + gl_pathc + 1) items. + */ +static int +globextend(path, pglob) + const Char *path; + glob_t *pglob; +{ + register char **pathv; + register int i; + u_int newsize; + char *copy; + const Char *p; + + newsize = sizeof(*pathv) * (2 + pglob->gl_pathc + pglob->gl_offs); + pathv = pglob->gl_pathv ? + realloc((char *)pglob->gl_pathv, newsize) : + malloc(newsize); + if (pathv == NULL) { + if (pglob->gl_pathv) + free(pglob->gl_pathv); + return(GLOB_NOSPACE); + } + + if (pglob->gl_pathv == NULL && pglob->gl_offs > 0) { + /* first time around -- clear initial gl_offs items */ + pathv += pglob->gl_offs; + for (i = pglob->gl_offs; --i >= 0; ) + *--pathv = NULL; + } + pglob->gl_pathv = pathv; + + for (p = path; *p++;) + continue; + if ((copy = malloc(p - path)) != NULL) { + g_Ctoc(path, copy); + pathv[pglob->gl_offs + pglob->gl_pathc++] = copy; + } + pathv[pglob->gl_offs + pglob->gl_pathc] = NULL; + return(copy == NULL ? GLOB_NOSPACE : 0); +} + + +/* + * pattern matching function for filenames. Each occurrence of the * + * pattern causes a recursion level. + */ +static int +match(name, pat, patend) + register Char *name, *pat, *patend; +{ + int ok, negate_range; + Char c, k; + + while (pat < patend) { + c = *pat++; + switch (c & M_MASK) { + case M_ALL: + if (pat == patend) + return(1); + do + if (match(name, pat, patend)) + return(1); + while (*name++ != EOS); + return(0); + case M_ONE: + if (*name++ == EOS) + return(0); + break; + case M_SET: + ok = 0; + if ((k = *name++) == EOS) + return(0); + if ((negate_range = ((*pat & M_MASK) == M_NOT)) != EOS) + ++pat; + while (((c = *pat++) & M_MASK) != M_END) + if ((*pat & M_MASK) == M_RNG) { + if (c <= k && k <= pat[1]) + ok = 1; + pat += 2; + } else if (c == k) + ok = 1; + if (ok == negate_range) + return(0); + break; + default: + if (*name++ != c) + return(0); + break; + } + } + return(*name == EOS); +} + +/* Free allocated data belonging to a glob_t structure. */ +void +openbsd_globfree(pglob) + glob_t *pglob; +{ + register int i; + register char **pp; + + if (pglob->gl_pathv != NULL) { + pp = pglob->gl_pathv + pglob->gl_offs; + for (i = pglob->gl_pathc; i--; ++pp) + if (*pp) + free(*pp); + free(pglob->gl_pathv); + } +} + +static DIR * +g_opendir(str, pglob) + register Char *str; + glob_t *pglob; +{ + char buf[MAXPATHLEN]; + + if (!*str) + strcpy(buf, "."); + else + g_Ctoc(str, buf); + + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return((*pglob->gl_opendir)(buf)); + + return(opendir(buf)); +} + +static int +g_lstat(fn, sb, pglob) + register Char *fn; + struct stat *sb; + glob_t *pglob; +{ + char buf[MAXPATHLEN]; + + g_Ctoc(fn, buf); + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return((*pglob->gl_lstat)(buf, sb)); + return(lstat(buf, sb)); +} + +static int +g_stat(fn, sb, pglob) + register Char *fn; + struct stat *sb; + glob_t *pglob; +{ + char buf[MAXPATHLEN]; + + g_Ctoc(fn, buf); + if (pglob->gl_flags & GLOB_ALTDIRFUNC) + return((*pglob->gl_stat)(buf, sb)); + return(stat(buf, sb)); +} + +static Char * +g_strchr(str, ch) + Char *str; + int ch; +{ + do { + if (*str == ch) + return (str); + } while (*str++); + return (NULL); +} + +#ifdef notdef +static Char * +g_strcat(dst, src) + Char *dst; + const Char* src; +{ + Char *sdst = dst; + + while (*dst++) + continue; + --dst; + while((*dst++ = *src++) != EOS) + continue; + + return (sdst); +} +#endif + +static void +g_Ctoc(str, buf) + register const Char *str; + char *buf; +{ + register char *dc; + + for (dc = buf; (*dc++ = *str++) != EOS;) + continue; +} + +#ifdef DEBUG +static void +qprintf(str, s) + const char *str; + register Char *s; +{ + register Char *p; + + (void)printf("%s:\n", str); + for (p = s; *p; p++) + (void)printf("%c", CHAR(*p)); + (void)printf("\n"); + for (p = s; *p; p++) + (void)printf("%c", *p & M_PROTECT ? '"' : ' '); + (void)printf("\n"); + for (p = s; *p; p++) + (void)printf("%c", ismeta(*p) ? '_' : ' '); + (void)printf("\n"); +} +#endif diff --git a/libtar/handle.c b/libtar/handle.c new file mode 100644 index 000000000..ae974b9f0 --- /dev/null +++ b/libtar/handle.c @@ -0,0 +1,129 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** handle.c - libtar code for initializing a TAR handle +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include + +#ifdef HAVE_UNISTD_H +# include +#endif + +#ifdef STDC_HEADERS +# include +#endif + + +const char libtar_version[] = PACKAGE_VERSION; + +static tartype_t default_type = { open, close, read, write }; + + +static int +tar_init(TAR **t, char *pathname, tartype_t *type, + int oflags, int mode, int options) +{ + if ((oflags & O_ACCMODE) == O_RDWR) + { + errno = EINVAL; + return -1; + } + + *t = (TAR *)calloc(1, sizeof(TAR)); + if (*t == NULL) + return -1; + + (*t)->pathname = pathname; + (*t)->options = options; + (*t)->type = (type ? type : &default_type); + (*t)->oflags = oflags; + + if ((oflags & O_ACCMODE) == O_RDONLY) + (*t)->h = libtar_hash_new(256, + (libtar_hashfunc_t)path_hashfunc); + else + (*t)->h = libtar_hash_new(16, (libtar_hashfunc_t)dev_hash); + if ((*t)->h == NULL) + { + free(*t); + return -1; + } + + return 0; +} + + +/* open a new tarfile handle */ +int +tar_open(TAR **t, char *pathname, tartype_t *type, + int oflags, int mode, int options) +{ + if (tar_init(t, pathname, type, oflags, mode, options) == -1) + return -1; + + if ((options & TAR_NOOVERWRITE) && (oflags & O_CREAT)) + oflags |= O_EXCL; + +#ifdef O_BINARY + oflags |= O_BINARY; +#endif + + (*t)->fd = (*((*t)->type->openfunc))(pathname, oflags, mode); + if ((*t)->fd == -1) + { + free(*t); + return -1; + } + + return 0; +} + + +int +tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type, + int oflags, int mode, int options) +{ + if (tar_init(t, pathname, type, oflags, mode, options) == -1) + return -1; + + (*t)->fd = fd; + return 0; +} + + +int +tar_fd(TAR *t) +{ + return t->fd; +} + + +/* close tarfile handle */ +int +tar_close(TAR *t) +{ + int i; + + i = (*(t->type->closefunc))(t->fd); + + if (t->h != NULL) + libtar_hash_free(t->h, ((t->oflags & O_ACCMODE) == O_RDONLY + ? free + : (libtar_freefunc_t)tar_dev_free)); + free(t); + + return i; +} + + diff --git a/libtar/inet_aton.c b/libtar/inet_aton.c new file mode 100644 index 000000000..a935d5a6b --- /dev/null +++ b/libtar/inet_aton.c @@ -0,0 +1,27 @@ +/* +** Copyright 2002 University of Illinois Board of Trustees +** Copyright 2002 Mark D. Roth +** All rights reserved. +** +** inet_aton.c - inet_aton() function for compatibility library +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include +#include +#include + + +int +inet_aton(const char *cp, struct in_addr *inp) +{ + inp->s_addr = inet_addr(cp); + if (inp->s_addr == -1) + return 0; + return 1; +} + + diff --git a/libtar/internal.h b/libtar/internal.h new file mode 100644 index 000000000..23243d296 --- /dev/null +++ b/libtar/internal.h @@ -0,0 +1,17 @@ +/* +** Copyright 2002-2003 University of Illinois Board of Trustees +** Copyright 2002-2003 Mark D. Roth +** All rights reserved. +** +** internal.h - internal header file for libtar +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include +#include + +#include + diff --git a/libtar/libtar.h b/libtar/libtar.h new file mode 100644 index 000000000..7a8f332ee --- /dev/null +++ b/libtar/libtar.h @@ -0,0 +1,300 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** libtar.h - header file for libtar library +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#ifndef LIBTAR_H +#define LIBTAR_H + +#include +#include +#include "tar.h" + +#include "libtar_listhash.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/* useful constants */ +#define T_BLOCKSIZE 512 +#define T_NAMELEN 100 +#define T_PREFIXLEN 155 +#define T_MAXPATHLEN (T_NAMELEN + T_PREFIXLEN) + +/* GNU extensions for typeflag */ +#define GNU_LONGNAME_TYPE 'L' +#define GNU_LONGLINK_TYPE 'K' + +/* our version of the tar header structure */ +struct tar_header +{ + char name[100]; + char mode[8]; + char uid[8]; + char gid[8]; + char size[12]; + char mtime[12]; + char chksum[8]; + char typeflag; + char linkname[100]; + char magic[6]; + char version[2]; + char uname[32]; + char gname[32]; + char devmajor[8]; + char devminor[8]; + char prefix[155]; + char padding[12]; + char *gnu_longname; + char *gnu_longlink; +}; + + +/***** handle.c ************************************************************/ + +typedef int (*openfunc_t)(const char *, int, ...); +typedef int (*closefunc_t)(int); +typedef ssize_t (*readfunc_t)(int, void *, size_t); +typedef ssize_t (*writefunc_t)(int, const void *, size_t); + +typedef struct +{ + openfunc_t openfunc; + closefunc_t closefunc; + readfunc_t readfunc; + writefunc_t writefunc; +} +tartype_t; + +typedef struct +{ + tartype_t *type; + char *pathname; + long fd; + int oflags; + int options; + struct tar_header th_buf; + libtar_hash_t *h; +} +TAR; + +/* constant values for the TAR options field */ +#define TAR_GNU 1 /* use GNU extensions */ +#define TAR_VERBOSE 2 /* output file info to stdout */ +#define TAR_NOOVERWRITE 4 /* don't overwrite existing files */ +#define TAR_IGNORE_EOT 8 /* ignore double zero blocks as EOF */ +#define TAR_CHECK_MAGIC 16 /* check magic in file header */ +#define TAR_CHECK_VERSION 32 /* check version in file header */ +#define TAR_IGNORE_CRC 64 /* ignore CRC in file header */ + +/* this is obsolete - it's here for backwards-compatibility only */ +#define TAR_IGNORE_MAGIC 0 + +extern const char libtar_version[]; + + +/* open a new tarfile handle */ +int tar_open(TAR **t, char *pathname, tartype_t *type, + int oflags, int mode, int options); + +/* make a tarfile handle out of a previously-opened descriptor */ +int tar_fdopen(TAR **t, int fd, char *pathname, tartype_t *type, + int oflags, int mode, int options); + +/* returns the descriptor associated with t */ +int tar_fd(TAR *t); + +/* close tarfile handle */ +int tar_close(TAR *t); + + +/***** append.c ************************************************************/ + +/* forward declaration to appease the compiler */ +struct tar_dev; + +/* cleanup function */ +void tar_dev_free(struct tar_dev *tdp); + +/* Appends a file to the tar archive. + * Arguments: + * t = TAR handle to append to + * realname = path of file to append + * savename = name to save the file under in the archive + */ +int tar_append_file(TAR *t, char *realname, char *savename); + +/* write EOF indicator */ +int tar_append_eof(TAR *t); + +/* add file contents to a tarchive */ +int tar_append_regfile(TAR *t, char *realname); + + +/***** block.c *************************************************************/ + +/* macros for reading/writing tarchive blocks */ +#define tar_block_read(t, buf) \ + (*((t)->type->readfunc))((t)->fd, (char *)(buf), T_BLOCKSIZE) +#define tar_block_write(t, buf) \ + (*((t)->type->writefunc))((t)->fd, (char *)(buf), T_BLOCKSIZE) + +/* read/write a header block */ +int th_read(TAR *t); +int th_write(TAR *t); + + +/***** decode.c ************************************************************/ + +/* determine file type */ +#define TH_ISREG(t) ((t)->th_buf.typeflag == REGTYPE \ + || (t)->th_buf.typeflag == AREGTYPE \ + || (t)->th_buf.typeflag == CONTTYPE \ + || (S_ISREG((mode_t)oct_to_int((t)->th_buf.mode)) \ + && (t)->th_buf.typeflag != LNKTYPE)) +#define TH_ISLNK(t) ((t)->th_buf.typeflag == LNKTYPE) +#define TH_ISSYM(t) ((t)->th_buf.typeflag == SYMTYPE \ + || S_ISLNK((mode_t)oct_to_int((t)->th_buf.mode))) +#define TH_ISCHR(t) ((t)->th_buf.typeflag == CHRTYPE \ + || S_ISCHR((mode_t)oct_to_int((t)->th_buf.mode))) +#define TH_ISBLK(t) ((t)->th_buf.typeflag == BLKTYPE \ + || S_ISBLK((mode_t)oct_to_int((t)->th_buf.mode))) +#define TH_ISDIR(t) ((t)->th_buf.typeflag == DIRTYPE \ + || S_ISDIR((mode_t)oct_to_int((t)->th_buf.mode)) \ + || ((t)->th_buf.typeflag == AREGTYPE \ + && ((t)->th_buf.name[strlen((t)->th_buf.name) - 1] == '/'))) +#define TH_ISFIFO(t) ((t)->th_buf.typeflag == FIFOTYPE \ + || S_ISFIFO((mode_t)oct_to_int((t)->th_buf.mode))) +#define TH_ISLONGNAME(t) ((t)->th_buf.typeflag == GNU_LONGNAME_TYPE) +#define TH_ISLONGLINK(t) ((t)->th_buf.typeflag == GNU_LONGLINK_TYPE) + +/* decode tar header info */ +#define th_get_crc(t) oct_to_int((t)->th_buf.chksum) +#define th_get_size(t) oct_to_int((t)->th_buf.size) +#define th_get_mtime(t) oct_to_int((t)->th_buf.mtime) +#define th_get_devmajor(t) oct_to_int((t)->th_buf.devmajor) +#define th_get_devminor(t) oct_to_int((t)->th_buf.devminor) +#define th_get_linkname(t) ((t)->th_buf.gnu_longlink \ + ? (t)->th_buf.gnu_longlink \ + : (t)->th_buf.linkname) +char *th_get_pathname(TAR *t); +mode_t th_get_mode(TAR *t); +uid_t th_get_uid(TAR *t); +gid_t th_get_gid(TAR *t); + + +/***** encode.c ************************************************************/ + +/* encode file info in th_header */ +void th_set_type(TAR *t, mode_t mode); +void th_set_path(TAR *t, char *pathname); +void th_set_link(TAR *t, char *linkname); +void th_set_device(TAR *t, dev_t device); +void th_set_user(TAR *t, uid_t uid); +void th_set_group(TAR *t, gid_t gid); +void th_set_mode(TAR *t, mode_t fmode); +#define th_set_mtime(t, fmtime) \ + int_to_oct_nonull((fmtime), (t)->th_buf.mtime, 12) +#define th_set_size(t, fsize) \ + int_to_oct_nonull((fsize), (t)->th_buf.size, 12) + +/* encode everything at once (except the pathname and linkname) */ +void th_set_from_stat(TAR *t, struct stat *s); + +/* encode magic, version, and crc - must be done after everything else is set */ +void th_finish(TAR *t); + + +/***** extract.c ***********************************************************/ + +/* sequentially extract next file from t */ +int tar_extract_file(TAR *t, char *realname); + +/* extract different file types */ +int tar_extract_dir(TAR *t, char *realname); +int tar_extract_hardlink(TAR *t, char *realname); +int tar_extract_symlink(TAR *t, char *realname); +int tar_extract_chardev(TAR *t, char *realname); +int tar_extract_blockdev(TAR *t, char *realname); +int tar_extract_fifo(TAR *t, char *realname); + +/* for regfiles, we need to extract the content blocks as well */ +int tar_extract_regfile(TAR *t, char *realname); +int tar_skip_regfile(TAR *t); + + +/***** output.c ************************************************************/ + +/* print the tar header */ +void th_print(TAR *t); + +/* print "ls -l"-like output for the file described by th */ +void th_print_long_ls(TAR *t); + + +/***** util.c *************************************************************/ + +/* hashing function for pathnames */ +int path_hashfunc(char *key, int numbuckets); + +/* matching function for dev_t's */ +int dev_match(dev_t *dev1, dev_t *dev2); + +/* matching function for ino_t's */ +int ino_match(ino_t *ino1, ino_t *ino2); + +/* hashing function for dev_t's */ +int dev_hash(dev_t *dev); + +/* hashing function for ino_t's */ +int ino_hash(ino_t *inode); + +/* create any necessary dirs */ +int mkdirhier(char *path); + +/* calculate header checksum */ +int th_crc_calc(TAR *t); + +/* calculate a signed header checksum */ +int th_signed_crc_calc(TAR *t); + +/* compare checksums in a forgiving way */ +#define th_crc_ok(t) (th_get_crc(t) == th_crc_calc(t) || th_get_crc(t) == th_signed_crc_calc(t)) + +/* string-octal to integer conversion */ +int oct_to_int(char *oct); + +/* integer to NULL-terminated string-octal conversion */ +#define int_to_oct(num, oct, octlen) \ + snprintf((oct), (octlen), "%*lo ", (octlen) - 2, (unsigned long)(num)) + +/* integer to string-octal conversion, no NULL */ +void int_to_oct_nonull(int num, char *oct, size_t octlen); + + +/***** wrapper.c **********************************************************/ + +/* extract groups of files */ +int tar_extract_glob(TAR *t, char *globname, char *prefix); +int tar_extract_all(TAR *t, char *prefix); + +/* add a whole tree of files */ +int tar_append_tree(TAR *t, char *realdir, char *savedir); + + +#ifdef __cplusplus +} +#endif + +#endif /* ! LIBTAR_H */ + diff --git a/libtar/libtar_hash.c b/libtar/libtar_hash.c new file mode 100644 index 000000000..796ebbee6 --- /dev/null +++ b/libtar/libtar_hash.c @@ -0,0 +1,344 @@ +/* listhash/libtar_hash.c. Generated from hash.c.in by configure. */ + +/* +** Copyright 1998-2002 University of Illinois Board of Trustees +** Copyright 1998-2002 Mark D. Roth +** All rights reserved. +** +** libtar_hash.c - hash table routines +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include +#include + +#include + +#include +#include + +#ifdef STDC_HEADERS +# include +#endif + + +/* +** libtar_hashptr_reset() - reset a hash pointer +*/ +void +libtar_hashptr_reset(libtar_hashptr_t *hp) +{ + libtar_listptr_reset(&(hp->node)); + hp->bucket = -1; +} + + +/* +** libtar_hashptr_data() - retrieve the data being pointed to +*/ +void * +libtar_hashptr_data(libtar_hashptr_t *hp) +{ + return libtar_listptr_data(&(hp->node)); +} + + +/* +** libtar_str_hashfunc() - default hash function, optimized for +** 7-bit strings +*/ +unsigned int +libtar_str_hashfunc(char *key, unsigned int num_buckets) +{ +#if 0 + register unsigned result = 0; + register int i; + + if (key == NULL) + return 0; + + for (i = 0; *key != '\0' && i < 32; i++) + result = result * 33U + *key++; + + return (result % num_buckets); +#else + if (key == NULL) + return 0; + + return (key[0] % num_buckets); +#endif +} + + +/* +** libtar_hash_nents() - return number of elements from hash +*/ +unsigned int +libtar_hash_nents(libtar_hash_t *h) +{ + return h->nents; +} + + +/* +** libtar_hash_new() - create a new hash +*/ +libtar_hash_t * +libtar_hash_new(int num, libtar_hashfunc_t hashfunc) +{ + libtar_hash_t *hash; + + hash = (libtar_hash_t *)calloc(1, sizeof(libtar_hash_t)); + if (hash == NULL) + return NULL; + hash->numbuckets = num; + if (hashfunc != NULL) + hash->hashfunc = hashfunc; + else + hash->hashfunc = (libtar_hashfunc_t)libtar_str_hashfunc; + + hash->table = (libtar_list_t **)calloc(num, sizeof(libtar_list_t *)); + if (hash->table == NULL) + { + free(hash); + return NULL; + } + + return hash; +} + + +/* +** libtar_hash_next() - get next element in hash +** returns: +** 1 data found +** 0 end of list +*/ +int +libtar_hash_next(libtar_hash_t *h, + libtar_hashptr_t *hp) +{ +#ifdef DS_DEBUG + printf("==> libtar_hash_next(h=0x%lx, hp={%d,0x%lx})\n", + h, hp->bucket, hp->node); +#endif + + if (hp->bucket >= 0 && hp->node != NULL && + libtar_list_next(h->table[hp->bucket], &(hp->node)) != 0) + { +#ifdef DS_DEBUG + printf(" libtar_hash_next(): found additional " + "data in current bucket (%d), returing 1\n", + hp->bucket); +#endif + return 1; + } + +#ifdef DS_DEBUG + printf(" libtar_hash_next(): done with bucket %d\n", + hp->bucket); +#endif + + for (hp->bucket++; hp->bucket < h->numbuckets; hp->bucket++) + { +#ifdef DS_DEBUG + printf(" libtar_hash_next(): " + "checking bucket %d\n", hp->bucket); +#endif + hp->node = NULL; + if (h->table[hp->bucket] != NULL && + libtar_list_next(h->table[hp->bucket], + &(hp->node)) != 0) + { +#ifdef DS_DEBUG + printf(" libtar_hash_next(): " + "found data in bucket %d, returing 1\n", + hp->bucket); +#endif + return 1; + } + } + + if (hp->bucket == h->numbuckets) + { +#ifdef DS_DEBUG + printf(" libtar_hash_next(): hash pointer " + "wrapped to 0\n"); +#endif + hp->bucket = -1; + hp->node = NULL; + } + +#ifdef DS_DEBUG + printf("<== libtar_hash_next(): no more data, " + "returning 0\n"); +#endif + return 0; +} + + +/* +** libtar_hash_del() - delete an entry from the hash +** returns: +** 0 success +** -1 (and sets errno) failure +*/ +int +libtar_hash_del(libtar_hash_t *h, + libtar_hashptr_t *hp) +{ + if (hp->bucket < 0 + || hp->bucket >= h->numbuckets + || h->table[hp->bucket] == NULL + || hp->node == NULL) + { + errno = EINVAL; + return -1; + } + + libtar_list_del(h->table[hp->bucket], &(hp->node)); + h->nents--; + return 0; +} + + +/* +** libtar_hash_empty() - empty the hash +*/ +void +libtar_hash_empty(libtar_hash_t *h, libtar_freefunc_t freefunc) +{ + int i; + + for (i = 0; i < h->numbuckets; i++) + if (h->table[i] != NULL) + libtar_list_empty(h->table[i], freefunc); + + h->nents = 0; +} + + +/* +** libtar_hash_free() - delete all of the nodes in the hash +*/ +void +libtar_hash_free(libtar_hash_t *h, libtar_freefunc_t freefunc) +{ + int i; + + for (i = 0; i < h->numbuckets; i++) + if (h->table[i] != NULL) + libtar_list_free(h->table[i], freefunc); + + free(h->table); + free(h); +} + + +/* +** libtar_hash_search() - iterative search for an element in a hash +** returns: +** 1 match found +** 0 no match +*/ +int +libtar_hash_search(libtar_hash_t *h, + libtar_hashptr_t *hp, void *data, + libtar_matchfunc_t matchfunc) +{ + while (libtar_hash_next(h, hp) != 0) + if ((*matchfunc)(data, libtar_listptr_data(&(hp->node))) != 0) + return 1; + + return 0; +} + + +/* +** libtar_hash_getkey() - hash-based search for an element in a hash +** returns: +** 1 match found +** 0 no match +*/ +int +libtar_hash_getkey(libtar_hash_t *h, + libtar_hashptr_t *hp, void *key, + libtar_matchfunc_t matchfunc) +{ +#ifdef DS_DEBUG + printf("==> libtar_hash_getkey(h=0x%lx, hp={%d,0x%lx}, " + "key=0x%lx, matchfunc=0x%lx)\n", + h, hp->bucket, hp->node, key, matchfunc); +#endif + + if (hp->bucket == -1) + { + hp->bucket = (*(h->hashfunc))(key, h->numbuckets); +#ifdef DS_DEBUG + printf(" libtar_hash_getkey(): hp->bucket " + "set to %d\n", hp->bucket); +#endif + } + + if (h->table[hp->bucket] == NULL) + { +#ifdef DS_DEBUG + printf(" libtar_hash_getkey(): no list " + "for bucket %d, returning 0\n", hp->bucket); +#endif + hp->bucket = -1; + return 0; + } + +#ifdef DS_DEBUG + printf("<== libtar_hash_getkey(): " + "returning libtar_list_search()\n"); +#endif + return libtar_list_search(h->table[hp->bucket], &(hp->node), + key, matchfunc); +} + + +/* +** libtar_hash_add() - add an element to the hash +** returns: +** 0 success +** -1 (and sets errno) failure +*/ +int +libtar_hash_add(libtar_hash_t *h, void *data) +{ + int bucket, i; + +#ifdef DS_DEBUG + printf("==> libtar_hash_add(h=0x%lx, data=0x%lx)\n", + h, data); +#endif + + bucket = (*(h->hashfunc))(data, h->numbuckets); +#ifdef DS_DEBUG + printf(" libtar_hash_add(): inserting in bucket %d\n", + bucket); +#endif + if (h->table[bucket] == NULL) + { +#ifdef DS_DEBUG + printf(" libtar_hash_add(): creating new list\n"); +#endif + h->table[bucket] = libtar_list_new(LIST_QUEUE, NULL); + } + +#ifdef DS_DEBUG + printf("<== libtar_hash_add(): " + "returning libtar_list_add()\n"); +#endif + i = libtar_list_add(h->table[bucket], data); + if (i == 0) + h->nents++; + return i; +} + + diff --git a/libtar/libtar_list.c b/libtar/libtar_list.c new file mode 100644 index 000000000..2c5febb01 --- /dev/null +++ b/libtar/libtar_list.c @@ -0,0 +1,458 @@ +/* listhash/libtar_list.c. Generated from list.c.in by configure. */ + +/* +** Copyright 1998-2002 University of Illinois Board of Trustees +** Copyright 1998-2002 Mark D. Roth +** All rights reserved. +** +** libtar_list.c - linked list routines +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include +#include + +#include + +#include +#include +#include + +#ifdef STDC_HEADERS +# include +# include +#endif + + +/* +** libtar_listptr_reset() - reset a list pointer +*/ +void +libtar_listptr_reset(libtar_listptr_t *lp) +{ + *lp = NULL; +} + + +/* +** libtar_listptr_data() - retrieve the data pointed to by lp +*/ +void * +libtar_listptr_data(libtar_listptr_t *lp) +{ + return (*lp)->data; +} + + +/* +** libtar_list_new() - create a new, empty list +*/ +libtar_list_t * +libtar_list_new(int flags, libtar_cmpfunc_t cmpfunc) +{ + libtar_list_t *newlist; + +#ifdef DS_DEBUG + printf("in libtar_list_new(%d, 0x%lx)\n", flags, cmpfunc); +#endif + + if (flags != LIST_USERFUNC + && flags != LIST_STACK + && flags != LIST_QUEUE) + { + errno = EINVAL; + return NULL; + } + + newlist = (libtar_list_t *)calloc(1, sizeof(libtar_list_t)); + if (cmpfunc != NULL) + newlist->cmpfunc = cmpfunc; + else + newlist->cmpfunc = (libtar_cmpfunc_t)strcmp; + newlist->flags = flags; + + return newlist; +} + + +/* +** libtar_list_iterate() - call a function for every element +** in a list +*/ +int +libtar_list_iterate(libtar_list_t *l, + libtar_iterate_func_t plugin, + void *state) +{ + libtar_listptr_t n; + + if (l == NULL) + return -1; + + for (n = l->first; n != NULL; n = n->next) + { + if ((*plugin)(n->data, state) == -1) + return -1; + } + + return 0; +} + + +/* +** libtar_list_empty() - empty the list +*/ +void +libtar_list_empty(libtar_list_t *l, libtar_freefunc_t freefunc) +{ + libtar_listptr_t n; + + for (n = l->first; n != NULL; n = l->first) + { + l->first = n->next; + if (freefunc != NULL) + (*freefunc)(n->data); + free(n); + } + + l->nents = 0; +} + + +/* +** libtar_list_free() - remove and free() the whole list +*/ +void +libtar_list_free(libtar_list_t *l, libtar_freefunc_t freefunc) +{ + libtar_list_empty(l, freefunc); + free(l); +} + + +/* +** libtar_list_nents() - return number of elements in the list +*/ +unsigned int +libtar_list_nents(libtar_list_t *l) +{ + return l->nents; +} + + +/* +** libtar_list_add() - adds an element to the list +** returns: +** 0 success +** -1 (and sets errno) failure +*/ +int +libtar_list_add(libtar_list_t *l, void *data) +{ + libtar_listptr_t n, m; + +#ifdef DS_DEBUG + printf("==> libtar_list_add(\"%s\")\n", (char *)data); +#endif + + n = (libtar_listptr_t)malloc(sizeof(struct libtar_node)); + if (n == NULL) + return -1; + n->data = data; + l->nents++; + +#ifdef DS_DEBUG + printf(" libtar_list_add(): allocated data\n"); +#endif + + /* if the list is empty */ + if (l->first == NULL) + { + l->last = l->first = n; + n->next = n->prev = NULL; +#ifdef DS_DEBUG + printf("<== libtar_list_add(): list was empty; " + "added first element and returning 0\n"); +#endif + return 0; + } + +#ifdef DS_DEBUG + printf(" libtar_list_add(): list not empty\n"); +#endif + + if (l->flags == LIST_STACK) + { + n->prev = NULL; + n->next = l->first; + if (l->first != NULL) + l->first->prev = n; + l->first = n; +#ifdef DS_DEBUG + printf("<== libtar_list_add(): LIST_STACK set; " + "added in front\n"); +#endif + return 0; + } + + if (l->flags == LIST_QUEUE) + { + n->prev = l->last; + n->next = NULL; + if (l->last != NULL) + l->last->next = n; + l->last = n; +#ifdef DS_DEBUG + printf("<== libtar_list_add(): LIST_QUEUE set; " + "added at end\n"); +#endif + return 0; + } + + for (m = l->first; m != NULL; m = m->next) + if ((*(l->cmpfunc))(data, m->data) < 0) + { + /* + ** if we find one that's bigger, + ** insert data before it + */ +#ifdef DS_DEBUG + printf(" libtar_list_add(): gotcha..." + "inserting data\n"); +#endif + if (m == l->first) + { + l->first = n; + n->prev = NULL; + m->prev = n; + n->next = m; +#ifdef DS_DEBUG + printf("<== libtar_list_add(): " + "added first, returning 0\n"); +#endif + return 0; + } + m->prev->next = n; + n->prev = m->prev; + m->prev = n; + n->next = m; +#ifdef DS_DEBUG + printf("<== libtar_list_add(): added middle," + " returning 0\n"); +#endif + return 0; + } + +#ifdef DS_DEBUG + printf(" libtar_list_add(): new data larger than current " + "list elements\n"); +#endif + + /* if we get here, data is bigger than everything in the list */ + l->last->next = n; + n->prev = l->last; + l->last = n; + n->next = NULL; +#ifdef DS_DEBUG + printf("<== libtar_list_add(): added end, returning 0\n"); +#endif + return 0; +} + + +/* +** libtar_list_del() - remove the element pointed to by n +** from the list l +*/ +void +libtar_list_del(libtar_list_t *l, libtar_listptr_t *n) +{ + libtar_listptr_t m; + +#ifdef DS_DEBUG + printf("==> libtar_list_del()\n"); +#endif + + l->nents--; + + m = (*n)->next; + + if ((*n)->prev) + (*n)->prev->next = (*n)->next; + else + l->first = (*n)->next; + if ((*n)->next) + (*n)->next->prev = (*n)->prev; + else + l->last = (*n)->prev; + + free(*n); + *n = m; +} + + +/* +** libtar_list_next() - get the next element in the list +** returns: +** 1 success +** 0 end of list +*/ +int +libtar_list_next(libtar_list_t *l, + libtar_listptr_t *n) +{ + if (*n == NULL) + *n = l->first; + else + *n = (*n)->next; + + return (*n != NULL ? 1 : 0); +} + + +/* +** libtar_list_prev() - get the previous element in the list +** returns: +** 1 success +** 0 end of list +*/ +int +libtar_list_prev(libtar_list_t *l, + libtar_listptr_t *n) +{ + if (*n == NULL) + *n = l->last; + else + *n = (*n)->prev; + + return (*n != NULL ? 1 : 0); +} + + +/* +** libtar_str_match() - string matching function +** returns: +** 1 match +** 0 no match +*/ +int +libtar_str_match(char *check, char *data) +{ + return !strcmp(check, data); +} + + +/* +** libtar_list_add_str() - splits string str into delim-delimited +** elements and adds them to list l +** returns: +** 0 success +** -1 (and sets errno) failure +*/ +int +libtar_list_add_str(libtar_list_t *l, + char *str, char *delim) +{ + char tmp[10240]; + char *tokp, *nextp = tmp; + + strlcpy(tmp, str, sizeof(tmp)); + while ((tokp = strsep(&nextp, delim)) != NULL) + { + if (*tokp == '\0') + continue; + if (libtar_list_add(l, strdup(tokp))) + return -1; + } + + return 0; +} + + +/* +** libtar_list_search() - find an entry in a list +** returns: +** 1 match found +** 0 no match +*/ +int +libtar_list_search(libtar_list_t *l, + libtar_listptr_t *n, void *data, + libtar_matchfunc_t matchfunc) +{ +#ifdef DS_DEBUG + printf("==> libtar_list_search(l=0x%lx, n=0x%lx, \"%s\")\n", + l, n, (char *)data); +#endif + + if (matchfunc == NULL) + matchfunc = (libtar_matchfunc_t)libtar_str_match; + + if (*n == NULL) + *n = l->first; + else + *n = (*n)->next; + + for (; *n != NULL; *n = (*n)->next) + { +#ifdef DS_DEBUG + printf("checking against \"%s\"\n", (char *)(*n)->data); +#endif + if ((*(matchfunc))(data, (*n)->data) != 0) + return 1; + } + +#ifdef DS_DEBUG + printf("no matches found\n"); +#endif + return 0; +} + + +/* +** libtar_list_dup() - copy an existing list +*/ +libtar_list_t * +libtar_list_dup(libtar_list_t *l) +{ + libtar_list_t *newlist; + libtar_listptr_t n; + + newlist = libtar_list_new(l->flags, l->cmpfunc); + for (n = l->first; n != NULL; n = n->next) + libtar_list_add(newlist, n->data); + +#ifdef DS_DEBUG + printf("returning from libtar_list_dup()\n"); +#endif + return newlist; +} + + +/* +** libtar_list_merge() - merge two lists into a new list +*/ +libtar_list_t * +libtar_list_merge(libtar_cmpfunc_t cmpfunc, int flags, + libtar_list_t *list1, + libtar_list_t *list2) +{ + libtar_list_t *newlist; + libtar_listptr_t n; + + newlist = libtar_list_new(flags, cmpfunc); + + n = NULL; + while (libtar_list_next(list1, &n) != 0) + libtar_list_add(newlist, n->data); + n = NULL; + while (libtar_list_next(list2, &n) != 0) + libtar_list_add(newlist, n->data); + + return newlist; +} + + diff --git a/libtar/libtar_listhash.h b/libtar/libtar_listhash.h new file mode 100644 index 000000000..fa33cfd25 --- /dev/null +++ b/libtar/libtar_listhash.h @@ -0,0 +1,196 @@ +/* listhash/libtar_listhash.h. Generated from listhash.h.in by configure. */ + +/* +** Copyright 1998-2002 University of Illinois Board of Trustees +** Copyright 1998-2002 Mark D. Roth +** All rights reserved. +** +** libtar_listhash.h - header file for listhash module +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#ifndef libtar_LISTHASH_H +#define libtar_LISTHASH_H + + +/***** list.c **********************************************************/ + +/* +** Comparison function (used to determine order of elements in a list) +** returns less than, equal to, or greater than 0 +** if data1 is less than, equal to, or greater than data2 +*/ +typedef int (*libtar_cmpfunc_t)(void *, void *); + +/* +** Free function (for freeing allocated memory in each element) +*/ +typedef void (*libtar_freefunc_t)(void *); + +/* +** Plugin function for libtar_list_iterate() +*/ +typedef int (*libtar_iterate_func_t)(void *, void *); + +/* +** Matching function (used to find elements in a list) +** first argument is the data to search for +** second argument is the list element it's being compared to +** returns 0 if no match is found, non-zero otherwise +*/ +typedef int (*libtar_matchfunc_t)(void *, void *); + + +struct libtar_node +{ + void *data; + struct libtar_node *next; + struct libtar_node *prev; +}; +typedef struct libtar_node *libtar_listptr_t; + +struct libtar_list +{ + libtar_listptr_t first; + libtar_listptr_t last; + libtar_cmpfunc_t cmpfunc; + int flags; + unsigned int nents; +}; +typedef struct libtar_list libtar_list_t; + + +/* values for flags */ +#define LIST_USERFUNC 0 /* use cmpfunc() to order */ +#define LIST_STACK 1 /* new elements go in front */ +#define LIST_QUEUE 2 /* new elements go at the end */ + + +/* reset a list pointer */ +void libtar_listptr_reset(libtar_listptr_t *); + +/* retrieve the data being pointed to */ +void *libtar_listptr_data(libtar_listptr_t *); + +/* creates a new, empty list */ +libtar_list_t *libtar_list_new(int, libtar_cmpfunc_t); + +/* call a function for every element in a list */ +int libtar_list_iterate(libtar_list_t *, + libtar_iterate_func_t, void *); + +/* empty the list */ +void libtar_list_empty(libtar_list_t *, + libtar_freefunc_t); + +/* remove and free() the entire list */ +void libtar_list_free(libtar_list_t *, + libtar_freefunc_t); + +/* add elements */ +int libtar_list_add(libtar_list_t *, void *); + +/* removes an element from the list - returns -1 on error */ +void libtar_list_del(libtar_list_t *, + libtar_listptr_t *); + +/* returns 1 when valid data is returned, or 0 at end of list */ +int libtar_list_next(libtar_list_t *, + libtar_listptr_t *); + +/* returns 1 when valid data is returned, or 0 at end of list */ +int libtar_list_prev(libtar_list_t *, + libtar_listptr_t *); + +/* return 1 if the data matches a list entry, 0 otherwise */ +int libtar_list_search(libtar_list_t *, + libtar_listptr_t *, void *, + libtar_matchfunc_t); + +/* return number of elements from list */ +unsigned int libtar_list_nents(libtar_list_t *); + +/* adds elements from a string delimited by delim */ +int libtar_list_add_str(libtar_list_t *, char *, char *); + +/* string matching function */ +int libtar_str_match(char *, char *); + + +/***** hash.c **********************************************************/ + +/* +** Hashing function (determines which bucket the given key hashes into) +** first argument is the key to hash +** second argument is the total number of buckets +** returns the bucket number +*/ +typedef unsigned int (*libtar_hashfunc_t)(void *, unsigned int); + + +struct libtar_hashptr +{ + int bucket; + libtar_listptr_t node; +}; +typedef struct libtar_hashptr libtar_hashptr_t; + +struct libtar_hash +{ + int numbuckets; + libtar_list_t **table; + libtar_hashfunc_t hashfunc; + unsigned int nents; +}; +typedef struct libtar_hash libtar_hash_t; + + +/* reset a hash pointer */ +void libtar_hashptr_reset(libtar_hashptr_t *); + +/* retrieve the data being pointed to */ +void *libtar_hashptr_data(libtar_hashptr_t *); + +/* default hash function, optimized for 7-bit strings */ +unsigned int libtar_str_hashfunc(char *, unsigned int); + +/* return number of elements from hash */ +unsigned int libtar_hash_nents(libtar_hash_t *); + +/* create a new hash */ +libtar_hash_t *libtar_hash_new(int, libtar_hashfunc_t); + +/* empty the hash */ +void libtar_hash_empty(libtar_hash_t *, + libtar_freefunc_t); + +/* delete all the libtar_nodes of the hash and clean up */ +void libtar_hash_free(libtar_hash_t *, + libtar_freefunc_t); + +/* returns 1 when valid data is returned, or 0 at end of list */ +int libtar_hash_next(libtar_hash_t *, + libtar_hashptr_t *); + +/* return 1 if the data matches a list entry, 0 otherwise */ +int libtar_hash_search(libtar_hash_t *, + libtar_hashptr_t *, void *, + libtar_matchfunc_t); + +/* return 1 if the key matches a list entry, 0 otherwise */ +int libtar_hash_getkey(libtar_hash_t *, + libtar_hashptr_t *, void *, + libtar_matchfunc_t); + +/* inserting data */ +int libtar_hash_add(libtar_hash_t *, void *); + +/* delete an entry */ +int libtar_hash_del(libtar_hash_t *, + libtar_hashptr_t *); + +#endif /* ! libtar_LISTHASH_H */ + diff --git a/libtar/output.c b/libtar/output.c new file mode 100644 index 000000000..a2db9293c --- /dev/null +++ b/libtar/output.c @@ -0,0 +1,134 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** output.c - libtar code to print out tar header blocks +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include +#include +#include +#include + +#ifdef STDC_HEADERS +# include +#endif + + +#ifndef _POSIX_LOGIN_NAME_MAX +# define _POSIX_LOGIN_NAME_MAX 9 +#endif + + +void +th_print(TAR *t) +{ + puts("\nPrinting tar header:"); + printf(" name = \"%.100s\"\n", t->th_buf.name); + printf(" mode = \"%.8s\"\n", t->th_buf.mode); + printf(" uid = \"%.8s\"\n", t->th_buf.uid); + printf(" gid = \"%.8s\"\n", t->th_buf.gid); + printf(" size = \"%.12s\"\n", t->th_buf.size); + printf(" mtime = \"%.12s\"\n", t->th_buf.mtime); + printf(" chksum = \"%.8s\"\n", t->th_buf.chksum); + printf(" typeflag = \'%c\'\n", t->th_buf.typeflag); + printf(" linkname = \"%.100s\"\n", t->th_buf.linkname); + printf(" magic = \"%.6s\"\n", t->th_buf.magic); + /*printf(" version = \"%.2s\"\n", t->th_buf.version); */ + printf(" version[0] = \'%c\',version[1] = \'%c\'\n", + t->th_buf.version[0], t->th_buf.version[1]); + printf(" uname = \"%.32s\"\n", t->th_buf.uname); + printf(" gname = \"%.32s\"\n", t->th_buf.gname); + printf(" devmajor = \"%.8s\"\n", t->th_buf.devmajor); + printf(" devminor = \"%.8s\"\n", t->th_buf.devminor); + printf(" prefix = \"%.155s\"\n", t->th_buf.prefix); + printf(" padding = \"%.12s\"\n", t->th_buf.padding); + printf(" gnu_longname = \"%s\"\n", + (t->th_buf.gnu_longname ? t->th_buf.gnu_longname : "[NULL]")); + printf(" gnu_longlink = \"%s\"\n", + (t->th_buf.gnu_longlink ? t->th_buf.gnu_longlink : "[NULL]")); +} + + +void +th_print_long_ls(TAR *t) +{ + char modestring[12]; + struct passwd *pw; + struct group *gr; + uid_t uid; + gid_t gid; + char username[_POSIX_LOGIN_NAME_MAX]; + char groupname[_POSIX_LOGIN_NAME_MAX]; + time_t mtime; + struct tm *mtm; + +#ifdef HAVE_STRFTIME + char timebuf[18]; +#else + const char *months[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" + }; +#endif + + uid = th_get_uid(t); + pw = getpwuid(uid); + if (pw == NULL) + snprintf(username, sizeof(username), "%d", uid); + else + strlcpy(username, pw->pw_name, sizeof(username)); + + gid = th_get_gid(t); + gr = getgrgid(gid); + if (gr == NULL) + snprintf(groupname, sizeof(groupname), "%d", gid); + else + strlcpy(groupname, gr->gr_name, sizeof(groupname)); + + strmode(th_get_mode(t), modestring); + printf("%.10s %-8.8s %-8.8s ", modestring, username, groupname); + + if (TH_ISCHR(t) || TH_ISBLK(t)) + printf(" %3d, %3d ", th_get_devmajor(t), th_get_devminor(t)); + else + printf("%9ld ", (long)th_get_size(t)); + + mtime = th_get_mtime(t); + mtm = localtime(&mtime); +#ifdef HAVE_STRFTIME + strftime(timebuf, sizeof(timebuf), "%h %e %H:%M %Y", mtm); + printf("%s", timebuf); +#else + printf("%.3s %2d %2d:%02d %4d", + months[mtm->tm_mon], + mtm->tm_mday, mtm->tm_hour, mtm->tm_min, mtm->tm_year + 1900); +#endif + + printf(" %s", th_get_pathname(t)); + + if (TH_ISSYM(t) || TH_ISLNK(t)) + { + if (TH_ISSYM(t)) + printf(" -> "); + else + printf(" link to "); + if ((t->options & TAR_GNU) && t->th_buf.gnu_longlink != NULL) + printf("%s", t->th_buf.gnu_longlink); + else + printf("%.100s", t->th_buf.linkname); + } + + putchar('\n'); +} + + diff --git a/libtar/snprintf.c b/libtar/snprintf.c new file mode 100644 index 000000000..8c228d447 --- /dev/null +++ b/libtar/snprintf.c @@ -0,0 +1,761 @@ +/************************************************************** + * Original: + * Patrick Powell Tue Apr 11 09:48:21 PDT 1995 + * A bombproof version of doprnt (dopr) included. + * Sigh. This sort of thing is always nasty do deal with. Note that + * the version here does not include floating point... + * + * snprintf() is used instead of sprintf() as it does limit checks + * for string length. This covers a nasty loophole. + * + * The other functions are there to prevent NULL pointers from + * causing nast effects. + * + * More Recently: + * Brandon Long 9/15/96 for mutt 0.43 + * This was ugly. It is still ugly. I opted out of floating point + * numbers, but the formatter understands just about everything + * from the normal C string format, at least as far as I can tell from + * the Solaris 2.5 printf(3S) man page. + * + * Brandon Long 10/22/97 for mutt 0.87.1 + * Ok, added some minimal floating point support, which means this + * probably requires libm on most operating systems. Don't yet + * support the exponent (e,E) and sigfig (g,G). Also, fmtint() + * was pretty badly broken, it just wasn't being exercised in ways + * which showed it, so that's been fixed. Also, formated the code + * to mutt conventions, and removed dead code left over from the + * original. Also, there is now a builtin-test, just compile with: + * gcc -DTEST_SNPRINTF -o snprintf snprintf.c -lm + * and run snprintf for results. + * + * Thomas Roessler 01/27/98 for mutt 0.89i + * The PGP code was using unsigned hexadecimal formats. + * Unfortunately, unsigned formats simply didn't work. + * + * Michael Elkins 03/05/98 for mutt 0.90.8 + * The original code assumed that both snprintf() and vsnprintf() were + * missing. Some systems only have snprintf() but not vsnprintf(), so + * the code is now broken down under HAVE_SNPRINTF and HAVE_VSNPRINTF. + * + * Chris Frey 2012/09/05 + * Removed varargs.h and all dependency on it. + * + **************************************************************/ + +#include + +#if !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) + +#include +# include +#include + +#include +#define VA_LOCAL_DECL va_list ap +#define VA_START(f) va_start(ap, f) +#define VA_SHIFT(v,t) ; /* no-op for ANSI */ +#define VA_END va_end(ap) + +/*int snprintf (char *str, size_t count, const char *fmt, ...);*/ +/*int vsnprintf (char *str, size_t count, const char *fmt, va_list arg);*/ + +static void dopr (char *buffer, size_t maxlen, const char *format, + va_list args); +static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, + char *value, int flags, int min, int max); +static void fmtint (char *buffer, size_t *currlen, size_t maxlen, + long value, int base, int min, int max, int flags); +static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, + long double fvalue, int min, int max, int flags); +static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c ); + +/* + * dopr(): poor man's version of doprintf + */ + +/* format read states */ +#define DP_S_DEFAULT 0 +#define DP_S_FLAGS 1 +#define DP_S_MIN 2 +#define DP_S_DOT 3 +#define DP_S_MAX 4 +#define DP_S_MOD 5 +#define DP_S_CONV 6 +#define DP_S_DONE 7 + +/* format flags - Bits */ +#define DP_F_MINUS (1 << 0) +#define DP_F_PLUS (1 << 1) +#define DP_F_SPACE (1 << 2) +#define DP_F_NUM (1 << 3) +#define DP_F_ZERO (1 << 4) +#define DP_F_UP (1 << 5) +#define DP_F_UNSIGNED (1 << 6) + +/* Conversion Flags */ +#define DP_C_SHORT 1 +#define DP_C_LONG 2 +#define DP_C_LDOUBLE 3 + +#define char_to_int(p) (p - '0') +#define MAX(p,q) ((p >= q) ? p : q) + +static void dopr (char *buffer, size_t maxlen, const char *format, va_list args) +{ + char ch; + long value; + long double fvalue; + char *strvalue; + int min; + int max; + int state; + int flags; + int cflags; + size_t currlen; + + state = DP_S_DEFAULT; + currlen = flags = cflags = min = 0; + max = -1; + ch = *format++; + + while (state != DP_S_DONE) + { + if ((ch == '\0') || (currlen >= maxlen)) + state = DP_S_DONE; + + switch(state) + { + case DP_S_DEFAULT: + if (ch == '%') + state = DP_S_FLAGS; + else + dopr_outch (buffer, &currlen, maxlen, ch); + ch = *format++; + break; + case DP_S_FLAGS: + switch (ch) + { + case '-': + flags |= DP_F_MINUS; + ch = *format++; + break; + case '+': + flags |= DP_F_PLUS; + ch = *format++; + break; + case ' ': + flags |= DP_F_SPACE; + ch = *format++; + break; + case '#': + flags |= DP_F_NUM; + ch = *format++; + break; + case '0': + flags |= DP_F_ZERO; + ch = *format++; + break; + default: + state = DP_S_MIN; + break; + } + break; + case DP_S_MIN: + if (isdigit((unsigned char)ch)) + { + min = 10*min + char_to_int (ch); + ch = *format++; + } + else if (ch == '*') + { + min = va_arg (args, int); + ch = *format++; + state = DP_S_DOT; + } + else + state = DP_S_DOT; + break; + case DP_S_DOT: + if (ch == '.') + { + state = DP_S_MAX; + ch = *format++; + } + else + state = DP_S_MOD; + break; + case DP_S_MAX: + if (isdigit((unsigned char)ch)) + { + if (max < 0) + max = 0; + max = 10*max + char_to_int (ch); + ch = *format++; + } + else if (ch == '*') + { + max = va_arg (args, int); + ch = *format++; + state = DP_S_MOD; + } + else + state = DP_S_MOD; + break; + case DP_S_MOD: + /* Currently, we don't support Long Long, bummer */ + switch (ch) + { + case 'h': + cflags = DP_C_SHORT; + ch = *format++; + break; + case 'l': + cflags = DP_C_LONG; + ch = *format++; + break; + case 'L': + cflags = DP_C_LDOUBLE; + ch = *format++; + break; + default: + break; + } + state = DP_S_CONV; + break; + case DP_S_CONV: + switch (ch) + { + case 'd': + case 'i': + if (cflags == DP_C_SHORT) + value = va_arg (args, int); + else if (cflags == DP_C_LONG) + value = va_arg (args, long int); + else + value = va_arg (args, int); + fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); + break; + case 'o': + flags |= DP_F_UNSIGNED; + if (cflags == DP_C_SHORT) + value = va_arg (args, unsigned int); + else if (cflags == DP_C_LONG) + value = va_arg (args, unsigned long int); + else + value = va_arg (args, unsigned int); + fmtint (buffer, &currlen, maxlen, value, 8, min, max, flags); + break; + case 'u': + flags |= DP_F_UNSIGNED; + if (cflags == DP_C_SHORT) + value = va_arg (args, unsigned int); + else if (cflags == DP_C_LONG) + value = va_arg (args, unsigned long int); + else + value = va_arg (args, unsigned int); + fmtint (buffer, &currlen, maxlen, value, 10, min, max, flags); + break; + case 'X': + flags |= DP_F_UP; + case 'x': + flags |= DP_F_UNSIGNED; + if (cflags == DP_C_SHORT) + value = va_arg (args, unsigned int); + else if (cflags == DP_C_LONG) + value = va_arg (args, unsigned long int); + else + value = va_arg (args, unsigned int); + fmtint (buffer, &currlen, maxlen, value, 16, min, max, flags); + break; + case 'f': + if (cflags == DP_C_LDOUBLE) + fvalue = va_arg (args, long double); + else + fvalue = va_arg (args, double); + /* um, floating point? */ + fmtfp (buffer, &currlen, maxlen, fvalue, min, max, flags); + break; + case 'E': + flags |= DP_F_UP; + case 'e': + if (cflags == DP_C_LDOUBLE) + fvalue = va_arg (args, long double); + else + fvalue = va_arg (args, double); + break; + case 'G': + flags |= DP_F_UP; + case 'g': + if (cflags == DP_C_LDOUBLE) + fvalue = va_arg (args, long double); + else + fvalue = va_arg (args, double); + break; + case 'c': + dopr_outch (buffer, &currlen, maxlen, va_arg (args, int)); + break; + case 's': + strvalue = va_arg (args, char *); + if (max < 0) + max = maxlen; /* ie, no max */ + fmtstr (buffer, &currlen, maxlen, strvalue, flags, min, max); + break; + case 'p': + strvalue = va_arg (args, void *); + fmtint (buffer, &currlen, maxlen, (long) strvalue, 16, min, max, flags); + break; + case 'n': + if (cflags == DP_C_SHORT) + { + short int *num; + num = va_arg (args, short int *); + *num = currlen; + } + else if (cflags == DP_C_LONG) + { + long int *num; + num = va_arg (args, long int *); + *num = currlen; + } + else + { + int *num; + num = va_arg (args, int *); + *num = currlen; + } + break; + case '%': + dopr_outch (buffer, &currlen, maxlen, ch); + break; + case 'w': + /* not supported yet, treat as next char */ + ch = *format++; + break; + default: + /* Unknown, skip */ + break; + } + ch = *format++; + state = DP_S_DEFAULT; + flags = cflags = min = 0; + max = -1; + break; + case DP_S_DONE: + break; + default: + /* hmm? */ + break; /* some picky compilers need this */ + } + } + if (currlen < maxlen - 1) + buffer[currlen] = '\0'; + else + buffer[maxlen - 1] = '\0'; +} + +static void fmtstr (char *buffer, size_t *currlen, size_t maxlen, + char *value, int flags, int min, int max) +{ + int padlen, strln; /* amount to pad */ + int cnt = 0; + + if (value == 0) + { + value = ""; + } + + for (strln = 0; value[strln]; ++strln); /* strlen */ + padlen = min - strln; + if (padlen < 0) + padlen = 0; + if (flags & DP_F_MINUS) + padlen = -padlen; /* Left Justify */ + + while ((padlen > 0) && (cnt < max)) + { + dopr_outch (buffer, currlen, maxlen, ' '); + --padlen; + ++cnt; + } + while (*value && (cnt < max)) + { + dopr_outch (buffer, currlen, maxlen, *value++); + ++cnt; + } + while ((padlen < 0) && (cnt < max)) + { + dopr_outch (buffer, currlen, maxlen, ' '); + ++padlen; + ++cnt; + } +} + +/* Have to handle DP_F_NUM (ie 0x and 0 alternates) */ + +static void fmtint (char *buffer, size_t *currlen, size_t maxlen, + long value, int base, int min, int max, int flags) +{ + int signvalue = 0; + unsigned long uvalue; + char convert[20]; + int place = 0; + int spadlen = 0; /* amount to space pad */ + int zpadlen = 0; /* amount to zero pad */ + int caps = 0; + + if (max < 0) + max = 0; + + uvalue = value; + + if(!(flags & DP_F_UNSIGNED)) + { + if( value < 0 ) { + signvalue = '-'; + uvalue = -value; + } + else + if (flags & DP_F_PLUS) /* Do a sign (+/i) */ + signvalue = '+'; + else + if (flags & DP_F_SPACE) + signvalue = ' '; + } + + if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ + + do { + convert[place++] = + (caps? "0123456789ABCDEF":"0123456789abcdef") + [uvalue % (unsigned)base ]; + uvalue = (uvalue / (unsigned)base ); + } while(uvalue && (place < 20)); + if (place == 20) place--; + convert[place] = 0; + + zpadlen = max - place; + spadlen = min - MAX (max, place) - (signvalue ? 1 : 0); + if (zpadlen < 0) zpadlen = 0; + if (spadlen < 0) spadlen = 0; + if (flags & DP_F_ZERO) + { + zpadlen = MAX(zpadlen, spadlen); + spadlen = 0; + } + if (flags & DP_F_MINUS) + spadlen = -spadlen; /* Left Justifty */ + +#ifdef DEBUG_SNPRINTF + dprint (1, (debugfile, "zpad: %d, spad: %d, min: %d, max: %d, place: %d\n", + zpadlen, spadlen, min, max, place)); +#endif + + /* Spaces */ + while (spadlen > 0) + { + dopr_outch (buffer, currlen, maxlen, ' '); + --spadlen; + } + + /* Sign */ + if (signvalue) + dopr_outch (buffer, currlen, maxlen, signvalue); + + /* Zeros */ + if (zpadlen > 0) + { + while (zpadlen > 0) + { + dopr_outch (buffer, currlen, maxlen, '0'); + --zpadlen; + } + } + + /* Digits */ + while (place > 0) + dopr_outch (buffer, currlen, maxlen, convert[--place]); + + /* Left Justified spaces */ + while (spadlen < 0) { + dopr_outch (buffer, currlen, maxlen, ' '); + ++spadlen; + } +} + +static long double abs_val (long double value) +{ + long double result = value; + + if (value < 0) + result = -value; + + return result; +} + +static long double pow10_ (int exp) +{ + long double result = 1; + + while (exp) + { + result *= 10; + exp--; + } + + return result; +} + +static long round_ (long double value) +{ + long intpart; + + intpart = value; + value = value - intpart; + if (value >= 0.5) + intpart++; + + return intpart; +} + +static void fmtfp (char *buffer, size_t *currlen, size_t maxlen, + long double fvalue, int min, int max, int flags) +{ + int signvalue = 0; + long double ufvalue; + char iconvert[20]; + char fconvert[20]; + int iplace = 0; + int fplace = 0; + int padlen = 0; /* amount to pad */ + int zpadlen = 0; + int caps = 0; + long intpart; + long fracpart; + + /* + * AIX manpage says the default is 0, but Solaris says the default + * is 6, and sprintf on AIX defaults to 6 + */ + if (max < 0) + max = 6; + + ufvalue = abs_val (fvalue); + + if (fvalue < 0) + signvalue = '-'; + else + if (flags & DP_F_PLUS) /* Do a sign (+/i) */ + signvalue = '+'; + else + if (flags & DP_F_SPACE) + signvalue = ' '; + +#if 0 + if (flags & DP_F_UP) caps = 1; /* Should characters be upper case? */ +#endif + + intpart = ufvalue; + + /* + * Sorry, we only support 9 digits past the decimal because of our + * conversion method + */ + if (max > 9) + max = 9; + + /* We "cheat" by converting the fractional part to integer by + * multiplying by a factor of 10 + */ + fracpart = round_ ((pow10_ (max)) * (ufvalue - intpart)); + + if (fracpart >= pow10_ (max)) + { + intpart++; + fracpart -= pow10_ (max); + } + +#ifdef DEBUG_SNPRINTF + dprint (1, (debugfile, "fmtfp: %f =? %d.%d\n", fvalue, intpart, fracpart)); +#endif + + /* Convert integer part */ + do { + iconvert[iplace++] = + (caps? "0123456789ABCDEF":"0123456789abcdef")[intpart % 10]; + intpart = (intpart / 10); + } while(intpart && (iplace < 20)); + if (iplace == 20) iplace--; + iconvert[iplace] = 0; + + /* Convert fractional part */ + do { + fconvert[fplace++] = + (caps? "0123456789ABCDEF":"0123456789abcdef")[fracpart % 10]; + fracpart = (fracpart / 10); + } while(fracpart && (fplace < 20)); + if (fplace == 20) fplace--; + fconvert[fplace] = 0; + + /* -1 for decimal point, another -1 if we are printing a sign */ + padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0); + zpadlen = max - fplace; + if (zpadlen < 0) + zpadlen = 0; + if (padlen < 0) + padlen = 0; + if (flags & DP_F_MINUS) + padlen = -padlen; /* Left Justifty */ + + if ((flags & DP_F_ZERO) && (padlen > 0)) + { + if (signvalue) + { + dopr_outch (buffer, currlen, maxlen, signvalue); + --padlen; + signvalue = 0; + } + while (padlen > 0) + { + dopr_outch (buffer, currlen, maxlen, '0'); + --padlen; + } + } + while (padlen > 0) + { + dopr_outch (buffer, currlen, maxlen, ' '); + --padlen; + } + if (signvalue) + dopr_outch (buffer, currlen, maxlen, signvalue); + + while (iplace > 0) + dopr_outch (buffer, currlen, maxlen, iconvert[--iplace]); + + /* + * Decimal point. This should probably use locale to find the correct + * char to print out. + */ + dopr_outch (buffer, currlen, maxlen, '.'); + + while (fplace > 0) + dopr_outch (buffer, currlen, maxlen, fconvert[--fplace]); + + while (zpadlen > 0) + { + dopr_outch (buffer, currlen, maxlen, '0'); + --zpadlen; + } + + while (padlen < 0) + { + dopr_outch (buffer, currlen, maxlen, ' '); + ++padlen; + } +} + +static void dopr_outch (char *buffer, size_t *currlen, size_t maxlen, char c) +{ + if (*currlen < maxlen) + buffer[(*currlen)++] = c; +} +#endif /* !defined(HAVE_SNPRINTF) || !defined(HAVE_VSNPRINTF) */ + +#ifndef HAVE_VSNPRINTF +int mutt_vsnprintf (char *str, size_t count, const char *fmt, va_list args) +{ + str[0] = 0; + dopr(str, count, fmt, args); + return(strlen(str)); +} +#endif /* !HAVE_VSNPRINTF */ + +#ifndef HAVE_SNPRINTF +int mutt_snprintf (char *str,size_t count,const char *fmt,...) +{ + VA_LOCAL_DECL; + + VA_START (fmt); + VA_SHIFT (str, char *); + VA_SHIFT (count, size_t ); + VA_SHIFT (fmt, char *); + (void) mutt_vsnprintf(str, count, fmt, ap); + VA_END; + return(strlen(str)); +} + +#ifdef TEST_SNPRINTF +#ifndef LONG_STRING +#define LONG_STRING 1024 +#endif +int main (void) +{ + char buf1[LONG_STRING]; + char buf2[LONG_STRING]; + char *fp_fmt[] = { + "%-1.5f", + "%1.5f", + "%123.9f", + "%10.5f", + "% 10.5f", + "%+22.9f", + "%+4.9f", + "%01.3f", + "%4f", + "%3.1f", + "%3.2f", + NULL + }; + double fp_nums[] = { -1.5, 134.21, 91340.2, 341.1234, 0203.9, 0.96, 0.996, + 0.9996, 1.996, 4.136, 0}; + char *int_fmt[] = { + "%-1.5d", + "%1.5d", + "%123.9d", + "%5.5d", + "%10.5d", + "% 10.5d", + "%+22.33d", + "%01.3d", + "%4d", + NULL + }; + long int_nums[] = { -1, 134, 91340, 341, 0203, 0}; + int x, y; + int fail = 0; + int num = 0; + + printf ("Testing snprintf format codes against system sprintf...\n"); + + for (x = 0; fp_fmt[x] != NULL ; x++) + for (y = 0; fp_nums[y] != 0 ; y++) + { + mutt_snprintf (buf1, sizeof (buf1), fp_fmt[x], fp_nums[y]); + sprintf (buf2, fp_fmt[x], fp_nums[y]); + if (strcmp (buf1, buf2)) + { + printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", + fp_fmt[x], buf1, buf2); + fail++; + } + num++; + } + + for (x = 0; int_fmt[x] != NULL ; x++) + for (y = 0; int_nums[y] != 0 ; y++) + { + mutt_snprintf (buf1, sizeof (buf1), int_fmt[x], int_nums[y]); + sprintf (buf2, int_fmt[x], int_nums[y]); + if (strcmp (buf1, buf2)) + { + printf("snprintf doesn't match Format: %s\n\tsnprintf = %s\n\tsprintf = %s\n", + int_fmt[x], buf1, buf2); + fail++; + } + num++; + } + printf ("%d tests failed out of %d.\n", fail, num); +} +#endif /* SNPRINTF_TEST */ + +#endif /* !HAVE_SNPRINTF */ diff --git a/libtar/strdup.c b/libtar/strdup.c new file mode 100644 index 000000000..75e9af505 --- /dev/null +++ b/libtar/strdup.c @@ -0,0 +1,62 @@ +/* $OpenBSD: strdup.c,v 1.3 1997/08/20 04:18:52 millert Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strdup.c 8.1 (Berkeley) 6/4/93"; +#else +static char *rcsid = "$OpenBSD: strdup.c,v 1.3 1997/08/20 04:18:52 millert Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +#include +#include +#include + +char * +openbsd_strdup(str) + const char *str; +{ + size_t siz; + char *copy; + + siz = strlen(str) + 1; + if ((copy = malloc(siz)) == NULL) + return(NULL); + (void)memcpy(copy, str, siz); + return(copy); +} diff --git a/libtar/strlcat.c b/libtar/strlcat.c new file mode 100644 index 000000000..39125393c --- /dev/null +++ b/libtar/strlcat.c @@ -0,0 +1,72 @@ +/* $OpenBSD: strlcat.c,v 1.5 2001/01/13 16:17:24 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strlcat.c,v 1.5 2001/01/13 16:17:24 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(initial dst) + strlen(src); if retval >= siz, + * truncation occurred. + */ +size_t strlcat(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + size_t dlen; + + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/libtar/strlcpy.c b/libtar/strlcpy.c new file mode 100644 index 000000000..300a28bc3 --- /dev/null +++ b/libtar/strlcpy.c @@ -0,0 +1,68 @@ +/* $OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $ */ + +/* + * Copyright (c) 1998 Todd C. Miller + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strlcpy.c,v 1.4 1999/05/01 18:56:41 millert Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t strlcpy(dst, src, siz) + char *dst; + const char *src; + size_t siz; +{ + register char *d = dst; + register const char *s = src; + register size_t n = siz; + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} diff --git a/libtar/strmode.c b/libtar/strmode.c new file mode 100644 index 000000000..5e7f15e85 --- /dev/null +++ b/libtar/strmode.c @@ -0,0 +1,152 @@ +/*- + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#if defined(LIBC_SCCS) && !defined(lint) +static char *rcsid = "$OpenBSD: strmode.c,v 1.3 1997/06/13 13:57:20 deraadt Exp $"; +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +void +strmode(mode, p) + register mode_t mode; + register char *p; +{ + /* print type */ + switch (mode & S_IFMT) { + case S_IFDIR: /* directory */ + *p++ = 'd'; + break; + case S_IFCHR: /* character special */ + *p++ = 'c'; + break; + case S_IFBLK: /* block special */ + *p++ = 'b'; + break; + case S_IFREG: /* regular */ + *p++ = '-'; + break; + case S_IFLNK: /* symbolic link */ + *p++ = 'l'; + break; + case S_IFSOCK: /* socket */ + *p++ = 's'; + break; +#ifdef S_IFIFO + case S_IFIFO: /* fifo */ + *p++ = 'p'; + break; +#endif +#ifdef S_IFWHT + case S_IFWHT: /* whiteout */ + *p++ = 'w'; + break; +#endif + default: /* unknown */ + *p++ = '?'; + break; + } + /* usr */ + if (mode & S_IRUSR) + *p++ = 'r'; + else + *p++ = '-'; + if (mode & S_IWUSR) + *p++ = 'w'; + else + *p++ = '-'; + switch (mode & (S_IXUSR | S_ISUID)) { + case 0: + *p++ = '-'; + break; + case S_IXUSR: + *p++ = 'x'; + break; + case S_ISUID: + *p++ = 'S'; + break; + case S_IXUSR | S_ISUID: + *p++ = 's'; + break; + } + /* group */ + if (mode & S_IRGRP) + *p++ = 'r'; + else + *p++ = '-'; + if (mode & S_IWGRP) + *p++ = 'w'; + else + *p++ = '-'; + switch (mode & (S_IXGRP | S_ISGID)) { + case 0: + *p++ = '-'; + break; + case S_IXGRP: + *p++ = 'x'; + break; + case S_ISGID: + *p++ = 'S'; + break; + case S_IXGRP | S_ISGID: + *p++ = 's'; + break; + } + /* other */ + if (mode & S_IROTH) + *p++ = 'r'; + else + *p++ = '-'; + if (mode & S_IWOTH) + *p++ = 'w'; + else + *p++ = '-'; + switch (mode & (S_IXOTH | S_ISVTX)) { + case 0: + *p++ = '-'; + break; + case S_IXOTH: + *p++ = 'x'; + break; + case S_ISVTX: + *p++ = 'T'; + break; + case S_IXOTH | S_ISVTX: + *p++ = 't'; + break; + } + *p++ = ' '; /* will be a '+' if ACL's implemented */ + *p = '\0'; +} diff --git a/libtar/strrstr.c b/libtar/strrstr.c new file mode 100644 index 000000000..097ef9182 --- /dev/null +++ b/libtar/strrstr.c @@ -0,0 +1,40 @@ +/* +** Copyright 1998-2002 University of Illinois Board of Trustees +** Copyright 1998-2002 Mark D. Roth +** All rights reserved. +** +** strrstr.c - strrstr() function for compatibility library +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include +#include + +#include + + +/* +** find the last occurrance of find in string +*/ +char * +strrstr(char *string, char *find) +{ + size_t stringlen, findlen; + char *cp; + + findlen = strlen(find); + stringlen = strlen(string); + if (findlen > stringlen) + return NULL; + + for (cp = string + stringlen - findlen; cp >= string; cp--) + if (strncmp(cp, find, findlen) == 0) + return cp; + + return NULL; +} + + diff --git a/libtar/strsep.c b/libtar/strsep.c new file mode 100644 index 000000000..3132962c9 --- /dev/null +++ b/libtar/strsep.c @@ -0,0 +1,87 @@ +/* $OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#include + +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)strsep.c 8.1 (Berkeley) 6/4/93"; +#else +static char *rcsid = "$OpenBSD: strsep.c,v 1.3 1997/08/20 04:28:14 millert Exp $"; +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifndef HAVE_STRSEP +/* + * Get next token from string *stringp, where tokens are possibly-empty + * strings separated by characters from delim. + * + * Writes NULs into the string at *stringp to end tokens. + * delim need not remain constant from call to call. + * On return, *stringp points past the last NUL written (if there might + * be further tokens), or is NULL (if there are definitely no more tokens). + * + * If *stringp is NULL, strsep returns NULL. + */ +char * +strsep(stringp, delim) + register char **stringp; + register const char *delim; +{ + register char *s; + register const char *spanp; + register int c, sc; + char *tok; + + if ((s = *stringp) == NULL) + return (NULL); + for (tok = s;;) { + c = *s++; + spanp = delim; + do { + if ((sc = *spanp++) == c) { + if (c == 0) + s = NULL; + else + s[-1] = 0; + *stringp = s; + return (tok); + } + } while (sc != 0); + } + /* NOTREACHED */ +} +#endif /* ! HAVE_STRSEP */ diff --git a/libtar/tar.h b/libtar/tar.h new file mode 100644 index 000000000..ddfef755d --- /dev/null +++ b/libtar/tar.h @@ -0,0 +1,108 @@ +/* Extended tar format from POSIX.1. + Copyright (C) 1992, 1996 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Written by David J. MacKenzie. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, write to the Free + Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + 02111-1307 USA. */ + +#ifndef _TAR_H +#define _TAR_H 1 + +/* A tar archive consists of 512-byte blocks. + Each file in the archive has a header block followed by 0+ data blocks. + Two blocks of NUL bytes indicate the end of the archive. */ + +/* The fields of header blocks: + All strings are stored as ISO 646 (approximately ASCII) strings. + + Fields are numeric unless otherwise noted below; numbers are ISO 646 + representations of octal numbers, with leading zeros as needed. + + linkname is only valid when typeflag==LNKTYPE. It doesn't use prefix; + files that are links to pathnames >100 chars long can not be stored + in a tar archive. + + If typeflag=={LNKTYPE,SYMTYPE,DIRTYPE} then size must be 0. + + devmajor and devminor are only valid for typeflag=={BLKTYPE,CHRTYPE}. + + chksum contains the sum of all 512 bytes in the header block, + treating each byte as an 8-bit unsigned value and treating the + 8 bytes of chksum as blank characters. + + uname and gname are used in preference to uid and gid, if those + names exist locally. + + Field Name Byte Offset Length in Bytes Field Type + name 0 100 NUL-terminated if NUL fits + mode 100 8 + uid 108 8 + gid 116 8 + size 124 12 + mtime 136 12 + chksum 148 8 + typeflag 156 1 see below + linkname 157 100 NUL-terminated if NUL fits + magic 257 6 must be TMAGIC (NUL term.) + version 263 2 must be TVERSION + uname 265 32 NUL-terminated + gname 297 32 NUL-terminated + devmajor 329 8 + devminor 337 8 + prefix 345 155 NUL-terminated if NUL fits + + If the first character of prefix is '\0', the file name is name; + otherwise, it is prefix/name. Files whose pathnames don't fit in that + length can not be stored in a tar archive. */ + +/* The bits in mode: */ +#define TSUID 04000 +#define TSGID 02000 +#define TSVTX 01000 +#define TUREAD 00400 +#define TUWRITE 00200 +#define TUEXEC 00100 +#define TGREAD 00040 +#define TGWRITE 00020 +#define TGEXEC 00010 +#define TOREAD 00004 +#define TOWRITE 00002 +#define TOEXEC 00001 + +/* The values for typeflag: + Values 'A'-'Z' are reserved for custom implementations. + All other values are reserved for future POSIX.1 revisions. */ + +#define REGTYPE '0' /* Regular file (preferred code). */ +#define AREGTYPE '\0' /* Regular file (alternate code). */ +#define LNKTYPE '1' /* Hard link. */ +#define SYMTYPE '2' /* Symbolic link (hard if not supported). */ +#define CHRTYPE '3' /* Character special. */ +#define BLKTYPE '4' /* Block special. */ +#define DIRTYPE '5' /* Directory. */ +#define FIFOTYPE '6' /* Named pipe. */ +#define CONTTYPE '7' /* Contiguous file */ + /* (regular file if not supported). */ + +/* Contents of magic field and its length. */ +#define TMAGIC "ustar" +#define TMAGLEN 6 + +/* Contents of the version field and its length. */ +#define TVERSION "00" +#define TVERSLEN 2 + +#endif /* tar.h */ diff --git a/libtar/util.c b/libtar/util.c new file mode 100644 index 000000000..31e831507 --- /dev/null +++ b/libtar/util.c @@ -0,0 +1,165 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** util.c - miscellaneous utility code for libtar +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#include + +#include +#include +#include + +#ifdef STDC_HEADERS +# include +#endif + + +/* hashing function for pathnames */ +int +path_hashfunc(char *key, int numbuckets) +{ + char buf[MAXPATHLEN]; + char *p; + + strcpy(buf, key); + p = basename(buf); + + return (((unsigned int)p[0]) % numbuckets); +} + + +/* matching function for dev_t's */ +int +dev_match(dev_t *dev1, dev_t *dev2) +{ + return !memcmp(dev1, dev2, sizeof(dev_t)); +} + + +/* matching function for ino_t's */ +int +ino_match(ino_t *ino1, ino_t *ino2) +{ + return !memcmp(ino1, ino2, sizeof(ino_t)); +} + + +/* hashing function for dev_t's */ +int +dev_hash(dev_t *dev) +{ + return *dev % 16; +} + + +/* hashing function for ino_t's */ +int +ino_hash(ino_t *inode) +{ + return *inode % 256; +} + + +/* +** mkdirhier() - create all directories in a given path +** returns: +** 0 success +** 1 all directories already exist +** -1 (and sets errno) error +*/ +int +mkdirhier(char *path) +{ + char src[MAXPATHLEN], dst[MAXPATHLEN] = ""; + char *dirp, *nextp = src; + int retval = 1; + + if (strlcpy(src, path, sizeof(src)) > sizeof(src)) + { + errno = ENAMETOOLONG; + return -1; + } + + if (path[0] == '/') + strcpy(dst, "/"); + + while ((dirp = strsep(&nextp, "/")) != NULL) + { + if (*dirp == '\0') + continue; + + if (dst[0] != '\0') + strcat(dst, "/"); + strcat(dst, dirp); + + if (mkdir(dst, 0777) == -1) + { + if (errno != EEXIST) + return -1; + } + else + retval = 0; + } + + return retval; +} + + +/* calculate header checksum */ +int +th_crc_calc(TAR *t) +{ + int i, sum = 0; + + for (i = 0; i < T_BLOCKSIZE; i++) + sum += ((unsigned char *)(&(t->th_buf)))[i]; + for (i = 0; i < 8; i++) + sum += (' ' - (unsigned char)t->th_buf.chksum[i]); + + return sum; +} + + +/* calculate a signed header checksum */ +int +th_signed_crc_calc(TAR *t) +{ + int i, sum = 0; + + for (i = 0; i < T_BLOCKSIZE; i++) + sum += ((signed char *)(&(t->th_buf)))[i]; + for (i = 0; i < 8; i++) + sum += (' ' - (signed char)t->th_buf.chksum[i]); + + return sum; +} + + +/* string-octal to integer conversion */ +int +oct_to_int(char *oct) +{ + int i; + + sscanf(oct, "%o", &i); + + return i; +} + + +/* integer to string-octal conversion, no NULL */ +void +int_to_oct_nonull(int num, char *oct, size_t octlen) +{ + snprintf(oct, octlen, "%*lo", octlen - 1, (unsigned long)num); + oct[octlen - 1] = ' '; +} + + diff --git a/libtar/wrapper.c b/libtar/wrapper.c new file mode 100644 index 000000000..7cd8ed12c --- /dev/null +++ b/libtar/wrapper.c @@ -0,0 +1,155 @@ +/* +** Copyright 1998-2003 University of Illinois Board of Trustees +** Copyright 1998-2003 Mark D. Roth +** All rights reserved. +** +** wrapper.c - libtar high-level wrapper code +** +** Mark D. Roth +** Campus Information Technologies and Educational Services +** University of Illinois at Urbana-Champaign +*/ + +#define DEBUG +#include + +#include +#include +#include +#include + +#ifdef STDC_HEADERS +# include +#endif + +int +tar_extract_glob(TAR *t, char *globname, char *prefix) +{ + char *filename; + char buf[MAXPATHLEN]; + int i; + + while ((i = th_read(t)) == 0) + { + filename = th_get_pathname(t); + if (fnmatch(globname, filename, FNM_PATHNAME | FNM_PERIOD)) + { + if (TH_ISREG(t) && tar_skip_regfile(t)) + return -1; + continue; + } + if (t->options & TAR_VERBOSE) + th_print_long_ls(t); + if (prefix != NULL) + snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); + else + strlcpy(buf, filename, sizeof(buf)); + if (tar_extract_file(t, filename) != 0) + return -1; + } + + return (i == 1 ? 0 : -1); +} + + +int +tar_extract_all(TAR *t, char *prefix) +{ + char *filename; + char buf[MAXPATHLEN]; + int i; + printf("prefix: %s\n", prefix); +#ifdef DEBUG + printf("==> tar_extract_all(TAR *t, \"%s\")\n", + (prefix ? prefix : "(null)")); +#endif + while ((i = th_read(t)) == 0) + { +#ifdef DEBUG + puts(" tar_extract_all(): calling th_get_pathname()"); +#endif + filename = th_get_pathname(t); + if (t->options & TAR_VERBOSE) + th_print_long_ls(t); + if (prefix != NULL) + snprintf(buf, sizeof(buf), "%s/%s", prefix, filename); + else + strlcpy(buf, filename, sizeof(buf)); +#ifdef DEBUG + printf(" tar_extract_all(): calling tar_extract_file(t, " + "\"%s\")\n", buf); +#endif + printf("filename: %s\n", filename); + /* + if (strcmp(filename, "/") == 0) { + printf("skipping /\n"); + continue; + } + */ + if (tar_extract_file(t, buf) != 0) + return -1; + } + return (i == 1 ? 0 : -1); +} + + +int +tar_append_tree(TAR *t, char *realdir, char *savedir) +{ + char realpath[MAXPATHLEN]; + char savepath[MAXPATHLEN]; + struct dirent *dent; + DIR *dp; + struct stat s; + +#ifdef DEBUG + printf("==> tar_append_tree(0x%lx, \"%s\", \"%s\")\n", + t, realdir, (savedir ? savedir : "[NULL]")); +#endif + + if (tar_append_file(t, realdir, savedir) != 0) + return -1; + +#ifdef DEBUG + puts(" tar_append_tree(): done with tar_append_file()..."); +#endif + + dp = opendir(realdir); + if (dp == NULL) + { + if (errno == ENOTDIR) + return 0; + return -1; + } + while ((dent = readdir(dp)) != NULL) + { + if (strcmp(dent->d_name, ".") == 0 || + strcmp(dent->d_name, "..") == 0) + continue; + + snprintf(realpath, MAXPATHLEN, "%s/%s", realdir, + dent->d_name); + if (savedir) + snprintf(savepath, MAXPATHLEN, "%s/%s", savedir, + dent->d_name); + + if (lstat(realpath, &s) != 0) + return -1; + + if (S_ISDIR(s.st_mode)) + { + if (tar_append_tree(t, realpath, + (savedir ? savepath : NULL)) != 0) + return -1; + continue; + } + + if (tar_append_file(t, realpath, + (savedir ? savepath : NULL)) != 0) + return -1; + } + + closedir(dp); + + return 0; +} diff --git a/makelist.cpp b/makelist.cpp deleted file mode 100644 index 7b3b3eb11..000000000 --- a/makelist.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 and - * only version 2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - * - * The code was written from scratch by Dees_Troy dees_troy at - * yahoo - * - * Copyright (c) 2012 -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include "common.h" -#include "variables.h" -#include "data.hpp" -#include "makelist.hpp" -#include "twrp-functions.hpp" - -int Makelist_File_Count; -unsigned long long Makelist_Current_Size; - -int MakeList::Add_Item(string Item_Name) { - char actual_filename[255]; - FILE *fp; - - if (Makelist_File_Count > 999) { - LOGE("File count is too large!\n"); - return -1; - } - - sprintf(actual_filename, "/tmp/list/filelist%03i", Makelist_File_Count); - - fp = fopen(actual_filename, "a"); - if (fp == NULL) { - LOGE("Failed to open '%s'\n", actual_filename); - return -1; - } - if (fprintf(fp, "%s\n", Item_Name.c_str()) < 0) { - LOGE("Failed to write to '%s'\n", actual_filename); - return -1; - } - if (fclose(fp) != 0) { - LOGE("Failed to close '%s'\n", actual_filename); - return -1; - } - return 0; -} - -int MakeList::Generate_File_Lists(string Path) { - DIR* d; - struct dirent* de; - struct stat st; - string FileName; - int has_data_media; - - DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); - if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0) - return 0; // Skip /data/media - - d = opendir(Path.c_str()); - if (d == NULL) - { - LOGE("error opening '%s'\n", Path.c_str()); - return -1; - } - - while ((de = readdir(d)) != NULL) - { - FileName = Path + "/"; - FileName += de->d_name; - if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0) - continue; // Skip /data/media - if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) - { - unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false); - if (Makelist_Current_Size + folder_size > MAX_ARCHIVE_SIZE) { - if (Generate_File_Lists(FileName) < 0) - return -1; - } else { - FileName += "/"; - if (Add_Item(FileName) < 0) - return -1; - Makelist_Current_Size += folder_size; - } - } - else if (de->d_type == DT_REG || de->d_type == DT_LNK) - { - stat(FileName.c_str(), &st); - - if (Makelist_Current_Size != 0 && Makelist_Current_Size + st.st_size > MAX_ARCHIVE_SIZE) { - Makelist_File_Count++; - Makelist_Current_Size = 0; - } - if (Add_Item(FileName) < 0) - return -1; - Makelist_Current_Size += st.st_size; - if (st.st_size > 2147483648LL) - LOGE("There is a file that is larger than 2GB in the file system\n'%s'\nThis file may not restore properly\n", FileName.c_str()); - } - } - closedir(d); - return 0; -} - -int MakeList::Make_File_List(string Path) -{ - Makelist_File_Count = 0; - Makelist_Current_Size = 0; - system("cd /tmp && rm -rf list"); - system("cd /tmp && mkdir list"); - if (Generate_File_Lists(Path) < 0) { - LOGE("Error generating file list\n"); - return -1; - } - LOGI("Done, generated %i files.\n", (Makelist_File_Count + 1)); - return (Makelist_File_Count + 1); -} diff --git a/makelist.hpp b/makelist.hpp deleted file mode 100644 index 6a3f6180e..000000000 --- a/makelist.hpp +++ /dev/null @@ -1,40 +0,0 @@ -/* This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License version 2 and - * only version 2 as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - * 02110-1301, USA. - * - * The code was written from scratch by Dees_Troy dees_troy at - * yahoo - * - * Copyright (c) 2012 -*/ -#ifndef _MAKELIST_HEADER -#define _MAKELIST_HEADER - -#include - -using namespace std; - -// Partition class -class MakeList -{ -public: - static int Make_File_List(string Path); - -private: - static int Add_Item(string Item_Name); - static int Generate_File_Lists(string Path); - -}; - -#endif // _MAKELIST_HEADER - diff --git a/openrecoveryscript.cpp b/openrecoveryscript.cpp index 47bf5d6dd..147214931 100644 --- a/openrecoveryscript.cpp +++ b/openrecoveryscript.cpp @@ -47,8 +47,6 @@ static const char *SCRIPT_FILE_TMP = "/tmp/openrecoveryscript"; #define SCRIPT_COMMAND_SIZE 512 int OpenRecoveryScript::check_for_script_file(void) { - char exec[512]; - if (!PartitionManager.Mount_By_Path(SCRIPT_FILE_CACHE, false)) { LOGE("Unable to mount /cache for OpenRecoveryScript support.\n"); return 0; @@ -56,15 +54,9 @@ int OpenRecoveryScript::check_for_script_file(void) { if (TWFunc::Path_Exists(SCRIPT_FILE_CACHE)) { LOGI("Script file found: '%s'\n", SCRIPT_FILE_CACHE); // Copy script file to /tmp - strcpy(exec, "cp "); - strcat(exec, SCRIPT_FILE_CACHE); - strcat(exec, " "); - strcat(exec, SCRIPT_FILE_TMP); - system(exec); + TWFunc::copy_file(SCRIPT_FILE_CACHE, SCRIPT_FILE_TMP, 0755); // Delete the file from /cache - strcpy(exec, "rm "); - strcat(exec, SCRIPT_FILE_CACHE); - system(exec); + unlink(SCRIPT_FILE_CACHE); return 1; } return 0; @@ -329,7 +321,8 @@ int OpenRecoveryScript::run_script_file(void) { // Reboot } else if (strcmp(command, "cmd") == 0) { if (cindex != 0) { - system(value); + string status; + TWFunc::Exec_Cmd(value, status); } else { LOGE("No value given for cmd\n"); } @@ -347,13 +340,14 @@ int OpenRecoveryScript::run_script_file(void) { return 1; } if (install_cmd && DataManager_GetIntValue(TW_HAS_INJECTTWRP) == 1 && DataManager_GetIntValue(TW_INJECT_AFTER_ZIP) == 1) { + string status; ui_print("Injecting TWRP into boot image...\n"); TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); if (Boot == NULL || Boot->Current_File_System != "emmc") - system("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash"); + TWFunc::Exec_Cmd("injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash", status); else { string injectcmd = "injecttwrp --dump /tmp/backup_recovery_ramdisk.img /tmp/injected_boot.img --flash bd=" + Boot->Actual_Block_Device; - system(injectcmd.c_str()); + TWFunc::Exec_Cmd(injectcmd.c_str(), status); } ui_print("TWRP injection complete.\n"); } diff --git a/partition.cpp b/partition.cpp index f5173b13d..7eac409f4 100644 --- a/partition.cpp +++ b/partition.cpp @@ -28,6 +28,8 @@ #include #include #include +#include +#include #ifdef TW_INCLUDE_CRYPTO #include "cutils/properties.h" @@ -38,12 +40,14 @@ #include "partitions.hpp" #include "data.hpp" #include "twrp-functions.hpp" -#include "makelist.hpp" +#include "twrpTar.hpp" extern "C" { #include "mtdutils/mtdutils.h" #include "mtdutils/mounts.h" } +using namespace std; + TWPartition::TWPartition(void) { Can_Be_Mounted = false; Can_Be_Wiped = false; @@ -94,7 +98,6 @@ bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { int line_len = Line.size(), index = 0, item_index = 0; char* ptr; string Flags; - strncpy(full_line, Line.c_str(), line_len); for (index = 0; index < line_len; index++) { @@ -240,9 +243,9 @@ bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { Wipe_Available_in_GUI = true; Wipe_During_Factory_Reset = true; if (Mount(false) && !TWFunc::Path_Exists("/cache/recovery/.")) { - string Recreate_Command = "cd /cache && mkdir recovery"; LOGI("Recreating /cache/recovery folder.\n"); - system(Recreate_Command.c_str()); + if (mkdir("/cache/recovery", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) + return -1; } } else if (Mount_Point == "/datadata") { Wipe_During_Factory_Reset = true; @@ -303,7 +306,6 @@ bool TWPartition::Process_Fstab_Line(string Line, bool Display_Error) { // Process any custom flags if (Flags.size() > 0) Process_Flags(Flags, Display_Error); - return true; } @@ -474,7 +476,7 @@ void TWPartition::Find_Real_Block_Device(string& Block, bool Display_Error) { void TWPartition::Mount_Storage_Retry(void) { // On some devices, storage doesn't want to mount right away, retry and sleep - if (!Mount(false)) { + if (!Mount(true)) { int retry_count = 5; while (retry_count > 0 && !Mount(false)) { usleep(500000); @@ -553,13 +555,14 @@ bool TWPartition::Get_Size_Via_df(bool Display_Error) { char command[255], line[512]; int include_block = 1; unsigned int min_len; + string result; if (!Mount(Display_Error)) return false; min_len = Actual_Block_Device.size() + 2; sprintf(command, "df %s > /tmp/dfoutput.txt", Mount_Point.c_str()); - system(command); + TWFunc::Exec_Cmd(command, result); fp = fopen("/tmp/dfoutput.txt", "rt"); if (fp == NULL) { LOGI("Unable to open /tmp/dfoutput.txt.\n"); @@ -663,7 +666,6 @@ bool TWPartition::Mount(bool Display_Error) { // Check the current file system before mounting Check_FS_Type(); - if (Fstab_File_System == "yaffs2") { // mount an MTD partition as a YAFFS2 filesystem. mtd_scan_partitions(); @@ -682,6 +684,12 @@ bool TWPartition::Mount(bool Display_Error) { return false; } else return true; + } else if (Fstab_File_System == "exfat") { + string cmd = "/sbin/exfat-fuse " + Actual_Block_Device + " " + Mount_Point; + LOGI("cmd: %s\n", cmd.c_str()); + string result; + if (TWFunc::Exec_Cmd(cmd, result) != 0) + return false; } else if (mount(Actual_Block_Device.c_str(), Mount_Point.c_str(), Current_File_System.c_str(), 0, NULL) != 0) { if (Display_Error) LOGE("Unable to mount '%s'\n", Mount_Point.c_str()); @@ -694,10 +702,7 @@ bool TWPartition::Mount(bool Display_Error) { Update_Size(Display_Error); if (!Symlink_Mount_Point.empty()) { - string Command; - - Command = "mount " + Symlink_Path + " " + Symlink_Mount_Point; - system(Command.c_str()); + mount(Symlink_Path.c_str(), Symlink_Mount_Point.c_str(), Fstab_File_System.c_str(), NULL, NULL); } return true; } @@ -752,6 +757,8 @@ bool TWPartition::Wipe(string New_File_System) { wiped = Wipe_EXT23(New_File_System); else if (New_File_System == "vfat") wiped = Wipe_FAT(); + if (New_File_System == "exfat") + wiped = Wipe_EXFAT(); else if (New_File_System == "yaffs2") wiped = Wipe_MTD(); else { @@ -786,16 +793,11 @@ bool TWPartition::Wipe_AndSec(void) { if (!Has_Android_Secure) return false; - char cmd[512]; - if (!Mount(true)) return false; - ui_print("Using rm -rf on .android_secure\n"); - sprintf(cmd, "rm -rf %s/.android_secure/* && rm -rf %s/.android_secure/.*", Mount_Point.c_str(), Mount_Point.c_str()); - - LOGI("rm -rf command is: '%s'\n", cmd); - system(cmd); + ui_print("Wiping .android_secure\n"); + TWFunc::removeDir(Mount_Point + "/.android_secure/", true); return true; } @@ -917,9 +919,8 @@ bool TWPartition::Wipe_Encryption() { } void TWPartition::Check_FS_Type() { - FILE *fp; - string blkCommand; - char blkOutput[255]; + string blkCommand, result; + string blkOutput; char* blk; char* arg; char* ptr; @@ -931,18 +932,13 @@ void TWPartition::Check_FS_Type() { if (!Is_Present) return; - if (TWFunc::Path_Exists("/tmp/blkidoutput.txt")) - system("rm /tmp/blkidoutput.txt"); - - blkCommand = "blkid " + Actual_Block_Device + " > /tmp/blkidoutput.txt"; - system(blkCommand.c_str()); - fp = fopen("/tmp/blkidoutput.txt", "rt"); - if (fp == NULL) - return; - while (fgets(blkOutput, sizeof(blkOutput), fp) != NULL) + blkCommand = "blkid " + Actual_Block_Device; + TWFunc::Exec_Cmd(blkCommand, result); + std::stringstream line(result); + while (getline(line, blkOutput)) { - blk = blkOutput; - ptr = blkOutput; + blk = (char*) blkOutput.c_str(); + ptr = (char*) blkOutput.c_str(); while (*ptr > 32 && *ptr != ':') ptr++; if (*ptr == 0) continue; *ptr = 0; @@ -981,13 +977,11 @@ void TWPartition::Check_FS_Type() { } else continue; - - if (strcmp(Current_File_System.c_str(), arg) != 0) { + if (Current_File_System != arg) { LOGI("'%s' was '%s' now set to '%s'\n", Mount_Point.c_str(), Current_File_System.c_str(), arg); Current_File_System = arg; } } - fclose(fp); return; } @@ -996,13 +990,13 @@ bool TWPartition::Wipe_EXT23(string File_System) { return false; if (TWFunc::Path_Exists("/sbin/mke2fs")) { - char command[512]; + string command, result; ui_print("Formatting %s using mke2fs...\n", Display_Name.c_str()); Find_Actual_Block_Device(); - sprintf(command, "mke2fs -t %s -m 0 %s", File_System.c_str(), Actual_Block_Device.c_str()); - LOGI("mke2fs command: %s\n", command); - if (system(command) == 0) { + command = "mke2fs -t " + File_System + " -m 0 " + Actual_Block_Device; + LOGI("mke2fs command: %s\n", command.c_str()); + if (TWFunc::Exec_Cmd(command, result) == 0) { Current_File_System = File_System; Recreate_AndSec_Folder(); ui_print("Done.\n"); @@ -1022,7 +1016,7 @@ bool TWPartition::Wipe_EXT4() { return false; if (TWFunc::Path_Exists("/sbin/make_ext4fs")) { - string Command; + string Command, result; ui_print("Formatting %s using make_ext4fs...\n", Display_Name.c_str()); Find_Actual_Block_Device(); @@ -1036,7 +1030,7 @@ bool TWPartition::Wipe_EXT4() { } Command += " " + Actual_Block_Device; LOGI("make_ext4fs command: %s\n", Command.c_str()); - if (system(Command.c_str()) == 0) { + if (TWFunc::Exec_Cmd(Command, result) == 0) { Current_File_System = "ext4"; Recreate_AndSec_Folder(); ui_print("Done.\n"); @@ -1052,7 +1046,7 @@ bool TWPartition::Wipe_EXT4() { } bool TWPartition::Wipe_FAT() { - char command[512]; + string command, result; if (TWFunc::Path_Exists("/sbin/mkdosfs")) { if (!UnMount(true)) @@ -1060,8 +1054,8 @@ bool TWPartition::Wipe_FAT() { ui_print("Formatting %s using mkdosfs...\n", Display_Name.c_str()); Find_Actual_Block_Device(); - sprintf(command,"mkdosfs %s", Actual_Block_Device.c_str()); // use mkdosfs to format it - if (system(command) == 0) { + command = "mkdosfs " + Actual_Block_Device; + if (TWFunc::Exec_Cmd(command, result) == 0) { Current_File_System = "vfat"; Recreate_AndSec_Folder(); ui_print("Done.\n"); @@ -1078,6 +1072,29 @@ bool TWPartition::Wipe_FAT() { return false; } +bool TWPartition::Wipe_EXFAT() { + string command, result; + + if (TWFunc::Path_Exists("/sbin/mkexfatfs")) { + if (!UnMount(true)) + return false; + + ui_print("Formatting %s using mkexfatfs...\n", Display_Name.c_str()); + Find_Actual_Block_Device(); + command = "mkexfatfs " + Actual_Block_Device; + if (TWFunc::Exec_Cmd(command, result) == 0) { + Recreate_AndSec_Folder(); + ui_print("Done.\n"); + return true; + } else { + LOGE("Unable to wipe '%s'.\n", Mount_Point.c_str()); + return false; + } + return true; + } + return false; +} + bool TWPartition::Wipe_MTD() { if (!UnMount(true)) return false; @@ -1112,22 +1129,17 @@ bool TWPartition::Wipe_MTD() { } bool TWPartition::Wipe_RMRF() { - char cmd[512]; - if (!Mount(true)) return false; - ui_print("Using rm -rf on '%s'\n", Mount_Point.c_str()); - sprintf(cmd, "rm -rf %s/* && rm -rf %s/.*", Mount_Point.c_str(), Mount_Point.c_str()); - - LOGI("rm -rf command is: '%s'\n", cmd); - system(cmd); + ui_print("Removing all files under '%s'\n", Mount_Point.c_str()); + TWFunc::removeDir(Mount_Point, true); Recreate_AndSec_Folder(); - return true; + return true; } bool TWPartition::Wipe_Data_Without_Wiping_Media() { - char cmd[256]; + string dir; // This handles wiping data on devices with "sdcard" in /data/media if (!Mount(true)) @@ -1145,9 +1157,10 @@ bool TWPartition::Wipe_Data_Without_Wiping_Media() { // The .layout_version file is responsible for determining whether 4.2 decides up upgrade // the media folder for multi-user. if (strcmp(de->d_name, "media") == 0 || strcmp(de->d_name, ".layout_version") == 0) continue; - - sprintf(cmd, "rm -fr /data/%s", de->d_name); - system(cmd); + + dir = "/data/"; + dir.append(de->d_name); + TWFunc::removeDir(dir, false); } closedir(d); ui_print("Done.\n"); @@ -1163,6 +1176,8 @@ bool TWPartition::Backup_Tar(string backup_folder) { int use_compression, index, backup_count; struct stat st; unsigned long long total_bsize = 0, file_size; + twrpTar tar; + vector files; if (!Mount(true)) return false; @@ -1176,48 +1191,32 @@ bool TWPartition::Backup_Tar(string backup_folder) { } DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); - if (use_compression) - Tar_Args = "-cz"; - else - Tar_Args = "-c"; sprintf(back_name, "%s.%s.win", Backup_Name.c_str(), Current_File_System.c_str()); Backup_FileName = back_name; - - if (Backup_Size > MAX_ARCHIVE_SIZE) { + Full_FileName = backup_folder + "/" + Backup_FileName; + if (Backup_Size > MAX_ARCHIVE_SIZE) { // This backup needs to be split into multiple archives - ui_print("Breaking backup file into multiple archives...\nGenerating file lists\n"); + ui_print("Breaking backup file into multiple archives...\n"); sprintf(back_name, "%s", Backup_Path.c_str()); - backup_count = MakeList::Make_File_List(back_name); - if (backup_count < 1) { - LOGE("Error generating file list!\n"); + backup_count = tar.Split_Archive(back_name, Full_FileName); + if (backup_count == -1) { + LOGE("Error tarring split files!\n"); return false; } - for (index=0; index #include #include - +#include +#include #include "variables.h" #include "common.h" #include "ui.h" @@ -471,18 +472,20 @@ int TWPartitionManager::Check_Backup_Name(bool Display_Error) { bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, string Backup_Filename) { - char command[512]; + string command; string Full_File = Backup_Folder + Backup_Filename; + string result; - if (!generate_md5) + if (!generate_md5) return true; TWFunc::GUI_Operation_Text(TW_GENERATE_MD5_TEXT, "Generating MD5"); ui_print(" * Generating md5...\n"); if (TWFunc::Path_Exists(Full_File)) { - sprintf(command, "cd '%s' && md5sum %s > %s.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), Backup_Filename.c_str()); - if (system(command) == 0) { + command = "md5sum " + Backup_Filename + " > " + Backup_Filename + ".md5"; + chdir(Backup_Folder.c_str()); + if (TWFunc::Exec_Cmd(command, result) == 0) { ui_print(" * MD5 Created.\n"); return true; } else { @@ -492,11 +495,15 @@ bool TWPartitionManager::Make_MD5(bool generate_md5, string Backup_Folder, strin } else { char filename[512]; int index = 0; - sprintf(filename, "%s%03i", Full_File.c_str(), index); while (TWFunc::Path_Exists(filename) == true) { - sprintf(command, "cd '%s' && md5sum %s%03i > %s%03i.md5",Backup_Folder.c_str(), Backup_Filename.c_str(), index, Backup_Filename.c_str(), index); - if (system(command) != 0) { + ostringstream intToStr; + intToStr << index; + ostringstream fn; + fn << setw(3) << setfill('0') << intToStr.str(); + command = "md5sum " + Backup_Filename + fn.str() + " >" + Backup_Filename + fn.str() + ".md5"; + chdir(Backup_Folder.c_str()); + if (TWFunc::Exec_Cmd(command, result) != 0) { ui_print(" * MD5 Error.\n"); return false; } @@ -878,7 +885,7 @@ int TWPartitionManager::Run_Backup(void) { Update_System_Details(); UnMount_Main_Partitions(); ui_print("[BACKUP COMPLETED IN %d SECONDS]\n\n", total_time); // the end - return true; + return true; } bool TWPartitionManager::Restore_Partition(TWPartition* Part, string Restore_Name, int partition_count) { @@ -1287,6 +1294,7 @@ int TWPartitionManager::Factory_Reset(void) { int TWPartitionManager::Wipe_Dalvik_Cache(void) { struct stat st; + vector dir; if (!Mount_By_Path("/data", true)) return false; @@ -1294,23 +1302,25 @@ int TWPartitionManager::Wipe_Dalvik_Cache(void) { if (!Mount_By_Path("/cache", true)) return false; + dir.push_back("/data/dalvik-cache"); + dir.push_back("/cache/dalvik-cache"); + dir.push_back("/cache/dc"); ui_print("\nWiping Dalvik Cache Directories...\n"); - system("rm -rf /data/dalvik-cache"); - ui_print("Cleaned: /data/dalvik-cache...\n"); - system("rm -rf /cache/dalvik-cache"); - ui_print("Cleaned: /cache/dalvik-cache...\n"); - system("rm -rf /cache/dc"); - ui_print("Cleaned: /cache/dc\n"); - + for (int i = 0; i < dir.size(); ++i) { + if (stat(dir.at(i).c_str(), &st) == 0) { + TWFunc::removeDir(dir.at(i), false); + ui_print("Cleaned: %s...\n", dir.at(i).c_str()); + } + } TWPartition* sdext = Find_Partition_By_Path("/sd-ext"); if (sdext != NULL) { if (sdext->Is_Present && sdext->Mount(false)) { if (stat("/sd-ext/dalvik-cache", &st) == 0) { - system("rm -rf /sd-ext/dalvik-cache"); - ui_print("Cleaned: /sd-ext/dalvik-cache...\n"); - } + TWFunc::removeDir("/sd-ext/dalvik-cache", false); + ui_print("Cleaned: /sd-ext/dalvik-cache...\n"); + } + } } - } ui_print("-- Dalvik Cache Directories Wipe Complete!\n\n"); return true; } @@ -1319,9 +1329,8 @@ int TWPartitionManager::Wipe_Rotate_Data(void) { if (!Mount_By_Path("/data", true)) return false; - system("rm -r /data/misc/akmd*"); - system("rm -r /data/misc/rild*"); - system("rm -r /data/misc/rild*"); + unlink("/data/misc/akmd*"); + unlink("/data/misc/rild*"); ui_print("Rotation data wiped.\n"); return true; } @@ -1388,8 +1397,9 @@ int TWPartitionManager::Wipe_Media_From_Data(void) { return false; ui_print("Wiping internal storage -- /data/media...\n"); - system("rm -rf /data/media"); - system("cd /data && mkdir media && chmod 775 media"); + TWFunc::removeDir("/data/media", false); + if (mkdir("/data/media", S_IRWXU | S_IRWXG | S_IWGRP | S_IXGRP) != 0) + return -1; if (dat->Has_Data_Media) { dat->Recreate_Media_Folder(); } @@ -1831,7 +1841,8 @@ int TWPartitionManager::Partition_SDCard(void) { if (!SDext->UnMount(true)) return false; } - system("umount \"$SWAPPATH\""); + string result; + TWFunc::Exec_Cmd("umount \"$SWAPPATH\"", result); Device = SDCard->Actual_Block_Device; // Just use the root block device Device.resize(strlen("/dev/block/mmcblkX")); @@ -1883,7 +1894,7 @@ int TWPartitionManager::Partition_SDCard(void) { ui_print("Removing partition table...\n"); Command = "parted -s " + Device + " mklabel msdos"; LOGI("Command is: '%s'\n", Command.c_str()); - if (system(Command.c_str()) != 0) { + if (TWFunc::Exec_Cmd(Command, result) != 0) { LOGE("Unable to remove partition table.\n"); Update_System_Details(); return false; @@ -1891,7 +1902,7 @@ int TWPartitionManager::Partition_SDCard(void) { ui_print("Creating FAT32 partition...\n"); Command = "parted " + Device + " mkpartfs primary fat32 0 " + fat_str + "MB"; LOGI("Command is: '%s'\n", Command.c_str()); - if (system(Command.c_str()) != 0) { + if (TWFunc::Exec_Cmd(Command, result) != 0) { LOGE("Unable to create FAT32 partition.\n"); return false; } @@ -1899,7 +1910,7 @@ int TWPartitionManager::Partition_SDCard(void) { ui_print("Creating EXT partition...\n"); Command = "parted " + Device + " mkpartfs primary ext2 " + fat_str + "MB " + ext_str + "MB"; LOGI("Command is: '%s'\n", Command.c_str()); - if (system(Command.c_str()) != 0) { + if (TWFunc::Exec_Cmd(Command, result) != 0) { LOGE("Unable to create EXT partition.\n"); Update_System_Details(); return false; @@ -1909,7 +1920,7 @@ int TWPartitionManager::Partition_SDCard(void) { ui_print("Creating swap partition...\n"); Command = "parted " + Device + " mkpartfs primary linux-swap " + ext_str + "MB " + swap_str + "MB"; LOGI("Command is: '%s'\n", Command.c_str()); - if (system(Command.c_str()) != 0) { + if (TWFunc::Exec_Cmd(Command, result) != 0) { LOGE("Unable to create swap partition.\n"); Update_System_Details(); return false; @@ -1944,7 +1955,7 @@ int TWPartitionManager::Partition_SDCard(void) { Command = "mke2fs -t " + ext_format + " -m 0 " + SDext->Actual_Block_Device; ui_print("Formatting sd-ext as %s...\n", ext_format.c_str()); LOGI("Formatting sd-ext after partitioning, command: '%s'\n", Command.c_str()); - system(Command.c_str()); + TWFunc::Exec_Cmd(Command, result); } Update_System_Details(); diff --git a/partitions.hpp b/partitions.hpp index 2c3ad7180..2b2ed2cef 100644 --- a/partitions.hpp +++ b/partitions.hpp @@ -120,7 +120,8 @@ private: unsigned long long Get_Size_Via_du(string Path, bool Display_Error); // Uses du to get sizes bool Wipe_EXT23(string File_System); // Formats as ext3 or ext2 bool Wipe_EXT4(); // Formats using ext4, uses make_ext4fs when present - bool Wipe_FAT(); // Formats as FAT except that mkdosfs from busybox usually fails so oftentimes this is actually a rm -rf wipe + bool Wipe_FAT(); // Formats as FAT if mkdosfs exits otherwise rm -rf wipe + bool Wipe_EXFAT(); // Formats as EXFAT bool Wipe_MTD(); // Formats as yaffs2 for MTD memory types bool Wipe_RMRF(); // Uses rm -rf to wipe bool Wipe_Data_Without_Wiping_Media(); // Uses rm -rf to wipe but does not wipe /data/media diff --git a/prebuilt/Android.mk b/prebuilt/Android.mk index e260598d3..d2024f39e 100644 --- a/prebuilt/Android.mk +++ b/prebuilt/Android.mk @@ -15,6 +15,9 @@ RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/flash_image RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/erase_image RELINK_SOURCE_FILES += $(TARGET_OUT_OPTIONAL_EXECUTABLES)/busybox RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/pigz +RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/dosfsck +RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/dosfslabel +RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/mkdosfs RELINK_SOURCE_FILES += $(TARGET_OUT_EXECUTABLES)/e2fsck RELINK_SOURCE_FILES += $(TARGET_OUT_EXECUTABLES)/mke2fs RELINK_SOURCE_FILES += $(TARGET_OUT_EXECUTABLES)/tune2fs @@ -34,12 +37,18 @@ RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libstdc++.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libz.so #RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libjpeg.so #RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmtdutils.so +RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libtar.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmmcutils.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libbmlutils.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libflashutils.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libstlport.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libmincrypt.so RELINK_SOURCE_FILES += $(TARGET_OUT_SHARED_LIBRARIES)/libext4_utils.so + +ifeq ($(TW_INCLUDE_EXFAT), true) + RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/exfat-fuse + RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/mkexfatfs +endif ifeq ($(TW_INCLUDE_BLOBPACK), true) RELINK_SOURCE_FILES += $(TARGET_RECOVERY_ROOT_OUT)/sbin/blobpack endif diff --git a/recovery.cpp b/recovery.cpp index b1333eaa5..e1b8daaeb 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -811,7 +811,7 @@ main(int argc, char **argv) { printf("Starting the UI..."); gui_init(); printf("=> Linking mtab\n"); - system("ln -s /proc/mounts /etc/mtab"); // Link mtab for mke2fs + symlink("/proc/mounts", "/etc/mtab"); printf("=> Processing recovery.fstab\n"); if (!PartitionManager.Process_Fstab("/etc/recovery.fstab", 1)) { LOGE("Failing out of recovery due to problem with recovery.fstab.\n"); @@ -894,13 +894,13 @@ main(int argc, char **argv) { #ifdef TW_INCLUDE_INJECTTWRP // Back up TWRP Ramdisk if needed: TWPartition* Boot = PartitionManager.Find_Partition_By_Path("/boot"); - + string result; LOGI("Backing up TWRP ramdisk...\n"); if (Boot == NULL || Boot->Current_File_System != "emmc") - system("injecttwrp --backup /tmp/backup_recovery_ramdisk.img"); + TWFunc::Exec_Cmd("injecttwrp --backup /tmp/backup_recovery_ramdisk.img", result); else { string injectcmd = "injecttwrp --backup /tmp/backup_recovery_ramdisk.img bd=" + Boot->Actual_Block_Device; - system(injectcmd.c_str()); + TWFunc::Exec_Cmd(injectcmd, result); } LOGI("Backup of TWRP ramdisk done.\n"); #endif @@ -954,7 +954,7 @@ main(int argc, char **argv) { finish_recovery(NULL); DataManager_ReadSettingsFile(); if (PartitionManager.Mount_By_Path("/system", false) && TWFunc::Path_Exists("/system/recovery-from-boot.p")) { - system("mv /system/recovery-from-boot.p /system/recovery-from-boot.bak"); + rename("/system/recovery-from-boot.p", "/system/recovery-from-boot.bak"); ui_print("Renamed stock recovery file in /system to prevent\nthe stock ROM from replacing TWRP.\n"); } PartitionManager.UnMount_By_Path("/system", false); diff --git a/resources.cpp b/resources.cpp deleted file mode 100644 index 2f2ea95b0..000000000 --- a/resources.cpp +++ /dev/null @@ -1,280 +0,0 @@ -// resource.cpp - Source to manage GUI resources - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -extern "C" { -#include "../common.h" -#include "../minuitwrp/minui.h" -#include "../recovery_ui.h" -} - -#include "rapidxml.hpp" -#include "objects.hpp" - -#define TMP_RESOURCE_NAME "/tmp/extract.bin" - -Resource::Resource(xml_node<>* node, ZipArchive* pZip) -{ - if (node && node->first_attribute("name")) - mName = node->first_attribute("name")->value(); -} - - -int Resource::ExtractResource(ZipArchive* pZip, - std::string folderName, - std::string fileName, - std::string fileExtn, - std::string destFile) -{ - if (!pZip) return -1; - - std::string src = folderName + "/" + fileName + fileExtn; - - const ZipEntry* binary = mzFindZipEntry(pZip, src.c_str()); - if (binary == NULL) { - return -1; - } - - unlink(destFile.c_str()); - int fd = creat(destFile.c_str(), 0666); - if (fd < 0) - return -1; - - int ret = 0; - if (!mzExtractZipEntryToFile(pZip, binary, fd)) - ret = -1; - - close(fd); - return ret; -} - -FontResource::FontResource(xml_node<>* node, ZipArchive* pZip) - : Resource(node, pZip) -{ - std::string file; - - mFont = NULL; - if (!node) return; - - if (node->first_attribute("filename")) - file = node->first_attribute("filename")->value(); - - if (ExtractResource(pZip, "fonts", file, ".dat", TMP_RESOURCE_NAME) == 0) - { - mFont = gr_loadFont(TMP_RESOURCE_NAME); - unlink(TMP_RESOURCE_NAME); - } - else - { - mFont = gr_loadFont(file.c_str()); - } -} - -FontResource::~FontResource() -{ -} - -ImageResource::ImageResource(xml_node<>* node, ZipArchive* pZip) - : Resource(node, pZip) -{ - std::string file; - - mSurface = NULL; - if (!node) return; - - if (node->first_attribute("filename")) - file = node->first_attribute("filename")->value(); - - if (ExtractResource(pZip, "images", file, ".png", TMP_RESOURCE_NAME) == 0) - { - res_create_surface(TMP_RESOURCE_NAME, &mSurface); - unlink(TMP_RESOURCE_NAME); - } else if (ExtractResource(pZip, "images", file, "", TMP_RESOURCE_NAME) == 0) - { - // JPG includes the .jpg extension in the filename so extension should be blank - res_create_surface(TMP_RESOURCE_NAME, &mSurface); - unlink(TMP_RESOURCE_NAME); - } - else - res_create_surface(file.c_str(), &mSurface); -} - -ImageResource::~ImageResource() -{ - if (mSurface) - res_free_surface(mSurface); -} - -AnimationResource::AnimationResource(xml_node<>* node, ZipArchive* pZip) - : Resource(node, pZip) -{ - std::string file; - int fileNum = 1; - - if (!node) return; - - if (node->first_attribute("filename")) - file = node->first_attribute("filename")->value(); - - for ( ; ; ) - { - std::ostringstream fileName; - fileName << file << std::setfill ('0') << std::setw (3) << fileNum; - - gr_surface surface; - if (pZip) - { - if (ExtractResource(pZip, "images", fileName.str(), ".png", TMP_RESOURCE_NAME) != 0) - break; - - if (res_create_surface(TMP_RESOURCE_NAME, &surface)) - break; - - unlink(TMP_RESOURCE_NAME); - } - else - { - if (res_create_surface(fileName.str().c_str(), &surface)) - break; - } - mSurfaces.push_back(surface); - fileNum++; - } -} - -AnimationResource::~AnimationResource() -{ - std::vector::iterator it; - - for (it = mSurfaces.begin(); it != mSurfaces.end(); ++it) - { - res_free_surface(*it); - } - mSurfaces.clear(); -} - -Resource* ResourceManager::FindResource(std::string name) -{ - std::vector::iterator iter; - - for (iter = mResources.begin(); iter != mResources.end(); iter++) - { - if (name == (*iter)->GetName()) - return (*iter); - } - return NULL; -} - -ResourceManager::ResourceManager(xml_node<>* resList, ZipArchive* pZip) -{ - xml_node<>* child; - - if (!resList) return; - - child = resList->first_node("resource"); - while (child != NULL) - { - xml_attribute<>* attr = child->first_attribute("type"); - if (!attr) - break; - - std::string type = attr->value(); - - if (type == "font") - { - FontResource* res = new FontResource(child, pZip); - if (res == NULL || res->GetResource() == NULL) - { - xml_attribute<>* attr_name = child->first_attribute("name"); - - if (!attr_name) { - std::string res_name = attr_name->value(); - LOGE("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); - } else - LOGE("Resource type (%s) failed to load\n", type.c_str()); - - delete res; - } - else - { - mResources.push_back((Resource*) res); - } - } - else if (type == "image") - { - ImageResource* res = new ImageResource(child, pZip); - if (res == NULL || res->GetResource() == NULL) - { - xml_attribute<>* attr_name = child->first_attribute("name"); - - if (!attr_name) { - std::string res_name = attr_name->value(); - LOGE("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); - } else - LOGE("Resource type (%s) failed to load\n", type.c_str()); - - delete res; - } - else - { - mResources.push_back((Resource*) res); - } - } - else if (type == "animation") - { - AnimationResource* res = new AnimationResource(child, pZip); - if (res == NULL || res->GetResource() == NULL) - { - xml_attribute<>* attr_name = child->first_attribute("name"); - - if (!attr_name) { - std::string res_name = attr_name->value(); - LOGE("Resource (%s)-(%s) failed to load\n", type.c_str(), res_name.c_str()); - } else - LOGE("Resource type (%s) failed to load\n", type.c_str()); - - delete res; - } - else - { - mResources.push_back((Resource*) res); - } - } - else - { - LOGE("Resource type (%s) not supported.\n", type.c_str()); - } - - child = child->next_sibling("resource"); - } -} - -ResourceManager::~ResourceManager() -{ - std::vector::iterator iter; - - for (iter = mResources.begin(); iter != mResources.end(); iter++) - { - delete *iter; - } - mResources.clear(); -} - diff --git a/twrp-functions.cpp b/twrp-functions.cpp index 3a2658e75..53a13f661 100644 --- a/twrp-functions.cpp +++ b/twrp-functions.cpp @@ -1,15 +1,19 @@ #include #include #include -#include -#include #include #include #include #include #include +#include +#include #include - +#include +#include +#include +#include +#include #include "twrp-functions.hpp" #include "partitions.hpp" #include "common.h" @@ -17,6 +21,23 @@ #include "bootloader.h" #include "variables.h" + +/* Execute a command */ + +int TWFunc::Exec_Cmd(string cmd, string &result) { + FILE* exec; + char buffer[128]; + int ret = 0; + exec = popen(cmd.c_str(), "r"); + if (!exec) return -1; + while(!feof(exec)) { + if (fgets(buffer, 128, exec) != NULL) + result += buffer; + } + ret = pclose(exec); + return ret; +} + /* Checks md5 for a path Return values: -1 : MD5 does not exist @@ -27,28 +48,20 @@ int TWFunc::Check_MD5(string File) { string Command, DirPath, MD5_File, Sline, Filename, MD5_File_Filename, OK; char line[255]; size_t pos; + string result; MD5_File = File + ".md5"; if (Path_Exists(MD5_File)) { DirPath = Get_Path(File); MD5_File = Get_Filename(MD5_File); - Command = "cd '" + DirPath + "' && /sbin/busybox md5sum -c '" + MD5_File + "' > /tmp/md5output"; - system(Command.c_str()); - FILE * cs = fopen("/tmp/md5output", "r"); - if (cs == NULL) { - LOGE("Unable to open md5 output file.\n"); - return 0; - } - - fgets(line, sizeof(line), cs); - fclose(cs); - - Sline = line; - pos = Sline.find(":"); + chdir(DirPath.c_str()); + Command = "/sbin/busybox md5sum -c " + MD5_File; + Exec_Cmd(Command, result); + pos = result.find(":"); if (pos != string::npos) { Filename = Get_Filename(File); - MD5_File_Filename = Sline.substr(0, pos); - OK = Sline.substr(pos + 2, Sline.size() - pos - 2); + MD5_File_Filename = result.substr(0, pos); + OK = result.substr(pos + 2, result.size() - pos - 2); if (Filename == MD5_File_Filename && (OK == "OK" || OK == "OK\n")) { //MD5 is good, return 1 ret = 1; @@ -116,49 +129,50 @@ void TWFunc::install_htc_dumlock(void) { return; ui_print("Installing HTC Dumlock to system...\n"); - system("cp /res/htcd/htcdumlocksys /system/bin/htcdumlock && chmod 755 /system/bin/htcdumlock"); + copy_file("/res/htcd/htcdumlocksys", "/system/bin/htcdumlock", 0755); if (!Path_Exists("/system/bin/flash_image")) { ui_print("Installing flash_image...\n"); - system("cp /res/htcd/flash_imagesys /system/bin/flash_image && chmod 755 /system/bin/flash_image"); + copy_file("/res/htcd/flash_imagesys", "/system/bin/flash_image", 0755); need_libs = 1; } else ui_print("flash_image is already installed, skipping...\n"); if (!Path_Exists("/system/bin/dump_image")) { ui_print("Installing dump_image...\n"); - system("cp /res/htcd/dump_imagesys /system/bin/dump_image && chmod 755 /system/bin/dump_image"); + copy_file("/res/htcd/dump_imagesys", "/system/bin/dump_image", 0755); need_libs = 1; } else ui_print("dump_image is already installed, skipping...\n"); if (need_libs) { ui_print("Installing libs needed for flash_image and dump_image...\n"); - system("cp /res/htcd/libbmlutils.so /system/lib && chmod 755 /system/lib/libbmlutils.so"); - system("cp /res/htcd/libflashutils.so /system/lib && chmod 755 /system/lib/libflashutils.so"); - system("cp /res/htcd/libmmcutils.so /system/lib && chmod 755 /system/lib/libmmcutils.so"); - system("cp /res/htcd/libmtdutils.so /system/lib && chmod 755 /system/lib/libmtdutils.so"); + copy_file("/res/htcd/libbmlutils.so", "/system/lib/libbmlutils.so", 0755); + copy_file("/res/htcd/libflashutils.so", "/system/lib/libflashutils.so", 0755); + copy_file("/res/htcd/libmmcutils.so", "/system/lib/libmmcutils.so", 0755); + copy_file("/res/htcd/libmtdutils.so", "/system/lib/libmtdutils.so", 0755); } ui_print("Installing HTC Dumlock app...\n"); mkdir("/data/app", 0777); - system("rm /data/app/com.teamwin.htcdumlock*"); - system("cp /res/htcd/HTCDumlock.apk /data/app/com.teamwin.htcdumlock.apk"); + unlink("/data/app/com.teamwin.htcdumlock*"); + copy_file("/res/htcd/HTCDumlock.apk", "/data/app/com.teamwin.htcdumlock.apk", 0777); sync(); ui_print("HTC Dumlock is installed.\n"); } void TWFunc::htc_dumlock_restore_original_boot(void) { + string status; if (!PartitionManager.Mount_By_Path("/sdcard", true)) return; ui_print("Restoring original boot...\n"); - system("htcdumlock restore"); + Exec_Cmd("htcdumlock restore", status); ui_print("Original boot restored.\n"); } void TWFunc::htc_dumlock_reflash_recovery_to_boot(void) { + string status; if (!PartitionManager.Mount_By_Path("/sdcard", true)) return; - ui_print("Reflashing recovery to boot...\n"); - system("htcdumlock recovery noreboot"); + Exec_Cmd("htcdumlock recovery noreboot", status); ui_print("Recovery is flashed to boot.\n"); } @@ -188,7 +202,7 @@ unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { struct stat st; char path2[4096], filename[4096]; unsigned long long dusize = 0; - + unsigned long long dutemp = 0; // Make a copy of path in case the data in the pointer gets overwritten later strcpy(path2, Path.c_str()); @@ -196,6 +210,7 @@ unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { if (d == NULL) { LOGE("error opening '%s'\n", path2); + LOGE("error: %s\n", strerror(errno)); return 0; } @@ -206,7 +221,9 @@ unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { strcpy(filename, path2); strcat(filename, "/"); strcat(filename, de->d_name); - dusize += Get_Folder_Size(filename, Display_Error); + dutemp = Get_Folder_Size(filename, Display_Error); + dusize += dutemp; + dutemp = 0; } else if (de->d_type == DT_REG) { @@ -218,14 +235,12 @@ unsigned long long TWFunc::Get_Folder_Size(string Path, bool Display_Error) { } } closedir(d); - return dusize; } bool TWFunc::Path_Exists(string Path) { // Check to see if the Path exists struct stat st; - if (stat(Path.c_str(), &st) != 0) return false; else @@ -305,39 +320,38 @@ void TWFunc::copy_log_file(const char* source, const char* destination, int appe // record any intent we were asked to communicate back to the system. // this function is idempotent: call it as many times as you like. void TWFunc::twfinish_recovery(const char *send_intent) { - // By this point, we're ready to return to the main system... - if (send_intent != NULL) { - FILE *fp = fopen_path(INTENT_FILE, "w"); - if (fp == NULL) { - LOGE("Can't open %s\n", INTENT_FILE); - } else { - fputs(send_intent, fp); - check_and_fclose(fp, INTENT_FILE); - } - } + // By this point, we're ready to return to the main system... + if (send_intent != NULL) { + FILE *fp = fopen_path(INTENT_FILE, "w"); + if (fp == NULL) { + LOGE("Can't open %s\n", INTENT_FILE); + } else { + fputs(send_intent, fp); + check_and_fclose(fp, INTENT_FILE); + } + } - // Copy logs to cache so the system can find out what happened. - copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); - copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); - copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); - chmod(LOG_FILE, 0600); - chown(LOG_FILE, 1000, 1000); // system user - chmod(LAST_LOG_FILE, 0640); - chmod(LAST_INSTALL_FILE, 0644); - - // Reset to normal system boot so recovery won't cycle indefinitely. - struct bootloader_message boot; - memset(&boot, 0, sizeof(boot)); - set_bootloader_message(&boot); - - // Remove the command file, so recovery won't repeat indefinitely. - if (system("mount /cache") != 0 || - (unlink(COMMAND_FILE) && errno != ENOENT)) { - LOGW("Can't unlink %s\n", COMMAND_FILE); - } + // Copy logs to cache so the system can find out what happened. + copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); + copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); + copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); + chmod(LOG_FILE, 0600); + chown(LOG_FILE, 1000, 1000); // system user + chmod(LAST_LOG_FILE, 0640); + chmod(LAST_INSTALL_FILE, 0644); + + // Reset to normal system boot so recovery won't cycle indefinitely. + struct bootloader_message boot; + memset(&boot, 0, sizeof(boot)); + set_bootloader_message(&boot); + + // Remove the command file, so recovery won't repeat indefinitely. + if (!PartitionManager.Mount_By_Path("/system", true) || (unlink(COMMAND_FILE) && errno != ENOENT)) { + LOGW("Can't unlink %s\n", COMMAND_FILE); + } - system("umount /cache"); - sync(); // For good measure. + PartitionManager.UnMount_By_Path("/cache", true); + sync(); // For good measure. } // reboot: Reboot the system. Return -1 on error, no return on success @@ -377,13 +391,65 @@ void TWFunc::check_and_run_script(const char* script_file, const char* display_n { // Check for and run startup script if script exists struct stat st; + string result; if (stat(script_file, &st) == 0) { ui_print("Running %s script...\n", display_name); - char command[255]; - strcpy(command, "chmod 755 "); - strcat(command, script_file); - system(command); - system(script_file); + chmod(script_file, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + TWFunc::Exec_Cmd(script_file, result); ui_print("\nFinished running %s script.\n", display_name); } } + +int TWFunc::removeDir(const string path, bool skipParent) { + DIR *d = opendir(path.c_str()); + int r = 0; + string new_path; + + if (d == NULL) { + LOGE("Error opening '%s'\n", path.c_str()); + return -1; + } + + if (d) { + struct dirent *p; + while (!r && (p = readdir(d))) { + LOGI("checking :%s\n", p->d_name); + if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) + continue; + new_path = path + "/"; + new_path.append(p->d_name); + if (p->d_type == DT_DIR) { + r = removeDir(new_path, true); + if (!r) { + if (p->d_type == DT_DIR) + r = rmdir(new_path.c_str()); + else + LOGI("Unable to removeDir '%s': %s\n", new_path.c_str(), strerror(errno)); + } + } else { + r = unlink(new_path.c_str()); + if (!r) + LOGI("Unable to unlink '%s'\n", new_path.c_str()); + } + } + closedir(d); + + if (!r) { + if (skipParent) + return 0; + else + r = rmdir(path.c_str()); + } + } + return r; +} + +int TWFunc::copy_file(string src, string dst, int mode) { + LOGI("Copying file %s to %s\n", src.c_str(), dst.c_str()); + ifstream srcfile(src.c_str(), ios::binary); + ofstream dstfile(dst.c_str(), ios::binary); + dstfile << srcfile.rdbuf(); + srcfile.close(); + dstfile.close(); + return 0; +} diff --git a/twrp-functions.hpp b/twrp-functions.hpp index b28d04ba5..7cef37f31 100644 --- a/twrp-functions.hpp +++ b/twrp-functions.hpp @@ -36,6 +36,9 @@ public: static void twfinish_recovery(const char *send_intent); // Writes the log to last_log static int tw_reboot(RebootCommand command); // Prepares the device for rebooting static void check_and_run_script(const char* script_file, const char* display_name); // checks for the existence of a script, chmods it to 755, then runs it + static int Exec_Cmd(string cmd, string &result); //execute a command and return the result as a string by reference + static int removeDir(const string path, bool removeParent); //recursively remove a directory + static int copy_file(string src, string dst, int mode); //copy file from src to dst with mode permissions private: static void check_and_fclose(FILE *fp, const char *name); diff --git a/twrpTar.cpp b/twrpTar.cpp new file mode 100644 index 000000000..0008de457 --- /dev/null +++ b/twrpTar.cpp @@ -0,0 +1,409 @@ +/* + Copyright 2012 bigbiff/Dees_Troy TeamWin + This file is part of TWRP/TeamWin Recovery Project. + + TWRP is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + TWRP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TWRP. If not, see . +*/ + +extern "C" { + #include "libtar/libtar.h" +} +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "twrpTar.hpp" +#include "common.h" +#include "data.hpp" +#include "variables.h" +#include +#include "twrp-functions.hpp" + +using namespace std; + +int twrpTar::Generate_Multiple_Archives(string Path, string fn) { + DIR* d; + struct dirent* de; + struct stat st; + string FileName; + char actual_filename[255]; + + sprintf(actual_filename, fn.c_str(), Archive_File_Count); + + if (has_data_media == 1 && Path.size() >= 11 && strncmp(Path.c_str(), "/data/media", 11) == 0) + return 0; // Skip /data/media + LOGI("Path: '%s', archive filename: '%s'\n", Path.c_str(), actual_filename); + + d = opendir(Path.c_str()); + if (d == NULL) + { + LOGE("error opening '%s' -- error: %s\n", Path.c_str(), strerror(errno)); + closedir(d); + return -1; + } + while ((de = readdir(d)) != NULL) + { + FileName = Path + "/"; + FileName += de->d_name; + if (has_data_media == 1 && FileName.size() >= 11 && strncmp(FileName.c_str(), "/data/media", 11) == 0) + continue; // Skip /data/media + if (de->d_type == DT_DIR && strcmp(de->d_name, ".") != 0 && strcmp(de->d_name, "..") != 0) + { + unsigned long long folder_size = TWFunc::Get_Folder_Size(FileName, false); + if (Archive_Current_Size + folder_size > MAX_ARCHIVE_SIZE) { + if (Generate_Multiple_Archives(FileName, fn) < 0) + return -1; + } else { + //FileName += "/"; + LOGI("Adding folder '%s'\n", FileName.c_str()); + if (tarDirs(FileName, actual_filename, true) < 0) + return -1; + Archive_Current_Size += folder_size; + } + } + else if (de->d_type == DT_REG || de->d_type == DT_LNK) + { + stat(FileName.c_str(), &st); + + if (Archive_Current_Size != 0 && Archive_Current_Size + st.st_size > MAX_ARCHIVE_SIZE) { + LOGI("Closing tar '%s', ", actual_filename); + closeTar(actual_filename, false); + Archive_File_Count++; + if (TWFunc::Get_File_Size(actual_filename) == 0) { + LOGE("Backup file size for '%s' is 0 bytes.\n", actual_filename); + return false; + } + if (Archive_File_Count > 999) { + LOGE("Archive count is too large!\n"); + return -1; + } + Archive_Current_Size = 0; + sprintf(actual_filename, fn.c_str(), Archive_File_Count); + LOGI("Creating tar '%s'\n", actual_filename); + ui_print("Creating archive %i...\n", Archive_File_Count + 1); + createTar(Path, actual_filename); + } + LOGI("Adding file: '%s'... ", FileName.c_str()); + if (addFile(FileName, true) < 0) + return -1; + Archive_Current_Size += st.st_size; + LOGI("added successfully, archive size: %llu\n", Archive_Current_Size); + if (st.st_size > 2147483648LL) + LOGE("There is a file that is larger than 2GB in the file system\n'%s'\nThis file may not restore properly\n", FileName.c_str()); + } + } + closedir(d); + return 0; +} + +int twrpTar::Split_Archive(string Path, string fn) +{ + string temp = fn + "%03i"; + char actual_filename[255]; + + Archive_File_Count = 0; + Archive_Current_Size = 0; + sprintf(actual_filename, temp.c_str(), Archive_File_Count); + createTar(Path, actual_filename); + DataManager::GetValue(TW_HAS_DATA_MEDIA, has_data_media); + ui_print("Creating archive 1...\n"); + if (Generate_Multiple_Archives(Path, temp) < 0) { + LOGE("Error generating file list\n"); + return -1; + } + sprintf(actual_filename, temp.c_str(), Archive_File_Count); + closeTar(actual_filename, false); + LOGI("Done, created %i archives.\n", (Archive_File_Count++)); + return (Archive_File_Count); +} + +int twrpTar::extractTar(string rootdir, string fn) { + char* charRootDir = (char*) rootdir.c_str(); + bool gzip = false; + if (openTar(rootdir, fn, gzip) == -1) + return -1; + if (tar_extract_all(t, charRootDir) != 0) { + LOGE("Unable to extract tar archive '%s'\n", fn.c_str()); + return -1; + } + if (tar_close(t) != 0) { + LOGE("Unable to close tar file\n"); + return -1; + } + return 0; +} + +int twrpTar::extract(string rootdir, string fn) { + int len = 3; + char header[len]; + string::size_type i = 0; + int firstbyte = 0; + int secondbyte = 0; + int ret; + ifstream f; + f.open(fn.c_str(), ios::in | ios::binary); + f.get(header, len); + firstbyte = header[i] & 0xff; + secondbyte = header[++i] & 0xff; + f.close(); + if (firstbyte == 0x1f && secondbyte == 0x8b) { + //if you return the extractTGZ function directly, stack crashes happen + LOGI("Extracting gzipped tar\n"); + ret = extractTGZ(rootdir, fn); + return ret; + } + else { + LOGI("Extracting uncompressed tar\n"); + return extractTar(rootdir, fn); + } +} + +int twrpTar::tarDirs(string dir, string fn, bool include_root) { + DIR* d; + string mainfolder = dir + "/", subfolder; + char buf[1024]; + char* charTarFile = (char*) fn.c_str(); + d = opendir(dir.c_str()); + if (d != NULL) { + struct dirent* de; + while ((de = readdir(d)) != NULL) { + LOGI("adding %s\n", de->d_name); +#ifdef RECOVERY_SDCARD_ON_DATA + if ((dir == "/data" || dir == "/data/") && strcmp(de->d_name, "media") == 0) continue; +#endif + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; + + subfolder = mainfolder; + subfolder += de->d_name; + strcpy(buf, subfolder.c_str()); + if (de->d_type == DT_DIR) { + if (include_root) { + if (tar_append_tree(t, buf, NULL) != 0) { + LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile); + return -1; + } + } else { + string temp = Strip_Root_Dir(buf); + char* charTarPath = (char*) temp.c_str(); + if (tar_append_tree(t, buf, charTarPath) != 0) { + LOGE("Error appending '%s' to tar archive '%s'\n", buf, charTarFile); + return -1; + } + } + } else if (dir != "/" && (de->d_type == DT_REG || de->d_type == DT_LNK)) { + if (addFile(buf, include_root) != 0) + return -1; + } + fflush(NULL); + } + closedir(d); + } + return 0; +} + +int twrpTar::createTGZ(string dir, string fn) { + bool gzip = true; + if (createTar(dir, fn) == -1) + return -1; + if (tarDirs(dir, fn, false) == -1) + return -1; + if (closeTar(fn, gzip) == -1) + return -1; + return 0; +} + +int twrpTar::create(string dir, string fn) { + bool gzip = false; + if (createTar(dir, fn) == -1) + return -1; + if (tarDirs(dir, fn, false) == -1) + return -1; + if (closeTar(fn, gzip) == -1) + return -1; + return 0; +} + +int twrpTar::addFilesToExistingTar(vector files, string fn) { + char* charTarFile = (char*) fn.c_str(); + + if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) == -1) + return -1; + removeEOT(charTarFile); + if (tar_open(&t, charTarFile, NULL, O_WRONLY | O_APPEND | O_LARGEFILE, 0644, TAR_GNU) == -1) + return -1; + for (unsigned int i = 0; i < files.size(); ++i) { + char* file = (char*) files.at(i).c_str(); + if (tar_append_file(t, file, file) == -1) + return -1; + } + if (tar_append_eof(t) == -1) + return -1; + if (tar_close(t) == -1) + return -1; + return 0; +} + +int twrpTar::createTar(string rootdir, string fn) { + char* charTarFile = (char*) fn.c_str(); + char* charRootDir = (char*) rootdir.c_str(); + int use_compression = 0; + + DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); + LOGI("2nd compression\n"); + if (use_compression) { + string cmd = "pigz - > '" + fn + "'"; + p = popen(cmd.c_str(), "w"); + fd = fileno(p); + if (!p) return -1; + if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { + pclose(p); + return -1; + } + } + else { + if (tar_open(&t, charTarFile, NULL, O_WRONLY | O_CREAT | O_LARGEFILE, 0644, TAR_GNU) == -1) + return -1; + } + return 0; +} + +int twrpTar::openTar(string rootdir, string fn, bool gzip) { + char* charRootDir = (char*) rootdir.c_str(); + char* charTarFile = (char*) fn.c_str(); + + if (gzip) { + LOGI("Opening as a gzip\n"); + string cmd = "pigz -d -c '" + fn + "'"; + FILE* pipe = popen(cmd.c_str(), "r"); + int fd = fileno(pipe); + if (!pipe) return -1; + if(tar_fdopen(&t, fd, charRootDir, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { + LOGI("tar_fdopen returned error\n"); + pclose(pipe); + return -1; + } + } + else { + if (tar_open(&t, charTarFile, NULL, O_RDONLY | O_LARGEFILE, 0644, TAR_GNU) != 0) { + LOGE("Unable to open tar archive '%s'\n", charTarFile); + return -1; + } + } + return 0; +} + +string twrpTar::Strip_Root_Dir(string Path) { + string temp; + size_t slash; + + if (Path.substr(0, 1) == "/") + temp = Path.substr(1, Path.size() - 1); + else + temp = Path; + slash = temp.find("/"); + if (slash == string::npos) + return temp; + else { + string stripped; + + stripped = temp.substr(slash, temp.size() - slash); + return stripped; + } + return temp; +} + +int twrpTar::addFile(string fn, bool include_root) { + char* charTarFile = (char*) fn.c_str(); + if (include_root) { + if (tar_append_file(t, charTarFile, NULL) == -1) + return -1; + } else { + string temp = Strip_Root_Dir(fn); + char* charTarPath = (char*) temp.c_str(); + if (tar_append_file(t, charTarFile, charTarPath) == -1) + return -1; + } + return 0; +} + +int twrpTar::closeTar(string fn, bool gzip) { + int use_compression; + DataManager::GetValue(TW_USE_COMPRESSION_VAR, use_compression); + + if (tar_append_eof(t) != 0) { + LOGE("tar_append_eof(): %s\n", strerror(errno)); + tar_close(t); + return -1; + } + if (tar_close(t) != 0) { + LOGE("Unable to close tar archive: '%s'\n", fn.c_str()); + return -1; + } + if (use_compression || gzip) { + LOGI("Closing popen and fd\n"); + pclose(p); + close(fd); + } + return 0; +} + +int twrpTar::removeEOT(string tarFile) { + char* charTarFile = (char*) tarFile.c_str(); + off_t tarFileEnd; + while (th_read(t) == 0) { + if (TH_ISREG(t)) + tar_skip_regfile(t); + tarFileEnd = lseek(t->fd, 0, SEEK_CUR); + } + if (tar_close(t) == -1) + return -1; + if (truncate(charTarFile, tarFileEnd) == -1) + return -1; + return 0; +} + +int twrpTar::compress(string fn) { + string cmd = "pigz " + fn; + p = popen(cmd.c_str(), "r"); + if (!p) return -1; + char buffer[128]; + string result = ""; + while(!feof(p)) { + if(fgets(buffer, 128, p) != NULL) + result += buffer; + } + pclose(p); + return 0; +} + +int twrpTar::extractTGZ(string rootdir, string fn) { + string splatrootdir(rootdir); + bool gzip = true; + char* splatCharRootDir = (char*) splatrootdir.c_str(); + if (openTar(rootdir, fn, gzip) == -1) + return -1; + int ret = tar_extract_all(t, splatCharRootDir); + if (tar_close(t) != 0) { + LOGE("Unable to close tar file\n"); + return -1; + } + return 0; +} diff --git a/twrpTar.hpp b/twrpTar.hpp new file mode 100644 index 000000000..554a01dbd --- /dev/null +++ b/twrpTar.hpp @@ -0,0 +1,61 @@ +/* + Copyright 2012 bigbiff/Dees_Troy TeamWin + This file is part of TWRP/TeamWin Recovery Project. + + TWRP is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + TWRP is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with TWRP. If not, see . +*/ + +extern "C" { + #include "libtar/libtar.h" +} +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +class twrpTar { + public: + int create(string dir, string fn); + int createTGZ(string dir, string fn); + int extract(string rootDir, string fn); + int compress(string fn); + int extractTGZ(string rootdir, string fn); + int uncompress(string fn); + int addFilesToExistingTar(vector files, string tarFile); + int createTar(string dir, string fn); + int openTar(string rootdir, string fn, bool gzip); + int addFile(string fn, bool include_root); + int closeTar(string fn, bool gzip); + int Split_Archive(string Path, string fn); + private: + int removeEOT(string tarFile); + int extractTar(string rootdir, string fn); + int tarDirs(string dir, string fn, bool include_root); + int Generate_Multiple_Archives(string Path, string fn); + + private: + int has_data_media; + int Archive_File_Count; + unsigned long long Archive_Current_Size; + string Strip_Root_Dir(string Path); + TAR *t; + FILE* p; + int fd; +}; diff --git a/variables.h b/variables.h index 0534ce019..f56a46943 100644 --- a/variables.h +++ b/variables.h @@ -17,13 +17,13 @@ #ifndef _VARIABLES_HEADER_ #define _VARIABLES_HEADER_ -#define TW_VERSION_STR "2.3.3.0" +#define TW_VERSION_STR "2.4.0.0alpha" #define TW_USE_COMPRESSION_VAR "tw_use_compression" #define TW_IGNORE_IMAGE_SIZE "tw_ignore_image_size" #define TW_FILENAME "tw_filename" #define TW_ZIP_INDEX "tw_zip_index" -#define TW_ZIP_QUEUE_COUNT "tw_zip_queue_count" +#define TW_ZIP_QUEUE_COUNT "tw_zip_queue_count" #define MAX_BACKUP_NAME_LEN 64 #define TW_BACKUP_TEXT "tw_backup_text" @@ -164,7 +164,7 @@ // tw_sp2_is_mountable // tw_sp3_is_mountable -// Max archive size for tar backups before we split (1.5GB) -#define MAX_ARCHIVE_SIZE 1610612736LLU +// Max archive size for tar backups before we split (4GB) +#define MAX_ARCHIVE_SIZE 4294967296LLU #endif // _VARIABLES_HEADER_ -- cgit v1.2.3