diff --git a/messages/make_message_src.sh b/messages/make_message_src.sh index 82488e81..41ffedc0 100755 --- a/messages/make_message_src.sh +++ b/messages/make_message_src.sh @@ -1,5 +1,6 @@ #!/bin/sh + set -e KERNEL_NAME=`uname -s` @@ -32,8 +33,11 @@ else fi if [ "$#" -ne "1" ]; then + echo "Missing 1 parameter" echo "Usage: $0 object_file" exit 1 +else + echo "$1" fi case $KERNEL_NAME in @@ -49,6 +53,23 @@ esac cd ${BASENAME} make_obj() { + RETREAT="../../" + MODE="static" + + case $KERNEL_NAME in + MINGW32_NT*) + CURRENT_MSG_PATH="../../oss/messages/$BASENAME" + if [ -d "${CURRENT_MSG_PATH}" ]; then + cd ${CURRENT_MSG_PATH} + echo "Changing directory" + RETREAT="../../../../messages/" + fi + MODE="common" + ;; + *) + ;; + esac + echo "Processing ${BASENAME}" # Create a fresh work directory @@ -60,12 +81,19 @@ make_obj() { # Generate files ${GENRB} -d work -q *.txt cd work + echo "dir: $(pwd)" ls *.res >packagelist.txt - ${PKGDATA} -p ${BASENAME} -m static -q packagelist.txt >/dev/null + ${PKGDATA} --verbose -p ${BASENAME} -m ${MODE} -q packagelist.txt > /dev/null + case $KERNEL_NAME in MINGW32_NT*) - mv ${BASENAME}.dat ../../ + mv ${BASENAME}.dat ${RETREAT}${BASENAME}.dat + if [ -e "${RETREAT}${BASENAME}.dat" ]; then + echo "File copied! -- ${RETREAT}" + else + echo "File does not exist" + fi ;; FreeBSD) # pkgdata with -m static generates an ar(1) archive @@ -98,6 +126,7 @@ make_obj() { # Clean up cd .. + echo "Finishing" rm -rf work } diff --git a/src/Makefile.am b/src/Makefile.am index 7afe5ef7..033bad39 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -122,6 +122,7 @@ nobase_pkginclude_HEADERS = \ libltfs/arch/arch_info.h \ libltfs/arch/time_internal.h \ libltfs/arch/errormap.h \ + libltfs/arch/ltfs_arch_ops.h \ tape_drivers/ibm_tape.h \ tape_drivers/spc_op_codes.h \ tape_drivers/ssc_op_codes.h \ diff --git a/src/iosched/unified.c b/src/iosched/unified.c index 43cdacd2..635b40c2 100644 --- a/src/iosched/unified.c +++ b/src/iosched/unified.c @@ -1742,7 +1742,7 @@ ssize_t _unified_insert_new_request(const char *buf, off_t offset, size_t count, { int ret; struct dentry_priv *dpr = d->iosched_priv; - struct write_request *new_req; + struct write_request *new_req = NULL; size_t copy_count; if (! (*cache)) { @@ -1764,7 +1764,7 @@ ssize_t _unified_insert_new_request(const char *buf, off_t offset, size_t count, memcpy(cache_manager_get_object_data(*cache), buf, copy_count); /* Store new write request */ - new_req = calloc(1, sizeof(struct write_request)); + new_req = (struct write_request*)calloc(1, sizeof(struct write_request)); if (! new_req) { ltfsmsg(LTFS_ERR, 13018E); _unified_cache_free(*cache, 0, priv); @@ -2307,10 +2307,10 @@ int unified_set_profiler(char *work_dir, bool enable, void *iosched_handle) { int rc = 0; char *path; - FILE *p; + FILE * p = NULL; struct timer_info timerinfo; struct unified_data *priv = iosched_handle; - + if (enable) { if (priv->profiler) return 0; @@ -2325,7 +2325,7 @@ int unified_set_profiler(char *work_dir, bool enable, void *iosched_handle) return -LTFS_NO_MEMORY; } - p = fopen(path, PROFILER_FILE_MODE); + arch_fopen(path, PROFILER_FILE_MODE,p); free(path); @@ -2361,6 +2361,7 @@ struct iosched_ops unified_ops = { .set_profiler = unified_set_profiler, }; + struct iosched_ops *iosched_get_ops(void) { return &unified_ops; @@ -2370,6 +2371,7 @@ struct iosched_ops *iosched_get_ops(void) extern char iosched_unified_dat[]; #endif + const char *iosched_get_message_bundle_name(void **message_data) { #ifndef mingw_PLATFORM diff --git a/src/kmi/flatfile.c b/src/kmi/flatfile.c index 53735d49..53d3c8b5 100644 --- a/src/kmi/flatfile.c +++ b/src/kmi/flatfile.c @@ -51,11 +51,12 @@ #include #include "libltfs/kmi_ops.h" #include "libltfs/ltfs_fuse_version.h" -#include -#include "key_format_ltfs.h" +#include "key_format_ltfs.h" +#include #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" + #endif struct kmi_flatfile_options_data { @@ -158,7 +159,7 @@ static int convert_option(const unsigned char * const path, unsigned char **dk_l */ void *flatfile_init(struct ltfs_volume *vol) { - void* km; + void *km; km = key_format_ltfs_init(vol); if (km) diff --git a/src/kmi/key_format_ltfs.c b/src/kmi/key_format_ltfs.c index 58886d45..4315153e 100644 --- a/src/kmi/key_format_ltfs.c +++ b/src/kmi/key_format_ltfs.c @@ -48,7 +48,7 @@ */ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #endif #include "libltfs/ltfs.h" #include "libltfs/base64.h" diff --git a/src/kmi/simple.c b/src/kmi/simple.c index e4ce950c..f5938871 100644 --- a/src/kmi/simple.c +++ b/src/kmi/simple.c @@ -49,11 +49,11 @@ #include "libltfs/kmi_ops.h" #include "libltfs/ltfs_fuse_version.h" -#include #include "key_format_ltfs.h" - +#include #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" + #endif struct kmi_simple_options_data { @@ -84,7 +84,7 @@ static struct fuse_opt kmi_simple_options[] = { */ void *simple_init(struct ltfs_volume *vol) { - void* km; + void *km; km = key_format_ltfs_init(vol); if (km) @@ -193,9 +193,9 @@ int simple_parse_opts(void *opt_args) + strlen((char *) key[i].dk) + strlen(":") + strlen((char *) key[i].dki) + 1; if (priv.dk_list) - priv.dk_list = realloc(priv.dk_list, dk_list_len); + priv.dk_list = (unsigned char*)realloc(priv.dk_list, dk_list_len); else - priv.dk_list = calloc(dk_list_len, sizeof(unsigned char)); + priv.dk_list = (unsigned char*)calloc(dk_list_len, sizeof(unsigned char)); if (priv.dk_list == NULL) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; @@ -203,10 +203,10 @@ int simple_parse_opts(void *opt_args) *(priv.dk_list + original_dk_list_len) = '\0'; if (original_dk_list_len) - strcat((char *) priv.dk_list, "/"); - strcat((char *) priv.dk_list, (char *) key[i].dk); - strcat((char *) priv.dk_list, ":"); - strcat((char *) priv.dk_list, (char *) key[i].dki); + arch_strcat((char *) priv.dk_list, dk_list_len, "/"); + arch_strcat((char *) priv.dk_list, dk_list_len,(char *) key[i].dk); + arch_strcat((char *) priv.dk_list, dk_list_len, ":"); + arch_strcat((char *) priv.dk_list, dk_list_len,(char *) key[i].dki); } return 0; diff --git a/src/libltfs/arch/errormap.c b/src/libltfs/arch/errormap.c index 9c5f34b6..c121d215 100644 --- a/src/libltfs/arch/errormap.c +++ b/src/libltfs/arch/errormap.c @@ -48,7 +48,7 @@ */ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #endif #ifdef __FreeBSD__ @@ -500,7 +500,7 @@ int errormap_fuse_error(int val) return -EIO; } -char* errormap_msg_id(int val) +char *errormap_msg_id(int val) { struct error_map *out; diff --git a/src/libltfs/arch/errormap.h b/src/libltfs/arch/errormap.h index 5318ce12..42292dd9 100644 --- a/src/libltfs/arch/errormap.h +++ b/src/libltfs/arch/errormap.h @@ -75,7 +75,7 @@ int errormap_fuse_error(int val); * @param val Error code to look up in the table. This value must be less than or equal to zero. * @return pointer of the mapped message. if val is not found in the error table, NULL is returned. */ -char* errormap_msg_id(int val); +char *errormap_msg_id(int val); #ifdef __cplusplus } diff --git a/src/libltfs/arch/filename_handling.c b/src/libltfs/arch/filename_handling.c index 626f55b9..20db7c76 100644 --- a/src/libltfs/arch/filename_handling.c +++ b/src/libltfs/arch/filename_handling.c @@ -55,7 +55,7 @@ bool _replace_invalid_chars(char * file_name, bool * dosdev); char * _generate_target_file_name(const char *prefix, const char *extension, int suffix, bool dosdev); int _utf8_strlen(const char *s); -int _utf8_strncpy(char *t, const char *s, int n); +int _utf8_strcpy(char *t, const char *s, int n); #endif /** @@ -73,11 +73,11 @@ void update_platform_safe_name(struct dentry* dentry, bool handle_invalid_char, char source_file_name[LTFS_FILENAME_MAX*4+1]; char *source_file_name_prefix, *source_file_name_extension; char *target_file_name; - struct dentry *d; + struct dentry* d = NULL; int ret; dentry->platform_safe_name = NULL; - strcpy(source_file_name, dentry->name.name); + arch_strcpy_auto(source_file_name, dentry->name.name); if (_replace_invalid_chars(source_file_name, &dosdev)) { if (! handle_invalid_char) @@ -130,7 +130,7 @@ void update_platform_safe_name(struct dentry* dentry, bool handle_invalid_char, } } #else - dentry->platform_safe_name = strdup(dentry->name.name); + dentry->platform_safe_name = arch_strdup(dentry->name.name); #endif } @@ -208,7 +208,7 @@ char * _generate_target_file_name(const char *prefix, const char *extension, int target = NULL; if (suffix) { - sprintf( suffix_string, "~%d", suffix ); + arch_sprintf_auto( suffix_string, "~%d", suffix ); prefix_length = prefix ? _utf8_strlen(prefix) : 0; extension_length = extension ? _utf8_strlen(extension) : 0; @@ -218,7 +218,7 @@ char * _generate_target_file_name(const char *prefix, const char *extension, int /* Need to trim source file name to add suffix */ if (! dosdev && prefix_length > suffix_length) { /* Prefix is to be trimmed. */ - _utf8_strncpy(trimmed_name, prefix, prefix_length - suffix_length); + _utf8_strcpy(trimmed_name, prefix, prefix_length - suffix_length); if (extension) ret = asprintf(&target, "%s%s.%s", trimmed_name, suffix_string, extension); else @@ -226,7 +226,7 @@ char * _generate_target_file_name(const char *prefix, const char *extension, int } else if (extension_length > suffix_length) { /* Extension is to be trimmed. */ - _utf8_strncpy(trimmed_name, extension, extension_length - suffix_length); + _utf8_strcpy(trimmed_name, extension, extension_length - suffix_length); ret = asprintf(&target, "%s%s.%s", prefix, suffix_string, trimmed_name); } else { /* Unable to generate target file name. NULL is to be returned. */ @@ -241,7 +241,7 @@ char * _generate_target_file_name(const char *prefix, const char *extension, int if (extension) ret = asprintf(&target, "%s.%s", prefix, extension); else { - target = strdup(prefix); + target = arch_strdup(prefix); ret = target ? strlen(target) : -1; } } @@ -273,7 +273,7 @@ int _utf8_strlen(const char *s) * @param s Source string to be copied. * @param n Maximum length to be copied. */ -int _utf8_strncpy(char *t, const char *s, int n) +int _utf8_strcpy(char *t, const char *s, int n) { int ret = 0; diff --git a/src/libltfs/arch/ltfs_arch_ops.h b/src/libltfs/arch/ltfs_arch_ops.h new file mode 100644 index 00000000..ab2463b9 --- /dev/null +++ b/src/libltfs/arch/ltfs_arch_ops.h @@ -0,0 +1,220 @@ +/************************************************************************************* +** OO_Copyright_BEGIN +** +** +** Copyright 2025 IBM Corp. 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. Neither the name of the copyright holder 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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. +** +** +** OO_Copyright_END +************************************************************************************* +** +** Module Name: +** +** arch/ltfs_arch_ops.h +** +** Abstract: +** +** LTFS specific platform methods version implementation +** +** Author(s): +** +** Gustavo Padilla Valdez +** IBM Mexico Software Laboratory +** gustavo.padilla@ibm.com +** +** Revision History: +** +** 04/23/2025 --- GPV --- ltfs_arch_ops created +************************************************************************************* +*/ + + +#ifndef __LTFS_ARCH_OPS_H__ +#define __LTFS_ARCH_OPS_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include + +#define arch_safe_free(memobject) \ + do { \ + if(memobject) \ + { \ + free(memobject); \ + memobject = NULL; \ + } \ + }while(0) + +static inline void arch_strcpy_limited(char *dest, const char *src, int count) + { + int i; + for (i = 0; i < (count) && (src)[i] != '\0'; i++) { + + (dest)[i] = (src)[i]; + } + if (i < (count)) { + + (dest)[i] = '\0'; + } + } + + + +#ifdef _MSC_VER +#include +#include +#include +#include +#include + +#define SHARE_FLAG_DENYNO _SH_DENYNO +#define SHARE_FLAG_DENYWR _SH_DENYWR +#define SHARE_FLAG_DENYRD _SH_DENYRD +#define SHARE_FLAG_DENYRW _SH_DENYRW +#define PERMISSION_READWRITE _S_IREAD | _S_IWRITE +#define PERMISSION_READ _S_IREAD +#define PERMISSION_WRITE _S_IWRITE +#define S_IFLNK 0xA000 +#define O_NONBLOCK 0 +#define INVALID_KEY UINT_MAX + + + #define arch_vsprintf vsprintf_s + + #define arch_sprintf sprintf_s + + #define arch_sscanf sscanf_s + + #define arch_open _sopen_s + + #define arch_fopen(file, mode, file_ptr) fopen_s(&(file_ptr), file, mode) + + #define arch_ctime(buf, time_ptr) ctime_s(buf, sizeof(buf), time_ptr) + + #define arch_getenv(buf, name) do { size_t len; _dupenv_s(&(buf), &(len), name); } while (0) + + #define arch_strtok(str, delm, ctxt) strtok_s((str), (delm), &(ctxt)) + + #define arch_strcpy(dest, size, src) strcpy_s((dest), (size), (src)) + + #define arch_strncpy(dest, src, size, cnt) strncpy_s((dest), (size), (src), (cnt)) + + #define arch_strcat(dest, size, src) strcat_s((dest), (size), (src)) + + #define arch_unlink _unlink + + #define arch_write _write + + #define arch_close _close + + #define arch_read _read + + #define arch_strdup _strdup + + #define arch_chmod _chmod + + #define arch_getpid _getpid + + #define arch_access _access + + #define arch_xmlfree xmlFree + + +#else +#define SHARE_FLAG_DENYNO 0 +#define SHARE_FLAG_DENYWR (S_IWUSR | S_IWGRP) // Deny write access +#define SHARE_FLAG_DENYRD (S_IRUSR | S_IRGRP | S_IROTH) // Deny read access +#define SHARE_FLAG_DENYRW (S_IWUSR | S_IWGRP | S_IRUSR | S_IRGRP | S_IROTH) // Deny both read and write access +#define PERMISSION_READWRITE 0666 +#define PERMISSION_READ 0444 +#define PERMISSION_WRITE 0222 +#define INVALID_KEY (-1U) + + #define arch_vsprintf(buffer,unused, fmt, ...) vsprintf((buffer), (fmt), __VA_ARGS__) + + #define arch_sprintf(buffer,unused, fmt, ...) sprintf((buffer), (fmt), __VA_ARGS__) + + #define arch_sscanf sscanf + + #define arch_open( descriptor_ptr, filename_ptr, open_flg, share_flg, unused) do{ *descriptor_ptr = open(filename_ptr, open_flg, share_flg); }while(0) + + #define arch_fopen(file, mode, file_ptr) do {file_ptr = fopen(file, mode);}while(0) + + #define arch_ctime(buf ,time_ptr) do { buf = ctime(time_ptr); } while (0) + + #define arch_getenv(buf ,name) do { buf = getenv(name); } while (0) + + #define arch_strcpy(dest, unused, src) ({if(unused || !unused) {strcpy(dest, src);}}) + + #define arch_strncpy(dest, src, unused, cnt) strncpy(dest, src, cnt) + + #define arch_strcat(dest, unused, src)( {if(unused || !unused){ strcat(dest, src);}}) + + #define arch_strtok(str, delim, unused) ((void)(unused), strtok(str, delim)) + + #define arch_unlink unlink + + #define arch_write write + + #define arch_close close + + #define arch_read read + + #define arch_strdup strdup + + #define arch_chmod chmod + + #define arch_getpid getpid + + #define arch_access access + + #define arch_xmlfree free + + +#endif /* _MSC_VER */ + + /* These needs to be declared at the end to avoid redefinition and to avoid code replication */ + #define arch_vsprintf_auto( buffer, fmt, ...) arch_vsprintf(buffer,sizeof(buffer),fmt,__VA_ARGS__) + + #define arch_strcpy_auto(dest, src) arch_strcpy(dest, sizeof(dest), src); + + #define arch_strncpy_auto(dest, src, destSize) arch_strncpy(dest, src, destSize, destSize); + + #define arch_strcat_auto(dest,src) arch_strcat(dest, sizeof(dest), src); + + #define arch_sprintf_auto(buffer, fmt, ...) arch_sprintf(buffer,sizeof(buffer),fmt, __VA_ARGS__) + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* __LTFS_ARCH_OPS_H__ */ diff --git a/src/libltfs/arch/signal_internal.h b/src/libltfs/arch/signal_internal.h index 6685470d..3eda9f94 100644 --- a/src/libltfs/arch/signal_internal.h +++ b/src/libltfs/arch/signal_internal.h @@ -51,7 +51,7 @@ #define signal_internal_h_ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #endif #include diff --git a/src/libltfs/arch/time_internal.c b/src/libltfs/arch/time_internal.c index a6bae2ce..821cafbb 100644 --- a/src/libltfs/arch/time_internal.c +++ b/src/libltfs/arch/time_internal.c @@ -67,8 +67,10 @@ #include "config.h" #endif +#ifndef mingw_PLATFORM #if ! ((SIZEOF_TIME_T == 4) || (SIZEOF_TIME_T == 8)) -#error time_t width is not 4 or 8 + #error time_t width is not 4 or 8 +#endif #endif ltfs_time_t ltfs_timegm(struct tm *t) @@ -169,7 +171,7 @@ int ltfs_get_days_of_year(int64_t nYear) return nDays; } -int ltfs_get_mday_from_yday(int64_t nYear, int nYday, int* pnMonth) +int ltfs_get_mday_from_yday(int64_t nYear, int nYday, int *pnMonth) { int i = 0; int nMday = nYday; diff --git a/src/libltfs/arch/time_internal.h b/src/libltfs/arch/time_internal.h index 71508d76..76adc5ee 100644 --- a/src/libltfs/arch/time_internal.h +++ b/src/libltfs/arch/time_internal.h @@ -55,7 +55,7 @@ extern "C" { #endif #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #elif defined(__APPLE__) #include typedef time_t ltfs_time_t; diff --git a/src/libltfs/arch/uuid_internal.h b/src/libltfs/arch/uuid_internal.h index 3fe0a5d9..9d896c2f 100644 --- a/src/libltfs/arch/uuid_internal.h +++ b/src/libltfs/arch/uuid_internal.h @@ -51,12 +51,12 @@ #define __uuid_internal_h__ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" -#define ltfs_gen_uuid(uuid_str) gen_uuid_win(uuid_str) +#include "arch/win/win_util.h" +#define ltfs_gen_uuid(uuid_str,uuid_str_size) gen_uuid_win(uuid_str,uuid_str_size) #else void gen_uuid_unix(char *uuid_str); -#define ltfs_gen_uuid(uuid_str) gen_uuid_unix(uuid_str) +#define ltfs_gen_uuid(uuid_str,unused) gen_uuid_unix(uuid_str) #endif /* mingw_PLATFORM */ diff --git a/src/libltfs/config_file.c b/src/libltfs/config_file.c index 6a354d21..fa445305 100644 --- a/src/libltfs/config_file.c +++ b/src/libltfs/config_file.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2025 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -58,15 +58,16 @@ #ifdef mingw_PLATFORM #include "arch/win/win_util.h" #endif - +#include #include #include #include -#include + #include #include #include +#include "libltfs/arch/ltfs_arch_ops.h" #include "libltfs/ltfslogging.h" #include "libltfs/ltfs_error.h" #include "config_file.h" @@ -211,7 +212,7 @@ char **config_file_get_plugins(const char *type, struct config_file *config) TAILQ_FOREACH(entry, &config->plugins, list) { if (! strcmp(entry->type, type)) { - list[pos] = strdup(entry->name); + list[pos] = arch_strdup(entry->name); if (! list[pos]) { ltfsmsg(LTFS_ERR, 10001E, "config_file_get_plugins: list entry"); for (count=0; countmount_options, list) { if (! strcmp(entry->type, type)) { - list[pos] = strdup(entry->option); + list[pos] = arch_strdup(entry->option); if (! list[pos]) { ltfsmsg(LTFS_ERR, 10001E, "config_file_get_options: list entry"); goto out_free; @@ -270,14 +271,14 @@ char **config_file_get_options(const char *type, struct config_file *config) */ int _config_file_parse(const char *path, bool ignore_error, struct config_file *config) { - FILE *conf_file; + FILE *conf_file = NULL; char line[65536], *saveline = NULL; char *tok, *saveptr, *strip_pos; char *include_file = NULL; struct option_entry *ol; int ret, offset; - - conf_file = fopen(path, "rb"); + + arch_fopen(path, "rb", conf_file); if (! conf_file) { if (!ignore_error) { ret = -errno; @@ -305,7 +306,7 @@ int _config_file_parse(const char *path, bool ignore_error, struct config_file * --strip_pos; *strip_pos = '\0'; - saveline = strdup(line); + saveline = arch_strdup(line); if (! saveline) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse: saveline"); ret = -LTFS_NO_MEMORY; @@ -439,7 +440,7 @@ int _config_file_parse_name(const char *directive, const char *name_desc, char * ltfsmsg(LTFS_ERR, 11273E, directive, name_desc); return -LTFS_CONFIG_INVALID; } - *out = strdup(tok); + *out = arch_strdup(tok); if (! (*out)) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; @@ -476,7 +477,7 @@ int _config_file_parse_default(char *saveptr, struct config_file *config) return -LTFS_CONFIG_INVALID; } - type = strdup(tok); + type = arch_strdup(tok); if (! type) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse_default: plugin type"); return -LTFS_NO_MEMORY; @@ -490,7 +491,7 @@ int _config_file_parse_default(char *saveptr, struct config_file *config) return -LTFS_CONFIG_INVALID; } - name = strdup(tok); + name = arch_strdup(tok); if (! name) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse_default: plugin name"); free(type); @@ -552,7 +553,7 @@ int _config_file_remove_default(char *saveptr, struct config_file *config) return -LTFS_CONFIG_INVALID; } - type = strdup(tok); + type = arch_strdup(tok); if (! type) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_remove_default: plugin type"); return -LTFS_NO_MEMORY; @@ -608,7 +609,7 @@ int _config_file_parse_plugin(char *saveptr, struct config_file *config) goto out_free; } - type = strdup(tok); + type = arch_strdup(tok); if (! type) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse_plugin: plugin type"); ret = -LTFS_NO_MEMORY; @@ -623,7 +624,7 @@ int _config_file_parse_plugin(char *saveptr, struct config_file *config) goto out_free; } - name = strdup(tok); + name = arch_strdup(tok); if (! name) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse_plugin: plugin name"); ret = -LTFS_NO_MEMORY; @@ -638,7 +639,7 @@ int _config_file_parse_plugin(char *saveptr, struct config_file *config) goto out_free; } - library = strdup(tok); + library = arch_strdup(tok); if (! library) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse_plugin: plugin path"); ret = -LTFS_NO_MEMORY; @@ -699,7 +700,7 @@ int _config_file_remove_plugin(char *saveptr, struct config_file *config) return -LTFS_CONFIG_INVALID; } - type = strdup(tok); + type = arch_strdup(tok); if (! type) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_remove_plugin: plugin type"); return -LTFS_NO_MEMORY; @@ -713,7 +714,7 @@ int _config_file_remove_plugin(char *saveptr, struct config_file *config) return -LTFS_CONFIG_INVALID; } - name = strdup(tok); + name = arch_strdup(tok); if (! name) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_remove_plugin: plugin name"); free(type); @@ -774,7 +775,7 @@ int _config_file_parse_option(const char *line, char *saveptr, struct option_ent } start = tok; - type = strdup(tok); + type = arch_strdup(tok); if (! type) { ltfsmsg(LTFS_ERR, 10001E, "_config_file_parse_mount_option: option"); return -LTFS_NO_MEMORY; diff --git a/src/libltfs/dcache.c b/src/libltfs/dcache.c index a10db152..1d829d31 100644 --- a/src/libltfs/dcache.c +++ b/src/libltfs/dcache.c @@ -153,15 +153,16 @@ int dcache_parse_options(const char **options, struct dcache_options **out) ltfsmsg(LTFS_ERR, 10001E, "dcache_parse_options: opt"); return -ENOMEM; } - + for (i=0; options[i]; ++i) { - line = strdup(options[i]); + line = arch_strdup(options[i]); if (! line) { ltfsmsg(LTFS_ERR, 10001E, "dcache_parse_options: line"); ret = -ENOMEM; goto out_free; } - option = strtok(line, " \t"); + char *contextVal = NULL; + option = arch_strtok(line, " \t", contextVal); if (! option) { /* Failed to parse LTFS dcache configuration rules: invalid option '%s' */ ltfsmsg(LTFS_ERR, 17170E, options[i]); @@ -181,7 +182,7 @@ int dcache_parse_options(const char **options, struct dcache_options **out) continue; } - value = strtok(NULL, " \t"); + value = arch_strtok(NULL, " \t", contextVal); if (! value) { /* Failed to parse LTFS dcache configuration rules: invalid option '%s' */ ltfsmsg(LTFS_ERR, 17170E, options[i]); @@ -189,6 +190,7 @@ int dcache_parse_options(const char **options, struct dcache_options **out) goto out_free; } + if (! strcmp(option, "minsize")) { opt->minsize = atoi(value); if (opt->minsize <= 0) { diff --git a/src/libltfs/dcache.h b/src/libltfs/dcache.h index d514fa14..470f053b 100644 --- a/src/libltfs/dcache.h +++ b/src/libltfs/dcache.h @@ -60,14 +60,18 @@ extern "C" { #ifdef mingw_PLATFORM #include "arch/win/win_util.h" +#include +#include #endif #ifndef mingw_PLATFORM #include -#endif -#include #include #include +#endif +#include + + #include "plugin.h" #include "dcache_ops.h" diff --git a/src/libltfs/dcache_ops.h b/src/libltfs/dcache_ops.h index 84395817..80eb6394 100644 --- a/src/libltfs/dcache_ops.h +++ b/src/libltfs/dcache_ops.h @@ -52,7 +52,13 @@ #include "ltfs.h" #include +#ifndef mingw_PLATFORM #include +#else +#include "ltfscommon/dirent.h" +#endif // !mingw_PLATFORM + + enum dcache_flush_flags { FLUSH_XATTRS = 0x01, /**< Flush extended attribute to the dentry cache */ diff --git a/src/libltfs/fs.c b/src/libltfs/fs.c index 01909508..bc2d8854 100644 --- a/src/libltfs/fs.c +++ b/src/libltfs/fs.c @@ -53,9 +53,11 @@ #ifdef mingw_PLATFORM #include "arch/win/win_util.h" +#else +#include #endif -#include + #include #include "libltfs/ltfslogging.h" @@ -72,10 +74,11 @@ int fs_hash_sort_by_uid(struct name_list *a, struct name_list *b) return (a->uid - b->uid); } -static char* generate_hash_key_name(const char *src_str, int *rc) +static char *generate_hash_key_name(const char *src_str, int *rc) { - char *key_name; - + char *key_name = NULL; + key_name = malloc(sizeof(char*)); + if (key_name == NULL) return NULL; #ifdef mingw_PLATFORM UChar *uchar_name; @@ -89,7 +92,7 @@ static char* generate_hash_key_name(const char *src_str, int *rc) } else free(uchar_name); #else - key_name = strdup(src_str); + key_name = arch_strdup(src_str); *rc = 0; #endif @@ -262,7 +265,7 @@ struct dentry * fs_allocate_dentry(struct dentry *parent, const char *name, cons d->name.name = NULL; d->platform_safe_name = NULL; } else if (name && !platform_safe_name) { - d->name.name = strdup(name); + d->name.name = arch_strdup(name); update_platform_safe_name(d, false, idx); if (! d->name.name || ! d->platform_safe_name) { ltfsmsg(LTFS_ERR, 10001E, "fs_allocate_dentry: name"); @@ -274,8 +277,8 @@ struct dentry * fs_allocate_dentry(struct dentry *parent, const char *name, cons return NULL; } } else if(!name && platform_safe_name) { - d->name.name = strdup(platform_safe_name); - d->platform_safe_name = strdup(platform_safe_name); + d->name.name = arch_strdup(platform_safe_name); + d->platform_safe_name = arch_strdup(platform_safe_name); if (! d->name.name || ! d->platform_safe_name) { ltfsmsg(LTFS_ERR, 10001E, "fs_allocate_dentry: name"); if (d->name.name) @@ -288,8 +291,8 @@ struct dentry * fs_allocate_dentry(struct dentry *parent, const char *name, cons } else { /* Currently, it can be assumed that one of these names should be NULL. The codes below are just in case. */ - d->name.name = strdup(name); - d->platform_safe_name = strdup(platform_safe_name); + d->name.name = arch_strdup(name); + d->platform_safe_name = arch_strdup(platform_safe_name); if (! d->name.name || ! d->platform_safe_name) { ltfsmsg(LTFS_ERR, 10001E, "fs_allocate_dentry: name"); if (d->name.name) @@ -453,7 +456,7 @@ int fs_dentry_lookup(struct dentry *dentry, char **name) } lookup_name = "/"; } - dentry_names[i] = strdup(lookup_name); + dentry_names[i] = arch_strdup(lookup_name); if (! dentry_names[i]) { ltfsmsg(LTFS_ERR, 10001E, "fs_dentry_lookup: dentry_names member"); goto out; @@ -469,7 +472,8 @@ int fs_dentry_lookup(struct dentry *dentry, char **name) parent = d->parent; } - tmp_name = calloc(namelen + names, sizeof(char)); + size_t tmp_len = ((namelen + names)* sizeof(char)); + tmp_name = (char*)calloc(namelen + names, sizeof(char)); if (! tmp_name) { ltfsmsg(LTFS_ERR, 10001E, "fs_dentry_lookup: tmp_name"); ret = -LTFS_NO_MEMORY; @@ -477,9 +481,9 @@ int fs_dentry_lookup(struct dentry *dentry, char **name) } for (namelen=0, i=0; i 0 && i < names-1) - strcat(tmp_name, "/"); + arch_strcat(tmp_name, tmp_len, "/"); } ret = 0; @@ -543,7 +547,7 @@ int fs_path_lookup(const char *path, int flags, struct dentry **dentry, struct l CHECK_ARG_NULL(dentry, -LTFS_NULL_ARG); CHECK_ARG_NULL(idx, -LTFS_NULL_ARG); - tmp_path = strdup(path); + tmp_path = arch_strdup(path); if (! tmp_path) { ltfsmsg(LTFS_ERR, 10001E, "fs_path_lookup: tmp_path"); return -LTFS_NO_MEMORY; diff --git a/src/libltfs/index_criteria.c b/src/libltfs/index_criteria.c index 88819b16..22579de7 100644 --- a/src/libltfs/index_criteria.c +++ b/src/libltfs/index_criteria.c @@ -105,7 +105,7 @@ bool index_criteria_contains_invalid_options(const char *str) /* Check the beginning of the string */ for (valid_option = false, i=0; options[i]; ++i) - if (! strncasecmp(options[i], str, strlen(options[i]))) { + if (! strncmp(options[i], str, strlen(options[i]))) { valid_option = true; break; } @@ -120,7 +120,7 @@ bool index_criteria_contains_invalid_options(const char *str) if (! ptr) break; for (valid_option = false, i=0; options[i]; ++i) - if (! strncasecmp(options[i], ptr+1, strlen(options[i]))) { + if (!strncmp(options[i], ptr+1, strlen(options[i]))) { valid_option = true; break; } @@ -152,7 +152,7 @@ bool index_criteria_find_option(const char *str, const char *substr, if (strlen(str) < 5) return false; - if (! strncasecmp(str, substr, substr_len)) { + if (!strncmp(str, substr, substr_len)) { /* Match at the start of the string */ str_start = str; } else { @@ -201,16 +201,21 @@ int index_criteria_parse_size(const char *criteria, size_t len, struct index_cri { int ret = 0, multiplier = 1; size_t sizelen = 0; - char rule[len+1], last, *ptr; - + char *rule = NULL, last, *ptr; + int lenrule = (sizeof(char) * (int)(len + 1)); + rule = (char*)calloc(lenrule,sizeof(char)); + if (rule == NULL) { + ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); + return -LTFS_NO_MEMORY; + } sizelen = strlen("size="); if (len <= sizelen) { ltfsmsg(LTFS_ERR, 11143E, len); return -LTFS_POLICY_INVALID; } + const char *param = criteria + sizelen; - memset(rule, 0, sizeof(rule)); - snprintf(rule, len - sizelen, "%s", criteria + sizelen); + snprintf(rule, len - sizelen, "%s", param); for (ptr=&rule[0]; *ptr; ptr++) { if (isalpha(*ptr) && *(ptr+1) && isalpha(*(ptr+1))) { @@ -229,6 +234,7 @@ int index_criteria_parse_size(const char *criteria, size_t len, struct index_cri multiplier = 1024 * 1024 * 1024; else { ltfsmsg(LTFS_ERR, 11149E, last); + arch_safe_free(rule); return -LTFS_POLICY_INVALID; } rule[strlen(rule)-1] = '\0'; @@ -236,13 +242,15 @@ int index_criteria_parse_size(const char *criteria, size_t len, struct index_cri if (strlen(rule) == 0) { ltfsmsg(LTFS_ERR, 11150E); + arch_safe_free(rule); return -LTFS_POLICY_INVALID; } else if (!isdigit(rule[0])) { ltfsmsg(LTFS_ERR, 11151E); + arch_safe_free(rule); return -LTFS_POLICY_INVALID; } ic->max_filesize_criteria = strtoull(rule, NULL, 10) * multiplier; - + arch_safe_free(rule); return ret; } @@ -255,7 +263,13 @@ int index_criteria_parse_size(const char *criteria, size_t len, struct index_cri */ int index_criteria_parse_name(const char *criteria, size_t len, struct index_criteria *ic) { - char *delim, *rule, rulebuf[len+1]; + char *delim, *rule, *rulebuf = NULL; + rulebuf = (char*)malloc(sizeof(char) * (len + 1)); + if (rulebuf == NULL) + { + ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); + return -LTFS_NO_MEMORY; + } struct ltfs_name *rule_ptr; int ret = 0, num_names = 0; @@ -266,12 +280,14 @@ int index_criteria_parse_name(const char *criteria, size_t len, struct index_cri /* Count the rules and check for empty ones */ if (rule[5] == ':') { ltfsmsg(LTFS_ERR, 11305E, rule); + arch_safe_free(rulebuf); return -LTFS_POLICY_EMPTY_RULE; } for (delim=rule+6; *delim; delim++) { if (*delim == ':') { if (*(delim-1) == ':' || *(delim+1) == '\0') { ltfsmsg(LTFS_ERR, 11305E, rule); + arch_safe_free(rulebuf); return -LTFS_POLICY_EMPTY_RULE; } ++num_names; @@ -281,6 +297,7 @@ int index_criteria_parse_name(const char *criteria, size_t len, struct index_cri ic->glob_patterns = calloc(num_names+1, sizeof(struct ltfs_name)); if (! ic->glob_patterns) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); + arch_safe_free(rulebuf); return -LTFS_NO_MEMORY; } rule_ptr = ic->glob_patterns; @@ -291,24 +308,24 @@ int index_criteria_parse_name(const char *criteria, size_t len, struct index_cri if (*delim == ':') { *delim = '\0'; rule_ptr->percent_encode = fs_is_percent_encode_required(rule); - rule_ptr->name = strdup(rule); + rule_ptr->name = arch_strdup(rule); rule_ptr++; rule = delim+1; } else if (*delim == '/') { *delim = '\0'; rule_ptr->percent_encode = fs_is_percent_encode_required(rule); - rule_ptr->name = strdup(rule); + rule_ptr->name = arch_strdup(rule); rule_ptr++; } else if (*(delim+1) == '\0') { rule_ptr->percent_encode = fs_is_percent_encode_required(rule); - rule_ptr->name = strdup(rule); + rule_ptr->name = arch_strdup(rule); rule_ptr++; } } if (ic->glob_patterns == rule_ptr) { rule_ptr->percent_encode = fs_is_percent_encode_required(rule); - rule_ptr->name = strdup(rule); + rule_ptr->name = arch_strdup(rule); } /* Validate rules */ @@ -325,7 +342,7 @@ int index_criteria_parse_name(const char *criteria, size_t len, struct index_cri ++rule_ptr; } } - + arch_safe_free(rulebuf); return ret; } @@ -439,7 +456,7 @@ int index_criteria_dup_rules(struct index_criteria *dest_ic, struct index_criter for (i = 0; i < counter; ++i) { dst_gp->percent_encode = src_gp->percent_encode; - dst_gp->name = strdup(src_gp->name); + dst_gp->name = arch_strdup(src_gp->name); if (! dst_gp->name) { ltfsmsg(LTFS_ERR, 10001E, "index_criteria_dup_rules: glob pattern"); while (--i >= 0) { diff --git a/src/libltfs/iosched.c b/src/libltfs/iosched.c index 12a00118..50671fa8 100644 --- a/src/libltfs/iosched.c +++ b/src/libltfs/iosched.c @@ -330,7 +330,7 @@ int iosched_update_data_placement(struct dentry *d, struct ltfs_volume *vol) * @param vol LTFS volume * @return 0 on succe ss or a negative value on error */ -int iosched_set_profiler(char* work_dir, bool enable, struct ltfs_volume *vol) +int iosched_set_profiler(char *work_dir, bool enable, struct ltfs_volume *vol) { int ret = 0; struct iosched_priv *priv = (struct iosched_priv *) vol ? vol->iosched_handle : NULL; diff --git a/src/libltfs/iosched.h b/src/libltfs/iosched.h index d53f178e..f89ab095 100644 --- a/src/libltfs/iosched.h +++ b/src/libltfs/iosched.h @@ -70,7 +70,7 @@ int iosched_flush(struct dentry *d, bool closeflag, struct ltfs_volume *vol); int iosched_truncate(struct dentry *d, off_t length, struct ltfs_volume *vol); uint64_t iosched_get_filesize(struct dentry *d, struct ltfs_volume *vol); int iosched_update_data_placement(struct dentry *d, struct ltfs_volume *vol); -int iosched_set_profiler(char* work_dir, bool enable, struct ltfs_volume *vol); +int iosched_set_profiler(char *work_dir, bool enable, struct ltfs_volume *vol); #ifdef __cplusplus } diff --git a/src/libltfs/ltfs.c b/src/libltfs/ltfs.c index 5400c578..2cbc82bd 100644 --- a/src/libltfs/ltfs.c +++ b/src/libltfs/ltfs.c @@ -81,11 +81,9 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif -/* O_BINARY is defined only in MinGW */ -#ifndef O_BINARY -#define O_BINARY 0 +#ifdef mingw_PLATFORM +static #endif - volatile char *copyright = LTFS_COPYRIGHT_0"\n"LTFS_COPYRIGHT_1"\n"LTFS_COPYRIGHT_2"\n" \ LTFS_COPYRIGHT_3"\n"LTFS_COPYRIGHT_4"\n"LTFS_COPYRIGHT_5"\n"; @@ -115,7 +113,7 @@ const char *ltfs_format_version() return LTFS_INDEX_VERSION_STR; } -static inline char* _get_barcode(struct ltfs_volume *vol) +static inline char *_get_barcode(struct ltfs_volume *vol) { char *barcode = NULL; @@ -133,7 +131,6 @@ static inline char* _get_barcode(struct ltfs_volume *vol) int ltfs_init(int log_level, bool use_syslog, bool print_thread_id) { int ret; - ret = ltfsprintf_init(log_level, use_syslog, print_thread_id); if (ret < 0) { fprintf(stderr, "LTFS9011E Logging initialization failed\n"); @@ -378,6 +375,7 @@ int ltfs_volume_alloc(const char *execname, struct ltfs_volume **volume) } } + *volume = newvol; return 0; @@ -404,7 +402,7 @@ int ltfs_volume_alloc(const char *execname, struct ltfs_volume **volume) * @param force force free memory if reference counter is not 0 * @param volume the volume to free, set to NULL on success */ -void _ltfs_volume_free(bool force, struct ltfs_volume **volume) +void _ltfs_volume_free(bool force, struct ltfs_volume** volume) { if (volume && *volume) { label_free(&(*volume)->label); @@ -1260,7 +1258,7 @@ int ltfs_get_index_commit_message(char **msg, struct ltfs_volume *vol) if (err < 0) return err; if (vol->index->commit_message) { - ret = strdup(vol->index->commit_message); + ret = arch_strdup(vol->index->commit_message); if (! ret) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); releaseread_mrsw(&vol->lock); @@ -1285,7 +1283,7 @@ int ltfs_get_index_creator(char **msg, struct ltfs_volume *vol) if (err < 0) return err; if (vol->index->creator) { - ret = strdup(vol->index->creator); + ret = arch_strdup(vol->index->creator); if (! ret) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); releaseread_mrsw(&vol->lock); @@ -1310,7 +1308,7 @@ int ltfs_get_volume_name(char **msg, struct ltfs_volume *vol) if (err < 0) return err; if (vol->index->volume_name.name) { - ret = strdup(vol->index->volume_name.name); + ret = arch_strdup(vol->index->volume_name.name); if (! ret) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); releaseread_mrsw(&vol->lock); @@ -1770,6 +1768,7 @@ int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool rec } INTERRUPTED_GOTO(ret, out_unlock); ltfsmsg(LTFS_INFO, 11022I); + ret = ltfs_write_index(vol->label->partid_ip, SYNC_RECOVERY, vol); if (ret < 0) goto out_unlock; @@ -2375,10 +2374,11 @@ size_t ltfs_max_cache_size(struct ltfs_volume *vol) * when the cartridge is known to be in a sane state. * The caller must hold vol->lock for write if thread safety is required. * @param partition partition in which the schema should be written to + * @param reason the reason to write down an index * @param vol LTFS volume * @return 0 on success or a negative value on error */ -int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol) +int ltfs_write_index(char partition, char *reason, struct ltfs_volume* vol) { int ret, ret_mam; struct tape_offset old_selfptr, old_backptr; @@ -2416,7 +2416,7 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol) /* Check read-only status. Ignore the out-of-space condition, as this should not * prevent writing an Index. */ ret = ltfs_get_partition_readonly(ltfs_ip_id(vol), vol); - if (! ret || ret == -LTFS_NO_SPACE || ret == -LTFS_LESS_SPACE) + if (!ret || ret == -LTFS_NO_SPACE || ret == -LTFS_LESS_SPACE) ret = ltfs_get_partition_readonly(ltfs_dp_id(vol), vol); if (ret < 0 && ret != -LTFS_NO_SPACE && ret != -LTFS_LESS_SPACE) return ret; @@ -2426,11 +2426,11 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol) * of the tape device do so with some kind of volume lock held, and this function * executes with an exclusive lock on the volume. */ - /* write to data partition first if required */ + /* write to data partition first if required */ if (partition == ltfs_ip_id(vol) && - ! write_perm && - (! vol->dp_index_file_end || - (vol->ip_index_file_end && vol->index->selfptr.partition == ltfs_ip_id(vol)))) { + !write_perm && + (!vol->dp_index_file_end || + (vol->ip_index_file_end && vol->index->selfptr.partition == ltfs_ip_id(vol)))) { /* Surpress on-disk index cache write on the recursive call */ cache_path_save = vol->index_cache_path; @@ -2532,7 +2532,7 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol) } ltfsmsg(LTFS_INFO, 17235I, bc_print, partition, reason, - (unsigned long long)vol->index->file_count, tape_get_serialnumber(vol->device)); + (unsigned long long)vol->index->file_count, tape_get_serialnumber(vol->device)); ret = tape_write_filemark(vol->device, 1, true, true, true); // immediate WFM if (ret < 0) { @@ -2578,11 +2578,11 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol) ltfs_update_cart_coherency(vol); ltfsmsg(LTFS_INFO, 17236I, - bc_print, - (unsigned long long)vol->index->generation, - vol->index->selfptr.partition, - (unsigned long long)vol->index->selfptr.block, - tape_get_serialnumber(vol->device)); + bc_print, + (unsigned long long)vol->index->generation, + vol->index->selfptr.partition, + (unsigned long long)vol->index->selfptr.block, + tape_get_serialnumber(vol->device)); /* update append position */ if (partition == ltfs_ip_id(vol)) { @@ -2635,7 +2635,7 @@ int ltfs_write_index(char partition, char *reason, struct ltfs_volume *vol) * @param vol LTFS volume * @return 0 on success or a negative value on error */ -int ltfs_save_index_to_disk(const char *work_dir, char * reason, bool need_gen, struct ltfs_volume *vol) +int ltfs_save_index_to_disk(const char *work_dir, char *reason, bool need_gen, struct ltfs_volume* vol) { char *path; int ret; @@ -2652,7 +2652,8 @@ int ltfs_save_index_to_disk(const char *work_dir, char * reason, bool need_gen, ret = asprintf(&path, "%s/%s-%d.schema", work_dir, vol->label->barcode, vol->index->generation); else ret = asprintf(&path, "%s/%s-%d.schema", work_dir, vol->label->vol_uuid, vol->index->generation); - } else { + } + else { if (strcmp(vol->label->barcode, " ")) ret = asprintf(&path, "%s/%s.schema", work_dir, vol->label->barcode); else @@ -2666,7 +2667,7 @@ int ltfs_save_index_to_disk(const char *work_dir, char * reason, bool need_gen, ltfsmsg(LTFS_INFO, 17235I, _get_barcode(vol), 'Z', "Volume Cache", - (unsigned long long)vol->index->file_count, path); + (unsigned long long)vol->index->file_count, path); ret = xml_schema_to_file(path, vol->index->creator, reason, vol->index); if (ret < 0) { @@ -2677,17 +2678,17 @@ int ltfs_save_index_to_disk(const char *work_dir, char * reason, bool need_gen, } /* Change index file's mode */ - if (chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) { + if (arch_chmod(path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) { ret = -errno; ltfsmsg(LTFS_ERR, 17184E, errno); } ltfsmsg(LTFS_INFO, 17236I, - _get_barcode(vol), - (unsigned long long)vol->index->generation, - 'Z', - (unsigned long long)vol->index->selfptr.block, - path); + _get_barcode(vol), + (unsigned long long)vol->index->generation, + 'Z', + (unsigned long long)vol->index->selfptr.block, + path); free(path); return ret; @@ -2781,9 +2782,9 @@ int ltfs_set_barcode(const char *barcode, struct ltfs_volume *vol) return -LTFS_BARCODE_INVALID; ++tmp; } - strcpy(vol->label->barcode, barcode); + arch_strcpy_auto(vol->label->barcode, barcode); } else - strcpy(vol->label->barcode, " "); + arch_strcpy_auto(vol->label->barcode, NO_BARCODE); return 0; } @@ -2805,7 +2806,7 @@ int ltfs_set_volume_name(const char *volname, struct ltfs_volume *vol) ret = pathname_validate_file(volname); if (ret < 0) return ret; - name_dup = strdup(volname); + name_dup = arch_strdup(volname); if (! name_dup) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; @@ -2997,21 +2998,21 @@ int ltfs_format_tape(struct ltfs_volume *vol, int density_code, bool destructive } /* Set up the label: generate UUID and format time */ - ltfs_gen_uuid(vol->label->vol_uuid); + ltfs_gen_uuid((vol->label->vol_uuid),sizeof(vol->label->vol_uuid)); get_current_timespec(&vol->label->format_time); /* Duplicate creator */ if (vol->label->creator) free(vol->label->creator); - vol->label->creator = strdup(vol->creator); + vol->label->creator = arch_strdup(vol->creator); if (!vol->label->creator) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; } /* Set appropriate volume modification time, UUID, and root directory's uid */ - strcpy(vol->index->vol_uuid, vol->label->vol_uuid); + arch_strcpy_auto(vol->index->vol_uuid, vol->label->vol_uuid); vol->index->mod_time = vol->label->format_time; vol->index->root->creation_time = vol->index->mod_time; vol->index->root->change_time = vol->index->mod_time; @@ -3070,6 +3071,7 @@ int ltfs_format_tape(struct ltfs_volume *vol, int density_code, bool destructive if (ret < 0) return ret; ltfsmsg(LTFS_INFO, 11278I, vol->label->partid_dp); /* "Writing Index to ..." */ + ret = ltfs_write_index(vol->label->partid_dp, SYNC_FORMAT, vol); if (ret < 0) { ltfsmsg(LTFS_ERR, 11279E, vol->label->partid_dp, ret); @@ -3486,9 +3488,11 @@ int ltfs_revalidate(bool have_write_lock, struct ltfs_volume *vol) * on the data partition. * @param vol LTFS volume * @param index_locking Take index lock while writing an index + * @param type index type to write + * @param vol LTFS volume * @return 0 on success or a negative value on error */ -int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol) +int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume* vol) { int ret = 0, ret_r = 0; bool dirty; @@ -3526,7 +3530,7 @@ int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol) /* If the DP ends in an index and the IP doesn't, then we're most likely positioned * at the end of the IP, and writing an index there is allowed without first putting * down a DP index. */ - if (dp_index_file_end && ! ip_index_file_end) + if (dp_index_file_end && !ip_index_file_end) partition = ltfs_ip_id(vol); else /* Otherwise, it's faster to write an index to the DP. */ partition = ltfs_dp_id(vol); @@ -3570,7 +3574,8 @@ int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol) if (!ret_r) { ltfsmsg(LTFS_INFO, 11344I, bc_print); ret = -LTFS_SYNC_FAIL_ON_DP; - } else { + } + else { ltfsmsg(LTFS_ERR, 11345E, bc_print); ltfsmsg(LTFS_ERR, 11346E, bc_print); } @@ -3584,20 +3589,21 @@ int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol) ret = ltfs_revalidate(true, vol); if (ret == 0) goto start; - } else if (index_locking) + } + else if (index_locking) releasewrite_mrsw(&vol->lock); if (ret) ltfsmsg(LTFS_ERR, 17069E); ltfsmsg(LTFS_INFO, 17070I, bc_print, ret, vol->device->serial_number); - } else { + } + else { /* Do nothing and return 0 when filesystem is not dirty */ ret = 0; } return ret; } - /** * Traverse index on a partition of EOD-less tape by forward direction * @param vol LTFS volume @@ -3608,8 +3614,8 @@ int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol) * @param priv private data for call back function * @return 0 on success or a negative value on error */ -int ltfs_traverse_index_no_eod(struct ltfs_volume *vol, char partition, unsigned int gen, - f_index_found func, void **list, void* priv) +int ltfs_traverse_index_no_eod(struct ltfs_volume* vol, char partition, unsigned int gen, + f_index_found func, void** list, void *priv) { int ret, func_ret; @@ -3626,7 +3632,8 @@ int ltfs_traverse_index_no_eod(struct ltfs_volume *vol, char partition, unsigned if (ret < 0 && ret != -LTFS_UNSUPPORTED_INDEX_VERSION) { ltfsmsg(LTFS_ERR, 17075E, 'N', (int)vol->device->position.block, partition); return ret; - } else if (ret == -LTFS_UNSUPPORTED_INDEX_VERSION) { + } + else if (ret == -LTFS_UNSUPPORTED_INDEX_VERSION) { ret = tape_spacefm(vol->device, 1); if (ret < 0) return ret; @@ -3640,15 +3647,16 @@ int ltfs_traverse_index_no_eod(struct ltfs_volume *vol, char partition, unsigned ltfsmsg(LTFS_DEBUG, 17080D, 'N', vol->index->generation, partition); if (func) { func_ret = (*func)(vol, gen, list, priv); - if(func_ret < 0) { + if (func_ret < 0) { ltfsmsg(LTFS_ERR, 17081E, 'N', func_ret, partition); return func_ret; - } else if (func_ret > 0) /* Break if call back function returns positive value */ + } + else if (func_ret > 0) /* Break if call back function returns positive value */ return 0; } INTERRUPTED_RETURN(); - if(vol->index->generation != (unsigned int)-1 && gen != 0 && vol->index->generation >= gen) + if (vol->index->generation != (unsigned int)-1 && gen != 0 && vol->index->generation >= gen) break; ret = tape_locate_next_index(vol->device); @@ -3658,11 +3666,12 @@ int ltfs_traverse_index_no_eod(struct ltfs_volume *vol, char partition, unsigned } } - if(gen != 0) { - if(vol->index->generation != gen) { + if (gen != 0) { + if (vol->index->generation != gen) { ltfsmsg(LTFS_DEBUG, 17078D, 'N', gen, partition); return -LTFS_NO_INDEX; - } else { + } + else { ltfsmsg(LTFS_INFO, 17077I, 'N', gen, partition); return 0; } @@ -3681,8 +3690,8 @@ int ltfs_traverse_index_no_eod(struct ltfs_volume *vol, char partition, unsigned * @param priv private data for call back function * @return 0 on success or a negative value on error */ -int ltfs_traverse_index_forward(struct ltfs_volume *vol, char partition, unsigned int gen, - f_index_found func, void **list, void* priv) +int ltfs_traverse_index_forward(struct ltfs_volume* vol, char partition, unsigned int gen, + f_index_found func, void** list, void *priv) { int ret, func_ret; struct tape_offset last_index; @@ -3709,7 +3718,8 @@ int ltfs_traverse_index_forward(struct ltfs_volume *vol, char partition, unsigne if (ret < 0 && ret != -LTFS_UNSUPPORTED_INDEX_VERSION) { ltfsmsg(LTFS_ERR, 17075E, 'F', (int)vol->device->position.block, partition); return ret; - } else if (ret == -LTFS_UNSUPPORTED_INDEX_VERSION) { + } + else if (ret == -LTFS_UNSUPPORTED_INDEX_VERSION) { ret = tape_spacefm(vol->device, 1); if (ret < 0) return ret; @@ -3722,15 +3732,16 @@ int ltfs_traverse_index_forward(struct ltfs_volume *vol, char partition, unsigne ltfsmsg(LTFS_DEBUG, 17080D, 'F', vol->index->generation, partition); if (func) { func_ret = (*func)(vol, gen, list, priv); - if(func_ret < 0) { + if (func_ret < 0) { ltfsmsg(LTFS_ERR, 17081E, 'F', func_ret, partition); return func_ret; - } else if (func_ret > 0) /* Break if call back function returns positive value */ + } + else if (func_ret > 0) /* Break if call back function returns positive value */ return 0; } INTERRUPTED_RETURN(); - if(vol->index->generation != (unsigned int)-1 && gen != 0 && vol->index->generation >= gen) + if (vol->index->generation != (unsigned int)-1 && gen != 0 && vol->index->generation >= gen) break; if (last_index.block > vol->device->position.block) { @@ -3742,11 +3753,12 @@ int ltfs_traverse_index_forward(struct ltfs_volume *vol, char partition, unsigne } } - if(gen != 0) { - if(vol->index->generation != gen) { + if (gen != 0) { + if (vol->index->generation != gen) { ltfsmsg(LTFS_DEBUG, 17078D, 'F', gen, partition); return -LTFS_NO_INDEX; - } else { + } + else { ltfsmsg(LTFS_INFO, 17077I, 'F', gen, partition); return 0; } @@ -3764,8 +3776,8 @@ int ltfs_traverse_index_forward(struct ltfs_volume *vol, char partition, unsigne * @param list this pointer is specified as 3rd arguments of call back function * @return 0 on success or a negative value on error */ -int ltfs_traverse_index_backward(struct ltfs_volume *vol, char partition, unsigned int gen, - f_index_found func, void **list, void* priv) +int ltfs_traverse_index_backward(struct ltfs_volume* vol, char partition, unsigned int gen, + f_index_found func, void** list, void *priv) { int ret, func_ret; @@ -3785,7 +3797,8 @@ int ltfs_traverse_index_backward(struct ltfs_volume *vol, char partition, unsign if (ret < 0 && ret != -LTFS_UNSUPPORTED_INDEX_VERSION) { ltfsmsg(LTFS_ERR, 17075E, 'B', (int)vol->device->position.block, partition); return ret; - } else if (ret == -LTFS_UNSUPPORTED_INDEX_VERSION) { + } + else if (ret == -LTFS_UNSUPPORTED_INDEX_VERSION) { ret = tape_spacefm(vol->device, 1); if (ret < 0) return ret; @@ -3799,15 +3812,16 @@ int ltfs_traverse_index_backward(struct ltfs_volume *vol, char partition, unsign if (func) { func_ret = (*func)(vol, gen, list, priv); - if(func_ret < 0) { + if (func_ret < 0) { ltfsmsg(LTFS_ERR, 17081E, 'B', func_ret, partition); return func_ret; - } else if (func_ret > 0) /* Break if call back function returns positive value */ + } + else if (func_ret > 0) /* Break if call back function returns positive value */ return 0; } INTERRUPTED_RETURN(); - if(vol->index->generation != (unsigned int)-1 && gen != 0 && vol->index->generation <= gen) + if (vol->index->generation != (unsigned int)-1 && gen != 0 && vol->index->generation <= gen) break; ret = tape_locate_previous_index(vol->device); @@ -3817,11 +3831,12 @@ int ltfs_traverse_index_backward(struct ltfs_volume *vol, char partition, unsign } } - if(gen != 0) { - if(vol->index->generation != gen) { + if (gen != 0) { + if (vol->index->generation != gen) { ltfsmsg(LTFS_DEBUG, 17078D, 'B', gen, partition); return -LTFS_NO_INDEX; - } else { + } + else { ltfsmsg(LTFS_INFO, 17077I, 'B', gen, partition); return 0; } @@ -3829,7 +3844,6 @@ int ltfs_traverse_index_backward(struct ltfs_volume *vol, char partition, unsign return 0; } - /** * Check EOD status * @param vol LTFS volume @@ -4274,7 +4288,7 @@ void ltfs_recover_eod_simple(struct ltfs_volume *vol) */ int ltfs_print_device_list(struct tape_ops *ops) { - struct tc_drive_info *buf; + struct tc_drive_info* buf = NULL; int i, count = 0, info_count = 0, c = 0, ret = 0; /* Get device count */ @@ -4371,10 +4385,10 @@ static int _ltfs_write_rao_file(char *file_path_org, unsigned char *buf, size_t ltfsmsg(LTFS_ERR, 10001E, __FILE__); return -LTFS_NO_MEMORY; } - - fd = open(path, - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); + + arch_open(&fd, path, + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, + SHARE_FLAG_DENYRW, PERMISSION_READWRITE); if (fd < 0) { ltfsmsg(LTFS_INFO, 17276I, path, errno); free(path); @@ -4382,7 +4396,7 @@ static int _ltfs_write_rao_file(char *file_path_org, unsigned char *buf, size_t return ret; } - size = write(fd, buf, len); + size = arch_write(fd, buf, len); if (size < 0) { ltfsmsg(LTFS_INFO, 17277I, path, errno); ret = -errno; @@ -4399,7 +4413,7 @@ static int _ltfs_write_rao_file(char *file_path_org, unsigned char *buf, size_t out: free(path); - close(fd); + arch_close(fd); return ret; } @@ -4417,8 +4431,7 @@ static int _ltfs_read_rao_file(char *file_path, unsigned char *buf, ltfsmsg(LTFS_ERR, 10001E, __FILE__); return -LTFS_NO_MEMORY; } - - fd = open(path, O_RDONLY | O_BINARY); + arch_open(&fd, path, O_RDONLY | O_BINARY, SHARE_FLAG_DENYWR, PERMISSION_READ); if (fd < 0) { ltfsmsg(LTFS_INFO, 17279I, path, errno); free(path); @@ -4433,7 +4446,7 @@ static int _ltfs_read_rao_file(char *file_path, unsigned char *buf, goto out; } - size = read(fd, buf, len); + size = arch_read(fd, buf, len); if (size < 0) { ltfsmsg(LTFS_INFO, 17281I, path, errno); ret = -errno; @@ -4449,7 +4462,7 @@ static int _ltfs_read_rao_file(char *file_path, unsigned char *buf, out: free(path); - close(fd); + arch_close(fd); return ret; } diff --git a/src/libltfs/ltfs.h b/src/libltfs/ltfs.h index 6e2fdb4a..f7f10549 100644 --- a/src/libltfs/ltfs.h +++ b/src/libltfs/ltfs.h @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -58,41 +58,43 @@ extern "C" { #endif #ifdef mingw_PLATFORM -#include "arch/win/win_util.h" + #include "arch/win/win_util.h" + #include +#else + #include + #include + #include + #ifndef __FreeBSD__ + #include + #endif + #ifdef __APPLE_MAKEFILE__ + #include + #else + #include + #endif #endif +/* O_BINARY is defined only in MinGW */ +#ifndef O_BINARY +#define O_BINARY 0 +#endif #include #include #include #include #include + #include #include #include #include #include #include -#include + #include #include -#ifndef mingw_PLATFORM -#include -#include -#endif - -#ifdef __APPLE_MAKEFILE__ -#include -#else -#include -#endif - -#ifndef mingw_PLATFORM - #ifndef __FreeBSD__ - #include - #endif -#endif #include "libltfs/arch/signal_internal.h" #include "libltfs/arch/arch_info.h" @@ -105,6 +107,7 @@ extern "C" { #include "libltfs/queue.h" #include "libltfs/uthash.h" #include "libltfs/arch/time_internal.h" +#include "libltfs/arch/ltfs_arch_ops.h" #include "tape_ops.h" /* forward declarations from tape.h, tape_ops.h */ @@ -114,6 +117,7 @@ struct device_data; #ifndef LTFS_DEFAULT_WORK_DIR #ifdef mingw_PLATFORM #define LTFS_DEFAULT_WORK_DIR "c:/tmp/ltfs" +#include #else #define LTFS_DEFAULT_WORK_DIR "/tmp/ltfs" #endif @@ -159,7 +163,7 @@ struct device_data; #define LTFS_NO_BARCODE "NO_BARCODE" #ifndef __APPLE_MAKEFILE__ -#include "config.h" +#include "config.h" #endif #define LTFS_LOSTANDFOUND_DIR "_ltfs_lostandfound" @@ -200,6 +204,9 @@ struct device_data; do {sleep(1);}while(1); \ }while(0) +#define NO_BARCODE " " +#define HAVE_BARCODE(v) strcmp(v->label->barcode, NO_BARCODE) + /* Callback prototype used to list directories. The function must return 0 on success * or a negative value on error. */ typedef int (*ltfs_dir_filler) (void *buf, const char *name, void *priv); @@ -391,9 +398,9 @@ struct ltfs_volume { /* LTFS format data */ struct tc_coherency ip_coh; /**< Index partition coherency info */ struct tc_coherency dp_coh; /**< Data partition coherency info */ - struct ltfs_label *label; /**< Information from the partition labels */ - struct ltfs_index *index; /**< Current cartridge index */ - char *index_cache_path; /**< File name of on-disk index cache */ + struct ltfs_label* label; /**< Information from the partition labels */ + struct ltfs_index* index; /**< Current cartridge index */ + char *index_cache_path; /**< File name of on-disk index cache */ /* Opaque handles to higher-level structures */ void *iosched_handle; /**< Handle to the I/O scheduler state */ @@ -403,7 +410,7 @@ struct ltfs_volume { void *kmi_handle; /**< Handle to the key manager interface state */ /* Internal state variables */ - struct device_data *device; /**< Device-specific data */ + struct device_data* device; /**< Device-specific data */ bool ip_index_file_end; /**< Does the index partition end in an index file? */ bool dp_index_file_end; /**< Does the data partition end in an index file? */ enum volume_mount_type mount_type; /**< Mount type defined by enum */ @@ -442,7 +449,7 @@ struct ltfs_volume { bool livelink; /**< Live Link enabled? (SDE) */ char *mountpoint; /**< Store mount point for Live Link (SDE) */ size_t mountpoint_len; /**< Store mount point path length (SDE) */ - struct tape_attr *t_attr; /**< Tape Attribute data */ + struct tape_attr* t_attr; /**< Tape Attribute data */ mam_lockval lock_status; /**< Total volume lock status from t_attr->vollock and index->vollock */ struct ltfs_timespec first_locate; /**< Time to first locate */ int file_open_count; /**< Number of opened files */ @@ -502,10 +509,10 @@ struct ltfs_index { struct index_criteria index_criteria; /**< Active index criteria */ struct dentry *root; /**< The directory tree */ - ltfs_mutex_t rename_lock; /**< Controls name tree access during renames */ + ltfs_mutex_t rename_lock; /**< Controls name tree access during renames */ /* Update tracking */ - ltfs_mutex_t dirty_lock; /**< Controls access to the update tracking bits */ + ltfs_mutex_t dirty_lock; /**< Controls access to the update tracking bits */ bool dirty; /**< Set on metadata update, cleared on write to tape */ bool atime_dirty; /**< Set on atime update, cleared on write to tape */ bool use_atime; /**< Set if atime updates should make the index dirty */ @@ -642,6 +649,7 @@ bool ltfs_get_criteria_allow_update(struct ltfs_volume *vol); int ltfs_start_mount(bool trial, struct ltfs_volume *vol); int ltfs_mount(bool force_full, bool deep_recovery, bool recover_extra, bool recover_symlink, unsigned short gen, struct ltfs_volume *vol); +int ltfs_print_device_list(struct tape_ops* ops); int ltfs_unmount(char *reason, struct ltfs_volume *vol); void ltfs_dump_tree_unlocked(struct ltfs_index *index); void ltfs_dump_tree(struct ltfs_volume *vol); @@ -685,14 +693,14 @@ char ltfs_dp_id(struct ltfs_volume *vol); char ltfs_ip_id(struct ltfs_volume *vol); const char *ltfs_get_volume_uuid(struct ltfs_volume *vol); -int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume *vol); +int ltfs_sync_index(char *reason, bool index_locking, struct ltfs_volume* vol); int ltfs_traverse_index_forward(struct ltfs_volume *vol, char partition, unsigned int gen, f_index_found func, void **list, void *priv); int ltfs_traverse_index_backward(struct ltfs_volume *vol, char partition, unsigned int gen, f_index_found func, void **list, void *priv); int ltfs_traverse_index_no_eod(struct ltfs_volume *vol, char partition, unsigned int gen, - f_index_found func, void **list, void *priv); + f_index_found func, void **list, void *priv); int ltfs_check_eod_status(struct ltfs_volume *vol); int ltfs_recover_eod(struct ltfs_volume *vol); int ltfs_release_medium(struct ltfs_volume *vol); @@ -703,7 +711,6 @@ int ltfs_mam(const tape_partition_t part, unsigned char *buf, int ltfs_wait_device_ready(struct ltfs_volume *vol); void ltfs_recover_eod_simple(struct ltfs_volume *vol); -int ltfs_print_device_list(struct tape_ops *ops); void ltfs_enable_livelink_mode(struct ltfs_volume *vol); int ltfs_profiler_set(uint64_t source, struct ltfs_volume *vol); diff --git a/src/libltfs/ltfs_fsops.c b/src/libltfs/ltfs_fsops.c index 89fa1e50..5c7ad673 100644 --- a/src/libltfs/ltfs_fsops.c +++ b/src/libltfs/ltfs_fsops.c @@ -369,7 +369,7 @@ int ltfs_fsops_create(const char *path, bool isdir, bool readonly, bool overwrit ltfs_set_index_dirty(false, false, vol->index); d->dirty = true; ltfs_mutex_unlock(&vol->index->dirty_lock); - vol->file_open_count ++; + vol->file_open_count++; *dentry = d; ret = 0; @@ -522,8 +522,9 @@ int ltfs_fsops_unlink(const char *path, ltfs_file_id *id, struct ltfs_volume *vo releasewrite_mrsw(&d->meta_lock); ltfs_mutex_lock(&vol->index->dirty_lock); - if (! d->isdir) + if (!d->isdir) { --vol->index->file_count; + } ltfs_set_index_dirty(false, false, vol->index); ltfs_mutex_unlock(&vol->index->dirty_lock); @@ -548,7 +549,7 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct int ret; char *from_norm = NULL, *to_norm = NULL; char *from_norm_copy = NULL, *to_norm_copy = NULL; - char *from_filename, *to_filename; + char *from_filename = NULL, *to_filename = NULL; char *to_filename_copy = NULL, *to_filename_copy2 = NULL; struct dentry *fromdir = NULL, *todir = NULL; struct dentry *fromdentry = NULL, *todentry = NULL; @@ -588,8 +589,8 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct } if (dcache_initialized(vol)) { - from_norm_copy = strdup(from_norm); - to_norm_copy = strdup(to_norm); + from_norm_copy = arch_strdup(from_norm); + to_norm_copy = arch_strdup(to_norm); if (! from_norm_copy || ! to_norm_copy) { ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_rename: file name copy"); ret = -LTFS_NO_MEMORY; @@ -602,8 +603,8 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct fs_split_path(to_norm, &to_filename, strlen(to_norm) + 1); /* Allocate memory for new file name */ - to_filename_copy = strdup(to_filename); - to_filename_copy2 = strdup(to_filename); + to_filename_copy = arch_strdup(to_filename); + to_filename_copy2 = arch_strdup(to_filename); if (! to_filename_copy || ! to_filename_copy2) { ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_rename: file name copy"); ret = -LTFS_NO_MEMORY; @@ -868,6 +869,7 @@ int ltfs_fsops_rename(const char *from, const char *to, ltfs_file_id *id, struct fromdentry->dirty = true; + /* Release dentry of source */ if (! iosched_initialized(vol)) fs_release_dentry_unlocked(fromdentry); else @@ -1888,7 +1890,7 @@ int ltfs_fsops_flush(struct dentry *d, bool closeflag, struct ltfs_volume *vol) return ret; } -int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id, struct ltfs_volume *vol) +int ltfs_fsops_symlink_path(const char *to, const char *from, ltfs_file_id *id, struct ltfs_volume *vol) { struct dentry *d; bool use_iosche=false; @@ -1912,7 +1914,7 @@ int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id, id->uid = d->uid; id->ino = d->ino; - d->target.name = strdup(to); + d->target.name = arch_strdup(to); d->target.percent_encode = fs_is_percent_encode_required(to); d->isslink = true; @@ -1938,7 +1940,7 @@ int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id, return ret; } -int ltfs_fsops_readlink_path(const char* path, char* buf, size_t size, ltfs_file_id *id, struct ltfs_volume *vol) +int ltfs_fsops_readlink_path(const char *path, char *buf, size_t size, ltfs_file_id *id, struct ltfs_volume *vol) { struct dentry *d; bool use_iosche=false; @@ -1964,14 +1966,14 @@ int ltfs_fsops_readlink_path(const char* path, char* buf, size_t size, ltfs_file if ( size < strlen(d->target.name) + 1 ) { return -LTFS_SMALL_BUFFER; } - strncpy(buf, d->target.name, size); + arch_strncpy_auto(buf, d->target.name, size); if ( vol->livelink ) { memset( value, 0, sizeof(value)); ret = xattr_get(d, LTFS_LIVELINK_EA_NAME, value, sizeof(value), vol); if ( ret > 0 ) { ltfsmsg(LTFS_DEBUG, 11323D, value); - ret = sscanf(value, "%d:%d", &num1, &num2); + ret = arch_sscanf(value, "%d:%d", &num1, &num2); if ( ( ret == 1 ) && ( num1 != 0 ) ){ memset( buf, 0, size); #ifndef mingw_PLATFORM @@ -1980,7 +1982,7 @@ int ltfs_fsops_readlink_path(const char* path, char* buf, size_t size, ltfs_file } strcpy(buf, vol->mountpoint); #endif - strcat(buf, d->target.name + num1); + arch_strcat(buf,size, d->target.name + num1); ltfsmsg(LTFS_DEBUG, 11324D, d->target.name, buf); } } @@ -1994,88 +1996,92 @@ int ltfs_fsops_readlink_path(const char* path, char* buf, size_t size, ltfs_file return 0; /* Shall be return 0, if success */ } -int ltfs_fsops_target_absolute_path(const char* link, const char* target, char* buf, size_t size ) +int ltfs_fsops_target_absolute_path(const char *link, const char *target, char *buf, size_t size ) { - char *work_buf, *target_buf, *temp_buf, *token, *next_token; // work buffer for string - int len=0,len2=0; // work variables for string length + char *work_buf, *target_buf, *temp_buf, *token, *next_token; /* work buffers for string */ + int len=0, len2=0; /* work variables for string length */ CHECK_ARG_NULL(link, -LTFS_NULL_ARG); CHECK_ARG_NULL(target, -LTFS_NULL_ARG); - // need to set message and return code + /* need to set message and return code */ if (link[0]!='/') { return -LTFS_BAD_ARG; } + /* Check input target string is already absolute path or not */ len2 = strlen(target); - // input target string is already absolute path if ( (target[0]=='/') && !strstr(target,"./" ) ) { if ( size < (size_t)len2+1) { return -LTFS_SMALL_BUFFER; } - strcpy( buf, target ); + arch_strcpy(buf,len2, target); return 0; } len=strlen(link); - work_buf = malloc(len+len2+1); + int work_buf_len = len + len2 + 1; + work_buf = malloc(work_buf_len); if (!work_buf) { + ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_target_absolute_path: work_buf"); return -LTFS_NO_MEMORY; } - - target_buf = malloc(len2+1); + int target_buf_len = len2 + 1; + target_buf = malloc(target_buf_len); if (!target_buf) { - free( work_buf ); + free(work_buf); + ltfsmsg(LTFS_ERR, 10001E, "ltfs_fsops_target_absolute_path: target_buf"); return -LTFS_NO_MEMORY; } - if ( target[0]=='/' ) { - temp_buf=strstr( target, "/." ); // get "/../ccc/target.txt" of "/aaa/../ccc/target.txt" - strcpy(target_buf,temp_buf+1); // copy "../ccc/target.txt" - len=strlen(target_buf)+1; - len=len2-len; - strncpy( work_buf, target, len ); // copy "/aaa" + if (target[0]=='/') { + temp_buf = strstr(target, "/."); /* get "/../ccc/target.txt" of "/aaa/../ccc/target.txt" */ + arch_strcpy(target_buf, target_buf_len, temp_buf + 1); /* copy "../ccc/target.txt" */ + len = strlen(target_buf) + 1; + len = len2 - len; + arch_strncpy(work_buf, target, work_buf_len,len); /* copy "/aaa" */ } else { - strcpy( work_buf, link ); - strcpy( target_buf, target ); - - // split link file name then get current directory - temp_buf=strrchr( work_buf, '/' ); // get "/link.txt" from "/aaa/bbb/link.txt" - len = len-strlen(temp_buf); // length of "/aaa/bbb" - } - - // split target path directory then modify current directory with target path information - token = strtok( target_buf,"/" ); // get ".." from "../ccc/target.txt" - while ( token ) { - next_token = strtok( NULL,"/" ); // if next_token is NULL then token is filename - if ( !next_token ) break; - if ( strcmp( token,".." )==0 ) { - work_buf[len]='\0'; // "/aaa/bbb\0link.txt" - temp_buf=strrchr( work_buf, '/' ); // get "/bbb" + arch_strcpy(work_buf, work_buf_len, link); + arch_strcpy(target_buf, target_buf_len, target); + + /* Split link file name then get current directory */ + temp_buf = strrchr(work_buf, '/'); /* get "/link.txt" from "/aaa/bbb/link.txt" */ + len -= strlen(temp_buf); /* length of "/aaa/bbb" */ + } + char *contextVal = NULL; + /* Split target path directory then modify current directory with target path information */ + token = arch_strtok(target_buf, "/", contextVal); /* get ".." from "../ccc/target.txt" */ + while (token) { + next_token = arch_strtok(NULL, "/", contextVal); /* if next_token is NULL then token is filename */ + if (!next_token) + break; + if (strcmp(token, "..") == 0) { + work_buf[len] = '\0'; /* "/aaa/bbb\0link.txt" */ + temp_buf = strrchr(work_buf, '/' ); /* get "/bbb" */ if (!temp_buf) { - *buf = '\0'; // out of ltfs range + *buf = '\0'; /* out of ltfs range */ return 0; } - len = len-strlen(temp_buf); // length of "/aaa" - } else if ( strcmp( token, "." ) ) { // have directory name - work_buf[len]='/'; // put '/ 'as "/aaa/" - strncpy( work_buf+len+1, token, strlen(token)+1 ); // "/aaa/ccc\0" - len=strlen(work_buf); + len -= strlen(temp_buf); /* length of "/aaa" */ + } else if (strcmp(token, "." )) { /* have directory name */ + work_buf[len] = '/'; /* put '/ 'as "/aaa/" */ + arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token) + 1); /* "/aaa/ccc\0" */ + len = strlen(work_buf); } token = next_token; } - work_buf[len]='/'; // put '/ 'as "/aaa/ccc/" - strncpy( work_buf+len+1, token, strlen(token)+1 ); // "/aaa/ccc/target.txt\0" + work_buf[len] = '/'; /* put '/ 'as "/aaa/ccc/" */ + arch_strncpy(work_buf+len+1, token, work_buf_len, strlen(token)+1); /* "/aaa/ccc/target.txt\0" */ - if ( size < strlen(work_buf)+1 ) { - free( work_buf ); - free( target_buf ); + if (size < strlen(work_buf) + 1) { + free(work_buf); + free(target_buf); return -LTFS_SMALL_BUFFER; } - strcpy( buf, work_buf ); - free( work_buf ); - free( target_buf ); + arch_strcpy(buf,size, work_buf); + free(work_buf); + free(target_buf); return 0; } @@ -2087,6 +2093,7 @@ int ltfs_fsops_volume_sync(char *reason, struct ltfs_volume *vol) if (ret < 0) return ret; + ret = ltfs_sync_index(reason, true, vol); return ret; diff --git a/src/libltfs/ltfs_fsops.h b/src/libltfs/ltfs_fsops.h index 7408cc47..19bef9e3 100644 --- a/src/libltfs/ltfs_fsops.h +++ b/src/libltfs/ltfs_fsops.h @@ -506,7 +506,7 @@ int ltfs_fsops_flush(struct dentry *d, bool closeflag, struct ltfs_volume *vol); * - -LTFS_NULL_ARG if 'vol' is NULL * - Another negative value if an internal error or device write error occurred. */ -int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id, struct ltfs_volume *vol); +int ltfs_fsops_symlink_path(const char *to, const char *from, ltfs_file_id *id, struct ltfs_volume *vol); /** * Read a target of the symbolic link node @@ -520,7 +520,7 @@ int ltfs_fsops_symlink_path(const char* to, const char* from, ltfs_file_id *id, * - -LTFS_NULL_ARG if 'vol' is NULL * - Another negative value if an internal error or device write error occurred. */ -int ltfs_fsops_readlink_path(const char* path, char* buf, size_t size, ltfs_file_id *id, struct ltfs_volume *vol); +int ltfs_fsops_readlink_path(const char *path, char *buf, size_t size, ltfs_file_id *id, struct ltfs_volume *vol); /** * Change target path from relative to absolute (Use for Windows) @@ -535,7 +535,7 @@ int ltfs_fsops_readlink_path(const char* path, char* buf, size_t size, ltfs_file * - -LTFS_SMALL_BUFFER if input buffer is smaller than the resolved string * - Another negative value if an internal error or device write error occurred. */ -int ltfs_fsops_target_absolute_path(const char* link, const char* target, char* buf, size_t size ); +int ltfs_fsops_target_absolute_path(const char *link, const char *target, char *buf, size_t size ); /** * Flush all cached data to the medium and write index. diff --git a/src/libltfs/ltfs_internal.c b/src/libltfs/ltfs_internal.c index f3d0055d..e81d0282 100644 --- a/src/libltfs/ltfs_internal.c +++ b/src/libltfs/ltfs_internal.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -216,9 +216,9 @@ int ltfs_read_labels(bool trial, struct ltfs_volume *vol) /* Store label data in the supplied volume */ vol->label->creator = label0->creator; label0->creator = NULL; - strncpy(vol->label->barcode, label0->barcode, 6); + arch_strcpy_auto(vol->label->barcode, label0->barcode); vol->label->barcode[6] = '\0'; - strncpy(vol->label->vol_uuid, label0->vol_uuid, 36); + arch_strcpy_auto(vol->label->vol_uuid, label0->vol_uuid); vol->label->vol_uuid[36] = '\0'; vol->label->format_time = label0->format_time; vol->label->blocksize = label0->blocksize; @@ -388,7 +388,7 @@ int ltfs_read_one_label(tape_partition_t partition, struct ltfs_label *label, * @return 0 on success, 1 if index file does not end with a file mark (but is otherwise valid), * or a negative value on error. */ -int ltfs_read_index(uint64_t eod_pos, bool recover_symlink, struct ltfs_volume *vol) +int ltfs_read_index(uint64_t eod_pos, bool recover_symlink, struct ltfs_volume* vol) { int ret, ret_sym; struct tc_position pos; @@ -411,25 +411,28 @@ int ltfs_read_index(uint64_t eod_pos, bool recover_symlink, struct ltfs_volume * /* Parse and validate the schema */ ret = xml_schema_from_tape(eod_pos, vol); - if ( vol->index->symerr_count ) { - if ( recover_symlink ) { - ret_sym = ltfs_split_symlink( vol ); + if (vol->index->symerr_count) { + if (recover_symlink) { + ret_sym = ltfs_split_symlink(vol); if (ret_sym < 0) { ret = ret_sym; - } else if (ret == -LTFS_SYMLINK_CONFLICT) { + } + else if (ret == -LTFS_SYMLINK_CONFLICT) { ret = 0; } - } else { + } + else { ltfsmsg(LTFS_ERR, 11321E); } - free( vol->index->symlink_conflict ); + free(vol->index->symlink_conflict); vol->index->symerr_count = 0; } if (ret < 0) { ltfsmsg(LTFS_WARN, 11194W, ret); return ret; - } else if (ret == LTFS_NO_TRAIL_FM) + } + else if (ret == LTFS_NO_TRAIL_FM) end_fm = false; /* check volume UUID */ @@ -450,13 +453,15 @@ int ltfs_read_index(uint64_t eod_pos, bool recover_symlink, struct ltfs_volume * vol->index->backptr.partition != vol->label->partid_dp) { ltfsmsg(LTFS_ERR, 11197E); return -LTFS_INDEX_INVALID; - } else if (vol->index->backptr.partition == vol->index->selfptr.partition && + } + else if (vol->index->backptr.partition == vol->index->selfptr.partition && vol->index->selfptr.block != 5 && vol->index->backptr.block != vol->index->selfptr.block && - vol->index->backptr.block >= vol->index->selfptr.block - 2 ) { + vol->index->backptr.block >= vol->index->selfptr.block - 2) { ltfsmsg(LTFS_ERR, 11197E); return -LTFS_INDEX_INVALID; - } else if (vol->index->backptr.partition != 0 && vol->index->backptr.block < 5) { + } + else if (vol->index->backptr.partition != 0 && vol->index->backptr.block < 5) { ltfsmsg(LTFS_ERR, 11197E); return -LTFS_INDEX_INVALID; } @@ -592,7 +597,7 @@ int ltfs_seek_index(char partition, tape_block_t *eod_pos, tape_block_t *index_e coh = &vol->ip_coh; else coh = &vol->dp_coh; - strcpy(coh->uuid, vol->label->vol_uuid); + arch_strcpy_auto(coh->uuid, vol->label->vol_uuid); coh->count = vol->index->generation; coh->set_id = vol->index->selfptr.block; @@ -993,7 +998,8 @@ int _ltfs_make_lost_found(tape_block_t ip_eod, tape_block_t dp_eod, * * @param fix allow simple fixes to make the tape consistent? * Here, simple means writing an additional logically unmodified copy - * or copies of an index file already present on the tape. + * or copies of an index file already present on the tape. Also + * allows truncating incomplete index at the end of the partition. * @param deep Allow fancy recovery procedures? In particular, this flag enables recovery in the * case where extra blocks (after the last index file on a partition) are found on the * tape. The nature of this recovery is controlled by the recover_extra flag. @@ -1083,7 +1089,7 @@ int ltfs_check_medium(bool fix, bool deep, bool recover_extra, bool recover_syml ltfs_index_free(&dp_index); ltfs_index_free(&ip_index); check_err(ltfs_index_alloc(&vol->index, vol), 11225E, out_unlock); - strcpy(vol->index->vol_uuid, vol->label->vol_uuid); + arch_strcpy_auto(vol->index->vol_uuid, vol->label->vol_uuid); vol->index->mod_time = vol->label->format_time; vol->index->root->creation_time = vol->index->mod_time; vol->index->root->change_time = vol->index->mod_time; @@ -1190,6 +1196,7 @@ int ltfs_check_medium(bool fix, bool deep, bool recover_extra, bool recover_syml } } /* write to data partition if it doesn't end in an index file */ + if (! dp_have_index || dp_blocks_after) { ltfsmsg(LTFS_INFO, 17259I, "DP", vol->index->selfptr.partition, (unsigned long long)vol->index->selfptr.block); ret = ltfs_write_index(vol->label->partid_dp, SYNC_RECOVERY, vol); @@ -1238,7 +1245,7 @@ int ltfs_update_cart_coherency(struct ltfs_volume *vol) vol->ip_coh.version = 1; /* From PGA2 */ vol->ip_coh.volume_change_ref = current_vcr; if (vol->ip_coh.uuid[0] == '\0') - strcpy(vol->ip_coh.uuid, vol->label->vol_uuid); + arch_strcpy_auto(vol->ip_coh.uuid, vol->label->vol_uuid); tape_set_cart_coherency(vol->device, ltfs_part_id2num(ltfs_ip_id(vol), vol), &vol->ip_coh); } @@ -1251,7 +1258,7 @@ int ltfs_update_cart_coherency(struct ltfs_volume *vol) vol->dp_coh.version = 1; /* From PGA2 */ vol->dp_coh.volume_change_ref = current_vcr; if (vol->dp_coh.uuid[0] == '\0') - strcpy(vol->dp_coh.uuid, vol->label->vol_uuid); + arch_strcpy_auto(vol->dp_coh.uuid, vol->label->vol_uuid); tape_set_cart_coherency(vol->device, ltfs_part_id2num(ltfs_dp_id(vol), vol), &vol->dp_coh); } @@ -1274,17 +1281,18 @@ int ltfs_write_index_conditional(char partition, struct ltfs_volume *vol) CHECK_ARG_NULL(vol, -LTFS_NULL_ARG); - if (partition == ltfs_ip_id(vol) && ! vol->ip_index_file_end) + if (partition == ltfs_ip_id(vol) && ! vol->ip_index_file_end) { ret = ltfs_write_index(partition, SYNC_CASCHE_PRESSURE, vol); - else if (partition == ltfs_dp_id(vol) && + } else if (partition == ltfs_dp_id(vol) && (! vol->dp_index_file_end || - (vol->ip_index_file_end && vol->index->selfptr.partition == ltfs_ip_id(vol)))) + (vol->ip_index_file_end && vol->index->selfptr.partition == ltfs_ip_id(vol)))) { ret = ltfs_write_index(partition, SYNC_CASCHE_PRESSURE, vol); + } return ret; } -int ltfs_split_symlink( struct ltfs_volume *vol ) +int ltfs_split_symlink(struct ltfs_volume *vol) { size_t i, size; struct dentry *d, *workd; @@ -1297,7 +1305,7 @@ int ltfs_split_symlink( struct ltfs_volume *vol ) if ( iosched_initialized(vol) ) use_iosche=true; /* check lost_and_found directory and make if it doesn't exist */ - asprintf( &lfdir, "/%s", LTFS_LOSTANDFOUND_DIR ); + int pathsize = asprintf( &lfdir, "/%s", LTFS_LOSTANDFOUND_DIR ); ret = fs_path_lookup(lfdir, 0, &workd, vol->index); if ( ret==-LTFS_NO_DENTRY ) { ret = ltfs_fsops_create( lfdir, true, false, false, &workd, vol); @@ -1312,7 +1320,7 @@ int ltfs_split_symlink( struct ltfs_volume *vol ) return ret; } ret = ltfs_fsops_close( workd, true, true, use_iosche, vol); - path=strdup(lfdir); + path=arch_strdup(lfdir); /* loop for conflicted files */ for( i=0; i<(vol->index->symerr_count); i++ ){ @@ -1321,8 +1329,9 @@ int ltfs_split_symlink( struct ltfs_volume *vol ) ret = fs_dentry_lookup(d, &name); if (ret<0) goto out_func; - tok=strtok( name+1, "/" ); - next_tok=strtok( NULL, "/" ); + char *contextVal = NULL; + tok = arch_strtok( name+1, "/", contextVal); + next_tok = arch_strtok( NULL, "/" , contextVal); /* check directory path and make if it doesn't exist */ while( next_tok ){ @@ -1332,20 +1341,22 @@ int ltfs_split_symlink( struct ltfs_volume *vol ) if ( ret==-LTFS_NO_DENTRY ) basedir=false; else if ( ret<0 ) + { goto err_out_func; + } } if( !basedir ) { ret = ltfs_fsops_create( path, true, false, false, &workd, vol); - if ( ret<0 ) + if (ret < 0) + { goto err_out_func; - + } } ret = ltfs_fsops_close( workd, true, true, use_iosche, vol); tok = next_tok; - next_tok=strtok( NULL, "/" ); + next_tok = arch_strtok( NULL, "/", contextVal); } - /* Make filename with path in lost_and_found */ asprintf( &path, "%s/%s", path, tok); ret = fs_path_lookup(path, 0, &workd, vol->index); @@ -1383,7 +1394,8 @@ int ltfs_split_symlink( struct ltfs_volume *vol ) d->isslink = false; free(d->target.name); free(name); - strcpy(path,lfdir); + + arch_strcpy(path, pathsize,lfdir); basedir=true; } goto out_func; diff --git a/src/libltfs/ltfslogging.c b/src/libltfs/ltfslogging.c index 9ff8e2bb..af44fe25 100644 --- a/src/libltfs/ltfslogging.c +++ b/src/libltfs/ltfslogging.c @@ -47,6 +47,7 @@ #ifdef mingw_PLATFORM #include "arch/win/win_util.h" +#include #endif #include #include @@ -88,7 +89,7 @@ #include "queue.h" #include "ltfssnmp.h" -/* Some hard-coded message bits. */ + /* Some hard-coded message bits. */ #define MSG_PREFIX_POSIX_TID "%016llx LTFS%s " #define MSG_PREFIX_TID "%lx LTFS%s " #define MSG_PREFIX "LTFS%s " @@ -100,8 +101,8 @@ struct plugin_bundle { TAILQ_ENTRY(plugin_bundle) list; int32_t start_id; /**< First message ID allocated to this plugin */ int32_t end_id; /**< Last message ID allocated to this plugin */ - UResourceBundle *bundle_root; /**< Root resource bundle for this plugin */ - UResourceBundle *bundle_messages; /**< Resource bundle containing this plugin's messages */ + UResourceBundle* bundle_root; /**< Root resource bundle for this plugin */ + UResourceBundle* bundle_messages; /**< Resource bundle containing this plugin's messages */ }; /* Syslog levels corresponding to the LTFS logging levels defined in libltfs/ltfslogging.h. */ @@ -117,9 +118,9 @@ static int syslog_levels[] = { }; #ifdef mingw_PLATFORM -char *libltfs_dat; -char *internal_error_dat; -char *tape_common_dat; +char* libltfs_dat; +char* internal_error_dat; +char* tape_common_dat; #else U_CFUNC char libltfs_dat[]; /* U_CFUNC is an ICU synonym for extern. */ U_CFUNC char internal_error_dat[]; /* U_CFUNC is an ICU synonym for extern. */ @@ -133,23 +134,23 @@ bool ltfs_print_thread_id = false; static bool ltfs_use_syslog = false; /* Resource bundles, used for quick indexing into message arrays. */ -static UResourceBundle *bundle_fallback; +static UResourceBundle* bundle_fallback; static TAILQ_HEAD(message_struct, plugin_bundle) plugin_bundles; /* Static output buffer: needed to avoid allocating memory on error. */ static ltfs_mutex_t output_lock; static char output_buf[OUTPUT_BUF_SIZE]; static char msg_buf[OUTPUT_BUF_SIZE * 2]; -static UConverter *output_conv = NULL; +static UConverter* output_conv = NULL; #ifdef mingw_PLATFORM -static int _open_message_file(char *bundle_name, void **bundle_data); +static int _open_message_file(char* bundle_name, void** bundle_data); #endif int ltfsprintf_init(int log_level, bool use_syslog, bool print_thread_id) { int ret; UErrorCode err = U_ZERO_ERROR; - struct plugin_bundle *pl; + struct plugin_bundle* pl; /* Open converter for generating output in the system locale. */ ret = ltfs_mutex_init(&output_lock); @@ -172,7 +173,7 @@ int ltfsprintf_init(int log_level, bool use_syslog, bool print_thread_id) #endif /* Load the libltfs message bundle and the primary message set */ - ret = ltfsprintf_load_plugin("libltfs", libltfs_dat, (void **)&pl); + ret = ltfsprintf_load_plugin("libltfs", libltfs_dat, (void**)&pl); if (ret < 0) { fprintf(stderr, "LTFS11293E Cannot load messages for libltfs (%d)\n", ret); ltfsprintf_finish(); @@ -189,7 +190,7 @@ int ltfsprintf_init(int log_level, bool use_syslog, bool print_thread_id) } /* Load the libltfs message bundle and the primary message set */ - ret = ltfsprintf_load_plugin("internal_error", internal_error_dat, (void **)&pl); + ret = ltfsprintf_load_plugin("internal_error", internal_error_dat, (void**)&pl); if (ret < 0) { fprintf(stderr, "LTFS11293E Cannot load messages for internal error (%d)\n", ret); ltfsprintf_finish(); @@ -197,7 +198,7 @@ int ltfsprintf_init(int log_level, bool use_syslog, bool print_thread_id) } /* Load the libltfs message bundle and the primary message set */ - ret = ltfsprintf_load_plugin("tape_common", tape_common_dat, (void **)&pl); + ret = ltfsprintf_load_plugin("tape_common", tape_common_dat, (void**)&pl); if (ret < 0) { fprintf(stderr, "LTFS11293E Cannot load messages for tape backend common messages (%d)\n", ret); ltfsprintf_finish(); @@ -223,7 +224,7 @@ void ltfsprintf_finish() bundle_fallback = NULL; } while (1) { - if (! TAILQ_EMPTY(&plugin_bundles)) + if (!TAILQ_EMPTY(&plugin_bundles)) ltfsprintf_unload_plugin(TAILQ_LAST(&plugin_bundles, message_struct)); else break; @@ -261,11 +262,11 @@ int ltfsprintf_set_log_level(int log_level) } -int ltfsprintf_load_plugin(const char *bundle_name, void *bundle_data, void **messages) +int ltfsprintf_load_plugin(const char* bundle_name, void* bundle_data, void** messages) { UErrorCode err = U_ZERO_ERROR; - UResourceBundle *bundle; - struct plugin_bundle *pl; + UResourceBundle* bundle; + struct plugin_bundle* pl; CHECK_ARG_NULL(bundle_name, -LTFS_NULL_ARG); CHECK_ARG_NULL(messages, -LTFS_NULL_ARG); @@ -282,7 +283,7 @@ int ltfsprintf_load_plugin(const char *bundle_name, void *bundle_data, void **me #endif pl = calloc(1, sizeof(struct plugin_bundle)); - if (! pl) { + if (!pl) { if (libltfs_dat_init) ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); else @@ -350,7 +351,8 @@ int ltfsprintf_load_plugin(const char *bundle_name, void *bundle_data, void **me pl->end_id = pl->start_id + 999; } ures_close(bundle); - } else + } + else pl->end_id = pl->start_id + 999; *messages = pl; @@ -360,9 +362,9 @@ int ltfsprintf_load_plugin(const char *bundle_name, void *bundle_data, void **me return 0; } -void ltfsprintf_unload_plugin(void *handle) +void ltfsprintf_unload_plugin(void* handle) { - struct plugin_bundle *pl = handle; + struct plugin_bundle* pl = handle; if (pl) { ltfs_mutex_lock(&output_lock); @@ -375,16 +377,16 @@ void ltfsprintf_unload_plugin(void *handle) } /* Print a formatted message in the current system locale. */ -int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, ...) +int ltfsmsg_internal(bool print_id, int level, char** msg_out, const char* _id, ...) { - const UChar *format_uc = NULL; + const UChar* format_uc = NULL; int32_t prefix_len, format_len; int32_t id_val; char id[16]; size_t idlen; UErrorCode err = U_ZERO_ERROR; va_list argp; - struct plugin_bundle *entry; + struct plugin_bundle* entry; /* * We accept quoted id used in HPE backend source, @@ -395,16 +397,17 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, goto internal_error; if (idlen > 1 && _id[0] == '"' && _id[idlen - 1] == '"') { - strncpy(id, _id + 1, idlen - 2); + arch_strcpy_limited(id, _id + 1, idlen - 2); id[idlen - 2] = '\0'; - } else { - strcpy(id, _id); + } + else { + arch_strcpy_auto(id, _id); } id_val = atol(id); /* Check loaded plugins for the message, most recently loaded first */ - if (! TAILQ_EMPTY(&plugin_bundles)) { + if (!TAILQ_EMPTY(&plugin_bundles)) { ltfs_mutex_lock(&output_lock); TAILQ_FOREACH(entry, &plugin_bundles, list) { if (entry->start_id <= id_val && id_val <= entry->end_id) { @@ -413,10 +416,12 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, if (U_FAILURE(err) && err != U_MISSING_RESOURCE_ERROR) { ltfs_mutex_unlock(&output_lock); goto internal_error; - } else if (U_SUCCESS(err)) + } + else if (U_SUCCESS(err)) break; format_uc = NULL; - } else if (id[0] == 'I' || id[0] == 'D') { + } + else if (id[0] == 'I' || id[0] == 'D') { err = U_ZERO_ERROR; format_uc = ures_getStringByKey(entry->bundle_messages, id, &format_len, &err); if (U_SUCCESS(err)) @@ -429,7 +434,7 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, } /* Try to get a fallback message if we didn't find the real message */ - if (! format_uc) { + if (!format_uc) { format_uc = ures_getStringByKey(bundle_fallback, "notfound", &format_len, &err); if (U_FAILURE(err)) goto internal_error; @@ -438,9 +443,9 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, /* Format and print the message string. */ ltfs_mutex_lock(&output_lock); if (ltfs_print_thread_id) - prefix_len = print_id ? sprintf(output_buf, MSG_PREFIX_TID, (unsigned long)ltfs_get_thread_id(), id) : 0; + prefix_len = print_id ? arch_sprintf_auto(output_buf, MSG_PREFIX_TID, (unsigned long)ltfs_get_thread_id(), id) : 0; else - prefix_len = print_id ? sprintf(output_buf, MSG_PREFIX, id) : 0; + prefix_len = print_id ? arch_sprintf_auto(output_buf, MSG_PREFIX, id) : 0; ucnv_fromUChars(output_conv, output_buf + prefix_len, OUTPUT_BUF_SIZE - prefix_len - 1, format_uc, format_len, &err); if (err == U_BUFFER_OVERFLOW_ERROR) { @@ -457,15 +462,22 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, ltfs_mutex_unlock(&output_lock); goto internal_error; } - } else if (U_FAILURE(err)) { + } + else if (U_FAILURE(err)) { ltfs_mutex_unlock(&output_lock); goto internal_error; } #ifdef mingw_PLATFORM - va_start(argp, _id); - vsyslog(level, output_buf, argp); - va_end(argp); + __try { + va_start(argp, _id); + vsyslog(level, output_buf, argp); + va_end(argp); + } + __except (EXCEPTION_EXECUTE_HANDLER) { + EXCEPTION_POINTERS* exceptionInfo = GetExceptionInformation(); + fprintf(stderr, "An exception occurred when logging! Exception code: 0x%X\n", exceptionInfo->ExceptionRecord->ExceptionCode); + } #else va_start(argp, _id); vfprintf(stderr, output_buf, argp); @@ -486,21 +498,21 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, if (msg_out) { va_start(argp, _id); - vsprintf(msg_buf, output_buf, argp); + arch_vsprintf(msg_buf, sizeof(msg_buf), output_buf, argp); va_end(argp); - *msg_out = strdup(msg_buf); + *msg_out = arch_strdup(msg_buf); } #ifdef ENABLE_SNMP if (is_snmp_enabled()) { if (is_snmp_trapid(id) == true) { /* Send a trap of Info (id and pos+1) */ - char *pos; + char* pos; va_start(argp, _id); - vsprintf(msg_buf, output_buf, argp); + arch_vsprintf(msg_buf, sizeof(msg_buf), output_buf, argp); va_end(argp); pos = strstr(msg_buf, " "); - send_ltfsInfoTrap(pos+1); + send_ltfsInfoTrap(pos + 1); } } #endif @@ -523,7 +535,8 @@ int ltfsmsg_internal(bool print_id, int level, char **msg_out, const char *_id, syslog(syslog_levels[LTFS_TRACE], MSG_PREFIX_TID MSG_FALLBACK, (unsigned long)ltfs_get_thread_id(), id); else syslog(syslog_levels[level], MSG_PREFIX_TID MSG_FALLBACK, (unsigned long)ltfs_get_thread_id(), id); - } else { + } + else { if (level <= LTFS_ERR) syslog(syslog_levels[LTFS_ERR], MSG_PREFIX MSG_FALLBACK, id); else if (level >= LTFS_TRACE) diff --git a/src/libltfs/ltfslogging.h b/src/libltfs/ltfslogging.h index b103e8d2..f98b7192 100644 --- a/src/libltfs/ltfslogging.h +++ b/src/libltfs/ltfslogging.h @@ -44,6 +44,8 @@ extern "C" { #include #include + + enum ltfs_log_levels { LTFS_NONE = -1, /* Don't print any log (special use for mkltfs/ltfsck) */ LTFS_ERR = 0, /* Fatal error or operation failed unexpectedly */ @@ -62,7 +64,7 @@ extern bool ltfs_print_thread_id; /* Wrapper for ltfsmsg_internal. It only invokes the message print function if the requested * log level is not too verbose. */ -#ifdef MSG_CHECK +#if 0 #include "ltfsmsg.h" #define ltfsmsg(level, id, ...) \ do { \ @@ -74,6 +76,12 @@ extern bool ltfs_print_thread_id; if (level <= ltfs_log_level) \ ltfsmsg_internal(true, level, NULL, #id, ##__VA_ARGS__); \ } while (0) + +#define ltfsmsgplain(level, id, ...) \ + do { \ + if (level <= ltfs_log_level) \ + ltfsmsg_internal(true, level, NULL, id, ##__VA_ARGS__); \ + } while (0) #endif /* CAUTION: ltfsmsg_buffer takes message ID as a text literal */ @@ -88,7 +96,7 @@ extern bool ltfs_print_thread_id; /* Wrapper for ltfsmsg_internal that prints a message without the LTFSnnnnn prefix. It * always invokes the message print function, regardless of the message level. */ -#ifdef MSG_CHECK +#if 0 #define ltfsresult(id, ...) \ do { \ printf(LTFS ## id, ##__VA_ARGS__); \ diff --git a/src/libltfs/ltfssnmp.c b/src/libltfs/ltfssnmp.c index bccd2f98..1422f64a 100644 --- a/src/libltfs/ltfssnmp.c +++ b/src/libltfs/ltfssnmp.c @@ -81,14 +81,15 @@ int read_trap_def_file(char *deffile) char *trapfile=DEFAULT_DEFFILE; char *strip_pos, *tok, *saveptr; struct trap_entry *entry; - FILE *fp; + FILE *fp = NULL; TAILQ_INIT(&trap_entries); if (deffile != NULL) trapfile = deffile; - fp = fopen(trapfile, TABLE_FILE_MODE); + + arch_fopen(trapfile, TABLE_FILE_MODE, fp); if (! fp) { ret = -errno; ltfsmsg(LTFS_ERR, 11268E, trapfile, ret); @@ -121,7 +122,7 @@ int read_trap_def_file(char *deffile) ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; } - entry->id = strdup(tok); + entry->id = arch_strdup(tok); TAILQ_INSERT_TAIL(&trap_entries, entry, list); } } diff --git a/src/libltfs/ltfssnmp.h b/src/libltfs/ltfssnmp.h index 690d9931..795d668f 100644 --- a/src/libltfs/ltfssnmp.h +++ b/src/libltfs/ltfssnmp.h @@ -61,7 +61,7 @@ #include "libltfs/queue.h" #include "libltfs/ltfs_error.h" #include "libltfs/ltfslogging.h" - +#include "libltfs/arch/ltfs_arch_ops.h" /* * function declarations */ diff --git a/src/libltfs/ltfstrace.c b/src/libltfs/ltfstrace.c index 86567bfb..e6fa3d5e 100644 --- a/src/libltfs/ltfstrace.c +++ b/src/libltfs/ltfstrace.c @@ -99,7 +99,7 @@ #define LTFS_TRACE_SIGNATURE "LTFS_TRC" #pragma pack(push, 1) struct trace_header { - char signature[8]; /**< Signature for LTFS trace */ + char signature[9]; /**< Signature for LTFS trace */ uint32_t header_size; /**< Size of trace header */ uint32_t req_header_offset; /**< Request trace header offset */ uint32_t fn_header_offset; /**< Function trace header offset */ @@ -454,7 +454,7 @@ int ltfs_request_profiler_start(const char *work_dir) return -LTFS_NO_MEMORY; } - req_trace->profiler = fopen(path, PROFILER_FILE_MODE); + arch_fopen(path, PROFILER_FILE_MODE, req_trace->profiler); free(path); @@ -486,7 +486,7 @@ int ltfs_header_init(void) ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; } - strncpy(trc_header->signature, LTFS_TRACE_SIGNATURE, strlen(LTFS_TRACE_SIGNATURE)); + arch_strncpy_auto(trc_header->signature, LTFS_TRACE_SIGNATURE, sizeof(trc_header->signature)); trc_header->header_size = sizeof(struct trace_header); trc_header->req_header_offset = sizeof(struct trace_header); trc_header->fn_header_offset = sizeof(struct trace_header) + sizeof(struct request_header) + REQ_TRACE_SIZE; @@ -512,7 +512,7 @@ int ltfs_header_init(void) return -LTFS_NO_MEMORY; } fn_trc_header->crc = 0xDEADBEEF; - return 0; + return 0; } int ltfs_trace_init(void) @@ -632,7 +632,7 @@ int ltfs_trace_dump(char *fname, const char *work_dir) } /* Open file */ - fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0666); + arch_open(&fd, path,O_WRONLY | O_CREAT | O_TRUNC, SHARE_FLAG_DENYRW, PERMISSION_READWRITE); if(fd < 0) return -errno; @@ -694,41 +694,41 @@ int ltfs_trace_dump(char *fname, const char *work_dir) trc_header->header_size + req_header->header_size + fn_trc_header->header_size; /* Write headers */ - (void)write(fd, trc_header, sizeof(struct trace_header)); - (void)write(fd, req_header, sizeof(struct request_header)); + (void)arch_write(fd, trc_header, sizeof(struct trace_header)); + (void)arch_write(fd, req_header, sizeof(struct request_header)); /* Write request trace data */ ltfs_mutex_lock(&req_trace->req_trace_lock); - (void)write(fd, req_trace->entries, REQ_TRACE_SIZE); + (void)arch_write(fd, req_trace->entries, REQ_TRACE_SIZE); ltfs_mutex_unlock(&req_trace->req_trace_lock); /* Write function trace header */ - (void)write(fd, &fn_trc_header->header_size, sizeof(uint32_t)); - (void)write(fd, &fn_trc_header->num_of_fn_trace, sizeof(uint32_t)); + (void)arch_write(fd, &fn_trc_header->header_size, sizeof(uint32_t)); + (void)arch_write(fd, &fn_trc_header->num_of_fn_trace, sizeof(uint32_t)); for (unsigned int i=0; ireq_t_desc[i], sizeof(struct function_trace_descriptor)); - (void)write(fd, &fn_trc_header->crc, sizeof(uint32_t)); + (void)arch_write(fd, &fn_trc_header->req_t_desc[i], sizeof(struct function_trace_descriptor)); + (void)arch_write(fd, &fn_trc_header->crc, sizeof(uint32_t)); free(fn_trc_header->req_t_desc); fn_trc_header->req_t_desc = NULL; /* Write function trace data */ for (fsitem=fs_tr_list; fsitem != NULL; fsitem=fsitem->hh.next) { acquireread_mrsw(&fsitem->fn_entry->trace_lock); - (void)write(fd, fsitem->fn_entry->entries, FS_FN_TRACE_SIZE); + (void)arch_write(fd, fsitem->fn_entry->entries, FS_FN_TRACE_SIZE); releaseread_mrsw(&fsitem->fn_entry->trace_lock); } for (admitem=admin_tr_list; admitem != NULL; admitem=admitem->hh.next) { acquireread_mrsw(&admitem->fn_entry->trace_lock); - (void)write(fd, admitem->fn_entry->entries, ADMIN_FN_TRACE_SIZE); + (void)arch_write(fd, admitem->fn_entry->entries, ADMIN_FN_TRACE_SIZE); releaseread_mrsw(&admitem->fn_entry->trace_lock); } TAILQ_FOREACH (tailq_item, acomp, list) { acquireread_mrsw(&tailq_item->fn_entry->trace_lock); - (void)write(fd, tailq_item->fn_entry->entries, ADMIN_FN_TRACE_SIZE); + (void)arch_write(fd, tailq_item->fn_entry->entries, ADMIN_FN_TRACE_SIZE); releaseread_mrsw(&tailq_item->fn_entry->trace_lock); } } - close(fd); + arch_close(fd); return 0; } @@ -743,7 +743,7 @@ int ltfs_get_trace_status(char **val) ltfsmsg(LTFS_ERR, 10001E, __FILE__); return -LTFS_NO_MEMORY; } - *val = strdup(trstat); + *val = arch_strdup(trstat); if (! (*val)) { ltfsmsg(LTFS_ERR, 10001E, __FILE__); return -LTFS_NO_MEMORY; diff --git a/src/libltfs/ltfstrace.h b/src/libltfs/ltfstrace.h index d336fa40..ccad606b 100644 --- a/src/libltfs/ltfstrace.h +++ b/src/libltfs/ltfstrace.h @@ -54,16 +54,18 @@ extern "C" { #ifdef mingw_PLATFORM #include "arch/win/win_util.h" +#include #define PROFILER_FILE_MODE "wb+" #else #define PROFILER_FILE_MODE "w+" #include +#include #endif #include #include #include -#include + #include #include @@ -140,7 +142,7 @@ void ltfs_admin_function_trace_completed(uint32_t); /* * Definitions for LTFS trace file */ -int ltfs_dump_trace(char* name); +int ltfs_dump_trace(char *name); /* * Definitions for LTFS profiler @@ -215,18 +217,21 @@ extern _time_stamp_t start_offset; static inline void ltfs_request_trace(uint32_t req_num, uint64_t info1, uint64_t info2) { +#ifdef mingw_PLATFORM + return; +#endif if (!trace_enable) return; - if (req_trace) { uint32_t index; ltfs_mutex_lock(&req_trace->req_trace_lock); - if(req_trace->cur_index >= req_trace->max_index) { + if (req_trace->cur_index >= req_trace->max_index) { index = req_trace->cur_index; req_trace->cur_index = 0; - } else + } + else index = req_trace->cur_index++; ltfs_mutex_unlock(&req_trace->req_trace_lock); @@ -243,6 +248,7 @@ static inline void ltfs_request_trace(uint32_t req_num, uint64_t info1, uint64_t ltfs_mutex_unlock(&req_trace->req_profiler_lock); } } + } static inline void ltfs_profiler_add_entry(FILE* file, ltfs_mutex_t *mutex, uint32_t req_num) diff --git a/src/libltfs/pathname.c b/src/libltfs/pathname.c index ca29c7b7..c4d39839 100644 --- a/src/libltfs/pathname.c +++ b/src/libltfs/pathname.c @@ -466,10 +466,10 @@ int _chars_valid_in_xml(UChar32 c) int _pathname_format_icu(const char *src, char **dest, bool validate, bool allow_slash) { int ret; - UChar *utf16_name, *utf16_name_norm; + UChar *utf16_name = NULL, *utf16_name_norm = NULL; /* convert to UTF-16 for normalization with ICU */ - ret = _pathname_system_to_utf16_icu(src, &utf16_name); + ret = _pathname_system_to_utf16_icu(src, &utf16_name); if (ret < 0) return ret; @@ -923,7 +923,7 @@ int _pathname_system_to_utf16_icu(const char *src, UChar **dest) return -LTFS_NO_MEMORY; } - ucnv_toUChars(syslocale, *dest, destlen + 1, src, -1, &err); + ucnv_toUChars(syslocale, *dest, destlen + 1, src, strlen(src), &err); if (U_FAILURE(err)) { ltfsmsg(LTFS_ERR, 11249E, err, src); ucnv_close(syslocale); @@ -952,7 +952,7 @@ int _pathname_utf8_to_system_icu(const char *src, char **dest) /* If current locale is UTF-8, no conversion needed */ syslocale = ucnv_getDefaultName(); if (! strcmp(syslocale, "UTF-8")) { - *dest = strdup(src); + *dest = arch_strdup(src); if (! *dest) return -LTFS_NO_MEMORY; return 0; diff --git a/src/libltfs/pathname.h b/src/libltfs/pathname.h index 3a26a8fd..2eee29c4 100644 --- a/src/libltfs/pathname.h +++ b/src/libltfs/pathname.h @@ -58,14 +58,21 @@ extern "C" { #endif -#include -#include -#ifdef __APPLE_MAKEFILE__ -#include +#ifdef mingw_PLATFORM + #include +#include #else -#include + #ifdef __APPLE_MAKEFILE__ + #include + #else + #include + #endif #endif +#include +#include + +#include int pathname_format(const char *name, char **new_name, bool validate, bool path); int pathname_unformat(const char *name, char **new_name); diff --git a/src/libltfs/periodic_sync.c b/src/libltfs/periodic_sync.c index 13571214..0fdf9e4e 100644 --- a/src/libltfs/periodic_sync.c +++ b/src/libltfs/periodic_sync.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -43,12 +43,27 @@ ** AUTHOR: Atsushi Abe ** IBM Yamato, Japan ** PISTE@jp.ibm.com +** ************************************************************************************* */ #include "ltfs.h" #include "ltfs_fsops.h" +#ifdef mingw_PLATFORM +#include +int gettimeofday(struct timeval* tv, struct timezone* tz) { + LARGE_INTEGER freq, count; + QueryPerformanceFrequency(&freq); + QueryPerformanceCounter(&count); + + tv->tv_sec = count.QuadPart / freq.QuadPart; + tv->tv_usec = (count.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart; + + return 0; // success +} +#endif + /** * Periodic sync scheduler private data structure. */ @@ -71,7 +86,7 @@ struct periodic_sync_data { #define REQ_SYNC fffe -ltfs_thread_return periodic_sync_thread(void* data) +ltfs_thread_return periodic_sync_thread(void *data) { struct periodic_sync_data *priv = (struct periodic_sync_data *) data; struct timeval now; diff --git a/src/libltfs/periodic_sync.h b/src/libltfs/periodic_sync.h index c9030d8e..7ce5fcb1 100644 --- a/src/libltfs/periodic_sync.h +++ b/src/libltfs/periodic_sync.h @@ -53,8 +53,8 @@ extern "C" { #endif int periodic_sync_thread_init(int sec, struct ltfs_volume *vol); -int periodic_sync_thread_destroy(void* syncer_handle); -bool periodic_sync_thread_initialized(void* syncer_handle); +int periodic_sync_thread_destroy(void *syncer_handle); +bool periodic_sync_thread_initialized(void *syncer_handle); #ifdef __cplusplus } diff --git a/src/libltfs/plugin.c b/src/libltfs/plugin.c index bcf9f6bd..c01c9160 100644 --- a/src/libltfs/plugin.c +++ b/src/libltfs/plugin.c @@ -82,7 +82,7 @@ int plugin_load(struct libltfs_plugin *pl, const char *type, const char *name, CHECK_ARG_NULL(name, -LTFS_NULL_ARG); CHECK_ARG_NULL(config, -LTFS_NULL_ARG); - pl->lib_handle = NULL; + memset(pl, 0, sizeof(*pl)); lib_path = config_file_get_lib(type, name, config); if (! lib_path) { @@ -92,7 +92,14 @@ int plugin_load(struct libltfs_plugin *pl, const char *type, const char *name, pl->lib_handle = dlopen(lib_path, RTLD_NOW); if (! pl->lib_handle) { +#ifdef _MSC_VER + char *err = dlerror(); + ltfsmsg(LTFS_ERR, 11261E,err); + free(err); +#else ltfsmsg(LTFS_ERR, 11261E, dlerror()); + +#endif // _MSC_VER return -LTFS_PLUGIN_LOAD; } @@ -115,7 +122,14 @@ int plugin_load(struct libltfs_plugin *pl, const char *type, const char *name, /* config_file_get_lib already verified that "type" contains one of the values above */ if (! get_ops) { +#ifdef _MSC_VER + char *err = dlerror(); + ltfsmsg(LTFS_ERR, 11263E, err); + free(err); +#else ltfsmsg(LTFS_ERR, 11263E, dlerror()); + +#endif // _MSC_VER dlclose(pl->lib_handle); pl->lib_handle = NULL; return -LTFS_PLUGIN_LOAD; @@ -137,7 +151,14 @@ int plugin_load(struct libltfs_plugin *pl, const char *type, const char *name, /* config_file_get_lib already verified that "type" contains one of the values above */ if (! get_messages) { - ltfsmsg(LTFS_ERR, 11284E, dlerror()); +#ifdef _MSC_VER + char *err = dlerror(); + ltfsmsg(LTFS_ERR, 11284E, err); + free(err); +#else + ltfsmsg(LTFS_ERR, 11263E, dlerror()); + +#endif // _MSC_VER dlclose(pl->lib_handle); pl->lib_handle = NULL; return -LTFS_PLUGIN_LOAD; @@ -173,7 +194,14 @@ int plugin_unload(struct libltfs_plugin *pl) #ifndef VALGRIND_FRIENDLY /* Valgrind cannot resolve function name after closing shared library */ if (dlclose(pl->lib_handle)) { +#ifdef _MSC_VER + char *err = dlerror(); + ltfsmsg(LTFS_ERR, 11262E, err); + free(err); +#else ltfsmsg(LTFS_ERR, 11262E, dlerror()); + +#endif // _MSC_VER return -LTFS_PLUGIN_UNLOAD; } #endif @@ -207,7 +235,7 @@ static void print_help_message(const char *progname, void *ops, const char * con ltfsmsg(LTFS_ERR, 11317E, type); } -void plugin_usage(const char* progname, const char *type, struct config_file *config) +void plugin_usage(const char *progname, const char *type, struct config_file *config) { struct libltfs_plugin pl = {0}; char **backends; diff --git a/src/libltfs/queue.h b/src/libltfs/queue.h index 8782b8c5..98d74889 100644 --- a/src/libltfs/queue.h +++ b/src/libltfs/queue.h @@ -33,7 +33,11 @@ #ifndef _SYS_QUEUE_H_ #define _SYS_QUEUE_H_ -#include +#ifndef mingw_PLATFORM +#include +#endif // !mingw_PLATFORM + + /* * This file defines four types of data structures: singly-linked lists, diff --git a/src/libltfs/tape.c b/src/libltfs/tape.c index 4de147a9..5e45125f 100644 --- a/src/libltfs/tape.c +++ b/src/libltfs/tape.c @@ -58,10 +58,10 @@ #ifdef mingw_PLATFORM #include "arch/win/win_util.h" #endif - +#include #include #include -#include + #ifdef __APPLE_MAKEFILE__ #include @@ -1760,7 +1760,7 @@ int tape_set_cart_coherency(struct device_data *dev, const tape_partition_t part /* APPLICATION CLIENT SPECIFIC INFORMATION LENGTH */ coh_data[30] = 0; /* Size of APPLICATION CLIENT SPECIFIC INFORMATION (Byte 1) */ coh_data[31] = 43; /* Size of APPLICATION CLIENT SPECIFIC INFORMATION (Byte 0) */ - strcpy((char *)coh_data + 32, "LTFS"); + arch_strcpy_auto((char *)coh_data + 32, "LTFS"); memcpy(coh_data + 37, coh->uuid, 37); /* Version field @@ -1894,12 +1894,12 @@ int tape_get_media_pool_info(struct ltfs_volume *vol, char **media_name, char ** if (add_start !=0) { name = strndup(vol->t_attr->media_pool, add_start); } - info = strdup(&(vol->t_attr->media_pool[add_start+1])); + info = arch_strdup(&(vol->t_attr->media_pool[add_start+1])); len = strlen(info); info[len-1] = '\0'; } else { - name = strdup(vol->t_attr->media_pool); + name = arch_strdup(vol->t_attr->media_pool); } if (name) @@ -2815,7 +2815,7 @@ int tape_takedump_drive(struct device_data *dev, bool nonforced_dump) #define CRYPTO_STATUS (24) #define MEDIUM_SUPPORT_CRYPTO (0x01) -char* tape_get_media_encrypted(struct device_data *dev) +char *tape_get_media_encrypted(struct device_data *dev) { unsigned char buf[TC_MP_READ_WRITE_CTRL_SIZE] = {0}; int ret = -EDEV_UNKNOWN; @@ -2979,16 +2979,16 @@ void set_tape_attribute(struct ltfs_volume *vol, struct tape_attr *t_attr) } /* APPLICATION VENDOR set */ - strncpy(t_attr->vender, LTFS_VENDOR_NAME, TC_MAM_APP_VENDER_SIZE); + arch_strncpy_auto(t_attr->vender, LTFS_VENDOR_NAME, TC_MAM_APP_VENDER_SIZE); parse_vol(t_attr->vender, strlen(LTFS_VENDOR_NAME), TC_MAM_APP_VENDER_SIZE); /* APPLICATION NAME set */ - strncpy(t_attr->app_name, PACKAGE_NAME, TC_MAM_APP_NAME_SIZE); + arch_strncpy_auto(t_attr->app_name, PACKAGE_NAME, TC_MAM_APP_NAME_SIZE); parse_vol(t_attr->app_name, strlen(PACKAGE_NAME), TC_MAM_APP_NAME_SIZE); /* APPLICATION VERSION set */ - strncpy(t_attr->app_ver, PACKAGE_VERSION, TC_MAM_APP_VERSION_SIZE); + arch_strncpy_auto(t_attr->app_ver, PACKAGE_VERSION, TC_MAM_APP_VERSION_SIZE); parse_vol(t_attr->app_ver, strlen(PACKAGE_VERSION), TC_MAM_APP_VERSION_SIZE); /* USER MEDIUM LABEL set */ @@ -3002,7 +3002,7 @@ void set_tape_attribute(struct ltfs_volume *vol, struct tape_attr *t_attr) if (len_volname == -LTFS_ICU_ERROR) len_volname = TC_MAM_USER_MEDIUM_LABEL_SIZE - 1; } - strncpy(t_attr->medium_label, vol->index->volume_name.name, len_volname); + arch_strncpy(t_attr->medium_label, vol->index->volume_name.name, sizeof(t_attr->medium_label), len_volname); } /* TEXT LOCALIZATION IDENTIFIER set */ @@ -3012,7 +3012,7 @@ void set_tape_attribute(struct ltfs_volume *vol, struct tape_attr *t_attr) if ( vol->label->barcode[0] ) { if ( strlen(vol->label->barcode) > TC_MAM_BARCODE_SIZE) ltfsmsg(LTFS_WARN, 17203W, "BARCODE", vol->label->barcode, TC_MAM_BARCODE_SIZE); - strncpy(t_attr->barcode, vol->label->barcode, TC_MAM_BARCODE_SIZE); + arch_strncpy_auto(t_attr->barcode, vol->label->barcode, TC_MAM_BARCODE_SIZE); parse_vol(t_attr->barcode, strlen(vol->label->barcode), TC_MAM_BARCODE_SIZE); } else { ltfsmsg(LTFS_WARN, 17230W); @@ -3020,7 +3020,7 @@ void set_tape_attribute(struct ltfs_volume *vol, struct tape_attr *t_attr) } /* APPLICATION FORMAT VERSION set */ - strncpy(t_attr->app_format_ver, LTFS_INDEX_VERSION_STR, TC_MAM_APP_FORMAT_VERSION_SIZE); + arch_strncpy_auto(t_attr->app_format_ver, LTFS_INDEX_VERSION_STR, TC_MAM_APP_FORMAT_VERSION_SIZE); parse_vol(t_attr->app_format_ver, strlen(LTFS_INDEX_VERSION_STR), TC_MAM_APP_FORMAT_VERSION_SIZE); /* VOLUME LOCKED set */ @@ -3081,41 +3081,42 @@ int tape_set_attribute_to_cm(struct device_data *dev, struct tape_attr *t_attr, return -1; } - unsigned char attr_data[attr_size + TC_MAM_PAGE_HEADER_SIZE]; - + unsigned char *attr_data = NULL; + attr_data = (unsigned char*)malloc(sizeof(unsigned char*)*(attr_size +TC_MAM_PAGE_HEADER_SIZE)); + if (attr_data == NULL) return -LTFS_NO_MEMORY; ltfs_u16tobe(attr_data, type); /* set attribute type */ attr_data[2] = format; /* set data format type */ ltfs_u16tobe(attr_data + 3, attr_size); /* set data size */ /* set attribute data */ if ( type == TC_MAM_APP_VENDER ) { - strncpy((char *)attr_data + 5, t_attr->vender, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->vender, attr_size+ TC_MAM_PAGE_HEADER_SIZE , attr_size); } else if ( type == TC_MAM_APP_NAME ) { - strncpy((char *)attr_data + 5, t_attr->app_name, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->app_name, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size); } else if ( type == TC_MAM_APP_VERSION ) { - strncpy((char *)attr_data + 5, t_attr->app_ver, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->app_ver, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size); } else if ( type == TC_MAM_USER_MEDIUM_LABEL ) { - strncpy((char *)attr_data + 5, t_attr->medium_label, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->medium_label, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size); } else if ( type == TC_MAM_TEXT_LOCALIZATION_IDENTIFIER ) { attr_data[5] = t_attr->tli; } else if ( type == TC_MAM_BARCODE ) { - strncpy((char *)attr_data + 5, t_attr->barcode, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->barcode, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size); } else if ( type == TC_MAM_APP_FORMAT_VERSION ) { - strncpy((char *)attr_data + 5, t_attr->app_format_ver, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->app_format_ver, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size); } else if ( type == TC_MAM_LOCKED_MAM ) { attr_data[5] = t_attr->vollock; } else if ( type == TC_MAM_MEDIA_POOL) { - strncpy((char *)attr_data + 5, t_attr->media_pool, attr_size); + arch_strncpy((char *)attr_data + 5, t_attr->media_pool, attr_size + TC_MAM_PAGE_HEADER_SIZE, attr_size); } ret = dev->backend->write_attribute(dev->backend_data, 0, /* partition */ attr_data, - sizeof(attr_data)); + (attr_size + TC_MAM_PAGE_HEADER_SIZE)); if (ret < 0) ltfsmsg(LTFS_ERR, 17205E, type, "tape_set_attribute_to_cm"); - + free(attr_data); return ret; } @@ -3233,8 +3234,9 @@ int tape_get_attribute_from_cm(struct device_data *dev, struct tape_attr *t_attr break; } - unsigned char attr_data[attr_len + TC_MAM_PAGE_HEADER_SIZE]; - + unsigned char *attr_data = NULL; + attr_data = malloc(sizeof(char*) * (attr_len + TC_MAM_PAGE_HEADER_SIZE)); + if (attr_data == NULL) return -LTFS_NO_MEMORY; ret = dev->backend->read_attribute(dev->backend_data, 0, /* partition */ type, @@ -3282,7 +3284,7 @@ int tape_get_attribute_from_cm(struct device_data *dev, struct tape_attr *t_attr } } else ltfsmsg(LTFS_DEBUG, 17198D, type, "tape_get_attribute_from_cm"); - + free(attr_data); return ret; } @@ -3398,7 +3400,7 @@ int update_tape_attribute(struct ltfs_volume *vol, const char *new_value, int ty if (size == -LTFS_ICU_ERROR) size = TC_MAM_USER_MEDIUM_LABEL_SIZE - 1; } - pre_attr = strdup(vol->t_attr->medium_label); + pre_attr = arch_strdup(vol->t_attr->medium_label); if (! pre_attr) { ltfsmsg(LTFS_ERR, 10001E, "update_tape_attribute: pre_attr"); ret = -ENOMEM; @@ -3406,14 +3408,14 @@ int update_tape_attribute(struct ltfs_volume *vol, const char *new_value, int ty } memset(vol->t_attr->medium_label, '\0', TC_MAM_USER_MEDIUM_LABEL_SIZE + 1); if ( new_value ) { - strncpy(vol->t_attr->medium_label, new_value, size); + arch_strncpy_auto(vol->t_attr->medium_label, new_value, size); } } else if (type == TC_MAM_BARCODE) { if ( size > TC_MAM_BARCODE_SIZE) { ltfsmsg(LTFS_WARN, 17226W, "BARCODE", TC_MAM_BARCODE_SIZE); return -LTFS_LARGE_XATTR; } - pre_attr = strdup(vol->t_attr->barcode); + pre_attr = arch_strdup(vol->t_attr->barcode); if (! pre_attr) { ltfsmsg(LTFS_ERR, 10001E, "update_tape_attribute: pre_attr"); ret = -ENOMEM; @@ -3421,7 +3423,7 @@ int update_tape_attribute(struct ltfs_volume *vol, const char *new_value, int ty } memset(vol->t_attr->barcode, '\0', TC_MAM_BARCODE_SIZE + 1); if ( new_value ) { - strncpy(vol->t_attr->barcode, new_value, size); + arch_strncpy_auto(vol->t_attr->barcode, new_value, size); } parse_vol(vol->t_attr->barcode, strlen(new_value), TC_MAM_BARCODE_SIZE); } else if (type == TC_MAM_LOCKED_MAM) { @@ -3440,7 +3442,7 @@ int update_tape_attribute(struct ltfs_volume *vol, const char *new_value, int ty } memset(vol->t_attr->media_pool, '\0', TC_MAM_MEDIA_POOL_SIZE + 1); if ( new_value ) { - strncpy(vol->t_attr->media_pool, new_value, size); + arch_strncpy_auto(vol->t_attr->media_pool, new_value, size); } } @@ -3448,10 +3450,10 @@ int update_tape_attribute(struct ltfs_volume *vol, const char *new_value, int ty if (ret < 0) { if ( type == TC_MAM_USER_MEDIUM_LABEL ) { memset(vol->t_attr->medium_label, '\0', TC_MAM_USER_MEDIUM_LABEL_SIZE + 1); - strncpy(vol->t_attr->medium_label, pre_attr, strlen(pre_attr)); + arch_strncpy_auto(vol->t_attr->medium_label, pre_attr, strlen(pre_attr)); } else if (type == TC_MAM_BARCODE) { memset(vol->t_attr->barcode, '\0', TC_MAM_BARCODE_SIZE + 1); - strncpy(vol->t_attr->barcode, pre_attr, strlen(pre_attr)); + arch_strncpy_auto(vol->t_attr->barcode, pre_attr, strlen(pre_attr)); } } @@ -3489,23 +3491,23 @@ int read_tape_attribute(struct ltfs_volume *vol, char **val, const char *name) if (! strcmp(name, "ltfs.mamBarcode")) { if (vol->t_attr->barcode[0] == '\0') return 0; - *val = strdup(vol->t_attr->barcode); + *val = arch_strdup(vol->t_attr->barcode); } else if (! strcmp(name, "ltfs.mamApplicationVendor")) { if (vol->t_attr->barcode[0] == '\0') return 0; - *val = strdup(vol->t_attr->vender); + *val = arch_strdup(vol->t_attr->vender); } else if (! strcmp(name, "ltfs.mamApplicationVersion")) { if (vol->t_attr->barcode[0] == '\0') return 0; - *val = strdup(vol->t_attr->app_ver); + *val = arch_strdup(vol->t_attr->app_ver); } else if (! strcmp(name, "ltfs.mamApplicationFormatVersion")) { if (vol->t_attr->barcode[0] == '\0') return 0; - *val = strdup(vol->t_attr->app_format_ver); + *val = arch_strdup(vol->t_attr->app_format_ver); } else if (!strncmp(name, "ltfs.mediaPool", sizeof("ltfs.mediaPool"))) { if (vol->t_attr->media_pool[0] == '\0') return 0; - *val = strdup(vol->t_attr->media_pool); + *val = arch_strdup(vol->t_attr->media_pool); } if (!*val) { diff --git a/src/libltfs/tape.h b/src/libltfs/tape.h index ca340130..d0e07f2f 100644 --- a/src/libltfs/tape.h +++ b/src/libltfs/tape.h @@ -221,9 +221,9 @@ int tape_set_key(struct device_data *dev, const unsigned char *keyalias, const u int tape_clear_key(struct device_data *device, void * const kmi_handle); int tape_get_keyalias(struct device_data *dev, unsigned char **keyalias); int tape_takedump_drive(struct device_data *dev, bool nonforced_dump); -char* tape_get_media_encrypted(struct device_data *dev); -char* tape_get_drive_encryption_state(struct device_data *dev); -char* tape_get_drive_encryption_method(struct device_data *dev); +char *tape_get_media_encrypted(struct device_data *dev); +char *tape_get_drive_encryption_state(struct device_data *dev); +char *tape_get_drive_encryption_method(struct device_data *dev); int tape_get_worm_status(struct device_data *dev, bool *is_worm); void set_tape_attribute(struct ltfs_volume *vol, struct tape_attr *t_attr); @@ -241,7 +241,7 @@ int tape_is_reformattable(struct device_data *dev, unsigned char cart_type, unsi int tape_set_profiler(struct device_data *dev, char *work_dir, bool enable); int tape_rao_request(struct device_data *dev, struct rao_mod *rao); -static inline char* tape_get_serialnumber(struct device_data *dev) +static inline char *tape_get_serialnumber(struct device_data *dev) { return dev->serial_number; } diff --git a/src/libltfs/tape_ops.h b/src/libltfs/tape_ops.h index bb08e6be..5a2518ce 100644 --- a/src/libltfs/tape_ops.h +++ b/src/libltfs/tape_ops.h @@ -62,12 +62,12 @@ #include #include -#include "ltfs_types.h" +#include #define VENDOR_ID_LENGTH (8) #define PRODUCT_ID_LENGTH (16) #define PRODUCT_REV_LENGTH (4) -#define PRODUCT_NAME_LENGTH (PRODUCT_ID_LENGTH + 3) /* " [PRODUCT_ID]" */ +#define PRODUCT_NAME_LENGTH (PRODUCT_ID_LENGTH + 5) /* " [PRODUCT_ID]" */ #define PRODUCT_NAME_REPORT_LENGTH (15) #define UNIT_SERIAL_LENGTH (255) diff --git a/src/libltfs/xattr.c b/src/libltfs/xattr.c index 055d50a9..6dd4a583 100644 --- a/src/libltfs/xattr.c +++ b/src/libltfs/xattr.c @@ -172,7 +172,7 @@ static int _xattr_get_version(int version, char **outval, const char *msg) { int ret; if (version == 10000) { - *outval = strdup("1.0"); + *outval = arch_strdup("1.0"); if (! (*outval)) { ltfsmsg(LTFS_ERR, 10001E, msg); return -LTFS_NO_MEMORY; @@ -1276,7 +1276,7 @@ int xattr_get_string(const char *val, char **outval, const char *msg) { if (! val) return 0; - *outval = strdup(val); + *outval = arch_strdup(val); if (! (*outval)) { ltfsmsg(LTFS_ERR, 10001E, msg); return -LTFS_NO_MEMORY; @@ -1312,7 +1312,7 @@ int xattr_do_set(struct dentry *d, const char *name, const char *value, size_t s ltfsmsg(LTFS_ERR, 10001E, "xattr_do_set: xattr"); return -LTFS_NO_MEMORY; } - xattr->key.name = strdup(name); + xattr->key.name = arch_strdup(name); if (! xattr->key.name) { ltfsmsg(LTFS_ERR, 10001E, "xattr_do_set: xattr key"); ret = -LTFS_NO_MEMORY; @@ -1725,10 +1725,9 @@ int xattr_remove(struct dentry *d, const char *name, struct ltfs_volume *vol) d->is_appendonly = false; ltfsmsg(LTFS_INFO, 17238I, "appendonly", d->is_appendonly, d->name.name); } - d->dirty = true; ltfs_set_index_dirty(true, false, vol->index); - + out_dunlk: _xattr_unlock_dentry(name, true, d, vol); return ret; @@ -1761,7 +1760,7 @@ const char *xattr_strip_name(const char *name) * @param vol LTFS volume * @return 0 on success or a negative value on error */ -int xattr_set_mountpoint_length(struct dentry *d, const char* value, size_t size ) +int xattr_set_mountpoint_length(struct dentry *d, const char *value, size_t size ) { #ifdef POSIXLINK_ONLY return 0; diff --git a/src/libltfs/xattr.h b/src/libltfs/xattr.h index 63cd1070..6e1ae0fd 100644 --- a/src/libltfs/xattr.h +++ b/src/libltfs/xattr.h @@ -66,6 +66,7 @@ extern "C" { #include "libltfs/arch/freebsd/xattr.h" #endif +#include "fuse.h" #include "ltfs.h" #define LTFS_PRIVATE_PREFIX "ltfs." @@ -87,7 +88,7 @@ int xattr_do_set(struct dentry *d, const char *name, const char *value, size_t s struct xattr_info *xattr); int xattr_do_remove(struct dentry *d, const char *name, bool force, struct ltfs_volume *vol); const char *xattr_strip_name(const char *name); -int xattr_set_mountpoint_length(struct dentry *d, const char* value, size_t size); +int xattr_set_mountpoint_length(struct dentry *d, const char *value, size_t size); #ifdef __cplusplus } diff --git a/src/libltfs/xml.h b/src/libltfs/xml.h index b81726f9..282d1d8d 100644 --- a/src/libltfs/xml.h +++ b/src/libltfs/xml.h @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -50,7 +50,7 @@ ** ** Atsushi Abe ** IBM Tokyo Lab., Japan -** piste@jp.ibm.com +** piste@jp.ibm.com ** ************************************************************************************* */ @@ -141,9 +141,11 @@ int xml_format_time(struct ltfs_timespec t, char** out); /* generate required/optional tag tracking arrays for the parser */ #define declare_tracking_arrays(num_req, num_opt) \ const int ntags_req = (num_req), ntags_opt = (num_opt); \ - bool have_required_tags[ntags_req], have_optional_tags[ntags_opt]; \ - if (ntags_req > 0) memset(have_required_tags, 0, sizeof(have_required_tags)); \ - if (ntags_opt > 0) memset(have_optional_tags, 0, sizeof(have_optional_tags)); + bool *have_required_tags = ntags_req > 0 ? (bool*)calloc(ntags_req, sizeof(bool)) : NULL; \ + bool *have_optional_tags = ntags_opt > 0 ? (bool*)calloc(ntags_opt, sizeof(bool)) : NULL; \ + if(! have_optional_tags) (void)(have_optional_tags); \ + if(! have_required_tags) (void)(have_required_tags); + /* grab the next tag inside the given tag. It breaks if the end of the given tag is detected. * NOTE: in order for break to work correctly, this macro is not wrapped in a do { ... } while (0) @@ -310,4 +312,8 @@ int xml_parse_time(bool msg, const char *fmt_time, struct ltfs_timespec *rawtime void xml_init(); void xml_finish(); +#ifdef _WIN32 +int ftruncate(int fd, off_t length); +#endif + #endif /* __xml_h */ diff --git a/src/libltfs/xml_libltfs.h b/src/libltfs/xml_libltfs.h index b50a3800..ba0217c5 100644 --- a/src/libltfs/xml_libltfs.h +++ b/src/libltfs/xml_libltfs.h @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -36,7 +36,7 @@ ** ** COMPONENT NAME: IBM Linear Tape File System ** -** FILE NAME: xml.h +** FILE NAME: xml_libltfs.h ** ** DESCRIPTION: Prototypes for XML read/write functions. ** @@ -85,7 +85,7 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol); int xml_label_from_file(const char *filename, struct ltfs_label *label); int xml_label_from_mem(const char *buf, int buf_size, struct ltfs_label *label); int xml_schema_from_file(const char *filename, struct ltfs_index *idx, struct ltfs_volume *vol); -int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol); +int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol); int xml_extent_symlink_info_from_file(const char *filename, struct dentry *d); #endif /* __xml_libltfs_h */ diff --git a/src/libltfs/xml_reader.c b/src/libltfs/xml_reader.c index 6087e592..664e818b 100644 --- a/src/libltfs/xml_reader.c +++ b/src/libltfs/xml_reader.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -275,7 +275,7 @@ int xml_parse_uuid(char *out_val, const char *val) ltfsmsg(LTFS_ERR, 17029E, val); return -1; } - strcpy(out_val, val); + arch_strcpy(out_val,(strlen(val) + 1), val); for (i=0; i<36; ++i) { if (i == 8 || i == 13 || i == 18 || i == 23) { @@ -473,7 +473,7 @@ int xml_parse_time(bool msg, const char *fmt_time, struct ltfs_timespec *rawtime CHECK_ARG_NULL(fmt_time, -LTFS_NULL_ARG); CHECK_ARG_NULL(rawtime, -LTFS_NULL_ARG); - ret = sscanf(fmt_time, "%d-%2d-%2dT%2d:%2d:%2d.%9ldZ", + ret = arch_sscanf(fmt_time, "%d-%2d-%2dT%2d:%2d:%2d.%9ldZ", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec, &rawtime->tv_nsec); @@ -504,7 +504,7 @@ int xml_parse_time(bool msg, const char *fmt_time, struct ltfs_timespec *rawtime */ int xml_input_tape_read_callback(void *context, char *buffer, int len) { - struct xml_input_tape *ctx = context; + struct xml_input_tape* ctx = context; ssize_t nread, nr2; char *buf2; int bytes_saved, bytes_remaining, ret_sp; @@ -513,20 +513,22 @@ int xml_input_tape_read_callback(void *context, char *buffer, int len) return 0; /* Try to fill the whole buffer from cache. If that fails, try to read from tape. */ - if (len <= (int32_t) ctx->buf_used) { + if (len <= (int32_t)ctx->buf_used) { memcpy(buffer, ctx->buf + ctx->buf_start, len); ctx->buf_used -= len; if (ctx->buf_used > 0) ctx->buf_start += len; else ctx->buf_start = 0; - } else { + } + else { if (ctx->buf_used > 0) { memcpy(buffer, ctx->buf + ctx->buf_start, ctx->buf_used); bytes_saved = ctx->buf_used; ctx->buf_used = 0; ctx->buf_start = 0; - } else + } + else bytes_saved = 0; bytes_remaining = len - bytes_saved; @@ -548,7 +550,8 @@ int xml_input_tape_read_callback(void *context, char *buffer, int len) ltfsmsg(LTFS_ERR, 17039E, (int)nread); ctx->err_code = nread; return -1; - } else if ((size_t) nread < ctx->buf_size) { + } + else if ((size_t)nread < ctx->buf_size) { /* Caught a small read. If this is a file mark, position before it. If * it's a record, look for a file mark following it. */ ctx->saw_small_block = true; @@ -560,7 +563,8 @@ int xml_input_tape_read_callback(void *context, char *buffer, int len) ctx->err_code = ret_sp; return -1; } - } else if (ctx->eod_pos == 0 || + } + else if (ctx->eod_pos == 0 || (ctx->eod_pos > 0 && ctx->current_pos < ctx->eod_pos)) { /* Look for a trailing file mark. */ buf2 = malloc(ctx->vol->label->blocksize); @@ -577,7 +581,8 @@ int xml_input_tape_read_callback(void *context, char *buffer, int len) ltfsmsg(LTFS_ERR, 17041E, (int)nr2); ctx->err_code = nr2; return -1; - } else if (nr2 == 0) { + } + else if (nr2 == 0) { ctx->saw_file_mark = true; ret_sp = tape_spacefm(ctx->vol->device, -1); if (ret_sp < 0) { @@ -594,7 +599,8 @@ int xml_input_tape_read_callback(void *context, char *buffer, int len) memcpy(buffer + bytes_saved, ctx->buf, nread); bytes_saved += nread; bytes_remaining -= nread; - } else { + } + else { memcpy(buffer + bytes_saved, ctx->buf, bytes_remaining); ctx->buf_start = bytes_remaining; ctx->buf_used = nread - bytes_remaining; @@ -607,6 +613,7 @@ int xml_input_tape_read_callback(void *context, char *buffer, int len) return len; } + /** Close callback for XML parser input using the libxml2 I/O routines. */ int xml_input_tape_close_callback(void *context) diff --git a/src/libltfs/xml_reader_libltfs.c b/src/libltfs/xml_reader_libltfs.c index 34dca1a0..9118050e 100644 --- a/src/libltfs/xml_reader_libltfs.c +++ b/src/libltfs/xml_reader_libltfs.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -153,7 +153,7 @@ static int decode_entry_name(char **new_name, const char *name) } tmp_name[j] = '\0'; - *new_name = strdup(tmp_name); + *new_name = arch_strdup(tmp_name); free(tmp_name); return 0; @@ -180,7 +180,7 @@ static int _xml_parse_nametype(xmlTextReaderPtr reader, struct ltfs_name *n, boo get_tag_text(); - encoded_name = strdup(value); + encoded_name = arch_strdup(value); if (!encoded_name) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; @@ -234,7 +234,7 @@ static int _xml_parse_nametype_allow_zero_length(xmlTextReaderPtr reader, struct return 0; } - encoded_name = strdup(value); + encoded_name = arch_strdup(value); if (!encoded_name) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; @@ -359,11 +359,11 @@ static int _xml_parser_init(xmlTextReaderPtr reader, const char *top_name, int * } if (ver < min_version || ver > max_version) { ltfsmsg(LTFS_ERR, 17021E, top_name, value); - free(value); + arch_xmlfree(value); return -LTFS_UNSUPPORTED_INDEX_VERSION; } *idx_version = ver; - free(value); + arch_xmlfree(value); return 0; } @@ -379,7 +379,6 @@ static int _xml_parse_label_location(xmlTextReaderPtr reader, struct ltfs_label { declare_parser_vars("location"); declare_tracking_arrays(1, 0); - while (true) { get_next_tag(); @@ -460,7 +459,7 @@ static int _xml_parse_label(xmlTextReaderPtr reader, struct ltfs_label *label) get_tag_text(); if (label->creator) free(label->creator); - label->creator = strdup(value); + label->creator = arch_strdup(value); if (! label->creator) { ltfsmsg(LTFS_ERR, 10001E, name); return -LTFS_NO_MEMORY; @@ -819,7 +818,7 @@ static int _xml_parse_one_xattr(xmlTextReaderPtr reader, struct dentry *d) } if (! xattr_type || ! strcmp(xattr_type, "text")) { - xattr->value = strdup(value); + xattr->value = arch_strdup(value); if (! xattr->value) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); free(xattr->key.name); @@ -1493,7 +1492,7 @@ static int _xml_parse_dirtree(xmlTextReaderPtr reader, struct dentry *parent, * @param vol LTFS volume to which the index belongs. May be NULL. * @return 0 on success or a negative value on error. */ -static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, struct ltfs_volume *vol) +static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index* idx, struct ltfs_volume* vol) { unsigned long long value_int; declare_parser_vars("ltfsindex"); @@ -1501,28 +1500,28 @@ static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, st /* start the parser: find top-level "index" tag, check version and encoding */ ret = _xml_parser_init(reader, parent_tag, &idx->version, - LTFS_INDEX_VERSION_MIN, LTFS_INDEX_VERSION_MAX); + LTFS_INDEX_VERSION_MIN, LTFS_INDEX_VERSION_MAX); if (ret < 0) return ret; if (idx->version < LTFS_INDEX_VERSION) ltfsmsg(LTFS_WARN, 17095W, - LTFS_INDEX_VERSION_STR, - LTFS_FORMAT_MAJOR(idx->version), - LTFS_FORMAT_MINOR(idx->version), - LTFS_FORMAT_REVISION(idx->version)); + LTFS_INDEX_VERSION_STR, + LTFS_FORMAT_MAJOR(idx->version), + LTFS_FORMAT_MINOR(idx->version), + LTFS_FORMAT_REVISION(idx->version)); else if (idx->version / 100 > LTFS_INDEX_VERSION / 100) ltfsmsg(LTFS_WARN, 17096W, - LTFS_INDEX_VERSION_STR, - LTFS_FORMAT_MAJOR(idx->version), - LTFS_FORMAT_MINOR(idx->version), - LTFS_FORMAT_REVISION(idx->version)); + LTFS_INDEX_VERSION_STR, + LTFS_FORMAT_MAJOR(idx->version), + LTFS_FORMAT_MINOR(idx->version), + LTFS_FORMAT_REVISION(idx->version)); else if (idx->version > LTFS_INDEX_VERSION) ltfsmsg(LTFS_WARN, 17234W, - LTFS_INDEX_VERSION_STR, - LTFS_FORMAT_MAJOR(idx->version), - LTFS_FORMAT_MINOR(idx->version), - LTFS_FORMAT_REVISION(idx->version)); + LTFS_INDEX_VERSION_STR, + LTFS_FORMAT_MAJOR(idx->version), + LTFS_FORMAT_MINOR(idx->version), + LTFS_FORMAT_REVISION(idx->version)); if (idx->commit_message) { free(idx->commit_message); @@ -1533,26 +1532,28 @@ static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, st while (true) { get_next_tag(); - if (! strcmp(name, "creator")) { + if (!strcmp(name, "creator")) { check_required_tag(0); get_tag_text(); if (idx->creator) free(idx->creator); - idx->creator = strdup(value); - if (! idx->creator) { + idx->creator = arch_strdup(value); + if (!idx->creator) { ltfsmsg(LTFS_ERR, 10001E, name); return -LTFS_NO_MEMORY; } check_tag_end("creator"); - } else if (! strcmp(name, "volumeuuid")) { + } + else if (!strcmp(name, "volumeuuid")) { check_required_tag(1); get_tag_text(); if (xml_parse_uuid(idx->vol_uuid, value) < 0) return -LTFS_XML_WRONG_UUID; check_tag_end("volumeuuid"); - } else if (! strcmp(name, "generationnumber")) { + } + else if (!strcmp(name, "generationnumber")) { check_required_tag(2); get_tag_text(); if (xml_parse_ull(&value_int, value) < 0) { @@ -1562,7 +1563,8 @@ static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, st idx->generation = value_int; check_tag_end("generationnumber"); - } else if (! strcmp(name, "updatetime")) { + } + else if (!strcmp(name, "updatetime")) { check_required_tag(3); get_tag_text(); ret = xml_parse_time(true, value, &idx->mod_time); @@ -1573,67 +1575,77 @@ static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, st check_tag_end("updatetime"); - } else if (! strcmp(name, "location")) { + } + else if (!strcmp(name, "location")) { check_required_tag(4); assert_not_empty(); if (_xml_parse_tapepos(reader, "location", &idx->selfptr) < 0) return -LTFS_XML_WRONG_LOC; - } else if (! strcmp(name, "allowpolicyupdate")) { + } + else if (!strcmp(name, "allowpolicyupdate")) { check_required_tag(5); get_tag_text(); if (xml_parse_bool(&idx->criteria_allow_update, value) < 0) return -LTFS_XML_WRONG_PA; check_tag_end("allowpolicyupdate"); - } else if (! strcmp(name, "directory")) { + } + else if (!strcmp(name, "directory")) { check_required_tag(6); assert_not_empty(); ret = _xml_parse_dirtree(reader, NULL, idx, vol, NULL); if (ret < 0) return ret; - } else if (! strcmp(name, "previousgenerationlocation")) { + } + else if (!strcmp(name, "previousgenerationlocation")) { check_optional_tag(0); assert_not_empty(); if (_xml_parse_tapepos(reader, "previousgenerationlocation", &idx->backptr) < 0) return -LTFS_XML_WRONG_LOC_PREV; - } else if (! strcmp(name, "dataplacementpolicy")) { + } + else if (!strcmp(name, "dataplacementpolicy")) { check_optional_tag(1); assert_not_empty(); ret = _xml_parse_policy(reader, idx); if (ret < 0) return ret; - } else if (! strcmp(name, "comment")) { + } + else if (!strcmp(name, "comment")) { check_optional_tag(2); get_tag_text(); if (strlen(value) > INDEX_MAX_COMMENT_LEN) { ltfsmsg(LTFS_ERR, 17094E); return -LTFS_XML_TOO_LONG_COMMENT; } - idx->commit_message = strdup(value); - if (! idx->commit_message) { + idx->commit_message = arch_strdup(value); + if (!idx->commit_message) { ltfsmsg(LTFS_ERR, 10001E, "_xml_parse_schema: index comment"); return -LTFS_NO_MEMORY; } check_tag_end("comment"); - } else if (! strcmp(name, "volumelockstate")) { + } + else if (!strcmp(name, "volumelockstate")) { check_optional_tag(3); get_tag_text(); if (!strcmp(value, "unlocked")) { idx->vollock = UNLOCKED_MAM; - } else if (!strcmp(value, "locked")) { + } + else if (!strcmp(value, "locked")) { idx->vollock = LOCKED_MAM; - } else if (!strcmp(value, "permlocked")) { + } + else if (!strcmp(value, "permlocked")) { idx->vollock = PERMLOCKED_MAM; } check_tag_end("volumelockstate"); - } else if (idx->version >= IDX_VERSION_UID && ! strcmp(name, NEXTUID_TAGNAME)) { + } + else if (idx->version >= IDX_VERSION_UID && !strcmp(name, NEXTUID_TAGNAME)) { check_required_tag(7); get_tag_text(); if (xml_parse_ull(&value_int, value) < 0) @@ -1642,10 +1654,12 @@ static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, st idx->uid_number = value_int; check_tag_end(NEXTUID_TAGNAME); - } else if (! strcmp(name, NEXTUID_TAGNAME)) { + } + else if (!strcmp(name, NEXTUID_TAGNAME)) { ignore_unrecognized_tag(); - } else + } + else preserve_unrecognized_tag(idx); } @@ -1657,7 +1671,7 @@ static int _xml_parse_schema(xmlTextReaderPtr reader, struct ltfs_index *idx, st check_required_tags(); - if ( idx->symerr_count != 0 ) { + if (idx->symerr_count != 0) { return -LTFS_SYMLINK_CONFLICT; } @@ -1829,7 +1843,7 @@ int xml_label_from_mem(const char *buf, int buf_size, struct ltfs_label *label) * with the nodes found during the scanning. * @param filename XML input file. * @param idx LTFS index. - * @param vol LTFS volume to which the index belongs. May be NULL. + * @param vol LTFS volume to which the index belongs. Can be NULL. * @return 0 on success or a negative value on error. */ int xml_schema_from_file(const char *filename, struct ltfs_index *idx, struct ltfs_volume *vol) @@ -1844,7 +1858,7 @@ int xml_schema_from_file(const char *filename, struct ltfs_index *idx, struct lt reader = xmlReaderForFile(filename, NULL, XML_PARSE_NOERROR | XML_PARSE_NOWARNING | XML_PARSE_HUGE); if (! reader) { ltfsmsg(LTFS_ERR, 17011E, filename); - return -1; + return -LTFS_FILE_ERR; } /* Workaround for old libxml2 version on OS X 10.5: the method used to preserve @@ -1878,11 +1892,11 @@ int xml_schema_from_file(const char *filename, struct ltfs_index *idx, struct lt * @return 0 on success, 1 if parsing succeeded but no file mark was encountered, * or a negative value on error. */ -int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) +int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume* vol) { int ret; struct tc_position current_pos; - struct xml_input_tape *ctx; + struct xml_input_tape* ctx; xmlParserInputBufferPtr read_buf; xmlTextReaderPtr reader; xmlDocPtr doc; @@ -1897,12 +1911,12 @@ int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) /* Create output callback context data structure. */ ctx = malloc(sizeof(struct xml_input_tape)); - if (! ctx) { + if (!ctx) { ltfsmsg(LTFS_ERR, 10001E, "xml_schema_from_tape: ctx"); return -LTFS_NO_MEMORY; } ctx->buf = malloc(vol->label->blocksize + LTFS_CRC_SIZE); - if (! ctx->buf) { + if (!ctx->buf) { ltfsmsg(LTFS_ERR, 10001E, "xml_schema_from_tape: ctx->buf"); free(ctx); return -LTFS_NO_MEMORY; @@ -1919,9 +1933,9 @@ int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) /* Create input buffer pointer. */ read_buf = xmlParserInputBufferCreateIO(xml_input_tape_read_callback, - xml_input_tape_close_callback, - ctx, XML_CHAR_ENCODING_NONE); - if (! read_buf) { + xml_input_tape_close_callback, + ctx, XML_CHAR_ENCODING_NONE); + if (!read_buf) { ltfsmsg(LTFS_ERR, 17014E); free(ctx->buf); free(ctx); @@ -1930,7 +1944,7 @@ int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) /* Create XML reader. */ reader = xmlNewTextReader(read_buf, NULL); - if (! reader) { + if (!reader) { ltfsmsg(LTFS_ERR, 17015E); xmlFreeParserInputBuffer(read_buf); free(ctx->buf); @@ -1969,8 +1983,9 @@ int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) ltfsmsg(LTFS_WARN, 17274W, ret); ret = -LTFS_INDEX_INVALID; } - } else if (ret == 0) { - if(!ctx->saw_file_mark) { + } + else if (ret == 0) { + if (!ctx->saw_file_mark) { /* Return positive value intentionally, for recovering later */ ret = LTFS_NO_TRAIL_FM; } @@ -1992,7 +2007,6 @@ int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) return ret; } - /** * Parse an XML file for dcache and reconstruct dentry * @param filename file name for dentry. diff --git a/src/libltfs/xml_writer.c b/src/libltfs/xml_writer.c index a0554dab..ef46846e 100644 --- a/src/libltfs/xml_writer.c +++ b/src/libltfs/xml_writer.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -54,6 +54,7 @@ #include #include +#include "libltfs/arch/ltfs_arch_ops.h" #include "ltfs.h" #include "xml.h" #include "fs.h" @@ -61,11 +62,6 @@ #include "pathname.h" #include "arch/time_internal.h" -/* O_BINARY is defined only in MinGW */ -#ifndef O_BINARY -#define O_BINARY 0 -#endif - /** * Format a raw timespec structure for the XML file. */ @@ -91,7 +87,7 @@ int xml_format_time(struct ltfs_timespec t, char** out) ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -1; } - sprintf(timebuf, "%04d-%02d-%02dT%02d:%02d:%02d.%09ldZ", tm.tm_year+1900, tm.tm_mon+1, + arch_sprintf(timebuf, (31*sizeof(char)), "%04d-%02d-%02dT%02d:%02d:%02d.%09ldZ", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, t.tv_nsec); *out = timebuf; @@ -131,7 +127,7 @@ int xml_output_tape_write_callback(void *context, const char *buffer, int len) } if (ctx->fd > 0) { - ret = write(ctx->fd, ctx->buf, ctx->buf_size); + ret = arch_write(ctx->fd, ctx->buf, ctx->buf_size); if (ret < 0) { ltfsmsg(LTFS_ERR, 17244E, (int)errno); ctx->errno_fd = -LTFS_CACHE_IO; @@ -167,7 +163,7 @@ int xml_output_tape_close_callback(void *context) ret = -1; } else { if (ctx->fd >= 0) - ret_d = write(ctx->fd, ctx->buf, ctx->buf_used); + ret_d = arch_write(ctx->fd, ctx->buf, ctx->buf_used); if (ret_d < 0) { ltfsmsg(LTFS_ERR, 17245E, (int)errno); ctx->errno_fd = -LTFS_CACHE_IO; @@ -197,7 +193,7 @@ int xml_output_fd_write_callback(void *context, const char *buffer, int len) struct xml_output_fd *ctx = context; if (len > 0) { - ret = write(ctx->fd, buffer, len); + ret = arch_write(ctx->fd, buffer, len); if (ret < 0) { ltfsmsg(LTFS_ERR, 17206E, "write callback (write)", errno, (unsigned long)len); return -1; @@ -228,6 +224,33 @@ int xml_output_fd_close_callback(void *context) #define COPY_BUF_SIZE (512 * KB) +#ifdef _WIN32 +#include +#include + +int ftruncate(int fd, off_t length) { + HANDLE hFile = (HANDLE)_get_osfhandle(fd); + if (hFile == INVALID_HANDLE_VALUE) { + return -1; + } + + LARGE_INTEGER li; + li.QuadPart = length; + + if (!SetFilePointerEx(hFile, li, NULL, FILE_BEGIN)) { + return -1; + } + + if (!SetEndOfFile(hFile)) { + return -1; + } + + return 0; +} + +#endif + + static int _copy_file_contents(int dest, int src) { int ret = 0; @@ -261,8 +284,8 @@ static int _copy_file_contents(int dest, int src) return -LTFS_CACHE_IO; } - while ((len_read = read(src, buf, COPY_BUF_SIZE)) > 0) { - len_written = write(dest, buf, len_read); + while ((len_read = arch_read(src, buf, COPY_BUF_SIZE)) > 0) { + len_written = arch_write(dest, buf, len_read); if (ret < 0) { ltfsmsg(LTFS_ERR, 17246E, "_copy_file", errno); free(buf); @@ -319,9 +342,7 @@ int xml_acquire_file_lock(const char *file, int *fd, int *bk_fd, bool is_write) *fd = *bk_fd = -1; /* Open specified file to lock */ - *fd = open(file, - O_RDWR | O_CREAT | O_BINARY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); + arch_open(fd,file,O_RDWR | O_CREAT | O_BINARY, SHARE_FLAG_DENYRW, PERMISSION_READWRITE); if (*fd < 0) { /* Failed to open the advisory lock '%s' (%d) */ errno_save = errno; @@ -391,17 +412,16 @@ int xml_acquire_file_lock(const char *file, int *fd, int *bk_fd, bool is_write) asprintf(&backup_file, "%s.%s", file, "bk"); if (!backup_file){ ltfsmsg(LTFS_ERR, 10001E, "xml_acquire_file_lock: backup name"); - close(*fd); + arch_close(*fd); *fd = -1; goto out; } - *bk_fd = open(backup_file, - O_RDWR | O_CREAT | O_BINARY | O_TRUNC, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ); + arch_open(bk_fd,backup_file, + O_RDWR | O_CREAT | O_BINARY | O_TRUNC,SHARE_FLAG_DENYRW, PERMISSION_READWRITE); if (*bk_fd < 0) { ltfsmsg(LTFS_ERR, 17246E, "backup file creation", errno); errno_save = errno; - close(*fd); + arch_close(*fd); *fd = -1; goto out; } @@ -411,9 +431,9 @@ int xml_acquire_file_lock(const char *file, int *fd, int *bk_fd, bool is_write) ret = _copy_file_contents(*bk_fd, *fd); if (ret < 0) { errno_save = errno; - close(*fd); + arch_close(*fd); *fd = -1; - close(*bk_fd); + arch_close(*bk_fd); *bk_fd = -1; goto out; } @@ -423,9 +443,9 @@ int xml_acquire_file_lock(const char *file, int *fd, int *bk_fd, bool is_write) if (ret < 0) { ltfsmsg(LTFS_ERR, 17246E, "seek", errno); errno_save = errno; - close(*fd); + arch_close(*fd); *fd = -1; - close(*bk_fd); + arch_close(*bk_fd); *bk_fd = -1; goto out; } @@ -434,9 +454,9 @@ int xml_acquire_file_lock(const char *file, int *fd, int *bk_fd, bool is_write) if (ret < 0){ ltfsmsg(LTFS_ERR, 17246E, "truncate", errno); errno_save = errno; - close(*fd); + arch_close(*fd); *fd = -1; - close(*bk_fd); + arch_close(*bk_fd); *bk_fd = -1; goto out; } @@ -465,8 +485,8 @@ int xml_release_file_lock(const char *file, int fd, int bk_fd, bool revert) ret = _copy_file_contents(fd, bk_fd); if (ret < 0) { ltfsmsg(LTFS_ERR, 17246E, "revert seek", errno); - close(bk_fd); - close(fd); + arch_close(bk_fd); + arch_close(fd); return -1; } } @@ -486,8 +506,8 @@ int xml_release_file_lock(const char *file, int fd, int bk_fd, bool revert) } #endif - if (fd >= 0) close(fd); - if (bk_fd >= 0) close(bk_fd); + if (fd >= 0) arch_close(fd); + if (bk_fd >= 0) arch_close(bk_fd); errno = errno_save; asprintf(&backup_file, "%s.%s", file, "bk"); @@ -495,7 +515,7 @@ int xml_release_file_lock(const char *file, int fd, int bk_fd, bool revert) ltfsmsg(LTFS_ERR, 10001E, "xml_release_file_lock: backup name"); ret = -LTFS_NO_MEMORY; } else { - unlink(backup_file); + arch_unlink(backup_file); free(backup_file); } diff --git a/src/libltfs/xml_writer_libltfs.c b/src/libltfs/xml_writer_libltfs.c index 8dffcc4b..37a0d271 100644 --- a/src/libltfs/xml_writer_libltfs.c +++ b/src/libltfs/xml_writer_libltfs.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -61,12 +61,13 @@ #include "tape.h" #include "pathname.h" #include "arch/time_internal.h" - -/* O_BINARY is defined only in MinGW */ -#ifndef O_BINARY -#define O_BINARY 0 +#ifdef mingw_PLATFORM +#include "unicode/umachine.h" +#include "unicode/utf8.h" +#ifdef _MSC_VER + #pragma warning(disable : 4996) +#endif #endif - /* Structure to control EE's file offset cache and sync file list */ struct ltfsee_cache { @@ -81,7 +82,7 @@ struct ltfsee_cache static int encode_entry_name(char **new_name, const char *name) { int len; - UChar32 c; + UChar32 c=0; /* Printable ASCII characters * !\"#$&`'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; @@ -119,7 +120,7 @@ static int encode_entry_name(char **new_name, const char *name) } while (count < i - prev) { - sprintf(buf_encode, "%02X", name[prev+count] & 0xFF); + arch_sprintf_auto(buf_encode, "%02X", name[prev+count] & 0xFF); tmp_name[j] = '%'; tmp_name[j+1] = buf_encode[0]; tmp_name[j+2] = buf_encode[1]; @@ -130,7 +131,7 @@ static int encode_entry_name(char **new_name, const char *name) tmp_name[j] = '\0'; - *new_name = strdup(tmp_name); + *new_name = arch_strdup(tmp_name); free(tmp_name); return 0; @@ -343,10 +344,11 @@ static int _xml_write_file(xmlTextWriterPtr writer, struct dentry *file, struct /* Write dirty file list */ if (sync_list->fp && file->dirty) { fprintf(sync_list->fp, "%s,%"PRIu64"\n", file->name.name, file->size); - file->dirty = false; sync_list->count++; + file->dirty = false; } + return 0; } @@ -359,13 +361,13 @@ static int _xml_write_file(xmlTextWriterPtr writer, struct dentry *file, struct * @param sync_list file pointer to write sync file list * @return 0 on success or negative on failure */ -static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry *dir, - const struct ltfs_index *idx, struct ltfsee_cache* offset_c, struct ltfsee_cache* sync_list) +static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry* dir, + const struct ltfs_index* idx, struct ltfsee_cache* offset_c, struct ltfsee_cache* sync_list) { size_t i; - char *offset_name, *sync_name; - struct ltfsee_cache *offset = offset_c, *sync = sync_list; - struct name_list *list_ptr, *list_tmp; + char *offset_name, * sync_name; + struct ltfsee_cache* offset = offset_c, * sync = sync_list; + struct name_list* list_ptr, * list_tmp; int ret; if (!dir) @@ -376,15 +378,17 @@ static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry *dir, if (dir == idx->root) { if (idx->volume_name.name) { xml_mktag(_xml_write_nametype(writer, "name", (struct ltfs_name*)(&idx->volume_name)), -1); - } else { + } + else { xml_mktag(xmlTextWriterStartElement(writer, BAD_CAST "name"), -1); xml_mktag(xmlTextWriterEndElement(writer), -1); } - } else + } + else xml_mktag(_xml_write_nametype(writer, "name", &dir->name), -1); xml_mktag(xmlTextWriterWriteElement( - writer, BAD_CAST "readonly", BAD_CAST (dir->readonly ? "true" : "false")), -1); + writer, BAD_CAST "readonly", BAD_CAST(dir->readonly ? "true" : "false")), -1); xml_mktag(_xml_write_dentry_times(writer, dir), -1); xml_mktag(xmlTextWriterWriteFormatElement( writer, BAD_CAST UID_TAGNAME, "%"PRIu64, dir->uid), -1); @@ -407,7 +411,8 @@ static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry *dir, free(offset_name); if (!offset->fp) ltfsmsg(LTFS_WARN, 17248W, "offset cache", list_ptr->d->vol->index_cache_path); - } else + } + else ltfsmsg(LTFS_WARN, 17247W, "offset cache", list_ptr->d->vol->index_cache_path); ret = asprintf(&sync_name, "%s.%s", list_ptr->d->vol->index_cache_path, "synclist.new"); @@ -416,7 +421,8 @@ static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry *dir, free(sync_name); if (!sync->fp) ltfsmsg(LTFS_WARN, 17248W, "sync list", list_ptr->d->vol->index_cache_path); - } else + } + else ltfsmsg(LTFS_WARN, 17247W, "sync list", list_ptr->d->vol->index_cache_path); } @@ -435,7 +441,8 @@ static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry *dir, sync->fp = NULL; } - } else + } + else xml_mktag(_xml_write_file(writer, list_ptr->d, offset_c, sync_list), -1); } @@ -443,7 +450,7 @@ static int _xml_write_dirtree(xmlTextWriterPtr writer, struct dentry *dir, /* Save unrecognized tags */ if (dir->tag_count > 0) { - for (i=0; itag_count; ++i) { + for (i = 0; i < dir->tag_count; ++i) { if (xmlTextWriterWriteRaw(writer, dir->preserved_tags[i]) < 0) { ltfsmsg(LTFS_ERR, 17092E, __FUNCTION__); return -1; @@ -711,7 +718,7 @@ xmlBufferPtr xml_make_schema(const char *creator, const struct ltfs_index *idx) return buf; } -static int _commit_offset_caches(const char* path, const struct ltfs_index *idx) +static int _commit_offset_caches(const char *path, const struct ltfs_index *idx) { int ret = 0, fd = -1; char *offset_name = NULL, *sync_name = NULL; @@ -723,13 +730,12 @@ static int _commit_offset_caches(const char* path, const struct ltfs_index *idx) if (ret > 0) { ret = asprintf(&offset_name, "%s.%s", path, "offsetcache"); if (ret > 0) { - unlink(offset_name); + arch_unlink(offset_name); rename(offset_new, offset_name); - fd = open(offset_name, O_RDWR | O_BINARY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + arch_open(&fd, offset_name, O_RDWR | O_BINARY, SHARE_FLAG_DENYRW, PERMISSION_READWRITE); if (fd >= 0) { fsync(fd); - close(fd); + arch_close(fd); fd = -1; } else { if (errno != ENOENT) @@ -744,13 +750,12 @@ static int _commit_offset_caches(const char* path, const struct ltfs_index *idx) if (ret > 0) { ret = asprintf(&sync_name, "%s.%s", path, "synclist"); if (ret > 0) { - unlink(sync_name); + arch_unlink(sync_name); rename(sync_new, sync_name); - fd = open(sync_name, O_RDWR | O_BINARY, - S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); + arch_open(&fd,sync_name, O_RDWR | O_BINARY, SHARE_FLAG_DENYRW , PERMISSION_READWRITE); if (fd >= 0) { fsync(fd); - close(fd); + arch_close(fd); fd = -1; } else { if (errno != ENOENT) @@ -791,7 +796,7 @@ int xml_schema_to_file(const char *filename, const char *creator, if (reason) asprintf(&alt_creator, "%s - %s", creator , reason); else - alt_creator = strdup(creator); + alt_creator = arch_strdup(creator); if (alt_creator) { ret = _xml_write_schema(writer, alt_creator, idx); @@ -817,12 +822,12 @@ int xml_schema_to_file(const char *filename, const char *creator, * @param vol LTFS volume. * @return 0 on success or a negative value on error. */ -int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) +int xml_schema_to_tape(char *reason, struct ltfs_volume* vol) { int ret, bk = -1; xmlOutputBufferPtr write_buf; xmlTextWriterPtr writer; - struct xml_output_tape *out_ctx; + struct xml_output_tape* out_ctx; char *creator = NULL; bool immed = false; @@ -831,32 +836,32 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) /* Create output callback context data structure. */ out_ctx = calloc(1, sizeof(struct xml_output_tape)); - if (! out_ctx) { + if (!out_ctx) { ltfsmsg(LTFS_ERR, 10001E, "xml_schema_to_tape: output context"); return -LTFS_NO_MEMORY; } out_ctx->buf = malloc(vol->label->blocksize + LTFS_CRC_SIZE); - if (! out_ctx->buf) { + if (!out_ctx->buf) { ltfsmsg(LTFS_ERR, 10001E, "xml_schema_to_tape: output buffer"); free(out_ctx); return -LTFS_NO_MEMORY; } - out_ctx->fd = -1; + out_ctx->fd = -1; out_ctx->errno_fd = 0; if (vol->index_cache_path) xml_acquire_file_lock(vol->index_cache_path, &out_ctx->fd, &bk, true); out_ctx->buf_size = vol->label->blocksize; out_ctx->buf_used = 0; - out_ctx->device = vol->device; + out_ctx->device = vol->device; out_ctx->err_code = 0; /* Create output buffer pointer. */ write_buf = xmlOutputBufferCreateIO(xml_output_tape_write_callback, - xml_output_tape_close_callback, - out_ctx, NULL); - if (! write_buf) { + xml_output_tape_close_callback, + out_ctx, NULL); + if (!write_buf) { ltfsmsg(LTFS_ERR, 17053E); if (out_ctx->fd >= 0) xml_release_file_lock(vol->index_cache_path, out_ctx->fd, bk, false); @@ -867,7 +872,7 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) /* Create XML writer. */ writer = xmlNewTextWriter(write_buf); - if (! writer) { + if (!writer) { ltfsmsg(LTFS_ERR, 17054E); if (out_ctx->fd >= 0) xml_release_file_lock(vol->index_cache_path, out_ctx->fd, bk, false); @@ -894,7 +899,8 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) if (out_ctx->fd >= 0) xml_release_file_lock(vol->index_cache_path, out_ctx->fd, bk, true); - } else { + } + else { /* New index is successfully sent to the internal buffer of tape drive */ immed = (strcmp(reason, SYNC_FORMAT) == 0); /* Use immediate write FM only at format */ ret = tape_write_filemark(vol->device, 1, true, true, immed); @@ -905,7 +911,8 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) */ if (vol->index_cache_path) _commit_offset_caches(vol->index_cache_path, vol->index); - } else { + } + else { ltfsmsg(LTFS_ERR, 11084E, ret); } @@ -914,18 +921,19 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) } /* Update the creator string */ - if (! vol->index->creator || strcmp(vol->creator, vol->index->creator)) { + if (!vol->index->creator || strcmp(vol->creator, vol->index->creator)) { if (vol->index->creator) free(vol->index->creator); vol->index->creator = strdup(vol->creator); - if (! vol->index->creator) { + if (!vol->index->creator) { ltfsmsg(LTFS_ERR, 10001E, "xml_schema_to_tape: new creator string"); ret = -LTFS_NO_MEMORY; } } free(creator); - } else { + } + else { ltfsmsg(LTFS_ERR, 10001E, "xml_schema_to_tape: creator string"); xmlFreeTextWriter(writer); xml_release_file_lock(vol->index_cache_path, out_ctx->fd, bk, true); @@ -936,4 +944,4 @@ int xml_schema_to_tape(char *reason, struct ltfs_volume *vol) free(out_ctx); return ret; -} +} \ No newline at end of file diff --git a/src/ltfs_fuse.c b/src/ltfs_fuse.c index db4d3a5c..85ceb355 100644 --- a/src/ltfs_fuse.c +++ b/src/ltfs_fuse.c @@ -1172,7 +1172,7 @@ void ltfs_fuse_umount(void *userdata) ltfs_request_trace(FUSE_REQ_EXIT(REQ_UNMOUNT), 0, 0); } -int ltfs_fuse_symlink(const char* to, const char* from) +int ltfs_fuse_symlink(const char *to, const char *from) { struct ltfs_fuse_data *priv = fuse_get_context()->private_data; ltfs_file_id id; @@ -1187,7 +1187,7 @@ int ltfs_fuse_symlink(const char* to, const char* from) return errormap_fuse_error(ret); } -int ltfs_fuse_readlink(const char* path, char* buf, size_t size) +int ltfs_fuse_readlink(const char *path, char *buf, size_t size) { struct ltfs_fuse_data *priv = fuse_get_context()->private_data; ltfs_file_id id; diff --git a/src/ltfs_fuse.h b/src/ltfs_fuse.h index 07a6cd35..3cf79f17 100644 --- a/src/ltfs_fuse.h +++ b/src/ltfs_fuse.h @@ -59,7 +59,7 @@ extern "C" { #endif #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #endif #include "libltfs/ltfs_fuse_version.h" diff --git a/src/tape_drivers/generic/file/filedebug_conf_tc.h b/src/tape_drivers/generic/file/filedebug_conf_tc.h index 3d97c222..9b8cb2c2 100644 --- a/src/tape_drivers/generic/file/filedebug_conf_tc.h +++ b/src/tape_drivers/generic/file/filedebug_conf_tc.h @@ -49,8 +49,11 @@ #ifndef __filedebug_conf_tc_h #define __filedebug_conf_tc_h +#ifdef mingw_PLATFORM +#include "xml.h" +#else #include - +#endif #define DEFAULT_CAPACITY_MB (3 * (GB / MB)) /* emulation of delays due to tape seeks */ diff --git a/src/tape_drivers/generic/file/filedebug_tc.c b/src/tape_drivers/generic/file/filedebug_tc.c index 28a4ec71..62b06dbd 100644 --- a/src/tape_drivers/generic/file/filedebug_tc.c +++ b/src/tape_drivers/generic/file/filedebug_tc.c @@ -51,7 +51,10 @@ ************************************************************************************* */ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" + +#else +#include #endif #include @@ -65,8 +68,30 @@ #include #include #include -#include + #include +#include "libltfs/arch/ltfs_arch_ops.h" + +#ifdef _WIN32 +char* my_basename(char* path) { + char* base = strrchr(path, '\\'); + return base ? base + 1 : path; +} + +char* my_dirname(char* path) { + static char buffer[256]; + arch_strcpy_auto(buffer, path); + char* last_slash = strrchr(buffer, '\\'); + if (last_slash) { + *last_slash = '\0'; + } + else { + arch_strcpy_auto(buffer, "."); + } + return buffer; +} +#endif + #include #include "libltfs/ltfs_fuse_version.h" @@ -83,14 +108,17 @@ #include "filedebug_conf_tc.h" -volatile char *copyright = LTFS_COPYRIGHT_0"\n"LTFS_COPYRIGHT_1"\n"LTFS_COPYRIGHT_2"\n" \ - LTFS_COPYRIGHT_3"\n"LTFS_COPYRIGHT_4"\n"LTFS_COPYRIGHT_5"\n"; +#ifdef _MSC_VER +volatile char* copyright = LTFS_COPYRIGHT_0"\n"LTFS_COPYRIGHT_1"\n"LTFS_COPYRIGHT_2"\n" \ +LTFS_COPYRIGHT_3"\n"LTFS_COPYRIGHT_4"\n"LTFS_COPYRIGHT_5"\n"; +#endif // !_MSC_VER + /* Default directory where the emulated tape contents go to */ #ifdef mingw_PLATFORM -const char *filedebug_default_device = "c:\\tmp\\ltfs\\tape"; +const char* filedebug_default_device = "c:\\tmp\\ltfs\\tape"; #else -const char *filedebug_default_device = "/tmp/ltfs/tape"; +const char* filedebug_default_device = "/tmp/ltfs/tape"; #endif #define MAX_PARTITIONS 2 @@ -122,8 +150,8 @@ const char *filedebug_default_device = "/tmp/ltfs/tape"; */ struct filedebug_data { int fd; /**< File descriptor to contain the pointer to directory where blocks reside */ - char *dirbase; /**< Base directory for searcing directoty from the pointer */ - char *dirname; /**< Directory where blocks reside */ + char* dirbase; /**< Base directory for searcing directoty from the pointer */ + char* dirname; /**< Directory where blocks reside */ bool device_reserved; /**< True when the device has been successfully reserved */ bool medium_locked; /**< True when preventing medium removal by the user */ bool null_backend; /**< True when runiing on null backend_mode */ @@ -151,9 +179,9 @@ struct filedebug_data { uint64_t read_counter; /**< read call counter for pseudo write perm */ int force_errortype; /**< 0 is R/W Perm, otherwise no sense */ int drive_type; /**< drive type defined by ltfs */ - char *serial_number; /**< Serial number of this dummy tape device */ + char* serial_number; /**< Serial number of this dummy tape device */ struct tc_drive_info info; /**< Device informaton (DUMMY) */ - char *product_id; /**< Product ID of this dummy tape device */ + char* product_id; /**< Product ID of this dummy tape device */ struct filedebug_conf_tc conf; /**< Bahavior option for this instance */ }; @@ -165,51 +193,51 @@ struct filedebug_global_data global_data; /* record suffixes for data block, filemark, EOD indicator */ long original_pid = 0; -static const char *rec_suffixes = "RFE"; +static const char* rec_suffixes = "RFE"; #define SUFFIX_RECORD (0) #define SUFFIX_FILEMARK (1) #define SUFFIX_EOD (2) /* Forward reference */ -int filedebug_get_device_list(struct tc_drive_info *buf, int count); +int filedebug_get_device_list(struct tc_drive_info* buf, int count); /* local prototypes */ -int filedebug_search_eod(struct filedebug_data *state, int partition); -int _filedebug_write_eod(struct filedebug_data *state); -int _filedebug_check_file(const char *fname); -char *_filedebug_make_current_filename(const struct filedebug_data *state, char type); -char *_filedebug_make_filename(const struct filedebug_data *state, +int filedebug_search_eod(struct filedebug_data* state, int partition); +int _filedebug_write_eod(struct filedebug_data* state); +int _filedebug_check_file(const char* fname); +char* _filedebug_make_current_filename(const struct filedebug_data* state, char type); +char* _filedebug_make_filename(const struct filedebug_data* state, int part, uint64_t pos, char type); -char *_filedebug_make_attrname(const struct filedebug_data *state, int part, int id); -int _filedebug_remove_current_record(const struct filedebug_data *state); -int _filedebug_remove_record(const struct filedebug_data *state, +char* _filedebug_make_attrname(const struct filedebug_data* state, int part, int id); +int _filedebug_remove_current_record(const struct filedebug_data* state); +int _filedebug_remove_record(const struct filedebug_data* state, int partition, uint64_t blknum); -int _filedebug_space_fm(struct filedebug_data *state, uint64_t count, bool back); -int _filedebug_space_rec(struct filedebug_data *state, uint64_t count, bool back); -int _get_wp(struct filedebug_data *state, uint64_t *wp); -int _set_wp(struct filedebug_data *state, uint64_t wp); +int _filedebug_space_fm(struct filedebug_data* state, uint64_t count, bool back); +int _filedebug_space_rec(struct filedebug_data* state, uint64_t count, bool back); +int _get_wp(struct filedebug_data* state, uint64_t* wp); +int _set_wp(struct filedebug_data* state, uint64_t wp); /* Command-line options recognized by this module */ #define FILEDEBUG_OPT(templ,offset,value) { templ, offsetof(struct filedebug_global_data, offset), value } -static inline uint64_t calc_p0_cap(struct filedebug_data *state) +static inline uint64_t calc_p0_cap(struct filedebug_data* state) { /* 5% of total capacity */ - return (state->conf.capacity_mb * 5 /100); + return (state->conf.capacity_mb * 5 / 100); } -static inline uint64_t calc_p0_remaining(struct filedebug_data *state) +static inline uint64_t calc_p0_remaining(struct filedebug_data* state) { /* Assume 512KB per 1 record */ return (calc_p0_cap(state) - state->eod[0] / 2); } -static inline uint64_t calc_p1_cap(struct filedebug_data *state) +static inline uint64_t calc_p1_cap(struct filedebug_data* state) { return (state->conf.capacity_mb - calc_p0_cap(state)); } -static inline uint64_t calc_p1_remaining(struct filedebug_data *state) +static inline uint64_t calc_p1_remaining(struct filedebug_data* state) { /* Assume 512KB per 1 record */ return (calc_p1_cap(state) - state->eod[1] / 2); @@ -221,15 +249,15 @@ static struct fuse_opt filedebug_opts[] = { FUSE_OPT_END }; -int null_parser(void *state, const char *arg, int key, struct fuse_args *outargs) +int null_parser(void* state, const char* arg, int key, struct fuse_args* outargs) { return 1; } -int filedebug_parse_opts(void *device, void *opt_args) +int filedebug_parse_opts(void* device, void* opt_args) { - struct filedebug_data *state = (struct filedebug_data *) device; - struct fuse_args *args = (struct fuse_args *) opt_args; + struct filedebug_data* state = (struct filedebug_data*)device; + struct fuse_args* args = (struct fuse_args*)opt_args; int ret; /* fuse_opt_parse can handle a NULL device parameter just fine */ @@ -240,13 +268,13 @@ int filedebug_parse_opts(void *device, void *opt_args) return 0; } -static void emulate_threading_wait(struct filedebug_data *state) +static void emulate_threading_wait(struct filedebug_data* state) { if (!state->conf.delay_mode) return; struct timespec t; - t.tv_sec = state->conf.threading_sec; + t.tv_sec = state->conf.threading_sec; t.tv_nsec = 0; /* TODO: Need to handle interrupted sleep */ @@ -261,7 +289,7 @@ static void emulate_threading_wait(struct filedebug_data *state) } } -static inline uint64_t calc_wrap(struct filedebug_data *state, struct tc_position *pos) +static inline uint64_t calc_wrap(struct filedebug_data* state, struct tc_position* pos) { /* Assume 512KB per 1 record */ uint64_t blocks_per_wrap = (state->conf.capacity_mb / state->conf.wraps) * 2; @@ -274,7 +302,7 @@ static inline uint64_t calc_wrap(struct filedebug_data *state, struct tc_positio return wrap; } -static void emulate_seek_wait(struct filedebug_data *state, struct tc_position *dest) +static void emulate_seek_wait(struct filedebug_data* state, struct tc_position* dest) { if (!state->conf.delay_mode) return; @@ -297,7 +325,7 @@ static void emulate_seek_wait(struct filedebug_data *state, struct tc_position * else distance = current_dist_from_bot - target_dist_from_bot; - float cost = ((float) state->conf.eot_to_bot_sec / blocks_per_wrap) * (distance-1.0); + float cost = ((float)state->conf.eot_to_bot_sec / blocks_per_wrap) * (distance - 1.0); time_t delay_us = 0; if (dest->partition != state->current_position.partition) { @@ -311,23 +339,27 @@ static void emulate_seek_wait(struct filedebug_data *state, struct tc_position * if (current_wrap == target_wrap && dest->block > state->current_position.block) { /* Same wrap, moving tape forward */ delay_us = MICROSECONDS(cost); - } else if (current_wrap == target_wrap && dest->block < state->current_position.block) { + } + else if (current_wrap == target_wrap && dest->block < state->current_position.block) { /* Same wrap, moving tape backward */ delay_us = MICROSECONDS(cost) + state->conf.change_direction_us; - } else if (current_wrap % 2 == target_wrap % 2 && dest->block > state->current_position.block) { + } + else if (current_wrap % 2 == target_wrap % 2 && dest->block > state->current_position.block) { /* Different wraps, same direction, can move tape forward */ delay_us = MICROSECONDS(cost) + state->conf.change_track_us; - } else if (current_wrap % 2 == target_wrap % 2 && dest->block < state->current_position.block) { + } + else if (current_wrap % 2 == target_wrap % 2 && dest->block < state->current_position.block) { /* Different wraps, same direction, must move tape backward */ delay_us = MICROSECONDS(cost) + state->conf.change_track_us + state->conf.change_direction_us; - } else if (current_wrap % 2 != target_wrap % 2) { + } + else if (current_wrap % 2 != target_wrap % 2) { /* Different wraps, different direction */ delay_us = MICROSECONDS(cost) + state->conf.change_track_us + state->conf.change_direction_us; } if (delay_us) { struct timespec t; - t.tv_sec = (delay_us / MICROSECONDS(1)); + t.tv_sec = (delay_us / MICROSECONDS(1)); t.tv_nsec = (delay_us % MICROSECONDS(1)) * 1000; /* TODO: Need to handle interrupted sleep */ @@ -343,56 +375,91 @@ static void emulate_seek_wait(struct filedebug_data *state, struct tc_position * } } -static void emulate_load_wait(struct filedebug_data *state) +static void emulate_load_wait(struct filedebug_data* state) { struct tc_position dest; - dest.block = 0; - dest.filemarks = 0; - dest.partition = 0; + dest.block = 0; + dest.filemarks = 0; + dest.partition = 0; dest.early_warning = false; dest.programmable_early_warning = false; emulate_seek_wait(state, &dest); } -static void emulate_rewind_wait(struct filedebug_data *state) +static void emulate_rewind_wait(struct filedebug_data* state) { struct tc_position dest; - dest.block = 0; - dest.filemarks = 0; - dest.partition = state->current_position.partition; + dest.block = 0; + dest.filemarks = 0; + dest.partition = state->current_position.partition; dest.early_warning = false; dest.programmable_early_warning = false; emulate_seek_wait(state, &dest); } -void filedebug_help_message(const char *progname) +void filedebug_help_message(const char* progname) { ltfsresult(30199I, filedebug_default_device); } +#ifdef mingw_PLATFORM +char* dirname(char* path) { + if (!path || !*path) { + return "."; + } + + // Create a copy of the input path to avoid modifying the original + char* copy = _strdup(path); + if (!copy) { + return NULL; + } + + // Find the last directory separator + char* last_sep = strrchr(copy, '\\'); + if (!last_sep) { + last_sep = strrchr(copy, '/'); // Handle forward slashes + } + + if (!last_sep) { + free(copy); + return "."; // No separator found, return current directory + } + + // Remove the trailing component + if (last_sep == copy) { + // Root directory case (e.g., "/") + copy[1] = '\0'; + } + else { + *last_sep = '\0'; + } + + return copy; +} +#endif -int filedebug_open(const char *name, void **handle) +int filedebug_open(const char* name, void** handle) { - struct filedebug_data *state; + struct filedebug_data* state; struct stat d; - char *tmp = NULL; - char *cur, *p; - char *pid = NULL, *ser = NULL; + char* tmp = NULL; + char* cur, * p; + char* pid = NULL, * ser = NULL; int ret; - char *devname = NULL; + char* devname = NULL; int i, devs = 0, info_devs = 0; - struct tc_drive_info *buf = NULL; + struct tc_drive_info* buf = NULL; ltfsmsg(LTFS_INFO, 30000I, name); CHECK_ARG_NULL(handle, -LTFS_NULL_ARG); *handle = NULL; - state = (struct filedebug_data *)calloc(1,sizeof(struct filedebug_data)); + state = (struct filedebug_data*)calloc(1, sizeof(struct filedebug_data)); if (!state) { ltfsmsg(LTFS_ERR, 10001E, "filedebug_open: private data"); return -EDEV_NO_MEMORY; @@ -402,18 +469,19 @@ int filedebug_open(const char *name, void **handle) ret = stat(name, &d); if (ret == 0 && S_ISDIR(d.st_mode)) { ltfsmsg(LTFS_INFO, 30003I, name); - state->dirname = strdup(name); + state->dirname = arch_strdup(name); if (!state->dirname) { ltfsmsg(LTFS_ERR, 10001E, "filedebug_open: dirname"); free(state); return -EDEV_NO_MEMORY; } state->product_id = "ULTRIUM-TD5"; - } else { + } + else { devs = filedebug_get_device_list(NULL, 0); if (devs) { - buf = (struct tc_drive_info *)calloc(devs * 2, sizeof(struct tc_drive_info)); - if (! buf) { + buf = (struct tc_drive_info*)calloc(devs * 2, sizeof(struct tc_drive_info)); + if (!buf) { ltfsmsg(LTFS_ERR, 10001E, __FUNCTION__); return -LTFS_NO_MEMORY; } @@ -421,8 +489,8 @@ int filedebug_open(const char *name, void **handle) } for (i = 0; i < info_devs; i++) { - if (! strncmp(buf[i].serial_number, name, TAPE_SERIAL_LEN_MAX) ) { - devname = strdup(buf[i].name); + if (!strncmp(buf[i].serial_number, name, TAPE_SERIAL_LEN_MAX)) { + devname = arch_strdup(buf[i].name); if (!devname) { ltfsmsg(LTFS_ERR, 10001E, "sg_ibmtape_open: devname"); if (buf) free(buf); @@ -440,9 +508,10 @@ int filedebug_open(const char *name, void **handle) /* Run on file mode */ if (devname == NULL) - devname = strdup(name); + devname = arch_strdup(name); ltfsmsg(LTFS_INFO, 30001I, devname); - state->fd = open(devname, O_RDWR | O_BINARY); + + arch_open(&(state->fd), devname, O_RDWR | O_BINARY, SHARE_FLAG_DENYWR, PERMISSION_READWRITE); if (state->fd < 0) { ltfsmsg(LTFS_ERR, 30002E, devname); return -EDEV_INTERNAL_ERROR; @@ -458,7 +527,7 @@ int filedebug_open(const char *name, void **handle) ser = cur + 1; break; } - cur --; + cur--; } if (pid && ser) { @@ -472,14 +541,14 @@ int filedebug_open(const char *name, void **handle) for (i = 0; i < (int)strlen(state->serial_number); i++) { if (state->serial_number[i] == '.') { state->serial_number[i] = '\0'; - state->product_id = &(state->serial_number[i+1]); + state->product_id = &(state->serial_number[i + 1]); break; } } } /* Store directory base */ - tmp = strdup(devname); + tmp = arch_strdup(devname); if (!tmp) { ltfsmsg(LTFS_ERR, 10001E, "filedebug_open: dirbase tmp"); free(state); @@ -489,46 +558,47 @@ int filedebug_open(const char *name, void **handle) /* The dirname() function may return a pointer to static storage that may then be overwritten by subsequent calls to dirname(). */ p = dirname(tmp); - state->dirbase = (char *) calloc(strlen(p) + 1, sizeof(char)); + int len = (strlen(p) + 1) * sizeof(char); + state->dirbase = (char*)calloc(strlen(p) + 1, sizeof(char)); if (!state->dirbase) { ltfsmsg(LTFS_ERR, 10001E, "filedebug_open: dirbase"); free(state); free(tmp); return -EDEV_NO_MEMORY; } - strcpy(state->dirbase, p); + arch_strcpy(state->dirbase, len, p); free(tmp); free(devname); - devname= NULL; + devname = NULL; } - state->ready = false; + state->ready = false; state->max_block_size = 16 * MB; /* Set default option value */ - state->conf.dummy_io = false; + state->conf.dummy_io = false; state->conf.emulate_readonly = false; - state->conf.capacity_mb = DEFAULT_CAPACITY_MB; - state->conf.cart_type = TC_MP_LTO5D_CART; - state->conf.density_code = 0x58; + state->conf.capacity_mb = DEFAULT_CAPACITY_MB; + state->conf.cart_type = TC_MP_LTO5D_CART; + state->conf.density_code = 0x58; /* Initial setting of force perm */ - state->clear_by_pc = false; + state->clear_by_pc = false; state->force_writeperm = DEFAULT_WRITEPERM; - state->force_readperm = DEFAULT_READPERM; + state->force_readperm = DEFAULT_READPERM; state->force_errortype = DEFAULT_ERRORTYPE; - state->conf.delay_mode = DELAY_NONE; - state->conf.wraps = DEFAULT_WRAPS; - state->conf.eot_to_bot_sec = DEFAULT_EOT_TO_BOT; + state->conf.delay_mode = DELAY_NONE; + state->conf.wraps = DEFAULT_WRAPS; + state->conf.eot_to_bot_sec = DEFAULT_EOT_TO_BOT; state->conf.change_direction_us = DEFAULT_CHANGE_DIRECTION; - state->conf.change_track_us = DEFAULT_CHANGE_TRACK; + state->conf.change_track_us = DEFAULT_CHANGE_TRACK; /* Set drive type if it is provided */ - struct supported_device **d_cur = ibm_supported_drives; - while(*d_cur) { - if((! strncmp(IBM_VENDOR_ID, (*d_cur)->vendor_id, strlen((*d_cur)->vendor_id)) ) && - (! strncmp(state->product_id, (*d_cur)->product_id, strlen((*d_cur)->product_id)) ) ) { + struct supported_device** d_cur = ibm_supported_drives; + while (*d_cur) { + if ((!strncmp(IBM_VENDOR_ID, (*d_cur)->vendor_id, strlen((*d_cur)->vendor_id))) && + (!strncmp(state->product_id, (*d_cur)->product_id, strlen((*d_cur)->product_id)))) { state->drive_type = (*d_cur)->drive_type; break; } @@ -542,24 +612,24 @@ int filedebug_open(const char *name, void **handle) snprintf(state->info.product_rev, PRODUCT_REV_LENGTH + 1, "%s", "REVS"); snprintf(state->info.product_name, PRODUCT_NAME_LENGTH + 1, "[%s]", state->product_id); - state->info.host = 0; + state->info.host = 0; state->info.channel = 0; - state->info.target = 0; - state->info.lun = -1; + state->info.target = 0; + state->info.lun = -1; - *handle = (void *) state; + *handle = (void*)state; return 0; } -int filedebug_reopen(const char *name, void *device) +int filedebug_reopen(const char* name, void* device) { /* Do nothing */ return 0; } -int filedebug_close(void *device) +int filedebug_close(void* device) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; /* Write EOD of DP here when dummy io mode is enebaled */ if (state->conf.dummy_io) { @@ -570,7 +640,7 @@ int filedebug_close(void *device) if (state) { if (state->fd > 0) - close(state->fd); + arch_close(state->fd); if (state->dirbase) free(state->dirbase); if (state->dirname) @@ -583,17 +653,17 @@ int filedebug_close(void *device) return 0; } -int filedebug_close_raw(void *device) +int filedebug_close_raw(void* device) { return 0; } -int filedebug_is_connected(const char *devname) +int filedebug_is_connected(const char* devname) { return 0; } -int filedebug_inquiry(void *device, struct tc_inq *inq) +int filedebug_inquiry(void* device, struct tc_inq* inq) { memset(inq, 0, sizeof(struct tc_inq)); memcpy(inq->vid, "DUMMY ", 8); @@ -603,25 +673,25 @@ int filedebug_inquiry(void *device, struct tc_inq *inq) return DEVICE_GOOD; } -int filedebug_inquiry_page(void *device, unsigned char page, struct tc_inq_page *inq) +int filedebug_inquiry_page(void* device, unsigned char page, struct tc_inq_page* inq) { memset(inq, 0, sizeof(struct tc_inq_page)); return DEVICE_GOOD; } -int filedebug_test_unit_ready(void *device) +int filedebug_test_unit_ready(void* device) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) return -EDEV_NEED_INITIALIZE; return DEVICE_GOOD; } -int filedebug_read(void *device, char *buf, size_t count, struct tc_position *pos, +int filedebug_read(void* device, char* buf, size_t count, struct tc_position* pos, const bool unusual_size) { - struct filedebug_data *state = (struct filedebug_data *)device; - char *fname; + struct filedebug_data* state = (struct filedebug_data*)device; + char* fname; size_t fname_len; int ret; ssize_t bytes_read; @@ -672,7 +742,8 @@ int filedebug_read(void *device, char *buf, size_t count, struct tc_position *po pos->block = state->current_position.block; return count; - } else { + } + else { fname = _filedebug_make_current_filename(state, rec_suffixes[SUFFIX_EOD]); if (!fname) return -EDEV_NO_MEMORY; @@ -715,7 +786,7 @@ int filedebug_read(void *device, char *buf, size_t count, struct tc_position *po return ret; } if (ret > 0) { - fd = open(fname, O_RDONLY | O_BINARY); + arch_open(&fd, fname, O_RDONLY | O_BINARY, SHARE_FLAG_DENYWR, PERMISSION_READWRITE); free(fname); if (fd < 0) { ltfsmsg(LTFS_ERR, 30011E, errno); @@ -723,14 +794,14 @@ int filedebug_read(void *device, char *buf, size_t count, struct tc_position *po } /* TODO: return -EDEV_INVALID_ARG if buffer is too small to hold complete record? */ - bytes_read = read(fd, buf, count); + bytes_read = arch_read(fd, buf, count); if (bytes_read < 0) { ltfsmsg(LTFS_ERR, 30012E, errno); - close(fd); + arch_close(fd); return -EDEV_RW_PERM; } - ret = close(fd); + ret = arch_close(fd); if (ret < 0) { ltfsmsg(LTFS_ERR, 30013E, errno); return -EDEV_RW_PERM; @@ -751,11 +822,11 @@ int filedebug_read(void *device, char *buf, size_t count, struct tc_position *po return -EDEV_RW_PERM; } -int filedebug_write(void *device, const char *buf, size_t count, struct tc_position *pos) +int filedebug_write(void* device, const char* buf, size_t count, struct tc_position* pos) { int ret = -1; - struct filedebug_data *state = (struct filedebug_data *)device; - char *fname; + struct filedebug_data* state = (struct filedebug_data*)device; + char* fname; int fd; ssize_t written; @@ -795,24 +866,26 @@ int filedebug_write(void *device, const char *buf, size_t count, struct tc_posit /* TODO: It is nicer if we have a append only mode support */ - if (! buf && count > 0) { + if (!buf && count > 0) { ltfsmsg(LTFS_ERR, 30018E); ret = -EDEV_INVALID_ARG; return ret; - } else if (count == 0) { + } + else if (count == 0) { ret = 0; /* nothing to do */ return ret; } - if ( state->force_writeperm ) { + if (state->force_writeperm) { state->write_counter++; - if ( state->write_counter > state->force_writeperm ) { + if (state->write_counter > state->force_writeperm) { ltfsmsg(LTFS_ERR, 30007E, "write"); if (state->force_errortype) return -EDEV_NO_SENSE; else return -EDEV_WRITE_PERM; - } else if ( state->write_counter > (state->force_writeperm - THRESHOLD_FORCE_WRITE_NO_WRITE) ) { + } + else if (state->write_counter > (state->force_writeperm - THRESHOLD_FORCE_WRITE_NO_WRITE)) { ltfsmsg(LTFS_INFO, 30019I); ++state->current_position.block; pos->block = state->current_position.block; @@ -840,7 +913,8 @@ int filedebug_write(void *device, const char *buf, size_t count, struct tc_posit state->eod[state->current_position.partition] = state->current_position.block; written = count; - } else { + } + else { /* clean up old records at this position */ ret = _filedebug_remove_current_record(state); if (ret < 0) { @@ -849,7 +923,7 @@ int filedebug_write(void *device, const char *buf, size_t count, struct tc_posit } /* Increment Write Pass for consistency check */ - if(state->write_pass_prev == state->write_pass){ + if (state->write_pass_prev == state->write_pass) { ret = _set_wp(device, ++(state->write_pass)); if (ret < 0) { ltfsmsg(LTFS_ERR, 30022E, ret); @@ -864,9 +938,7 @@ int filedebug_write(void *device, const char *buf, size_t count, struct tc_posit ret = -EDEV_NO_MEMORY; return ret; } - fd = open(fname, - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); + arch_open(&fd, fname, (O_WRONLY | O_CREAT | O_TRUNC | O_BINARY), SHARE_FLAG_DENYWR, PERMISSION_READWRITE); if (fd < 0) { ltfsmsg(LTFS_ERR, 30024E, fname, errno); free(fname); @@ -875,13 +947,13 @@ int filedebug_write(void *device, const char *buf, size_t count, struct tc_posit free(fname); /* write and close the file */ - written = write(fd, buf, count); + written = arch_write(fd, buf, count); if (written < 0) { ltfsmsg(LTFS_ERR, 30025E, errno); - close(fd); + arch_close(fd); return -EDEV_RW_PERM; } - ret = close(fd); + ret = arch_close(fd); if (ret < 0) { ltfsmsg(LTFS_ERR, 30026E, errno); return -EDEV_RW_PERM; @@ -916,11 +988,11 @@ int filedebug_write(void *device, const char *buf, size_t count, struct tc_posit return ret; } -int filedebug_writefm(void *device, size_t count, struct tc_position *pos, bool immed) +int filedebug_writefm(void* device, size_t count, struct tc_position* pos, bool immed) { int ret = -1; - struct filedebug_data *state = (struct filedebug_data *)device; - char *fname; + struct filedebug_data* state = (struct filedebug_data*)device; + char* fname; int fd; size_t i; @@ -948,7 +1020,7 @@ int filedebug_writefm(void *device, size_t count, struct tc_position *pos, bool * Do not write any data on partition1 (DP) */ - for (i=0; icurrent_position.block; ++state->current_position.filemarks; pos->block = state->current_position.block; @@ -957,9 +1029,10 @@ int filedebug_writefm(void *device, size_t count, struct tc_position *pos, bool state->eod[state->current_position.partition] = state->current_position.block; ret = DEVICE_GOOD; - } else { + } + else { /* Increment Write Pass for consistency check */ - if(state->write_pass_prev == state->write_pass){ + if (state->write_pass_prev == state->write_pass) { ret = _set_wp(device, ++(state->write_pass)); if (ret < 0) { ltfsmsg(LTFS_ERR, 30030E, ret); @@ -967,7 +1040,7 @@ int filedebug_writefm(void *device, size_t count, struct tc_position *pos, bool } } - for (i=0; icurrent_position.block; @@ -1025,9 +1098,9 @@ int filedebug_writefm(void *device, size_t count, struct tc_position *pos, bool return ret; } -int filedebug_rewind(void *device, struct tc_position *pos) +int filedebug_rewind(void* device, struct tc_position* pos) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30036E); @@ -1042,13 +1115,13 @@ int filedebug_rewind(void *device, struct tc_position *pos) emulate_rewind_wait(state); /* Does rewinding reset the partition? */ - state->current_position.block = 0; + state->current_position.block = 0; state->current_position.filemarks = 0; - state->clear_by_pc = false; - state->force_writeperm = DEFAULT_WRITEPERM; - state->force_readperm = DEFAULT_READPERM; - state->write_counter = 0; - state->read_counter = 0; + state->clear_by_pc = false; + state->force_writeperm = DEFAULT_WRITEPERM; + state->force_readperm = DEFAULT_READPERM; + state->write_counter = 0; + state->read_counter = 0; pos->block = state->current_position.block; pos->filemarks = 0; pos->early_warning = false; @@ -1057,10 +1130,10 @@ int filedebug_rewind(void *device, struct tc_position *pos) return DEVICE_GOOD; } -int filedebug_locate(void *device, struct tc_position dest, struct tc_position *pos) +int filedebug_locate(void* device, struct tc_position dest, struct tc_position* pos) { int ret = 0; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; tape_filemarks_t count_fm = 0; tape_block_t i; @@ -1087,9 +1160,9 @@ int filedebug_locate(void *device, struct tc_position dest, struct tc_position * if (state->current_position.partition != dest.partition) { if (state->clear_by_pc) { - state->clear_by_pc = false; + state->clear_by_pc = false; state->force_writeperm = DEFAULT_WRITEPERM; - state->force_readperm = DEFAULT_READPERM; + state->force_readperm = DEFAULT_READPERM; state->force_errortype = DEFAULT_ERRORTYPE; } } @@ -1099,19 +1172,19 @@ int filedebug_locate(void *device, struct tc_position dest, struct tc_position * state->current_position.partition = dest.partition; if (state->eod[dest.partition] == MISSING_EOD && state->last_block[dest.partition] < dest.block) - state->current_position.block = state->last_block[dest.partition] + 1; + state->current_position.block = state->last_block[dest.partition] + 1; else if (state->eod[dest.partition] < dest.block) state->current_position.block = state->eod[dest.partition]; else state->current_position.block = dest.block; pos->partition = state->current_position.partition; - pos->block = state->current_position.block; + pos->block = state->current_position.block; - for(i = 0; i < state->current_position.block; ++i) { - char *fname; + for (i = 0; i < state->current_position.block; ++i) { + char* fname; fname = _filedebug_make_filename(state, state->current_position.partition, - i, rec_suffixes[SUFFIX_FILEMARK]); + i, rec_suffixes[SUFFIX_FILEMARK]); if (!fname) { ltfsmsg(LTFS_ERR, 30039E); ret = -EDEV_NO_MEMORY; @@ -1143,10 +1216,10 @@ int filedebug_locate(void *device, struct tc_position dest, struct tc_position * return ret; } -int filedebug_space(void *device, size_t count, TC_SPACE_TYPE type, struct tc_position *pos) +int filedebug_space(void* device, size_t count, TC_SPACE_TYPE type, struct tc_position* pos) { int ret = 0; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; int ret_fm; tape_filemarks_t count_fm = 0; tape_block_t i; @@ -1163,61 +1236,66 @@ int filedebug_space(void *device, size_t count, TC_SPACE_TYPE type, struct tc_po return ret; } - switch(type) { - case TC_SPACE_EOD: - ltfsmsg(LTFS_DEBUG, 30195D, "space to EOD"); - state->current_position.block = state->eod[state->current_position.partition]; - if(state->current_position.block == MISSING_EOD) { - ret = -EDEV_RW_PERM; - return ret; - } else - ret = 0; - break; - case TC_SPACE_FM_F: - ltfsmsg(LTFS_DEBUG, 30196D, "space forward file marks", (unsigned long long)count); - if(state->current_position.block == MISSING_EOD) { - ret = -EDEV_RW_PERM; - return ret; - } else - ret = _filedebug_space_fm(state, count, false); - break; - case TC_SPACE_FM_B: - ltfsmsg(LTFS_DEBUG, 30196D, "space back file marks", (unsigned long long)count); - if(state->current_position.block == MISSING_EOD) { - ret = -EDEV_RW_PERM; - return ret; - } else - ret = _filedebug_space_fm(state, count, true); - break; - case TC_SPACE_F: - ltfsmsg(LTFS_DEBUG, 30196D, "space forward records", (unsigned long long)count); - if(state->current_position.block == MISSING_EOD) { - ret = -EDEV_RW_PERM; - return ret; - } else - ret = _filedebug_space_rec(state, count, false); - break; - case TC_SPACE_B: - ltfsmsg(LTFS_DEBUG, 30196D, "space back records", (unsigned long long)count); - if(state->current_position.block == MISSING_EOD) { - ret = -EDEV_RW_PERM; - return ret; - } else - ret = _filedebug_space_rec(state, count, true); - break; - default: - ltfsmsg(LTFS_ERR, 30041E); - ret = -EDEV_INVALID_ARG; + switch (type) { + case TC_SPACE_EOD: + ltfsmsg(LTFS_DEBUG, 30195D, "space to EOD"); + state->current_position.block = state->eod[state->current_position.partition]; + if (state->current_position.block == MISSING_EOD) { + ret = -EDEV_RW_PERM; return ret; + } + else + ret = 0; + break; + case TC_SPACE_FM_F: + ltfsmsg(LTFS_DEBUG, 30196D, "space forward file marks", (unsigned long long)count); + if (state->current_position.block == MISSING_EOD) { + ret = -EDEV_RW_PERM; + return ret; + } + else + ret = _filedebug_space_fm(state, count, false); + break; + case TC_SPACE_FM_B: + ltfsmsg(LTFS_DEBUG, 30196D, "space back file marks", (unsigned long long)count); + if (state->current_position.block == MISSING_EOD) { + ret = -EDEV_RW_PERM; + return ret; + } + else + ret = _filedebug_space_fm(state, count, true); + break; + case TC_SPACE_F: + ltfsmsg(LTFS_DEBUG, 30196D, "space forward records", (unsigned long long)count); + if (state->current_position.block == MISSING_EOD) { + ret = -EDEV_RW_PERM; + return ret; + } + else + ret = _filedebug_space_rec(state, count, false); + break; + case TC_SPACE_B: + ltfsmsg(LTFS_DEBUG, 30196D, "space back records", (unsigned long long)count); + if (state->current_position.block == MISSING_EOD) { + ret = -EDEV_RW_PERM; + return ret; + } + else + ret = _filedebug_space_rec(state, count, true); + break; + default: + ltfsmsg(LTFS_ERR, 30041E); + ret = -EDEV_INVALID_ARG; + return ret; } pos->block = state->current_position.block; - for(i = 0; i < state->current_position.block; ++i) { - char *fname; + for (i = 0; i < state->current_position.block; ++i) { + char* fname; fname = _filedebug_make_filename(state, state->current_position.partition, - i, rec_suffixes[SUFFIX_FILEMARK]); + i, rec_suffixes[SUFFIX_FILEMARK]); if (!fname) { ltfsmsg(LTFS_ERR, 30042E); ret = -EDEV_NO_MEMORY; @@ -1253,10 +1331,10 @@ int filedebug_space(void *device, size_t count, TC_SPACE_TYPE type, struct tc_po * partition. The erase function is unused externally, but this implementation will need to be * fixed if it is ever needed. */ -int filedebug_erase(void *device, struct tc_position *pos, bool long_erase) +int filedebug_erase(void* device, struct tc_position* pos, bool long_erase) { int ret; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30043E); @@ -1264,14 +1342,14 @@ int filedebug_erase(void *device, struct tc_position *pos, bool long_erase) } ltfsmsg(LTFS_DEBUG, 30044D, (unsigned long)state->current_position.partition); - pos->block = state->current_position.block; + pos->block = state->current_position.block; pos->filemarks = state->current_position.filemarks; ret = _filedebug_write_eod(state); return ret; } -static inline int _sanitize_tape(struct filedebug_data *state) +static inline int _sanitize_tape(struct filedebug_data* state) { int ret = 0; int gen = DRIVE_FAMILY_GEN(state->drive_type); @@ -1280,170 +1358,181 @@ static inline int _sanitize_tape(struct filedebug_data *state) if (state->drive_type == 0) { state->unsupported_tape = true; - } else if (gen == DRIVE_GEN_LTO5) { + } + else if (gen == DRIVE_GEN_LTO5) { switch (state->conf.cart_type) { - case TC_MP_LTO5D_CART: - /* Do nothing */ - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "LTO5", state->conf.cart_type); - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_LTO5D_CART: + /* Do nothing */ + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "LTO5", state->conf.cart_type); + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_LTO6) { + } + else if (gen == DRIVE_GEN_LTO6) { switch (state->conf.cart_type) { - case TC_MP_LTO5D_CART: - case TC_MP_LTO6D_CART: - /* Do nothing */ - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "LTO6", state->conf.cart_type); - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_LTO5D_CART: + case TC_MP_LTO6D_CART: + /* Do nothing */ + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "LTO6", state->conf.cart_type); + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_LTO7) { + } + else if (gen == DRIVE_GEN_LTO7) { switch (state->conf.cart_type) { - case TC_MP_LTO5D_CART: - case TC_MP_LTO6D_CART: - case TC_MP_LTO7D_CART: - /* Do nothing */ - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "LTO7", state->conf.cart_type); - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_LTO5D_CART: + case TC_MP_LTO6D_CART: + case TC_MP_LTO7D_CART: + /* Do nothing */ + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "LTO7", state->conf.cart_type); + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_LTO8) { + } + else if (gen == DRIVE_GEN_LTO8) { switch (state->conf.cart_type) { - case TC_MP_LTO7D_CART: - case TC_MP_LTO8D_CART: - /* Do nothing */ - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "LTO8", state->conf.cart_type); - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_LTO7D_CART: + case TC_MP_LTO8D_CART: + /* Do nothing */ + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "LTO8", state->conf.cart_type); + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_LTO9) { + } + else if (gen == DRIVE_GEN_LTO9) { switch (state->conf.cart_type) { - case TC_MP_LTO8D_CART: - case TC_MP_LTO9D_CART: - /* Do nothing */ - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "LTO9", state->conf.cart_type); - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_LTO8D_CART: + case TC_MP_LTO9D_CART: + /* Do nothing */ + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "LTO9", state->conf.cart_type); + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_LTOA) { + } + else if (gen == DRIVE_GEN_LTOA) { switch (state->conf.cart_type) { - case TC_MP_LTOPAD_CART: - case TC_MP_LTOAD_CART: - /* Do nothing */ - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "LTOA", state->conf.cart_type); - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_LTOPAD_CART: + case TC_MP_LTOAD_CART: + /* Do nothing */ + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "LTOA", state->conf.cart_type); + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } + } else if (gen == DRIVE_GEN_JAG4) { switch (state->conf.cart_type) { - case TC_MP_JB: - case TC_MP_JC: - case TC_MP_JK: - state->is_worm = false; - break; - case TC_MP_JX: - case TC_MP_JY: - state->is_worm = true; - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "TS1140", state->conf.cart_type); - state->is_worm = false; - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_JB: + case TC_MP_JC: + case TC_MP_JK: + state->is_worm = false; + break; + case TC_MP_JX: + case TC_MP_JY: + state->is_worm = true; + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "TS1140", state->conf.cart_type); + state->is_worm = false; + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_JAG5) { + } + else if (gen == DRIVE_GEN_JAG5) { switch (state->conf.cart_type) { - case TC_MP_JC: - case TC_MP_JK: - case TC_MP_JD: - case TC_MP_JL: - state->is_worm = false; - break; - case TC_MP_JY: - case TC_MP_JZ: - state->is_worm = true; - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "TS1150", state->conf.cart_type); - state->is_worm = false; - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_JC: + case TC_MP_JK: + case TC_MP_JD: + case TC_MP_JL: + state->is_worm = false; + break; + case TC_MP_JY: + case TC_MP_JZ: + state->is_worm = true; + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "TS1150", state->conf.cart_type); + state->is_worm = false; + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_JAG5A) { + } + else if (gen == DRIVE_GEN_JAG5A) { switch (state->conf.cart_type) { - case TC_MP_JC: - case TC_MP_JK: - case TC_MP_JD: - case TC_MP_JL: - state->is_worm = false; - break; - case TC_MP_JY: - case TC_MP_JZ: - state->is_worm = true; - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "TS1155", state->conf.cart_type); - state->is_worm = false; - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_JC: + case TC_MP_JK: + case TC_MP_JD: + case TC_MP_JL: + state->is_worm = false; + break; + case TC_MP_JY: + case TC_MP_JZ: + state->is_worm = true; + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "TS1155", state->conf.cart_type); + state->is_worm = false; + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_JAG6) { + } + else if (gen == DRIVE_GEN_JAG6) { switch (state->conf.cart_type) { - case TC_MP_JC: - case TC_MP_JK: - case TC_MP_JD: - case TC_MP_JL: - case TC_MP_JE: - case TC_MP_JM: - state->is_worm = false; - break; - case TC_MP_JY: - case TC_MP_JZ: - case TC_MP_JV: - state->is_worm = true; - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "TS1160", state->conf.cart_type); - state->is_worm = false; - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_JC: + case TC_MP_JK: + case TC_MP_JD: + case TC_MP_JL: + case TC_MP_JE: + case TC_MP_JM: + state->is_worm = false; + break; + case TC_MP_JY: + case TC_MP_JZ: + case TC_MP_JV: + state->is_worm = true; + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "TS1160", state->conf.cart_type); + state->is_worm = false; + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else if (gen == DRIVE_GEN_JAG7) { + } + else if (gen == DRIVE_GEN_JAG7) { switch (state->conf.cart_type) { - case TC_MP_JF: - state->is_worm = false; - break; - default: - ltfsmsg(LTFS_INFO, 30086I, "TS1170", state->conf.cart_type); - state->is_worm = false; - state->unsupported_tape = true; - ret = -EDEV_MEDIUM_FORMAT_ERROR; - break; + case TC_MP_JF: + state->is_worm = false; + break; + default: + ltfsmsg(LTFS_INFO, 30086I, "TS1170", state->conf.cart_type); + state->is_worm = false; + state->unsupported_tape = true; + ret = -EDEV_MEDIUM_FORMAT_ERROR; + break; } - } else { + } + else { ltfsmsg(LTFS_INFO, 30086I, "Unexpected Drive", state->conf.cart_type); state->is_worm = false; state->unsupported_tape = true; @@ -1455,25 +1544,25 @@ static inline int _sanitize_tape(struct filedebug_data *state) #define BARCODE_SIZE (36) -int filedebug_load(void *device, struct tc_position *pos) +int filedebug_load(void* device, struct tc_position* pos) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; int ret; unsigned int i; uint64_t wp; - char buf[BARCODE_SIZE], *dirlink, *config_file; + char buf[BARCODE_SIZE], * dirlink, * config_file; struct stat d; if (state->ready) { emulate_load_wait(state); state->current_position.partition = 0; - state->current_position.block = 0; + state->current_position.block = 0; state->current_position.filemarks = 0; - state->clear_by_pc = false; - state->force_writeperm = DEFAULT_WRITEPERM; - state->force_readperm = DEFAULT_READPERM; - state->write_counter = 0; - state->read_counter = 0; + state->clear_by_pc = false; + state->force_writeperm = DEFAULT_WRITEPERM; + state->force_readperm = DEFAULT_READPERM; + state->write_counter = 0; + state->read_counter = 0; return DEVICE_GOOD; /* already loaded the tape */ } @@ -1483,7 +1572,7 @@ int filedebug_load(void *device, struct tc_position *pos) if (ret < 0) return -EDEV_HARDWARE_ERROR; - ret = read(state->fd, buf, sizeof(buf)); + ret = arch_read(state->fd, buf, sizeof(buf)); if (ret != sizeof(buf)) { ltfsmsg(LTFS_ERR, 30045E, ""); return -EDEV_HARDWARE_ERROR; @@ -1491,15 +1580,15 @@ int filedebug_load(void *device, struct tc_position *pos) dirlink = buf; - if(dirlink[strlen(dirlink) - 1] == '\n') + if (dirlink[strlen(dirlink) - 1] == '\n') dirlink[strlen(dirlink) - 1] = '\0'; - if(!strcmp(dirlink, "empty")) { + if (!strcmp(dirlink, "empty")) { ltfsmsg(LTFS_INFO, 30046I, ""); return -EDEV_NO_MEDIUM; } - if(state->dirname) { + if (state->dirname) { free(state->dirname); state->dirname = NULL; } @@ -1521,14 +1610,14 @@ int filedebug_load(void *device, struct tc_position *pos) ltfsmsg(LTFS_INFO, 30048I, state->dirname); /* Load configuration of cartridge */ - ret = asprintf(&config_file, "%s/%s", state->dirname, CARTRIDGE_CONFIG ); + ret = asprintf(&config_file, "%s/%s", state->dirname, CARTRIDGE_CONFIG); if (ret < 0) { ltfsmsg(LTFS_ERR, 30049E, ret); return -EDEV_INTERNAL_ERROR; } ret = stat(config_file, &d); - if (! ret && S_ISDIR(d.st_mode)) { + if (!ret && S_ISDIR(d.st_mode)) { ltfsmsg(LTFS_ERR, 30050E, ret); free(config_file); return -EDEV_INTERNAL_ERROR; @@ -1536,7 +1625,7 @@ int filedebug_load(void *device, struct tc_position *pos) if (ret < 0 && errno == ENOENT) filedebug_conf_tc_write_xml(config_file, &state->conf); - else if (! ret) + else if (!ret) filedebug_conf_tc_read_xml(config_file, &state->conf); else { ltfsmsg(LTFS_ERR, 30051E, ret); @@ -1547,7 +1636,7 @@ int filedebug_load(void *device, struct tc_position *pos) free(config_file); state->ready = true; - state->unsupported_tape = false; + state->unsupported_tape = false; state->unsupported_format = false; /* Sanitize by cartridge type and configure WORM emulation flag */ @@ -1557,37 +1646,37 @@ int filedebug_load(void *device, struct tc_position *pos) } /* Configure internal read_only flag */ - ret = ibm_tape_is_mountable( state->drive_type, - NULL, - state->conf.cart_type, - state->conf.density_code, - false); - switch(ret) { - case MEDIUM_PERFECT_MATCH: - case MEDIUM_WRITABLE: - if (state->conf.emulate_readonly) - state->is_readonly = true; - else - state->is_readonly = false; - break; - case MEDIUM_READONLY: + ret = ibm_tape_is_mountable(state->drive_type, + NULL, + state->conf.cart_type, + state->conf.density_code, + false); + switch (ret) { + case MEDIUM_PERFECT_MATCH: + case MEDIUM_WRITABLE: + if (state->conf.emulate_readonly) state->is_readonly = true; - break; - case MEDIUM_CANNOT_ACCESS: - ltfsmsg(LTFS_INFO, 30088I, state->drive_type, state->conf.density_code); - state->unsupported_format = true; - if (IS_LTO(state->drive_type)) - return -EDEV_MEDIUM_FORMAT_ERROR; - break; - case MEDIUM_UNKNOWN: - case MEDIUM_PROBABLY_WRITABLE: - default: - /* Unexpected condition */ - return -LTFS_UNEXPECTED_VALUE; - break; - } - - for (i=0; iis_readonly = false; + break; + case MEDIUM_READONLY: + state->is_readonly = true; + break; + case MEDIUM_CANNOT_ACCESS: + ltfsmsg(LTFS_INFO, 30088I, state->drive_type, state->conf.density_code); + state->unsupported_format = true; + if (IS_LTO(state->drive_type)) + return -EDEV_MEDIUM_FORMAT_ERROR; + break; + case MEDIUM_UNKNOWN: + case MEDIUM_PROBABLY_WRITABLE: + default: + /* Unexpected condition */ + return -LTFS_UNEXPECTED_VALUE; + break; + } + + for (i = 0; i < MAX_PARTITIONS; ++i) { ret = filedebug_search_eod(state, i); if (ret < 0) { ltfsmsg(LTFS_ERR, 30052E, i, ret); @@ -1599,7 +1688,7 @@ int filedebug_load(void *device, struct tc_position *pos) _filedebug_remove_record(state, 1, state->eod[1]); state->current_position.partition = 0; - state->current_position.block = 0; + state->current_position.block = 0; state->current_position.filemarks = 0; if (state->eod[1] == 0) state->partitions = 1; @@ -1607,11 +1696,11 @@ int filedebug_load(void *device, struct tc_position *pos) state->partitions = MAX_PARTITIONS; pos->partition = state->current_position.partition; - pos->block = state->current_position.block; + pos->block = state->current_position.block; pos->filemarks = state->current_position.filemarks; wp = 0; - if(_get_wp(device, &wp) != 0) { + if (_get_wp(device, &wp) != 0) { ltfsmsg(LTFS_ERR, 30053E); return -EDEV_INTERNAL_ERROR; } @@ -1626,7 +1715,8 @@ int filedebug_load(void *device, struct tc_position *pos) state->p1_warning = calc_p1_cap(state) * 2; state->p0_p_warning = state->p0_warning / 2; state->p1_p_warning = state->p1_warning - state->p0_p_warning; - } else { + } + else { state->p0_warning = calc_p0_cap(state) * 2; state->p1_warning = 0; state->p0_p_warning = state->p0_warning * 2; @@ -1638,10 +1728,10 @@ int filedebug_load(void *device, struct tc_position *pos) return DEVICE_GOOD; } -int filedebug_unload(void *device, struct tc_position *pos) +int filedebug_unload(void* device, struct tc_position* pos) { - struct filedebug_data *state = (struct filedebug_data *)device; - char *config_file; + struct filedebug_data* state = (struct filedebug_data*)device; + char* config_file; int ret; /* Write EOD of DP here when dummy io mode is enabled */ @@ -1655,20 +1745,20 @@ int filedebug_unload(void *device, struct tc_position *pos) state->ready = false; state->current_position.partition = 0; - state->current_position.block = 0; + state->current_position.block = 0; state->current_position.filemarks = 0; - state->clear_by_pc = false; - state->force_writeperm = DEFAULT_WRITEPERM; - state->force_readperm = DEFAULT_READPERM; - state->write_counter = 0; - state->read_counter = 0; + state->clear_by_pc = false; + state->force_writeperm = DEFAULT_WRITEPERM; + state->force_readperm = DEFAULT_READPERM; + state->write_counter = 0; + state->read_counter = 0; pos->partition = state->current_position.partition; - pos->block = state->current_position.block; + pos->block = state->current_position.block; pos->filemarks = state->current_position.filemarks; /* Save configuration of cartridge */ - ret = asprintf(&config_file, "%s/%s", state->dirname, CARTRIDGE_CONFIG ); + ret = asprintf(&config_file, "%s/%s", state->dirname, CARTRIDGE_CONFIG); if (ret < 0) { ltfsmsg(LTFS_ERR, 30049E, ret); return -EDEV_INTERNAL_ERROR; @@ -1684,9 +1774,9 @@ int filedebug_unload(void *device, struct tc_position *pos) return DEVICE_GOOD; } -int filedebug_readpos(void *device, struct tc_position *pos) +int filedebug_readpos(void* device, struct tc_position* pos) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30054E); @@ -1694,7 +1784,7 @@ int filedebug_readpos(void *device, struct tc_position *pos) } pos->partition = state->current_position.partition; - pos->block = state->current_position.block; + pos->block = state->current_position.block; pos->filemarks = state->current_position.filemarks; ltfsmsg(LTFS_DEBUG, 30198D, "readpos", (unsigned long long)state->current_position.partition, @@ -1703,12 +1793,12 @@ int filedebug_readpos(void *device, struct tc_position *pos) return DEVICE_GOOD; } -int filedebug_setcap(void *device, uint16_t proportion) +int filedebug_setcap(void* device, uint16_t proportion) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; struct tc_position pos; - if(state->current_position.partition != 0 || + if (state->current_position.partition != 0 || state->current_position.block != 0) { ltfsmsg(LTFS_ERR, 30055E); @@ -1728,13 +1818,13 @@ int filedebug_setcap(void *device, uint16_t proportion) return DEVICE_GOOD; } -int filedebug_format(void *device, TC_FORMAT_TYPE format, const char *vol_name, const char *barcode_name, const char *vol_mam_uuid) +int filedebug_format(void* device, TC_FORMAT_TYPE format, const char* vol_name, const char* barcode_name, const char* vol_mam_uuid) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; struct tc_position pos; int ret = 0; - if(state->current_position.partition != 0 || + if (state->current_position.partition != 0 || state->current_position.block != 0) { ltfsmsg(LTFS_ERR, 30056E); @@ -1743,7 +1833,7 @@ int filedebug_format(void *device, TC_FORMAT_TYPE format, const char *vol_name, /* Emulate WORM */ if (state->is_worm && - (state->eod[0] != 0 || state->eod[1] != 0) ) { + (state->eod[0] != 0 || state->eod[1] != 0)) { return -EDEV_WRITE_PROTECTED_WORM;; } @@ -1759,17 +1849,17 @@ int filedebug_format(void *device, TC_FORMAT_TYPE format, const char *vol_name, return ret; } - switch(format){ - case TC_FORMAT_DEFAULT: - state->partitions = 1; - break; - case TC_FORMAT_PARTITION: - case TC_FORMAT_DEST_PART: - state->partitions = 2; - break; - default: - ltfsmsg(LTFS_ERR, 30057E); - return -EDEV_INVALID_ARG; + switch (format) { + case TC_FORMAT_DEFAULT: + state->partitions = 1; + break; + case TC_FORMAT_PARTITION: + case TC_FORMAT_DEST_PART: + state->partitions = 2; + break; + default: + ltfsmsg(LTFS_ERR, 30057E); + return -EDEV_INVALID_ARG; } /* erase all partitions */ @@ -1787,7 +1877,8 @@ int filedebug_format(void *device, TC_FORMAT_TYPE format, const char *vol_name, state->p1_warning = calc_p1_cap(state) * 2; state->p0_p_warning = state->p0_warning / 2; state->p1_p_warning = state->p1_warning - state->p0_p_warning; - } else { + } + else { state->p0_warning = calc_p0_cap(state) * 2; state->p1_warning = 0; state->p0_p_warning = state->p0_warning * 2; @@ -1797,64 +1888,65 @@ int filedebug_format(void *device, TC_FORMAT_TYPE format, const char *vol_name, return DEVICE_GOOD; } -int filedebug_remaining_capacity(void *device, struct tc_remaining_cap *cap) +int filedebug_remaining_capacity(void* device, struct tc_remaining_cap* cap) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30058E); return DEVICE_GOOD; } - if(state->partitions == 2) { - cap->max_p0 = calc_p0_cap(state); + if (state->partitions == 2) { + cap->max_p0 = calc_p0_cap(state); cap->remaining_p0 = calc_p0_remaining(state); - cap->max_p1 = calc_p1_cap(state); + cap->max_p1 = calc_p1_cap(state); cap->remaining_p1 = calc_p1_remaining(state); - } else { - cap->max_p0 = state->conf.capacity_mb; + } + else { + cap->max_p0 = state->conf.capacity_mb; cap->remaining_p0 = 0; - cap->max_p1 = 0; + cap->max_p1 = 0; cap->remaining_p1 = 0; } return DEVICE_GOOD; } -int filedebug_get_cartridge_health(void *device, struct tc_cartridge_health *cart_health) +int filedebug_get_cartridge_health(void* device, struct tc_cartridge_health* cart_health) { - cart_health->mounts = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->written_ds = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->write_temps = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->write_perms = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->read_ds = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->read_temps = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->read_perms = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->mounts = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->written_ds = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->write_temps = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->write_perms = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->read_ds = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->read_temps = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->read_perms = UNSUPPORTED_CARTRIDGE_HEALTH; cart_health->write_perms_prev = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->read_perms_prev = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->written_mbytes = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->read_mbytes = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->passes_begin = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->passes_middle = UNSUPPORTED_CARTRIDGE_HEALTH; - cart_health->tape_efficiency = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->read_perms_prev = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->written_mbytes = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->read_mbytes = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->passes_begin = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->passes_middle = UNSUPPORTED_CARTRIDGE_HEALTH; + cart_health->tape_efficiency = UNSUPPORTED_CARTRIDGE_HEALTH; return DEVICE_GOOD; } -int filedebug_get_tape_alert(void *device, uint64_t *tape_alert) +int filedebug_get_tape_alert(void* device, uint64_t* tape_alert) { *tape_alert = 0; return DEVICE_GOOD; } -int filedebug_clear_tape_alert(void *device, uint64_t tape_alert) +int filedebug_clear_tape_alert(void* device, uint64_t tape_alert) { return DEVICE_GOOD; } -int filedebug_get_xattr(void *device, const char *name, char **buf) +int filedebug_get_xattr(void* device, const char* name, char** buf) { - struct filedebug_data *state = (struct filedebug_data *) device; + struct filedebug_data* state = (struct filedebug_data*)device; int ret = -LTFS_NO_XATTR; if (!strcmp(name, "ltfs.vendor.IBM.seekLatency")) { @@ -1864,58 +1956,64 @@ int filedebug_get_xattr(void *device, const char *name, char **buf) if (ret < 0) { ltfsmsg(LTFS_ERR, 10001E, "get_xattr buffer"); ret = -LTFS_NO_MEMORY; - } else + } + else ret = DEVICE_GOOD; } return ret; } -int filedebug_set_xattr(void *device, const char *name, const char *buf, size_t size) +int filedebug_set_xattr(void* device, const char* name, const char* buf, size_t size) { - struct filedebug_data *state = (struct filedebug_data *) device; + struct filedebug_data* state = (struct filedebug_data*)device; int ret = -LTFS_NO_XATTR; uint64_t attr_val; - char *null_terminated; + char* null_terminated; int64_t perm_count = 0; if (!size) return -LTFS_BAD_ARG; null_terminated = calloc(1, size + 1); - if (! null_terminated) { + if (!null_terminated) { ltfsmsg(LTFS_ERR, 10001E, "ibmtape_set_xattr: null_term"); return -LTFS_NO_MEMORY; } memcpy(null_terminated, buf, size); - if (! strcmp(name, "ltfs.vendor.IBM.forceErrorWrite")) { + if (!strcmp(name, "ltfs.vendor.IBM.forceErrorWrite")) { perm_count = strtoll(null_terminated, NULL, 0); if (perm_count < 0) { state->force_writeperm = -perm_count; - state->clear_by_pc = true; - } else { + state->clear_by_pc = true; + } + else { state->force_writeperm = perm_count; - state->clear_by_pc = false; + state->clear_by_pc = false; } state->write_counter = 0; ret = DEVICE_GOOD; - } else if (! strcmp(name, "ltfs.vendor.IBM.forceErrorType")) { + } + else if (!strcmp(name, "ltfs.vendor.IBM.forceErrorType")) { state->force_errortype = strtol(null_terminated, NULL, 0); ret = DEVICE_GOOD; - } else if (! strcmp(name, "ltfs.vendor.IBM.forceErrorRead")) { + } + else if (!strcmp(name, "ltfs.vendor.IBM.forceErrorRead")) { perm_count = strtoll(null_terminated, NULL, 0); if (perm_count < 0) { state->force_readperm = -perm_count; - state->clear_by_pc = true; - } else { + state->clear_by_pc = true; + } + else { state->force_readperm = perm_count; - state->clear_by_pc = false; + state->clear_by_pc = false; } state->read_counter = 0; ret = DEVICE_GOOD; - } else if (!strcmp(name, "ltfs.vendor.IBM.seekLatency")) { + } + else if (!strcmp(name, "ltfs.vendor.IBM.seekLatency")) { attr_val = strtoull(null_terminated, NULL, 0); if ((attr_val == ULLONG_MAX && errno) || attr_val > 0) ret = -EDEV_INVALID_ARG; @@ -1931,16 +2029,16 @@ int filedebug_set_xattr(void *device, const char *name, const char *buf, size_t return ret; } -int filedebug_logsense(void *device, const uint8_t page, const uint8_t subpage, unsigned char *buf, const size_t size) +int filedebug_logsense(void* device, const uint8_t page, const uint8_t subpage, unsigned char* buf, const size_t size) { ltfsmsg(LTFS_ERR, 10007E, __FUNCTION__); return -EDEV_UNSUPPORTED_FUNCTION; } -int filedebug_modesense(void *device, const uint8_t page, const TC_MP_PC_TYPE pc, const uint8_t subpage, unsigned char *buf, const size_t size) +int filedebug_modesense(void* device, const uint8_t page, const TC_MP_PC_TYPE pc, const uint8_t subpage, unsigned char* buf, const size_t size) { uint16_t pews = 0; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; memset(buf, 0, size); @@ -1955,16 +2053,16 @@ int filedebug_modesense(void *device, const uint8_t page, const TC_MP_PC_TYPE pc pews = calc_p0_cap(state) / 2; buf[17] = subpage; buf[22] = (uint8_t)(pews >> 8 & 0xFF); - buf[23] = (uint8_t)(pews & 0xFF); + buf[23] = (uint8_t)(pews & 0xFF); } return DEVICE_GOOD; } -int filedebug_modeselect(void *device, unsigned char *buf, const size_t size) +int filedebug_modeselect(void* device, unsigned char* buf, const size_t size) { int ret = 0; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (buf[16] == TC_MP_READ_WRITE_CTRL && buf[26] != 0) { /* Update density code, if specific value is set */ @@ -1973,43 +2071,43 @@ int filedebug_modeselect(void *device, unsigned char *buf, const size_t size) /* TODO: Create a function to update state for read-only handling */ /* Recalculate read-only condition */ state->unsupported_format = false; - ret = ibm_tape_is_mountable( state->drive_type, - NULL, - state->conf.cart_type, - state->conf.density_code, - false); - switch(ret) { - case MEDIUM_PERFECT_MATCH: - case MEDIUM_WRITABLE: - if (state->conf.emulate_readonly) - state->is_readonly = true; - else - state->is_readonly = false; - break; - case MEDIUM_READONLY: + ret = ibm_tape_is_mountable(state->drive_type, + NULL, + state->conf.cart_type, + state->conf.density_code, + false); + switch (ret) { + case MEDIUM_PERFECT_MATCH: + case MEDIUM_WRITABLE: + if (state->conf.emulate_readonly) state->is_readonly = true; - break; - case MEDIUM_CANNOT_ACCESS: - ltfsmsg(LTFS_INFO, 30088I, state->drive_type, state->conf.density_code); - state->unsupported_format = true; - if (IS_LTO(state->drive_type)) - return -EDEV_MEDIUM_FORMAT_ERROR; - break; - case MEDIUM_UNKNOWN: - case MEDIUM_PROBABLY_WRITABLE: - default: - /* Unexpected condition */ - return -LTFS_UNEXPECTED_VALUE; - break; + else + state->is_readonly = false; + break; + case MEDIUM_READONLY: + state->is_readonly = true; + break; + case MEDIUM_CANNOT_ACCESS: + ltfsmsg(LTFS_INFO, 30088I, state->drive_type, state->conf.density_code); + state->unsupported_format = true; + if (IS_LTO(state->drive_type)) + return -EDEV_MEDIUM_FORMAT_ERROR; + break; + case MEDIUM_UNKNOWN: + case MEDIUM_PROBABLY_WRITABLE: + default: + /* Unexpected condition */ + return -LTFS_UNEXPECTED_VALUE; + break; } } return DEVICE_GOOD; } -int filedebug_reserve_unit(void *device) +int filedebug_reserve_unit(void* device) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (state->device_reserved) { ltfsmsg(LTFS_ERR, 30059E); return -EDEV_ILLEGAL_REQUEST; @@ -2018,16 +2116,16 @@ int filedebug_reserve_unit(void *device) return DEVICE_GOOD; } -int filedebug_release_unit(void *device) +int filedebug_release_unit(void* device) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; state->device_reserved = false; return DEVICE_GOOD; } -int filedebug_prevent_medium_removal(void *device) +int filedebug_prevent_medium_removal(void* device) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30060E); return -EDEV_NOT_READY; @@ -2036,9 +2134,9 @@ int filedebug_prevent_medium_removal(void *device) return DEVICE_GOOD; } -int filedebug_allow_medium_removal(void *device) +int filedebug_allow_medium_removal(void* device) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30061E); return -EDEV_NOT_READY; @@ -2047,11 +2145,11 @@ int filedebug_allow_medium_removal(void *device) return DEVICE_GOOD; } -int filedebug_read_attribute(void *device, const tape_partition_t part, const uint16_t id - , unsigned char *buf, const size_t size) +int filedebug_read_attribute(void* device, const tape_partition_t part, const uint16_t id + , unsigned char* buf, const size_t size) { - struct filedebug_data *state = (struct filedebug_data *)device; - char *fname; + struct filedebug_data* state = (struct filedebug_data*)device; + char* fname; int fd; ssize_t bytes_read; @@ -2061,40 +2159,41 @@ int filedebug_read_attribute(void *device, const tape_partition_t part, const ui fname = _filedebug_make_attrname(state, part, id); if (!fname) return -EDEV_NO_MEMORY; - fd = open(fname, O_RDONLY | O_BINARY); + arch_open(&fd, fname, O_RDONLY | O_BINARY, SHARE_FLAG_DENYNO, PERMISSION_READ); free(fname); if (fd < 0) { if (errno == ENOENT) { return -EDEV_INVALID_FIELD_CDB; - } else { + } + else { ltfsmsg(LTFS_WARN, 30062W, errno); return -EDEV_CM_PERM; } } /* TODO: return -EDEV_INVALID_ARG if buffer is too small to hold complete record? */ - bytes_read = read(fd, buf, size); - if(bytes_read == -1) { + bytes_read = arch_read(fd, buf, size); + if (bytes_read == -1) { ltfsmsg(LTFS_WARN, 30063W, errno); - close(fd); + arch_close(fd); return -EDEV_CM_PERM; } - close(fd); + arch_close(fd); return DEVICE_GOOD; } -int filedebug_write_attribute(void *device, const tape_partition_t part - , const unsigned char *buf, const size_t size) +int filedebug_write_attribute(void* device, const tape_partition_t part + , const unsigned char* buf, const size_t size) { - struct filedebug_data *state = (struct filedebug_data *)device; - char *fname; + struct filedebug_data* state = (struct filedebug_data*)device; + char* fname; int fd; ssize_t written; uint16_t id, attr_size; size_t i = 0; - while(size > i) + while (size > i) { id = ltfs_betou16(buf + i); attr_size = ltfs_betou16(buf + (i + 3)); @@ -2107,9 +2206,11 @@ int filedebug_write_attribute(void *device, const tape_partition_t part ltfsmsg(LTFS_ERR, 30064E); return -EDEV_NO_MEMORY; } - fd = open(fname, - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); + arch_open(&fd, fname, + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, + SHARE_FLAG_DENYRW, PERMISSION_READWRITE); + + free(fname); if (fd < 0) { ltfsmsg(LTFS_ERR, 30065E, errno); @@ -2117,13 +2218,13 @@ int filedebug_write_attribute(void *device, const tape_partition_t part } /* write and close the file */ - written = write(fd, buf, size); + written = arch_write(fd, buf, size); if (written < 0) { ltfsmsg(LTFS_ERR, 30066E, errno); - close(fd); + arch_close(fd); return -EDEV_CM_PERM; } - close(fd); + arch_close(fd); i += (attr_size + 5); /* Add header size of an attribute */ } @@ -2131,7 +2232,7 @@ int filedebug_write_attribute(void *device, const tape_partition_t part return DEVICE_GOOD; } -int filedebug_allow_overwrite(void *device, const struct tc_position pos) +int filedebug_allow_overwrite(void* device, const struct tc_position pos) { return DEVICE_GOOD; } @@ -2139,7 +2240,7 @@ int filedebug_allow_overwrite(void *device, const struct tc_position pos) /** * GRAO command is currently unsupported on this device */ -int filedebug_grao(void *device, unsigned char *buf, const uint32_t len) +int filedebug_grao(void* device, unsigned char* buf, const uint32_t len) { int ret = -EDEV_UNSUPPORETD_COMMAND; return ret; @@ -2148,25 +2249,25 @@ int filedebug_grao(void *device, unsigned char *buf, const uint32_t len) /** * RRAO command is currently unsupported on this device */ -int filedebug_rrao(void *device, unsigned char *buf, const uint32_t len, size_t *out_size) +int filedebug_rrao(void* device, unsigned char* buf, const uint32_t len, size_t* out_size) { int ret = -EDEV_UNSUPPORETD_COMMAND; return ret; } -int filedebug_get_eod_status(void *device, int partition) +int filedebug_get_eod_status(void* device, int partition) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; - if(state->eod[partition] == MISSING_EOD) + if (state->eod[partition] == MISSING_EOD) return EOD_MISSING; else return EOD_GOOD; } -int filedebug_set_compression(void *device, bool enable_compression, struct tc_position *pos) +int filedebug_set_compression(void* device, bool enable_compression, struct tc_position* pos) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; if (!state->ready) { ltfsmsg(LTFS_ERR, 30067E); return -EDEV_NOT_READY; @@ -2176,23 +2277,23 @@ int filedebug_set_compression(void *device, bool enable_compression, struct tc_p return DEVICE_GOOD; } -int filedebug_set_default(void *device) +int filedebug_set_default(void* device) { return DEVICE_GOOD; } -int filedebug_get_parameters(void *device, struct tc_drive_param *params) +int filedebug_get_parameters(void* device, struct tc_drive_param* params) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; - params->max_blksize = FILE_DEBUG_MAX_BLOCK_SIZE; + params->max_blksize = FILE_DEBUG_MAX_BLOCK_SIZE; - params->cart_type = state->conf.cart_type; - params->density = state->conf.density_code; + params->cart_type = state->conf.cart_type; + params->density = state->conf.density_code; - params->write_protect = 0; - if ( state->conf.emulate_readonly ) - params->write_protect |= VOL_PHYSICAL_WP; + params->write_protect = 0; + if (state->conf.emulate_readonly) + params->write_protect |= VOL_PHYSICAL_WP; /* TODO: Following field shall be implemented in the future */ //params->is_encrypted = false; @@ -2201,7 +2302,7 @@ int filedebug_get_parameters(void *device, struct tc_drive_param *params) return DEVICE_GOOD; } -const char *filedebug_default_device_name(void) +const char* filedebug_default_device_name(void) { return filedebug_default_device; } @@ -2211,20 +2312,20 @@ const char *filedebug_default_device_name(void) * returns 0 on success, negative value on error * on success, sets the tape position to EOD on the given partition. */ -int filedebug_search_eod(struct filedebug_data *state, int partition) +int filedebug_search_eod(struct filedebug_data* state, int partition) { - char *fname; + char* fname; size_t fname_len; int ret; int i; int f[3] = { 1, 1, 0 }; - DIR *dp; + DIR* dp; int p; tape_block_t b; - struct dirent *entry; + struct dirent* entry; state->current_position.partition = partition; - state->current_position.block = 0; + state->current_position.block = 0; /* loop until an EOD mark is found or no record is found */ while ((f[0] || f[1]) && !f[2]) { @@ -2236,8 +2337,8 @@ int filedebug_search_eod(struct filedebug_data *state, int partition) } fname_len = strlen(fname); - for (i=0; i<3; ++i) { - fname[fname_len-1] = rec_suffixes[i]; + for (i = 0; i < 3; ++i) { + fname[fname_len - 1] = rec_suffixes[i]; f[i] = _filedebug_check_file(fname); if (f[i] < 0) { ltfsmsg(LTFS_ERR, 30069E, f[i]); @@ -2251,18 +2352,18 @@ int filedebug_search_eod(struct filedebug_data *state, int partition) } --state->current_position.block; - if(!f[2] && state->current_position.block != 0) { + if (!f[2] && state->current_position.block != 0) { state->last_block[state->current_position.partition] = state->current_position.block; state->eod[state->current_position.partition] = MISSING_EOD; if (state->conf.dummy_io) { dp = opendir(state->dirname); - if (! dp) { + if (!dp) { ltfsmsg(LTFS_ERR, 30004E, state->dirname); return 0; } while ((entry = readdir(dp))) { - if ( entry->d_name[strlen(entry->d_name) - 1] == 'E') { + if (entry->d_name[strlen(entry->d_name) - 1] == 'E') { entry->d_name[strlen(entry->d_name) - 2] = '\0'; entry->d_name[1] = '\0'; p = atoi(entry->d_name); @@ -2282,7 +2383,8 @@ int filedebug_search_eod(struct filedebug_data *state, int partition) } closedir(dp); } - } else { + } + else { ret = _filedebug_write_eod(state); if (ret < 0) { ltfsmsg(LTFS_ERR, 30070E, ret); @@ -2298,15 +2400,15 @@ int filedebug_search_eod(struct filedebug_data *state, int partition) * update the EOD in the state variable. * Returns 0 on success, negative value on failure. */ -int _filedebug_write_eod(struct filedebug_data *state) +int _filedebug_write_eod(struct filedebug_data* state) { - char *fname; + char* fname; int fd; int ret; uint64_t i; bool remove_extra_rec = true; - if(state->eod[state->current_position.partition] == MISSING_EOD) + if (state->eod[state->current_position.partition] == MISSING_EOD) remove_extra_rec = false; /* remove any existing record at this position */ @@ -2322,18 +2424,20 @@ int _filedebug_write_eod(struct filedebug_data *state) ltfsmsg(LTFS_ERR, 30072E); return -EDEV_NO_MEMORY; } - fd = open(fname, - O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, - S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH); + arch_open(&fd, fname, + O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, + SHARE_FLAG_DENYRW + , PERMISSION_READWRITE + ); free(fname); - if (fd < 0 || close(fd) < 0) { + if (fd < 0 || arch_close(fd) < 0) { ltfsmsg(LTFS_ERR, 30073E, errno); return -EDEV_RW_PERM; } - if(remove_extra_rec) { + if (remove_extra_rec) { /* remove records following this position */ - for (i=state->current_position.block+1; i<=state->eod[state->current_position.partition]; ++i) { + for (i = state->current_position.block + 1; i <= state->eod[state->current_position.partition]; ++i) { ret = _filedebug_remove_record(state, state->current_position.partition, i); if (ret < 0) { ltfsmsg(LTFS_ERR, 30074E, ret); @@ -2350,21 +2454,21 @@ int _filedebug_write_eod(struct filedebug_data *state) /** * Delete the file associated with the current tape position. */ -int _filedebug_remove_current_record(const struct filedebug_data *state) +int _filedebug_remove_current_record(const struct filedebug_data* state) { return _filedebug_remove_record(state - , state->current_position.partition - , state->current_position.block); + , state->current_position.partition + , state->current_position.block); } /** * Delete the file associated with a given tape position. * @return 1 on successful delete, 0 if no file found, negative on error. */ -int _filedebug_remove_record(const struct filedebug_data *state, +int _filedebug_remove_record(const struct filedebug_data* state, int partition, uint64_t blknum) { - char *fname; + char* fname; size_t fname_len; int i; int ret; @@ -2376,9 +2480,9 @@ int _filedebug_remove_record(const struct filedebug_data *state, } fname_len = strlen(fname); - for (i=0; i<3; ++i) { - fname[fname_len-1] = rec_suffixes[i]; - ret = unlink(fname); + for (i = 0; i < 3; ++i) { + fname[fname_len - 1] = rec_suffixes[i]; + ret = arch_unlink(fname); if (ret < 0 && errno != ENOENT) { ltfsmsg(LTFS_ERR, 30076E, errno); free(fname); @@ -2395,19 +2499,20 @@ int _filedebug_remove_record(const struct filedebug_data *state, * This function is silent: callers are expected to report errors for themselves. * @return 1 on success, 0 if file does not exist, and -errno on error */ -int _filedebug_check_file(const char *fname) +int _filedebug_check_file(const char* fname) { int fd; int ret; - fd = open(fname, O_RDWR | O_BINARY); + arch_open(&fd, fname, O_RDWR | O_BINARY, SHARE_FLAG_DENYNO, 0); if (fd < 0) { if (errno == ENOENT) return 0; else return -EDEV_RW_PERM; - } else { - ret = close(fd); + } + else { + ret = arch_close(fd); if (ret < 0) return -EDEV_RW_PERM; else @@ -2418,12 +2523,12 @@ int _filedebug_check_file(const char *fname) /** * Call _filedebug_make_filename with the current tape position */ -char *_filedebug_make_current_filename(const struct filedebug_data *state, char type) +char* _filedebug_make_current_filename(const struct filedebug_data* state, char type) { return _filedebug_make_filename(state - , state->current_position.partition - , state->current_position.block - , type); + , state->current_position.partition + , state->current_position.block + , type); } /** @@ -2431,10 +2536,10 @@ char *_filedebug_make_current_filename(const struct filedebug_data *state, char * Returns a string on success or NULL on failure. The caller is responsible for freeing * the returned memory. Failure probably means asprintf couldn't allocate memory. */ -char *_filedebug_make_filename(const struct filedebug_data *state, +char* _filedebug_make_filename(const struct filedebug_data* state, int part, uint64_t pos, char type) { - char *fname; + char* fname; int ret; ret = asprintf(&fname, "%s/%d_%"PRIu64"_%c", state->dirname, part, pos, type); if (ret < 0) { @@ -2449,9 +2554,9 @@ char *_filedebug_make_filename(const struct filedebug_data *state, * Returns a string on success or NULL on failure. The caller is responsible for freeing * the returned memory. Failure probably means asprintf couldn't allocate memory. */ -char *_filedebug_make_attrname(const struct filedebug_data *state, int part, int id) +char* _filedebug_make_attrname(const struct filedebug_data* state, int part, int id) { - char *fname; + char* fname; int ret; ret = asprintf(&fname, "%s/attr_%d_%x", state->dirname, part, id); if (ret < 0) { @@ -2469,9 +2574,9 @@ char *_filedebug_make_attrname(const struct filedebug_data *state, int part, int * @param back true to skip backwards, false to skip forwards * @return 0 on success or a negative value on error */ -int _filedebug_space_fm(struct filedebug_data *state, uint64_t count, bool back) +int _filedebug_space_fm(struct filedebug_data* state, uint64_t count, bool back) { - char *fname; + char* fname; uint64_t fm_count = 0; int ret; @@ -2503,7 +2608,8 @@ int _filedebug_space_fm(struct filedebug_data *state, uint64_t count, bool back) if (ret < 0) { ltfsmsg(LTFS_ERR, 30079E, ret); return ret; - } else if (ret > 0) { + } + else if (ret > 0) { ++fm_count; if (fm_count == count) { if (!back) @@ -2518,7 +2624,8 @@ int _filedebug_space_fm(struct filedebug_data *state, uint64_t count, bool back) return -EDEV_BOP_DETECTED; } --state->current_position.block; - } else { + } + else { ++state->current_position.block; } } @@ -2535,9 +2642,9 @@ int _filedebug_space_fm(struct filedebug_data *state, uint64_t count, bool back) * @param back true to skip backwards, false to skip forwards * @return 0 on success or a negative value on error */ -int _filedebug_space_rec(struct filedebug_data *state, uint64_t count, bool back) +int _filedebug_space_rec(struct filedebug_data* state, uint64_t count, bool back) { - char *fname; + char* fname; uint64_t rec_count = 0; int ret; @@ -2574,7 +2681,8 @@ int _filedebug_space_rec(struct filedebug_data *state, uint64_t count, bool back return -EDEV_BOP_DETECTED; } --state->current_position.block; - } else { + } + else { ++state->current_position.block; } @@ -2585,7 +2693,7 @@ int _filedebug_space_rec(struct filedebug_data *state, uint64_t count, bool back } } -int _get_wp(struct filedebug_data *device, uint64_t *wp) +int _get_wp(struct filedebug_data* device, uint64_t* wp) { int ret; unsigned char wp_data[TC_MAM_PAGE_VCR_SIZE + TC_MAM_PAGE_HEADER_SIZE]; @@ -2594,8 +2702,8 @@ int _get_wp(struct filedebug_data *device, uint64_t *wp) *wp = 0; ret = filedebug_read_attribute(device, 0, TC_MAM_PAGE_VCR - , wp_data, sizeof(wp_data)); - if(ret == 0) + , wp_data, sizeof(wp_data)); + if (ret == 0) *wp = ltfs_betou32(wp_data + 5); else ret = _set_wp(device, (uint64_t)1); @@ -2603,7 +2711,7 @@ int _get_wp(struct filedebug_data *device, uint64_t *wp) return ret; } -int _set_wp(struct filedebug_data *device, uint64_t wp) +int _set_wp(struct filedebug_data* device, uint64_t wp) { int ret; unsigned char wp_data[TC_MAM_PAGE_VCR_SIZE + TC_MAM_PAGE_HEADER_SIZE]; @@ -2624,18 +2732,18 @@ int _set_wp(struct filedebug_data *device, uint64_t wp) */ #define DRIVE_FILE_PREFIX "Drive_" -int filedebug_get_device_list(struct tc_drive_info *buf, int count) +int filedebug_get_device_list(struct tc_drive_info* buf, int count) { - char *filename, *devdir, line[1024]; - FILE *infile; - DIR *dp; - struct dirent *entry; + char* filename, * devdir, line[1024]; + FILE* infile; + DIR* dp; + struct dirent* entry; int deventries = 0; - char *ser= NULL, *pid = NULL, *tmp; + char* ser = NULL, * pid = NULL, * tmp; int i; - if (! original_pid) { - original_pid = (long)getpid(); + if (!original_pid) { + original_pid = (long)arch_getpid(); } /* Create a file to indicate current directory of drive link (for tape file backend) */ @@ -2645,13 +2753,17 @@ int filedebug_get_device_list(struct tc_drive_info *buf, int count) return -LTFS_NO_MEMORY; } ltfsmsg(LTFS_INFO, 30081I, filename); - infile = fopen(filename, "r"); + arch_fopen(filename, "r", infile); + if (infile == NULL) { + return 1; + } if (!infile) { ltfsmsg(LTFS_INFO, 30082I, filename); return 0; - } else { + } + else { devdir = fgets(line, sizeof(line), infile); - if(devdir[strlen(devdir) - 1] == '\n') + if (devdir[strlen(devdir) - 1] == '\n') devdir[strlen(devdir) - 1] = '\0'; fclose(infile); free(filename); @@ -2659,7 +2771,7 @@ int filedebug_get_device_list(struct tc_drive_info *buf, int count) ltfsmsg(LTFS_INFO, 30083I, devdir); dp = opendir(devdir); - if (! dp) { + if (!dp) { ltfsmsg(LTFS_ERR, 30004E, devdir); return 0; } @@ -2668,9 +2780,9 @@ int filedebug_get_device_list(struct tc_drive_info *buf, int count) if (strncmp(entry->d_name, DRIVE_FILE_PREFIX, strlen(DRIVE_FILE_PREFIX))) continue; - if (buf && deventries < count) { - tmp = strdup(entry->d_name); - if (! *tmp) { + if (buf&& deventries < count) { + tmp = arch_strdup(entry->d_name); + if (!*tmp) { ltfsmsg(LTFS_ERR, 10001E, "filedebug_get_device_list"); return -ENOMEM; } @@ -2693,13 +2805,13 @@ int filedebug_get_device_list(struct tc_drive_info *buf, int count) snprintf(buf[deventries].serial_number, TAPE_SERIAL_LEN_MAX, "%s", ser); snprintf(buf[deventries].product_name, PRODUCT_NAME_LENGTH, "[%s]", pid); - buf[deventries].host = 0; + buf[deventries].host = 0; buf[deventries].channel = 0; - buf[deventries].target = 0; - buf[deventries].lun = -1; + buf[deventries].target = 0; + buf[deventries].lun = -1; ltfsmsg(LTFS_DEBUG, 30084D, buf[deventries].name, buf[deventries].vendor, - buf[deventries].model, buf[deventries].serial_number); + buf[deventries].model, buf[deventries].serial_number); free(tmp); } @@ -2712,47 +2824,47 @@ int filedebug_get_device_list(struct tc_drive_info *buf, int count) return deventries; } -int filedebug_set_key(void *device, const unsigned char *keyalias, const unsigned char *key) +int filedebug_set_key(void* device, const unsigned char* keyalias, const unsigned char* key) { return -EDEV_UNSUPPORTED_FUNCTION; } -int filedebug_get_keyalias(void *device, unsigned char **keyalias) +int filedebug_get_keyalias(void* device, unsigned char** keyalias) { return -EDEV_UNSUPPORTED_FUNCTION; } -int filedebug_takedump_drive(void *device, bool nonforced_dump) +int filedebug_takedump_drive(void* device, bool nonforced_dump) { /* Do nothing */ return DEVICE_GOOD; } -int filedebug_is_mountable(void *device, const char *barcode, const unsigned char cart_type, - const unsigned char density) +int filedebug_is_mountable(void* device, const char* barcode, const unsigned char cart_type, + const unsigned char density) { int ret; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; - ret = ibm_tape_is_mountable( state->drive_type, - barcode, - cart_type, - density, - global_data.strict_drive); + ret = ibm_tape_is_mountable(state->drive_type, + barcode, + cart_type, + density, + global_data.strict_drive); return ret; } -bool filedebug_is_readonly(void *device) +bool filedebug_is_readonly(void* device) { int ret; - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; - ret = ibm_tape_is_mountable( state->drive_type, - NULL, - state->conf.cart_type, - state->conf.density_code, - global_data.strict_drive); + ret = ibm_tape_is_mountable(state->drive_type, + NULL, + state->conf.cart_type, + state->conf.density_code, + global_data.strict_drive); if (ret == MEDIUM_READONLY) return true; @@ -2760,117 +2872,117 @@ bool filedebug_is_readonly(void *device) return false; } -int filedebug_get_worm_status(void *device, bool *is_worm) +int filedebug_get_worm_status(void* device, bool* is_worm) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; *is_worm = state->is_worm; return DEVICE_GOOD; } -int filedebug_get_serialnumber(void *device, char **result) +int filedebug_get_serialnumber(void* device, char** result) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; CHECK_ARG_NULL(device, -LTFS_NULL_ARG); CHECK_ARG_NULL(result, -LTFS_NULL_ARG); if (state->serial_number) - *result = strdup((const char *) state->serial_number); + *result = arch_strdup((const char*)state->serial_number); else - *result = strdup("DUMMY"); + *result = arch_strdup("DUMMY"); - if (! *result) + if (!*result) return -EDEV_NO_MEMORY; return DEVICE_GOOD; } -int filedebug_get_info(void *device, struct tc_drive_info *info) +int filedebug_get_info(void* device, struct tc_drive_info* info) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; memcpy(info, &state->info, sizeof(struct tc_drive_info)); return 0; } -int filedebug_set_profiler(void *device, char *work_dir, bool enable) +int filedebug_set_profiler(void* device, char* work_dir, bool enable) { /* Do nohting: file backend does not support profiler */ return 0; } -int filedebug_get_next_block_to_xfer(void *device, struct tc_position *pos) +int filedebug_get_next_block_to_xfer(void* device, struct tc_position* pos) { - struct filedebug_data *state = (struct filedebug_data *)device; + struct filedebug_data* state = (struct filedebug_data*)device; pos->partition = state->current_position.partition; - pos->block = state->current_position.block - THRESHOLD_FORCE_WRITE_NO_WRITE; + pos->block = state->current_position.block - THRESHOLD_FORCE_WRITE_NO_WRITE; return 0; } struct tape_ops filedebug_handler = { - .open = filedebug_open, - .reopen = filedebug_reopen, - .close = filedebug_close, - .close_raw = filedebug_close_raw, - .is_connected = filedebug_is_connected, - .inquiry = filedebug_inquiry, - .inquiry_page = filedebug_inquiry_page, - .test_unit_ready = filedebug_test_unit_ready, - .read = filedebug_read, - .write = filedebug_write, - .writefm = filedebug_writefm, - .rewind = filedebug_rewind, - .locate = filedebug_locate, - .space = filedebug_space, - .erase = filedebug_erase, - .load = filedebug_load, - .unload = filedebug_unload, - .readpos = filedebug_readpos, - .setcap = filedebug_setcap, - .format = filedebug_format, - .remaining_capacity = filedebug_remaining_capacity, - .logsense = filedebug_logsense, - .modesense = filedebug_modesense, - .modeselect = filedebug_modeselect, - .reserve_unit = filedebug_reserve_unit, - .release_unit = filedebug_release_unit, + .open = filedebug_open, + .reopen = filedebug_reopen, + .close = filedebug_close, + .close_raw = filedebug_close_raw, + .is_connected = filedebug_is_connected, + .inquiry = filedebug_inquiry, + .inquiry_page = filedebug_inquiry_page, + .test_unit_ready = filedebug_test_unit_ready, + .read = filedebug_read, + .write = filedebug_write, + .writefm = filedebug_writefm, + .rewind = filedebug_rewind, + .locate = filedebug_locate, + .space = filedebug_space, + .erase = filedebug_erase, + .load = filedebug_load, + .unload = filedebug_unload, + .readpos = filedebug_readpos, + .setcap = filedebug_setcap, + .format = filedebug_format, + .remaining_capacity = filedebug_remaining_capacity, + .logsense = filedebug_logsense, + .modesense = filedebug_modesense, + .modeselect = filedebug_modeselect, + .reserve_unit = filedebug_reserve_unit, + .release_unit = filedebug_release_unit, .prevent_medium_removal = filedebug_prevent_medium_removal, - .allow_medium_removal = filedebug_allow_medium_removal, - .write_attribute = filedebug_write_attribute, - .read_attribute = filedebug_read_attribute, - .allow_overwrite = filedebug_allow_overwrite, - .grao = filedebug_grao, - .rrao = filedebug_rrao, - .set_compression = filedebug_set_compression, - .set_default = filedebug_set_default, - .get_cartridge_health = filedebug_get_cartridge_health, - .get_tape_alert = filedebug_get_tape_alert, - .clear_tape_alert = filedebug_clear_tape_alert, - .get_xattr = filedebug_get_xattr, - .set_xattr = filedebug_set_xattr, - .get_parameters = filedebug_get_parameters, - .get_eod_status = filedebug_get_eod_status, - .get_device_list = filedebug_get_device_list, - .help_message = filedebug_help_message, - .parse_opts = filedebug_parse_opts, - .default_device_name = filedebug_default_device_name, - .set_key = filedebug_set_key, - .get_keyalias = filedebug_get_keyalias, - .takedump_drive = filedebug_takedump_drive, - .is_mountable = filedebug_is_mountable, - .get_worm_status = filedebug_get_worm_status, - .get_serialnumber = filedebug_get_serialnumber, - .get_info = filedebug_get_info, - .set_profiler = filedebug_set_profiler, + .allow_medium_removal = filedebug_allow_medium_removal, + .write_attribute = filedebug_write_attribute, + .read_attribute = filedebug_read_attribute, + .allow_overwrite = filedebug_allow_overwrite, + .grao = filedebug_grao, + .rrao = filedebug_rrao, + .set_compression = filedebug_set_compression, + .set_default = filedebug_set_default, + .get_cartridge_health = filedebug_get_cartridge_health, + .get_tape_alert = filedebug_get_tape_alert, + .clear_tape_alert = filedebug_clear_tape_alert, + .get_xattr = filedebug_get_xattr, + .set_xattr = filedebug_set_xattr, + .get_parameters = filedebug_get_parameters, + .get_eod_status = filedebug_get_eod_status, + .get_device_list = filedebug_get_device_list, + .help_message = filedebug_help_message, + .parse_opts = filedebug_parse_opts, + .default_device_name = filedebug_default_device_name, + .set_key = filedebug_set_key, + .get_keyalias = filedebug_get_keyalias, + .takedump_drive = filedebug_takedump_drive, + .is_mountable = filedebug_is_mountable, + .get_worm_status = filedebug_get_worm_status, + .get_serialnumber = filedebug_get_serialnumber, + .get_info = filedebug_get_info, + .set_profiler = filedebug_set_profiler, .get_next_block_to_xfer = filedebug_get_next_block_to_xfer, - .is_readonly = filedebug_is_readonly, + .is_readonly = filedebug_is_readonly, }; -struct tape_ops *tape_dev_get_ops(void) +struct tape_ops* tape_dev_get_ops(void) { return &filedebug_handler; } @@ -2879,12 +2991,12 @@ struct tape_ops *tape_dev_get_ops(void) extern char tape_generic_file_dat[]; #endif -const char *tape_dev_get_message_bundle_name(void **message_data) +const char* tape_dev_get_message_bundle_name(void** message_data) { #ifndef mingw_PLATFORM - *message_data = tape_generic_file_dat; + * message_data = tape_generic_file_dat; #else - *message_data = NULL; + * message_data = NULL; #endif return "tape_generic_file"; } diff --git a/src/tape_drivers/generic/itdtimg/itdtimg_tc.c b/src/tape_drivers/generic/itdtimg/itdtimg_tc.c index 133df2c0..ac6d97a6 100644 --- a/src/tape_drivers/generic/itdtimg/itdtimg_tc.c +++ b/src/tape_drivers/generic/itdtimg/itdtimg_tc.c @@ -243,7 +243,7 @@ int itdtimage_open(const char *name, void **handle) _itdtimage_free(state); return -EDEV_DEVICE_UNOPENABLE; } - state->filename = strdup(name); + state->filename = arch_strdup(name); if ( !state->filename ) { ltfsmsg(LTFS_ERR, 10001E, "itdtimage_open: filename"); _itdtimage_free(state); @@ -1369,9 +1369,9 @@ int itdtimage_get_device_list(struct tc_drive_info *buf, int count) if (buf && deventries < count) { snprintf(buf[deventries].name, TAPE_DEVNAME_LEN_MAX, "%s/%s", devdir, entry->d_name); - strncpy(buf[deventries].vendor, "DUMMY", TAPE_VENDOR_NAME_LEN_MAX); - strncpy(buf[deventries].model, "DUMMYDEV", TAPE_MODEL_NAME_LEN_MAX); - strncpy(buf[deventries].serial_number, &(entry->d_name[strlen(DRIVE_FILE_PREFIX)]), TAPE_SERIAL_LEN_MAX); + arch_strncpy_auto(buf[deventries].vendor, "DUMMY", TAPE_VENDOR_NAME_LEN_MAX); + arch_strncpy_auto(buf[deventries].model, "DUMMYDEV", TAPE_MODEL_NAME_LEN_MAX); + arch_strncpy_auto(buf[deventries].serial_number, &(entry->d_name[strlen(DRIVE_FILE_PREFIX)]), TAPE_SERIAL_LEN_MAX); ltfsmsg(LTFS_DEBUG, 31030D, buf[deventries].name, buf[deventries].vendor, buf[deventries].model, buf[deventries].serial_number); } @@ -1427,9 +1427,9 @@ int itdtimage_get_serialnumber(void *vstate, char **result) CHECK_ARG_NULL(result, -LTFS_NULL_ARG); if (state->serial_number) - *result = strdup((const char *) state->serial_number); + *result = arch_strdup((const char *) state->serial_number); else - *result = strdup("DUMMY"); + *result = arch_strdup("DUMMY"); if (! *result) return -EDEV_NO_MEMORY; diff --git a/src/tape_drivers/ibm_tape.c b/src/tape_drivers/ibm_tape.c index 52c29261..7783e4ad 100644 --- a/src/tape_drivers/ibm_tape.c +++ b/src/tape_drivers/ibm_tape.c @@ -1085,7 +1085,7 @@ static inline unsigned char _assume_cartridge_type(char product, char btype) return ctype; } -unsigned char ibm_tape_assume_cart_type(const char* type_name) +unsigned char ibm_tape_assume_cart_type(const char *type_name) { unsigned char c_type = 0; @@ -1099,7 +1099,7 @@ unsigned char ibm_tape_assume_cart_type(const char* type_name) return c_type; } -char* ibm_tape_assume_cart_name(unsigned char type) +char *ibm_tape_assume_cart_name(unsigned char type) { char *name = NULL; @@ -1283,7 +1283,7 @@ int ibm_tape_genkey(unsigned char *key) #ifdef mingw_PLATFORM memset(key, 0x00, KEYLEN); *key = KEY_PREFIX_HOST; - strncpy(key + 1, "WINLTFS", KEYLEN - 1); + arch_strncpy_auto(key + 1, "WINLTFS", KEYLEN - 1); #else unsigned char host[KEYLEN]; diff --git a/src/tape_drivers/ibm_tape.h b/src/tape_drivers/ibm_tape.h index 289b035f..a2cc6b14 100644 --- a/src/tape_drivers/ibm_tape.h +++ b/src/tape_drivers/ibm_tape.h @@ -172,8 +172,8 @@ int ibm_tape_init_timeout(struct timeout_tape **table, int type); #define ibm_tape_destroy_timeout destroy_timeout #define ibm_tape_get_timeout get_timeout -unsigned char ibm_tape_assume_cart_type(const char* type_name); -char* ibm_tape_assume_cart_name(unsigned char type); +unsigned char ibm_tape_assume_cart_type(const char *type_name); +char *ibm_tape_assume_cart_name(unsigned char type); int ibm_tape_is_mountable(const int drive_type, const char *barcode, diff --git a/src/tape_drivers/tape_drivers.h b/src/tape_drivers/tape_drivers.h index 2b1b5dd2..a717c59a 100644 --- a/src/tape_drivers/tape_drivers.h +++ b/src/tape_drivers/tape_drivers.h @@ -86,7 +86,7 @@ typedef void (*crc_enc)(void *buf, size_t n); typedef int (*crc_check)(void *buf, size_t n); -typedef void* (*memcpy_crc_enc)(void *dest, const void *src, size_t n); +typedef void *(*memcpy_crc_enc)(void *dest, const void *src, size_t n); typedef int (*memcpy_crc_check)(void *dest, const void *src, size_t n); diff --git a/src/tape_drivers/vendor_compat.c b/src/tape_drivers/vendor_compat.c index 4f49a9ac..6393b63e 100644 --- a/src/tape_drivers/vendor_compat.c +++ b/src/tape_drivers/vendor_compat.c @@ -281,7 +281,7 @@ struct error_table standard_tape_errors[] = { {0xFFFFFF, -EDEV_UNKNOWN, "Unknown Error code"}, }; -int get_vendor_id(char* vendor) +int get_vendor_id(char *vendor) { if (!strncmp(vendor, IBM_VENDOR_ID, strlen(IBM_VENDOR_ID))) return VENDOR_IBM; diff --git a/src/tape_drivers/vendor_compat.h b/src/tape_drivers/vendor_compat.h index e6eec141..b768681c 100644 --- a/src/tape_drivers/vendor_compat.h +++ b/src/tape_drivers/vendor_compat.h @@ -65,7 +65,7 @@ extern "C" { extern struct error_table standard_tape_errors[]; -int get_vendor_id(char* vendor); +int get_vendor_id(char *vendor); struct supported_device **get_supported_devs(int vendor); bool drive_has_supported_fw(int vendor, int drive_type, const unsigned char * const revision); unsigned char assume_cart_type(const unsigned char dc); diff --git a/src/utils/ltfsck.c b/src/utils/ltfsck.c index d8e2b296..de4da52d 100644 --- a/src/utils/ltfsck.c +++ b/src/utils/ltfsck.c @@ -3,7 +3,7 @@ ** OO_Copyright_BEGIN ** ** -** Copyright 2010, 2020 IBM Corp. All rights reserved. +** Copyright 2010, 2022 IBM Corp. All rights reserved. ** ** Redistribution and use in source and binary forms, with or without ** modification, are permitted provided that the following conditions @@ -48,10 +48,11 @@ */ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #else #include #include +#include #endif /* mingw_PLATFORM */ #include @@ -59,7 +60,6 @@ #include "libltfs/ltfs_fuse_version.h" #include -#include #include "libltfs/ltfs_internal.h" #include "libltfs/ltfs.h" #include "ltfs_copyright.h" @@ -68,8 +68,11 @@ #include "libltfs/arch/time_internal.h" #include "libltfs/kmi.h" +#ifdef mingw_PLATFORM +static +#endif volatile char *copyright = LTFS_COPYRIGHT_0"\n"LTFS_COPYRIGHT_1"\n"LTFS_COPYRIGHT_2"\n" \ - LTFS_COPYRIGHT_3"\n"LTFS_COPYRIGHT_4"\n"LTFS_COPYRIGHT_5"\n"; +LTFS_COPYRIGHT_3"\n"LTFS_COPYRIGHT_4"\n"LTFS_COPYRIGHT_5"\n"; #ifdef __APPLE__ #include "libltfs/arch/osx/osx_string.h" @@ -96,7 +99,7 @@ enum { }; struct other_check_opts { - struct config_file *config; /**< Configurate data read from the global LTFS config file. */ + struct config_file* config; /**< Configurate data read from the global LTFS config file. */ char *devname; /**< Device to format */ char *backend_path; /**< Path to tape backend shared library */ char *kmi_backend_name; /**< Name or path to the key manager interface backend library */ @@ -187,6 +190,9 @@ static struct option long_options[] = { {0, 0, 0, 0} }; +#ifdef mingw_PLATFORM +static +#endif void show_usage(char *appname, struct config_file *config, bool full) { ltfsresult(16400I, appname); /* Usage: %s [options] filesys */ @@ -230,7 +236,7 @@ int main(int argc, char **argv) struct ltfs_volume *vol; struct other_check_opts opt; int ret, log_level, syslog_level, i, cmd_args_len; - char *lang, *cmd_args; + char *lang = NULL, *cmd_args; const char *config_file = NULL; void *message_handle; @@ -240,7 +246,7 @@ int main(int argc, char **argv) return LTFSCK_OPERATIONAL_ERROR; } for (i = 0; i < fuse_argc; ++i) { - fuse_argv[i] = strdup(argv[i]); + fuse_argv[i] = arch_strdup(argv[i]); if (! fuse_argv[i]) { return LTFSCK_OPERATIONAL_ERROR; } @@ -248,7 +254,7 @@ int main(int argc, char **argv) struct fuse_args args = FUSE_ARGS_INIT(fuse_argc, fuse_argv); /* Check for LANG variable and set it to en_US.UTF-8 if it is unset. */ - lang = getenv("LANG"); + arch_getenv(lang,"LANG"); if (! lang) { fprintf(stderr, "LTFS9015W Setting the locale to 'en_US.UTF-8'. If this is wrong, please set the LANG environment variable before starting ltfsck.\n"); ret = setenv("LANG", "en_US.UTF-8", 1); @@ -297,7 +303,7 @@ int main(int argc, char **argv) if (c == -1) break; if (c == 'i') { - config_file = strdup(optarg); + config_file = arch_strdup(optarg); break; } } @@ -329,13 +335,13 @@ int main(int argc, char **argv) case 'i': break; case 'e': - opt.backend_path = strdup(optarg); + opt.backend_path = arch_strdup(optarg); break; case 'g': if(opt.op_mode == MODE_CHECK) opt.op_mode = MODE_VERIFY; opt.search_mode = SEARCH_BY_GEN; - opt.str_gen = strdup(optarg); + opt.str_gen = arch_strdup(optarg); break; case 'v': if ( strcmp(optarg, "forward") == 0) @@ -346,7 +352,7 @@ int main(int argc, char **argv) opt.traverse_mode = TRAVERSE_UNKNOWN; break; case '-': - opt.kmi_backend_name = strdup(optarg); + opt.kmi_backend_name = arch_strdup(optarg); break; case '+': opt.op_mode = MODE_LIST_POINT; @@ -416,14 +422,14 @@ int main(int argc, char **argv) ltfsmsg(LTFS_ERR, 10009E); return LTFSCK_OPERATIONAL_ERROR; } - opt.backend_path = strdup(default_backend); + opt.backend_path = arch_strdup(default_backend); } if (! opt.kmi_backend_name) { const char *default_backend = config_file_get_default_plugin("kmi", opt.config); if (default_backend) - opt.kmi_backend_name = strdup(default_backend); + opt.kmi_backend_name = arch_strdup(default_backend); else - opt.kmi_backend_name = strdup("none"); + opt.kmi_backend_name = arch_strdup("none"); } if (opt.kmi_backend_name && strcmp(opt.kmi_backend_name, "none") == 0) opt.kmi_backend_name = NULL; @@ -465,17 +471,17 @@ int main(int argc, char **argv) ltfsmsg(LTFS_ERR, 10001E, "ltfsck (arguments)"); return LTFSCK_OPERATIONAL_ERROR; } - strcat(cmd_args, argv[0]); + arch_strcat(cmd_args, cmd_args_len, argv[0]); for (i = 1; i < argc; i++) { - strcat(cmd_args, " "); - strcat(cmd_args, argv[i]); + arch_strcat(cmd_args, cmd_args_len, " "); + arch_strcat(cmd_args, cmd_args_len, argv[i]); } ltfsmsg(LTFS_INFO, 16088I, cmd_args); free(cmd_args); /* Show build time information */ ltfsmsg(LTFS_INFO, 16089I, BUILD_SYS_FOR); - ltfsmsg(LTFS_INFO, 16090I, BUILD_SYS_GCC); + //ltfsmsg(LTFS_INFO, 16090I, BUILD_SYS_GCC); /* Show run time information */ show_runtime_system_info(); @@ -488,9 +494,9 @@ int main(int argc, char **argv) } if(argv[optind + num_of_o]) - opt.devname = strdup(argv[optind + num_of_o]); + opt.devname = arch_strdup(argv[optind + num_of_o]); - opt.prg_name = strdup(argv[0]); + opt.prg_name = arch_strdup(argv[0]); if (_ltfsck_validate_options(&opt)) { ltfsmsg(LTFS_ERR, 16002E); @@ -504,10 +510,10 @@ int main(int argc, char **argv) ret = ltfsck(vol, &opt, &args); - free(opt.prg_name); - free(opt.backend_path); - free(opt.kmi_backend_name); - free(opt.devname); + arch_safe_free(opt.prg_name); + arch_safe_free(opt.backend_path); + arch_safe_free(opt.kmi_backend_name); + arch_safe_free(opt.devname); config_file_free(opt.config); ltfsprintf_unload_plugin(message_handle); ltfs_finish(); @@ -711,6 +717,7 @@ int check_ltfs_volume(struct ltfs_volume *vol, struct other_check_opts *opt) return LTFSCK_UNCORRECTED; } else { print_criteria_info(vol); + ltfs_unmount(SYNC_CHECK, vol); ltfsmsg(LTFS_INFO, 16022I); return LTFSCK_CORRECTED; @@ -751,7 +758,7 @@ void _store_index(struct index_info *dst, struct ltfs_index *src) dst->selfptr = src->selfptr; dst->backptr = src->backptr; if(src->commit_message) - dst->commit_message= strdup(src->commit_message); + dst->commit_message= arch_strdup(src->commit_message); dst->next = NULL; } #endif @@ -837,64 +844,68 @@ void _print_index_header(bool full_info) printf("-------------------------------------------------------------------------------\n"); } -void _print_index(struct ltfs_volume *vol, struct index_info *list, struct other_check_opts *opt) +void _print_index(struct ltfs_volume* vol, struct index_info* list, struct other_check_opts* opt) { - struct tm *t_st; + struct tm* t_st; int i; - if(!opt) + if (!opt) return; t_st = get_localtime(&list->mod_time.tv_sec); - if (list->generation == (unsigned int)-1){ + if (list->generation == (unsigned int)-1) { printf("%s: %04d-%02d-%02d %02d:%02d:%02d.%09ld %s (%d, %"PRIu64")->(\?\?, \?\?)\n", - " WRONG VER", 0, 0, 0, - 0, 0, 0, (unsigned long) 0, "---", - ltfs_part_id2num(list->selfptr.partition, vol), list->selfptr.block); - } else if(list->backptr.partition == 0 && + " WRONG VER", 0, 0, 0, + 0, 0, 0, (unsigned long)0, "---", + ltfs_part_id2num(list->selfptr.partition, vol), list->selfptr.block); + } + else if (list->backptr.partition == 0 && list->backptr.block == 0) { printf("%10d: %04d-%02d-%02d %02d:%02d:%02d.%09ld %s (%d, %"PRIu64") <>\n", - list->generation, t_st->tm_year+1900, t_st->tm_mon+1, t_st->tm_mday, + list->generation, t_st->tm_year + 1900, t_st->tm_mon + 1, t_st->tm_mday, #ifdef mingw_PLATFORM - t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, " ", + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, " ", #else - t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, t_st->tm_zone, + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, t_st->tm_zone, #endif - ltfs_part_id2num(list->selfptr.partition, vol), list->selfptr.block); - } else { + ltfs_part_id2num(list->selfptr.partition, vol), list->selfptr.block); + } + else { printf("%10d: %04d-%02d-%02d %02d:%02d:%02d.%09ld %s (%d, %"PRIu64")->(%d, %"PRIu64")\n", - list->generation, t_st->tm_year+1900, t_st->tm_mon+1, t_st->tm_mday, + list->generation, t_st->tm_year + 1900, t_st->tm_mon + 1, t_st->tm_mday, #ifdef mingw_PLATFORM - t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, " ", + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, " ", #else - t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, t_st->tm_zone, + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, t_st->tm_zone, #endif - ltfs_part_id2num(list->selfptr.partition, vol), list->selfptr.block, - ltfs_part_id2num(list->backptr.partition, vol), list->backptr.block); + ltfs_part_id2num(list->selfptr.partition, vol), list->selfptr.block, + ltfs_part_id2num(list->backptr.partition, vol), list->backptr.block); } /* Print UTC time */ if (list->generation == (unsigned int)-1) { printf(" (%04d-%02d-%02d %02d:%02d:%02d.%09ld %s)\n", - 0, 0, 0, - 0, 0, 0, (unsigned long) 0, "---"); + 0, 0, 0, + 0, 0, 0, (unsigned long)0, "---"); #ifdef mingw_PLATFORM - } else if(strcmp(get_local_timezone(), TIMEZONE_UTC) != 0) { + } + else if (strcmp(get_local_timezone(), TIMEZONE_UTC) != 0) { t_st = get_gmtime(&list->mod_time.tv_sec); printf(" (%04d-%02d-%02d %02d:%02d:%02d.%09ld)\n", - t_st->tm_year+1900, t_st->tm_mon+1, t_st->tm_mday, - t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec); + t_st->tm_year + 1900, t_st->tm_mon + 1, t_st->tm_mday, + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec); #else - }else if(strcmp(t_st->tm_zone, "UTC") != 0) { + } + else if (strcmp(t_st->tm_zone, "UTC") != 0) { const char *tz = getenv("TZ"); setenv("TZ", "", 1); tzset(); t_st = get_localtime(&list->mod_time.tv_sec); printf(" (%04d-%02d-%02d %02d:%02d:%02d.%09ld %s)\n", - t_st->tm_year+1900, t_st->tm_mon+1, t_st->tm_mday, - t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, t_st->tm_zone); - if(tz) + t_st->tm_year + 1900, t_st->tm_mon + 1, t_st->tm_mday, + t_st->tm_hour, t_st->tm_min, t_st->tm_sec, list->mod_time.tv_nsec, t_st->tm_zone); + if (tz) setenv("TZ", tz, 1); else unsetenv("TZ"); @@ -904,33 +915,34 @@ void _print_index(struct ltfs_volume *vol, struct index_info *list, struct other if (opt->full_index_info) { printf(" %d.%d.%d, \"%s\"\n", - list->version / 10000, (list->version / 100) % 100, list->version % 100, - list->creator); + list->version / 10000, (list->version / 100) % 100, list->version % 100, + list->creator); - if(list->volume_name) + if (list->volume_name) printf(" %s\n", list->volume_name); else printf(" No Volume Name\n"); - if(list->criteria && list->criteria->have_criteria) { + if (list->criteria && list->criteria->have_criteria) { printf(" [%s] ", list->criteria_allow_update ? " Allowed " : "Not allowed"); printf("%llu ", (long long unsigned int)list->criteria->max_filesize_criteria); - if(list->criteria->glob_patterns) { + if (list->criteria->glob_patterns) { i = 0; - while(1) { - if(list->criteria->glob_patterns[i].name == NULL) + while (1) { + if (list->criteria->glob_patterns[i].name == NULL) break; printf("%s ", list->criteria->glob_patterns[i].name); i++; } } printf("\n"); - } else + } + else printf(" No criteria\n"); } - if(list->commit_message) + if (list->commit_message) printf(" %s\n", list->commit_message); else printf(" No commit message\n"); @@ -968,7 +980,7 @@ void print_index_array(struct ltfs_volume *vol, struct index_info *list, void *o while (cur) { _print_index(vol, cur, opt); - cur = cur-> next; + cur = cur->next; } return; @@ -1350,13 +1362,13 @@ int rollback(struct ltfs_volume *vol, struct other_check_opts *opt) return ret; } -int list_rollback_points_normal(struct ltfs_volume *vol, struct other_check_opts *opt) +int list_rollback_points_normal(struct ltfs_volume* vol, struct other_check_opts* opt) { int ret = LTFSCK_NO_ERRORS; /* Load tape and read labels */ ret = load_tape(vol); - if (ret != LTFSCK_NO_ERRORS) { + if (ret != LTFSCK_NO_ERRORS) { ltfsmsg(LTFS_ERR, 16074E, ret); return ret; } @@ -1364,7 +1376,7 @@ int list_rollback_points_normal(struct ltfs_volume *vol, struct other_check_opts /* Attempt to mount a medium to confirm the consistency */ ret = ltfs_mount(false, false, false, false, 0, vol); if (ret < 0) { - if(ret == -LTFS_BOTH_EOD_MISSING) + if (ret == -LTFS_BOTH_EOD_MISSING) ltfsmsg(LTFS_WARN, 16096W); else { ltfsmsg(LTFS_ERR, 16087E); @@ -1375,31 +1387,30 @@ int list_rollback_points_normal(struct ltfs_volume *vol, struct other_check_opts _print_index_header(opt->full_index_info); /* read index from the index partition */ - if(opt->traverse_mode == TRAVERSE_FORWARD) + if (opt->traverse_mode == TRAVERSE_FORWARD) ret = ltfs_traverse_index_forward(vol, ltfs_ip_id(vol), opt->point_gen, - print_a_index_noheader, NULL, (void *)opt); + print_a_index_noheader, NULL, (void*)opt); else ret = ltfs_traverse_index_backward(vol, ltfs_ip_id(vol), opt->point_gen, - print_a_index_noheader, NULL, (void *)opt); - if (ret != LTFSCK_NO_ERRORS) { + print_a_index_noheader, NULL, (void*)opt); + if (ret != LTFSCK_NO_ERRORS) { ltfsmsg(LTFS_ERR, 16075E, ret); return ret; } /* read index from the data partition */ - if(opt->traverse_mode == TRAVERSE_FORWARD) + if (opt->traverse_mode == TRAVERSE_FORWARD) ret = ltfs_traverse_index_forward(vol, ltfs_dp_id(vol), opt->point_gen, - print_a_index_noheader, NULL, (void *)opt); + print_a_index_noheader, NULL, (void*)opt); else ret = ltfs_traverse_index_backward(vol, ltfs_dp_id(vol), opt->point_gen, - print_a_index_noheader, NULL, (void *)opt); - if (ret != LTFSCK_NO_ERRORS) { + print_a_index_noheader, NULL, (void*)opt); + if (ret != LTFSCK_NO_ERRORS) { ltfsmsg(LTFS_ERR, 16076E, ret); } return ret; } - int list_rollback_points_no_eod(struct ltfs_volume *vol, struct other_check_opts *opt) { int ret = LTFSCK_NO_ERRORS; @@ -1450,9 +1461,9 @@ int list_rollback_points(struct ltfs_volume *vol, struct other_check_opts *opt) return ret; } -int _ltfsck_validate_options(struct other_check_opts *opt) +int _ltfsck_validate_options(struct other_check_opts* opt) { - if(opt->op_mode == MODE_VERIFY || opt->op_mode == MODE_ROLLBACK) { + if (opt->op_mode == MODE_VERIFY || opt->op_mode == MODE_ROLLBACK) { if (!opt->str_gen) { ltfsmsg(LTFS_ERR, 16003E); return LTFSCK_USAGE_SYNTAX_ERROR; @@ -1462,11 +1473,12 @@ int _ltfsck_validate_options(struct other_check_opts *opt) if (!opt->str_gen) { ltfsmsg(LTFS_ERR, 16004E); return LTFSCK_USAGE_SYNTAX_ERROR; - } else { + } + else { char *invalid_start; errno = 0; opt->point_gen = strtoul(opt->str_gen, &invalid_start, 0); - if( (*invalid_start == '\0') && opt->str_gen ) + if ((*invalid_start == '\0') && opt->str_gen) ltfsmsg(LTFS_INFO, 16006I, opt->point_gen); else { ltfsmsg(LTFS_ERR, 16005E, opt->str_gen); @@ -1481,8 +1493,8 @@ int _ltfsck_validate_options(struct other_check_opts *opt) return LTFSCK_USAGE_SYNTAX_ERROR; } - if(opt->op_mode == MODE_LIST_POINT) { - if ( opt->traverse_mode == TRAVERSE_FORWARD ) + if (opt->op_mode == MODE_LIST_POINT) { + if (opt->traverse_mode == TRAVERSE_FORWARD) ltfsmsg(LTFS_INFO, 16083I); else if (opt->traverse_mode == TRAVERSE_BACKWARD) ltfsmsg(LTFS_INFO, 16084I); @@ -1491,11 +1503,12 @@ int _ltfsck_validate_options(struct other_check_opts *opt) if (!opt->str_gen) { ltfsmsg(LTFS_ERR, 16004E); return LTFSCK_USAGE_SYNTAX_ERROR; - } else { + } + else { char *invalid_start; errno = 0; opt->point_gen = strtoul(opt->str_gen, &invalid_start, 0); - if( (*invalid_start == '\0') && opt->str_gen ) + if ((*invalid_start == '\0') && opt->str_gen) ltfsmsg(LTFS_INFO, 16006I, opt->point_gen); else { ltfsmsg(LTFS_ERR, 16005E, opt->str_gen); diff --git a/src/utils/mkltfs.c b/src/utils/mkltfs.c index 75b5a03d..53863663 100644 --- a/src/utils/mkltfs.c +++ b/src/utils/mkltfs.c @@ -52,15 +52,15 @@ */ #ifdef mingw_PLATFORM -#include "libltfs/arch/win/win_util.h" +#include "arch/win/win_util.h" #else #include #endif /* mingw_PLATFORM */ -#include + #include "libltfs/ltfs_fuse_version.h" #include - +#include #include "libltfs/ltfs.h" #include "ltfs_copyright.h" #include "libltfs/index_criteria.h" @@ -69,6 +69,9 @@ #include "libltfs/kmi.h" #include "libltfs/tape.h" +#ifdef mingw_PLATFORM +static +#endif volatile char *copyright = LTFS_COPYRIGHT_0"\n"LTFS_COPYRIGHT_1"\n"LTFS_COPYRIGHT_2"\n" \ LTFS_COPYRIGHT_3"\n"LTFS_COPYRIGHT_4"\n"LTFS_COPYRIGHT_5"\n"; @@ -143,6 +146,9 @@ static struct option long_options[] = { {0, 0, 0, 0} }; +#ifdef mingw_PLATFORM +static +#endif void show_usage(char *appname, struct config_file *config, bool full) { struct libltfs_plugin backend; @@ -151,12 +157,12 @@ void show_usage(char *appname, struct config_file *config, bool full) default_backend = config_file_get_default_plugin("tape", config); if (default_backend && plugin_load(&backend, "tape", default_backend, config) == 0) { - devname = strdup(ltfs_default_device_name(backend.ops)); + devname = arch_strdup(ltfs_default_device_name(backend.ops)); plugin_unload(&backend); } if (! devname) - devname = strdup(""); + devname = arch_strdup(""); fprintf(stderr, "\n"); ltfsresult(15400I, appname); /* Usage: %s */ @@ -207,17 +213,16 @@ int main(int argc, char **argv) struct ltfs_volume *newvol; struct other_format_opts opt; int ret, log_level, syslog_level, i, cmd_args_len; - char *lang, *cmd_args; + char *lang = NULL, *cmd_args; const char *config_file = NULL; void *message_handle; - int fuse_argc = argc; char **fuse_argv = calloc(fuse_argc, sizeof(char *)); if (! fuse_argv) { return MKLTFS_OPERATIONAL_ERROR; } for (i = 0; i < fuse_argc; ++i) { - fuse_argv[i] = strdup(argv[i]); + fuse_argv[i] = arch_strdup(argv[i]); if (! fuse_argv[i]) { return MKLTFS_OPERATIONAL_ERROR; } @@ -225,7 +230,7 @@ int main(int argc, char **argv) struct fuse_args args = FUSE_ARGS_INIT(fuse_argc, fuse_argv); /* Check for LANG variable and set it to en_US.UTF-8 if it is unset. */ - lang = getenv("LANG"); + arch_getenv(lang,"LANG"); if (! lang) { fprintf(stderr, "LTFS9015W Setting the locale to 'en_US.UTF-8'. If this is wrong, please set the LANG environment variable before starting mkltfs.\n"); ret = setenv("LANG", "en_US.UTF-8", 1); @@ -277,7 +282,7 @@ int main(int argc, char **argv) if (c == -1) break; if (c == 'i') { - config_file = strdup(optarg); + config_file = arch_strdup(optarg); break; } } @@ -303,25 +308,25 @@ int main(int argc, char **argv) break; case 'e': free(opt.backend_path); - opt.backend_path = strdup(optarg); + opt.backend_path = arch_strdup(optarg); break; case 'd': - opt.devname = strdup(optarg); + opt.devname = arch_strdup(optarg); break; case 'b': opt.blocksize = atoi(optarg); break; case 's': - opt.barcode = strdup(optarg); + opt.barcode = arch_strdup(optarg); break; case 'n': - opt.volume_name = strdup(optarg); + opt.volume_name = arch_strdup(optarg); break; case 'r': - opt.filterrules = strdup(optarg); + opt.filterrules = arch_strdup(optarg); break; case '-': - opt.kmi_backend_name = strdup(optarg); + opt.kmi_backend_name = arch_strdup(optarg); break; case 'c': opt.enable_compression = false; @@ -390,14 +395,14 @@ int main(int argc, char **argv) ltfsmsg(LTFS_ERR, 10009E); return MKLTFS_OPERATIONAL_ERROR; } - opt.backend_path = strdup(default_backend); + opt.backend_path = arch_strdup(default_backend); } if (! opt.kmi_backend_name) { const char *default_backend = config_file_get_default_plugin("kmi", opt.config); if (default_backend) - opt.kmi_backend_name = strdup(default_backend); + opt.kmi_backend_name = arch_strdup(default_backend); else - opt.kmi_backend_name = strdup("none"); + opt.kmi_backend_name = arch_strdup("none"); } if (opt.kmi_backend_name && strcmp(opt.kmi_backend_name, "none") == 0) opt.kmi_backend_name = NULL; @@ -439,10 +444,10 @@ int main(int argc, char **argv) ltfsmsg(LTFS_ERR, 10001E, "mkltfs (arguments)"); return MKLTFS_OPERATIONAL_ERROR; } - strcat(cmd_args, argv[0]); + arch_strcat(cmd_args, cmd_args_len,argv[0]); for (i = 1; i < argc; i++) { - strcat(cmd_args, " "); - strcat(cmd_args, argv[i]); + arch_strcat(cmd_args, cmd_args_len," "); + arch_strcat(cmd_args, cmd_args_len, argv[i]); } ltfsmsg(LTFS_INFO, 15041I, cmd_args); free(cmd_args); @@ -500,9 +505,10 @@ int main(int argc, char **argv) else ret = format_tape(newvol, &opt, &args); - free(opt.backend_path); - free(opt.kmi_backend_name); - free(opt.devname); + + arch_safe_free(opt.backend_path); + arch_safe_free(opt.kmi_backend_name); + arch_safe_free(opt.devname); config_file_free(opt.config); ltfsprintf_unload_plugin(message_handle); ltfs_finish();