Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F6198286
raw.txt
No One
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
jbeich
Feb 27 2020, 5:40 PM
2020-02-27 17:40:15 (UTC+0)
Size
898 B
Referenced Files
None
Subscribers
None
raw.txt
View Options
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#define HAVE_POSIX_FALLOCATE 1
int
os_resize_anonymous_file(int fd, off_t size)
{
#ifdef HAVE_POSIX_FALLOCATE
/*
* Filesystems that do support fallocate will return EINVAL or
* EOPNOTSUPP. In this case we need to fall back to ftruncate
*/
errno = posix_fallocate(fd, 0, size);
if (errno == 0)
return 0;
else if (errno != EINVAL && errno != EOPNOTSUPP) {
warn("posix_fallocate failed");
return -1;
}
#endif
if (ftruncate(fd, size) < 0) {
warn("ftruncate failed");
return -1;
}
return 0;
}
int
main(void)
{
int fd;
off_t size = 12345;
fd = shm_open(SHM_ANON, O_CREAT | O_RDWR | O_CLOEXEC, 0600); // shm_open is always CLOEXEC
if (fd < 0)
err(1, "shm_open(SHM_ANON) failed");
if (os_resize_anonymous_file(fd, size) < 0) {
close(fd);
return -1;
}
return 0;
}
File Metadata
Details
Attached
Mime Type
text/plain; charset=utf-8
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2531154
Default Alt Text
raw.txt (898 B)
Attached To
Mode
P367 graphics/wayland: standalone posix_fallocate snippet
Attached
Detach File
Event Timeline
Log In to Comment