util: remove the dependency on kcmp.h
[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)
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 return buf;
134 }
135
136 bool
137 os_same_file_description(int fd1, int fd2)
138 {
139 pid_t pid = getpid();
140
141 return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2) == 0;
142 }
143
144 #else
145
146 #include "u_debug.h"
147
148 char *
149 os_read_file(const char *filename)
150 {
151 errno = -ENOSYS;
152 return NULL;
153 }
154
155 bool
156 os_same_file_description(int fd1, int fd2)
157 {
158 if (fd1 == fd2)
159 return true;
160
161 debug_warn_once("Can't tell if different file descriptors reference the same"
162 " file description, false negatives might cause trouble!\n");
163 return false;
164 }
165
166 #endif