radv: enable radv_enable_mrt_output_nan_fixup for RAGE 2
[mesa.git] / src / util / os_file.c
1 /*
2 * Copyright 2019 Intel Corporation
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "os_file.h"
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdlib.h>
11 #include <sys/stat.h>
12
13
14 #if defined(WIN32)
15 #include <io.h>
16 #define open _open
17 #define fdopen _fdopen
18 #define O_CREAT _O_CREAT
19 #define O_EXCL _O_EXCL
20 #define O_WRONLY _O_WRONLY
21 #endif
22
23
24 FILE *
25 os_file_create_unique(const char *filename, int filemode)
26 {
27 int fd = open(filename, O_CREAT | O_EXCL | O_WRONLY, filemode);
28 if (fd == -1)
29 return NULL;
30 return fdopen(fd, "w");
31 }
32
33
34 #if defined(__linux__)
35
36 #include <fcntl.h>
37 #include <sys/stat.h>
38 #include <sys/syscall.h>
39 #include <unistd.h>
40
41 /* copied from <linux/kcmp.h> */
42 #define KCMP_FILE 0
43
44 static ssize_t
45 readN(int fd, char *buf, size_t len)
46 {
47 int err = -ENODATA;
48 size_t total = 0;
49 do {
50 ssize_t ret = read(fd, buf + total, len - total);
51
52 if (ret < 0)
53 ret = -errno;
54
55 if (ret == -EINTR || ret == -EAGAIN)
56 continue;
57
58 if (ret <= 0) {
59 err = ret;
60 break;
61 }
62
63 total += ret;
64 } while (total != len);
65
66 return total ? (ssize_t)total : err;
67 }
68
69 char *
70 os_read_file(const char *filename, size_t *size)
71 {
72 /* Note that this also serves as a slight margin to avoid a 2x grow when
73 * the file is just a few bytes larger when we read it than when we
74 * fstat'ed it.
75 * The string's NULL terminator is also included in here.
76 */
77 size_t len = 64;
78
79 int fd = open(filename, O_RDONLY);
80 if (fd == -1) {
81 /* errno set by open() */
82 return NULL;
83 }
84
85 /* Pre-allocate a buffer at least the size of the file if we can read
86 * that information.
87 */
88 struct stat stat;
89 if (fstat(fd, &stat) == 0)
90 len += stat.st_size;
91
92 char *buf = malloc(len);
93 if (!buf) {
94 close(fd);
95 errno = -ENOMEM;
96 return NULL;
97 }
98
99 ssize_t actually_read;
100 size_t offset = 0, remaining = len - 1;
101 while ((actually_read = readN(fd, buf + offset, remaining)) == (ssize_t)remaining) {
102 char *newbuf = realloc(buf, 2 * len);
103 if (!newbuf) {
104 free(buf);
105 close(fd);
106 errno = -ENOMEM;
107 return NULL;
108 }
109
110 buf = newbuf;
111 len *= 2;
112 offset += actually_read;
113 remaining = len - offset - 1;
114 }
115
116 close(fd);
117
118 if (actually_read > 0)
119 offset += actually_read;
120
121 /* Final resize to actual size */
122 len = offset + 1;
123 char *newbuf = realloc(buf, len);
124 if (!newbuf) {
125 free(buf);
126 errno = -ENOMEM;
127 return NULL;
128 }
129 buf = newbuf;
130
131 buf[offset] = '\0';
132
133 if (size)
134 *size = offset;
135
136 return buf;
137 }
138
139 int
140 os_same_file_description(int fd1, int fd2)
141 {
142 pid_t pid = getpid();
143
144 /* Same file descriptor trivially implies same file description */
145 if (fd1 == fd2)
146 return 0;
147
148 return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2);
149 }
150
151 #else
152
153 #include "u_debug.h"
154
155 char *
156 os_read_file(const char *filename, size_t *size)
157 {
158 errno = -ENOSYS;
159 return NULL;
160 }
161
162 int
163 os_same_file_description(int fd1, int fd2)
164 {
165 /* Same file descriptor trivially implies same file description */
166 if (fd1 == fd2)
167 return 0;
168
169 /* Otherwise we can't tell */
170 return -1;
171 }
172
173 #endif