util: Add os_same_file_description helper
[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 <linux/kcmp.h>
38 #include <sys/stat.h>
39 #include <sys/syscall.h>
40 #include <unistd.h>
41
42
43 static ssize_t
44 readN(int fd, char *buf, size_t len)
45 {
46 int err = -ENODATA;
47 size_t total = 0;
48 do {
49 ssize_t ret = read(fd, buf + total, len - total);
50
51 if (ret < 0)
52 ret = -errno;
53
54 if (ret == -EINTR || ret == -EAGAIN)
55 continue;
56
57 if (ret <= 0) {
58 err = ret;
59 break;
60 }
61
62 total += ret;
63 } while (total != len);
64
65 return total ? (ssize_t)total : err;
66 }
67
68 char *
69 os_read_file(const char *filename)
70 {
71 /* Note that this also serves as a slight margin to avoid a 2x grow when
72 * the file is just a few bytes larger when we read it than when we
73 * fstat'ed it.
74 * The string's NULL terminator is also included in here.
75 */
76 size_t len = 64;
77
78 int fd = open(filename, O_RDONLY);
79 if (fd == -1) {
80 /* errno set by open() */
81 return NULL;
82 }
83
84 /* Pre-allocate a buffer at least the size of the file if we can read
85 * that information.
86 */
87 struct stat stat;
88 if (fstat(fd, &stat) == 0)
89 len += stat.st_size;
90
91 char *buf = malloc(len);
92 if (!buf) {
93 close(fd);
94 errno = -ENOMEM;
95 return NULL;
96 }
97
98 ssize_t actually_read;
99 size_t offset = 0, remaining = len - 1;
100 while ((actually_read = readN(fd, buf + offset, remaining)) == (ssize_t)remaining) {
101 char *newbuf = realloc(buf, 2 * len);
102 if (!newbuf) {
103 free(buf);
104 close(fd);
105 errno = -ENOMEM;
106 return NULL;
107 }
108
109 buf = newbuf;
110 len *= 2;
111 offset += actually_read;
112 remaining = len - offset - 1;
113 }
114
115 close(fd);
116
117 if (actually_read > 0)
118 offset += actually_read;
119
120 /* Final resize to actual size */
121 len = offset + 1;
122 char *newbuf = realloc(buf, len);
123 if (!newbuf) {
124 free(buf);
125 errno = -ENOMEM;
126 return NULL;
127 }
128 buf = newbuf;
129
130 buf[offset] = '\0';
131
132 return buf;
133 }
134
135 bool
136 os_same_file_description(int fd1, int fd2)
137 {
138 pid_t pid = getpid();
139
140 return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2) == 0;
141 }
142
143 #else
144
145 #include "u_debug.h"
146
147 char *
148 os_read_file(const char *filename)
149 {
150 errno = -ENOSYS;
151 return NULL;
152 }
153
154 bool
155 os_same_file_description(int fd1, int fd2)
156 {
157 if (fd1 == fd2)
158 return true;
159
160 debug_warn_once("Can't tell if different file descriptors reference the same"
161 " file description, false negatives might cause trouble!\n");
162 return false;
163 }
164
165 #endif