1 //========================================================================
2 //
3 // gfile.h
4 //
5 // Miscellaneous file and directory name manipulation.
6 //
7 // Copyright 1996-2003 Glyph & Cog, LLC
8 //
9 //========================================================================
10 
11 //========================================================================
12 //
13 // Modified under the Poppler project - http://poppler.freedesktop.org
14 //
15 // All changes made under the Poppler project to this file are licensed
16 // under GPL version 2 or later
17 //
18 // Copyright (C) 2006 Kristian Høgsberg <krh@redhat.com>
19 // Copyright (C) 2009, 2011, 2012, 2017, 2018, 2021 Albert Astals Cid <aacid@kde.org>
20 // Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
21 // Copyright (C) 2013 Adam Reichold <adamreichold@myopera.com>
22 // Copyright (C) 2013, 2017 Adrian Johnson <ajohnson@redneon.com>
23 // Copyright (C) 2014 Bogdan Cristea <cristeab@gmail.com>
24 // Copyright (C) 2014 Peter Breitenlohner <peb@mppmu.mpg.de>
25 // Copyright (C) 2017 Christoph Cullmann <cullmann@kde.org>
26 // Copyright (C) 2017 Thomas Freitag <Thomas.Freitag@alfa.de>
27 // Copyright (C) 2018 Mojca Miklavec <mojca@macports.org>
28 // Copyright (C) 2019, 2021 Christian Persch <chpe@src.gnome.org>
29 //
30 // To see a description of the changes please see the Changelog file that
31 // came with your tarball or type make ChangeLog if you are building from git
32 //
33 //========================================================================
34 
35 #ifndef GFILE_H
36 #define GFILE_H
37 
38 #include "poppler-config.h"
39 #include "poppler_private_export.h"
40 #include <cstdio>
41 #include <cstdlib>
42 #include <cstddef>
43 #include <ctime>
44 #include <string>
45 extern "C" {
46 #if defined(_WIN32)
47 #    include <sys/stat.h>
48 #    ifdef FPTEX
49 #        include <win32lib.h>
50 #    else
51 #        ifndef NOMINMAX
52 #            define NOMINMAX
53 #        endif
54 #        include <windows.h>
55 #    endif
56 #else
57 #    include <unistd.h>
58 #    include <sys/types.h>
59 #    if defined(HAVE_DIRENT_H)
60 #        include <dirent.h>
61 #        define NAMLEN(d) strlen((d)->d_name)
62 #    else
63 #        define dirent direct
64 #        define NAMLEN(d) (d)->d_namlen
65 #        ifdef HAVE_SYS_NDIR_H
66 #            include <sys/ndir.h>
67 #        endif
68 #        ifdef HAVE_SYS_DIR_H
69 #            include <sys/dir.h>
70 #        endif
71 #        ifdef HAVE_NDIR_H
72 #            include <ndir.h>
73 #        endif
74 #    endif
75 #endif
76 }
77 
78 class GooString;
79 
80 /* Integer type for all file offsets and file sizes */
81 typedef long long Goffset;
82 
83 //------------------------------------------------------------------------
84 
85 // Append a file name to a path string.  <path> may be an empty
86 // string, denoting the current directory).  Returns <path>.
87 extern GooString POPPLER_PRIVATE_EXPORT *appendToPath(GooString *path, const char *fileName);
88 
89 #ifndef _WIN32
90 // Open a file descriptor
91 // Could be implemented on WIN32 too, but the only external caller of
92 // this function is not used on WIN32
93 extern int POPPLER_PRIVATE_EXPORT openFileDescriptor(const char *path, int flags);
94 #endif
95 
96 // Open a file.  On Windows, this converts the path from UTF-8 to
97 // UCS-2 and calls _wfopen (if available).  On other OSes, this simply
98 // calls fopen.
99 extern FILE POPPLER_PRIVATE_EXPORT *openFile(const char *path, const char *mode);
100 
101 // Just like fgets, but handles Unix, Mac, and/or DOS end-of-line
102 // conventions.
103 extern char POPPLER_PRIVATE_EXPORT *getLine(char *buf, int size, FILE *f);
104 
105 // Like fseek/ftell but uses platform specific variants that support large files
106 extern int POPPLER_PRIVATE_EXPORT Gfseek(FILE *f, Goffset offset, int whence);
107 extern Goffset POPPLER_PRIVATE_EXPORT Gftell(FILE *f);
108 
109 // Largest offset supported by Gfseek/Gftell
110 extern Goffset GoffsetMax();
111 
112 //------------------------------------------------------------------------
113 // GooFile
114 //------------------------------------------------------------------------
115 
116 class POPPLER_PRIVATE_EXPORT GooFile
117 {
118 public:
119     GooFile(const GooFile &) = delete;
120     GooFile &operator=(const GooFile &other) = delete;
121 
122     int read(char *buf, int n, Goffset offset) const;
123     Goffset size() const;
124 
125     static GooFile *open(const std::string &fileName);
126 #ifndef _WIN32
127     static GooFile *open(int fdA);
128 #endif
129 
130 #ifdef _WIN32
131     static GooFile *open(const wchar_t *fileName);
132 
~GooFile()133     ~GooFile() { CloseHandle(handle); }
134 
135     // Asuming than on windows you can't change files that are already open
136     bool modificationTimeChangedSinceOpen() const;
137 
138 private:
139     GooFile(HANDLE handleA);
140     HANDLE handle;
141     struct _FILETIME modifiedTimeOnOpen;
142 #else
~GooFile()143     ~GooFile() { close(fd); }
144 
145     bool modificationTimeChangedSinceOpen() const;
146 
147 private:
148     explicit GooFile(int fdA);
149     int fd;
150     struct timespec modifiedTimeOnOpen;
151 #endif // _WIN32
152 };
153 
154 #endif
155