summaryrefslogtreecommitdiff
path: root/support/dktools/dk4fileit.h
blob: a809e4d4d004af90fa59b951a419516fc94222ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef DK4FILEIT_H_INCLUDED
/** Protection against multiple inclusions. */
#define	DK4FILEIT_H_INCLUDED 1

/** @file dk4fileit.h	File information data type.

    The dk4_file_info_t data type and related functions allow to
    properly identify a path name as symbolic link on both non-Windows
    and Windows systems.

    On non-Windows systems calls to stat() and lstat() are used to
    retrieve the information.

    On Windows systems calls to GetFileAttributes(),
    CreateFile()/GetFileInformationByHandle()
    and FindFirstFile() are used.
    As the involved data types are Windows-specific the windows.h
    header file is pulled in by this header.

    The Windows functions to retrieve information about files differ:
    - GetFileAttributes() returns a DWORD containing bits for
      file attributes only.
    - From CreateFile()/GetFileInformationByHandle() we can receive
      very detailed information.
    - From FindFirstFile() we can retrieve some of the information
      also provided by CreateFile()/GetFileInformationByHandle(), but
      number of links, volume serial number, and file index are not
      available.
    Unfortunately the functions behave differently: While FindFirstFile()
    succeeds for c:/pagefile.sys and other files in the root directory,
    GetFileAttributes() and CreateFile()/GetFileInformationByHandle()
    fail in non-privileged processes. Some forum messages state that
    FindFirstFile() is significantly faster than
    CreateFile()/GetFileInformationByHandle(), at least for files on servers.
    When called for a symbolic link, FindFirstFile() always returns information
    about the link. For CreateFile()/GetFileInformationByHandle() one can
    choose whether to obtain information about the or about the target.

    So the dk4fileinfo() function attempts GetFileAttributes() and
    CreateFile()/GetFileInformationByHandle() first to obtain detailed
    information. If these functions fail, the FindFirstFile() function is
    used. If only FindFirstFile() returned successfully, the resio
    component is set to a non-zero value to indicate that only restricted
    information (without number of links, volume serial number, and
    file index) is available.
*/


#include "dk4conf.h"

#if DK4_ON_WINDOWS
#ifndef WINDOWS_H_INCLUDED
#include <windows.h>
#define	WINDOWS_H_INCLUDED 1
#endif
#endif

#include "dk4statt.h"

/**	File information contents found.
*/
enum {
					/**	Information about the link
						from lstat().
					*/
  DK4_FILE_INFO_CONTENTS_DATA_LINK	= 1,

					/**	Information about the link
						target from stat().
					*/
  DK4_FILE_INFO_CONTENTS_DATA_TARGET	= 2
};

/**	Choose timestamp to convert to text.
*/
enum {
					/**	Creation time.
					*/
  DK4_FILE_INFO_TIME_CREATE		= 1,

					/**	Modification time.
					*/
  DK4_FILE_INFO_TIME_MODIFY ,

					/**	Last access time.
					*/
  DK4_FILE_INFO_TIME_ACCESS
};

/**	File information data type.
*/
typedef struct {
  int		contents;		/**< The available contents. */
#if DK4_ON_WINDOWS
  BY_HANDLE_FILE_INFORMATION	linfo;	/**< Information about link. */
  BY_HANDLE_FILE_INFORMATION	tinfo;	/**< Information about target. */
  DWORD				fattr;	/**< Link file attributes. */
  DWORD				rppnt;	/**< Link reparse point tag. */
  DWORD				resio;	/**< Flag: Restricted information. */
#else
  dk4_stat_t	lstb;			/**< Buffer for lstat(). */
  dk4_stat_t	tstb;			/**< Buffer for stat(). */
#endif
} dk4_file_info_t;

#endif