LCOV - code coverage report
Current view: top level - cmdline - portable.h (source / functions) Hit Total Coverage
Test: lcov.info Lines: 2 2 100.0 %
Date: 2016-01-16 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef __PORTABLE_H
      19             : #define __PORTABLE_H
      20             : 
      21             : #if HAVE_CONFIG_H
      22             : #include "config.h" /* Use " to include first in the same directory of this file */
      23             : #endif
      24             : 
      25             : /***************************************************************************/
      26             : /* Config */
      27             : 
      28             : #ifdef __MINGW32__
      29             : /**
      30             :  * Enable the GNU printf functions instead of using the MSVCRT ones.
      31             :  *
      32             :  * Note that this is the default if _POSIX is also defined.
      33             :  * To disable it you have to set it to 0.
      34             :  */
      35             : #define __USE_MINGW_ANSI_STDIO 1
      36             : 
      37             : /*
      38             :  * Define the MSVCRT version targetting Windows XP.
      39             :  */
      40             : #define __MSVCRT_VERSION__ 0x0601
      41             : 
      42             : /**
      43             :  * Include Windows XP headers.
      44             :  *
      45             :  * Like for CreateHardLinkW().
      46             :  */
      47             : #define _WIN32_WINNT 0x501
      48             : 
      49             : #include <windows.h>
      50             : #endif
      51             : 
      52             : /**
      53             :  * Specify the format attribute for printf.
      54             :  */
      55             : #ifdef __MINGW32__
      56             : #if defined(__USE_MINGW_ANSI_STDIO) && __USE_MINGW_ANSI_STDIO == 1
      57             : #define attribute_printf gnu_printf /* GNU format */
      58             : #else
      59             : #define attribute_printf ms_printf /* MSVCRT format */
      60             : #endif
      61             : #else
      62             : #define attribute_printf printf /* GNU format is the default one */
      63             : #endif
      64             : 
      65             : /**
      66             :  * Compiler extension
      67             :  */
      68             : #ifndef __always_inline
      69             : #define __always_inline inline __attribute__((always_inline))
      70             : #endif
      71             : 
      72             : #ifndef __noreturn
      73             : #define __noreturn __attribute__((noreturn))
      74             : #endif
      75             : 
      76             : 
      77             : /**
      78             :  * Architecture.
      79             :  */
      80             : #if defined(__i386__)
      81             : #define CONFIG_X86 1
      82             : #define CONFIG_X86_32 1
      83             : #endif
      84             : 
      85             : #if defined(__x86_64__)
      86             : #define CONFIG_X86 1
      87             : #define CONFIG_X86_64 1
      88             : #endif
      89             : 
      90             : /**
      91             :  * Includes some platform specific headers.
      92             :  */
      93             : #if HAVE_SYS_PARAM_H
      94             : #include <sys/param.h>
      95             : #endif
      96             : 
      97             : #if HAVE_SYS_MOUNT_H
      98             : #include <sys/mount.h>
      99             : #endif
     100             : 
     101             : #if HAVE_SYS_VFS_H
     102             : #include <sys/vfs.h>
     103             : #endif
     104             : 
     105             : #if HAVE_SYS_STATFS_H
     106             : #include <sys/statfs.h>
     107             : #endif
     108             : 
     109             : #if HAVE_SYS_FILE_H
     110             : #include <sys/file.h>
     111             : #endif
     112             : 
     113             : #if HAVE_SYS_IOCTL_H
     114             : #include <sys/ioctl.h>
     115             : #endif
     116             : 
     117             : #if HAVE_LINUX_FS_H
     118             : #include <linux/fs.h>
     119             : #endif
     120             : 
     121             : #if HAVE_LINUX_FIEMAP_H
     122             : #include <linux/fiemap.h>
     123             : #endif
     124             : 
     125             : #if HAVE_BLKID_BLKID_H
     126             : #include <blkid/blkid.h>
     127             : #if HAVE_BLKID_DEVNO_TO_DEVNAME && HAVE_BLKID_GET_TAG_VALUE
     128             : #define HAVE_BLKID 1
     129             : #endif
     130             : #endif
     131             : 
     132             : /**
     133             :  * Includes some standard headers.
     134             :  */
     135             : #include <stdio.h>
     136             : #include <stdlib.h> /* On many systems (e.g., Darwin), `stdio.h' is a prerequisite. */
     137             : #include <stdarg.h>
     138             : #include <string.h>
     139             : #include <ctype.h>
     140             : #include <fcntl.h>
     141             : #include <assert.h>
     142             : #include <errno.h>
     143             : #include <signal.h>
     144             : #include <limits.h>
     145             : 
     146             : #if HAVE_STDINT_H
     147             : #include <stdint.h>
     148             : #endif
     149             : 
     150             : #if HAVE_INTTYPES_H
     151             : #include <inttypes.h>
     152             : #endif
     153             : 
     154             : #if HAVE_UNISTD_H
     155             : #include <unistd.h>
     156             : #endif
     157             : 
     158             : #if TIME_WITH_SYS_TIME
     159             : #include <sys/time.h>
     160             : #include <time.h>
     161             : #else
     162             : #if HAVE_SYS_TIME_H
     163             : #include <sys/time.h>
     164             : #else
     165             : #include <time.h>
     166             : #endif
     167             : #endif
     168             : 
     169             : #if HAVE_MACH_MACH_TIME_H
     170             : #include <mach/mach_time.h>
     171             : #endif
     172             : 
     173             : #if HAVE_DIRENT_H
     174             : #include <dirent.h>
     175             : #define NAMLEN(dirent) strlen((dirent)->d_name)
     176             : #else
     177             : #define dirent direct
     178             : #define NAMLEN(dirent) (dirent)->d_namlen
     179             : #if HAVE_SYS_NDIR_H
     180             : #include <sys/ndir.h>
     181             : #endif
     182             : #if HAVE_SYS_DIR_H
     183             : #include <sys/dir.h>
     184             : #endif
     185             : #if HAVE_NDIR_H
     186             : #include <ndir.h>
     187             : #endif
     188             : #endif
     189             : 
     190             : #if HAVE_SYS_TYPES_H
     191             : #include <sys/types.h>
     192             : #endif
     193             : 
     194             : #if HAVE_SYS_STAT_H
     195             : #include <sys/stat.h>
     196             : #endif
     197             : 
     198             : #if HAVE_SYS_WAIT_H
     199             : #include <sys/wait.h>
     200             : #endif
     201             : #ifndef WEXITSTATUS
     202             : #define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
     203             : #endif
     204             : #ifndef WIFEXITED
     205             : #define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
     206             : #endif
     207             : 
     208             : #if HAVE_GETOPT_H
     209             : #include <getopt.h>
     210             : #endif
     211             : 
     212             : #if HAVE_FNMATCH_H
     213             : #include <fnmatch.h>
     214             : #else
     215             : #include "fnmatch.h"
     216             : #endif
     217             : 
     218             : #if HAVE_PTHREAD_H
     219             : #include <pthread.h>
     220             : #endif
     221             : 
     222             : #if HAVE_MATH_H
     223             : #include <math.h>
     224             : #endif
     225             : 
     226             : #if HAVE_EXECINFO_H
     227             : #include <execinfo.h>
     228             : #endif
     229             : 
     230             : /**
     231             :  * Enable thread use.
     232             :  */
     233             : #if HAVE_PTHREAD_H && HAVE_PTHREAD_CREATE
     234             : #define HAVE_PTHREAD 1
     235             : #endif
     236             : 
     237             : /**
     238             :  * Disable case check in Windows.
     239             :  */
     240             : #ifdef _WIN32
     241             : #define FNM_CASEINSENSITIVE_FOR_WIN FNM_CASEFOLD
     242             : #else
     243             : #define FNM_CASEINSENSITIVE_FOR_WIN 0
     244             : #endif
     245             : 
     246             : #if HAVE_IO_H
     247             : #include <io.h>
     248             : #endif
     249             : 
     250             : #if HAVE_GETOPT_LONG
     251             : #define SWITCH_GETOPT_LONG(a, b) a
     252             : #else
     253             : #define SWITCH_GETOPT_LONG(a, b) b
     254             : #endif
     255             : 
     256             : /**
     257             :  * Enables lock file support.
     258             :  */
     259             : #if HAVE_FLOCK && HAVE_FTRUNCATE
     260             : #define HAVE_LOCKFILE 1
     261             : #endif
     262             : 
     263             : /**
     264             :  * Includes specific support for Windows or Linux.
     265             :  */
     266             : #ifdef __MINGW32__
     267             : #include "mingw.h"
     268             : #else
     269             : #include "unix.h"
     270             : #endif
     271             : 
     272             : /**
     273             :  * Include list support to have tommy_node.
     274             :  */
     275             : #include "tommyds/tommylist.h"
     276             : 
     277             : /**
     278             :  * Another name for link() to avoid confusion with local variables called "link".
     279             :  */
     280          12 : static inline int hardlink(const char* a, const char* b)
     281             : {
     282          12 :         return link(a, b);
     283             : }
     284             : 
     285             : /**
     286             :  * Get the device UUID.
     287             :  * Return 0 on success.
     288             :  */
     289             : int devuuid(uint64_t device, char* uuid, size_t size);
     290             : 
     291             : /**
     292             :  * Physical offset not yet read.
     293             :  */
     294             : #define FILEPHY_UNREAD_OFFSET 0
     295             : 
     296             : /**
     297             :  * Special value returned when the file-system doesn't report any offset for unknown reason.
     298             :  */
     299             : #define FILEPHY_UNREPORTED_OFFSET 1
     300             : 
     301             : /**
     302             :  * Special value returned when the file doesn't have a real offset.
     303             :  * For example, because it's stored in the NTFS MFT.
     304             :  */
     305             : #define FILEPHY_WITHOUT_OFFSET 2
     306             : 
     307             : /**
     308             :  * Value indicating real offsets. All offsets greater or equal at this one are real.
     309             :  */
     310             : #define FILEPHY_REAL_OFFSET 3
     311             : 
     312             : /**
     313             :  * Get the physcal address of the specified file.
     314             :  * This is expected to be just a hint and not necessarily correct or unique.
     315             :  * Return 0 on success.
     316             :  */
     317             : int filephy(const char* path, uint64_t size, uint64_t* physical);
     318             : 
     319             : /**
     320             :  * Check if the underline file-system support persistent inodes.
     321             :  * Return -1 on error, 0 on success.
     322             :  */
     323             : int fsinfo(const char* path, int* has_persistent_inode, uint64_t* total_space, uint64_t* free_space);
     324             : 
     325             : /**
     326             :  * Get the tick counter value.
     327             :  *
     328             :  * Note that the frequency is unspecified, because the time measure
     329             :  * is meant to be used to compare the ratio between usage times.
     330             :  */
     331             : uint64_t tick(void);
     332             : 
     333             : /**
     334             :  * Get the tick counter value in millisecond.
     335             :  */
     336             : uint64_t tick_ms(void);
     337             : 
     338             : /**
     339             :  * Initializes the system.
     340             :  */
     341             : void os_init(int opt);
     342             : 
     343             : /**
     344             :  * Deinitialize the system.
     345             :  */
     346             : void os_done(void);
     347             : 
     348             : /**
     349             :  * Abort the process with a stacktrace.
     350             :  */
     351             : void os_abort(void) __noreturn;
     352             : 
     353             : /**
     354             :  * Log file.
     355             :  *
     356             :  * This stream if fully buffered.
     357             :  *
     358             :  * If no log file is selected, it's 0.
     359             :  */
     360             : FILE* stdlog;
     361             : 
     362             : /**
     363             :  * Exit codes for testing.
     364             :  */
     365             : int exit_success;
     366             : int exit_failure;
     367             : int exit_sync_needed;
     368             : #undef EXIT_SUCCESS
     369             : #undef EXIT_FAILURE
     370             : #define EXIT_SUCCESS exit_success
     371             : #define EXIT_FAILURE exit_failure
     372             : #define EXIT_SYNC_NEEDED exit_sync_needed
     373             : 
     374             : /**
     375             :  * Fill memory with psudeo-random values.
     376             :  */
     377             : int randomize(void* ptr, size_t size);
     378             : 
     379             : /**
     380             :  * Additional SMART attributes.
     381             :  */
     382             : #define SMART_ERROR 256 /**< ATA Error count. */
     383             : #define SMART_SIZE 257 /**< Size in bytes. */
     384             : #define SMART_ROTATION_RATE 258 /**< Rotation speed. 0 for SSD. */
     385             : #define SMART_FLAGS 259 /**< Flags returned by smartctl. */
     386             : 
     387             : /**
     388             :  * SMART attributes count.
     389             :  */
     390             : #define SMART_COUNT 260
     391             : 
     392             : /**
     393             :  * Flags returned by smartctl.
     394             :  */
     395             : #define SMARTCTL_FLAG_UNSUPPORTED (1 << 0) /**< Device not recognized, requiring the -d option. */
     396             : #define SMARTCTL_FLAG_OPEN (1 << 1) /**< Device open or identification failed. */
     397             : #define SMARTCTL_FLAG_COMMAND (1 << 2) /**< Some SMART or ATA commands failed. This is a common error, also happening with full info gathering. */
     398             : #define SMARTCTL_FLAG_FAIL (1 << 3) /**< SMART status check returned "DISK FAILING". */
     399             : #define SMARTCTL_FLAG_PREFAIL (1 << 4) /**< We found prefail Attributes <= threshold. */
     400             : #define SMARTCTL_FLAG_PREFAIL_LOGGED (1 << 5) /**< SMART status check returned "DISK OK" but we found that some (usage or prefail) Attributes have been <= threshold at some time in the past. */
     401             : #define SMARTCTL_FLAG_ERROR (1 << 6) /**< The device error log contains records of errors. */
     402             : #define SMARTCTL_FLAG_ERROR_LOGGED (1 << 7) /**< The device self-test log contains records of errors. */
     403             : 
     404             : /**
     405             :  * SMART max attribute length.
     406             :  */
     407             : #define SMART_MAX 64
     408             : 
     409             : /**
     410             :  * Value for unassigned SMART attribute.
     411             :  */
     412             : #define SMART_UNASSIGNED 0xFFFFFFFFFFFFFFFFULL
     413             : 
     414             : /**
     415             :  * Device info entry.
     416             :  */
     417             : struct devinfo_struct {
     418             :         uint64_t device; /**< Device ID. */
     419             :         char name[PATH_MAX]; /**< Name of the disk. */
     420             :         char mount[PATH_MAX]; /**< Mount point or other contained directory. */
     421             :         char smartctl[PATH_MAX]; /**< Options for smartctl. */
     422             :         char file[PATH_MAX]; /**< File device. */
     423             : #ifdef _WIN32
     424             :         char wfile[PATH_MAX]; /**< File device in Windows format. Like \\.\PhysicalDriveX, or \\?\Volume{X}. */
     425             : #endif
     426             :         struct devinfo_struct* parent; /**< Pointer at the parent if any. */
     427             :         uint64_t smart[SMART_COUNT]; /**< SMART raw attributes. */
     428             :         char smart_serial[SMART_MAX]; /**< SMART serial number. */
     429             : #if HAVE_PTHREAD
     430             :         pthread_t thread;
     431             : #endif
     432             :         tommy_node node;
     433             : };
     434             : typedef struct devinfo_struct devinfo_t;
     435             : 
     436             : #define DEVICE_LIST 0
     437             : #define DEVICE_DOWN 1
     438             : #define DEVICE_UP 2
     439             : #define DEVICE_SMART 3
     440             : 
     441             : /**
     442             :  * Query all the "high" level devices with the specified operation,
     443             :  * and produces a list of "low" level devices to operate on.
     444             :  *
     445             :  * The passed "low" device list must be already initialized.
     446             :  */
     447             : int devquery(tommy_list* high, tommy_list* low, int operation);
     448             : 
     449             : #endif
     450             : 

Generated by: LCOV version 1.10