1 /*-------------------------------------------------------------------------
2  *
3  * fd.h
4  *	  Virtual file descriptor definitions.
5  *
6  *
7  * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * src/include/storage/fd.h
11  *
12  *-------------------------------------------------------------------------
13  */
14 
15 /*
16  * calls:
17  *
18  *	File {Close, Read, Write, Seek, Tell, Sync}
19  *	{Path Name Open, Allocate, Free} File
20  *
21  * These are NOT JUST RENAMINGS OF THE UNIX ROUTINES.
22  * Use them for all file activity...
23  *
24  *	File fd;
25  *	fd = PathNameOpenFile("foo", O_RDONLY, 0600);
26  *
27  *	AllocateFile();
28  *	FreeFile();
29  *
30  * Use AllocateFile, not fopen, if you need a stdio file (FILE*); then
31  * use FreeFile, not fclose, to close it.  AVOID using stdio for files
32  * that you intend to hold open for any length of time, since there is
33  * no way for them to share kernel file descriptors with other files.
34  *
35  * Likewise, use AllocateDir/FreeDir, not opendir/closedir, to allocate
36  * open directories (DIR*), and OpenTransientFile/CloseTransient File for an
37  * unbuffered file descriptor.
38  */
39 #ifndef FD_H
40 #define FD_H
41 
42 #include <dirent.h>
43 
44 
45 /*
46  * FileSeek uses the standard UNIX lseek(2) flags.
47  */
48 
49 typedef char *FileName;
50 
51 typedef int File;
52 
53 
54 /* GUC parameter */
55 extern PGDLLIMPORT int max_files_per_process;
56 extern PGDLLIMPORT bool data_sync_retry;
57 
58 /*
59  * This is private to fd.c, but exported for save/restore_backend_variables()
60  */
61 extern int	max_safe_fds;
62 
63 
64 /*
65  * prototypes for functions in fd.c
66  */
67 
68 /* Operations on virtual Files --- equivalent to Unix kernel file ops */
69 extern File PathNameOpenFile(FileName fileName, int fileFlags, int fileMode);
70 extern File OpenTemporaryFile(bool interXact);
71 extern void FileClose(File file);
72 extern int	FilePrefetch(File file, off_t offset, int amount);
73 extern int	FileRead(File file, char *buffer, int amount);
74 extern int	FileWrite(File file, char *buffer, int amount);
75 extern int	FileSync(File file);
76 extern off_t FileSeek(File file, off_t offset, int whence);
77 extern int	FileTruncate(File file, off_t offset);
78 extern void FileWriteback(File file, off_t offset, off_t nbytes);
79 extern char *FilePathName(File file);
80 extern int	FileGetRawDesc(File file);
81 extern int	FileGetRawFlags(File file);
82 extern int	FileGetRawMode(File file);
83 
84 /* Operations that allow use of regular stdio --- USE WITH CAUTION */
85 extern FILE *AllocateFile(const char *name, const char *mode);
86 extern int	FreeFile(FILE *file);
87 
88 /* Operations that allow use of pipe streams (popen/pclose) */
89 extern FILE *OpenPipeStream(const char *command, const char *mode);
90 extern int	ClosePipeStream(FILE *file);
91 
92 /* Operations to allow use of the <dirent.h> library routines */
93 extern DIR *AllocateDir(const char *dirname);
94 extern struct dirent *ReadDir(DIR *dir, const char *dirname);
95 extern struct dirent *ReadDirExtended(DIR *dir, const char *dirname,
96 				int elevel);
97 extern int	FreeDir(DIR *dir);
98 
99 /* Operations to allow use of a plain kernel FD, with automatic cleanup */
100 extern int	OpenTransientFile(FileName fileName, int fileFlags, int fileMode);
101 extern int	CloseTransientFile(int fd);
102 
103 /* If you've really really gotta have a plain kernel FD, use this */
104 extern int	BasicOpenFile(FileName fileName, int fileFlags, int fileMode);
105 
106 /* Miscellaneous support routines */
107 extern void InitFileAccess(void);
108 extern void set_max_safe_fds(void);
109 extern void closeAllVfds(void);
110 extern void SetTempTablespaces(Oid *tableSpaces, int numSpaces);
111 extern bool TempTablespacesAreSet(void);
112 extern Oid	GetNextTempTableSpace(void);
113 extern void AtEOXact_Files(void);
114 extern void AtEOSubXact_Files(bool isCommit, SubTransactionId mySubid,
115 				  SubTransactionId parentSubid);
116 extern void RemovePgTempFiles(void);
117 
118 extern int	pg_fsync(int fd);
119 extern int	pg_fsync_no_writethrough(int fd);
120 extern int	pg_fsync_writethrough(int fd);
121 extern int	pg_fdatasync(int fd);
122 extern void pg_flush_data(int fd, off_t offset, off_t amount);
123 extern void fsync_fname(const char *fname, bool isdir);
124 extern int	durable_rename(const char *oldfile, const char *newfile, int loglevel);
125 extern int	durable_link_or_rename(const char *oldfile, const char *newfile, int loglevel);
126 extern void SyncDataDirectory(void);
127 extern int data_sync_elevel(int elevel);
128 
129 /* Filename components for OpenTemporaryFile */
130 #define PG_TEMP_FILES_DIR "pgsql_tmp"
131 #define PG_TEMP_FILE_PREFIX "pgsql_tmp"
132 
133 #endif   /* FD_H */
134