os: fallback for unsupported posix_fallocate
Some filesystems do not support fallocate and return EOPNOTSUPP.
On musl-based distros libwayland-cursor exits abruptly which causes the
application to crash. Unlike glibc, musl does not provide a fallback
mechanism for handling unsupported fallocate. Instead, musl developers
argue that application should handle the case of unsupported system
call.
This commit allows falback to ftruncate in case when EOPNOTSUPP
was recieved.
Signed-off-by: Ihor Antonov <ihor@antonovs.family>
Ihor Antonov authored 4 years ago
Héctor Orón Martínez committed 4 years ago
155 | 155 | } |
156 | 156 | |
157 | 157 | #ifdef HAVE_POSIX_FALLOCATE |
158 | /* | |
159 | * Filesystems that do support fallocate will return EOPNOTSUPP. | |
160 | * In this case we need to fall back to ftruncate | |
161 | */ | |
158 | 162 | ret = posix_fallocate(fd, 0, size); |
159 | if (ret != 0) { | |
163 | if (ret == 0) { | |
164 | return fd; | |
165 | } else if (ret != EOPNOTSUPP) { | |
160 | 166 | close(fd); |
161 | 167 | errno = ret; |
162 | 168 | return -1; |
163 | 169 | } |
164 | #else | |
170 | #endif | |
165 | 171 | ret = ftruncate(fd, size); |
166 | 172 | if (ret < 0) { |
167 | 173 | close(fd); |
168 | 174 | return -1; |
169 | 175 | } |
170 | #endif | |
171 | 176 | |
172 | 177 | return fd; |
173 | 178 | } |