ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hello all,
I am trying to lseek into a file till offset 4GB (4294967296 bytes).
Code:
off64_t FILESIZE = 4294967296;
result = lseek64(fd, FILESIZE-1, SEEK_SET);
Please note that fd is a valid file descriptor, I have made sure that the file open call is successful.
Heres what I observe:
1. If FILESIZE < 4GB, lseek works.
2. If FILESIZE = 4GB, lseek fails with errno 29 (ESPIPE), which states that fd is associated with a pipe, socket, or FIFO.
3. If FILESIZE > 4GB, lseek works again.
So I am confused why lseek fails when FILESIZE is exactly 4GB. I am running this program on a dual core 64 bit Opteron machine running OpenSuse. Any pointers into this error are greatly appreciated.
Heres the complete program:
Code:
#include <stdio.h>
#include <stdlib.h>
#define _LARGEFILE64_SOURCE 1
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#define FILEPATH "/tmp/km_mmap.txt"
off64_t FILESIZE = 4294967296; //2147483648;
int main(int argc, char *argv[])
{
int fd;
int result;
fd = open64(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600);
if (fd == -1) {
perror("Error opening file for writing");
exit(EXIT_FAILURE);
}
result = lseek64(fd, (off64_t)(FILESIZE-1), SEEK_SET);
if (result == -1) {
close(fd);
perror("Error calling lseek() to 'stretch' the file");
fprintf(stdout, "errno: %d\n", errno);
exit(EXIT_FAILURE);
}
return 0;
}
Several suggestions:
1. Make sure nothing in any of your expressions casts the value down to a 32-bit int (that's what I was suggesting with "LL"; that's what Sergei Steshenko was suggestiong with "off64_t offset = (off64_t)FILESIZE - (off64_t)1").
2. Lose the "open64()" and substitute "open()" with "O_LARGEFILE".
3. Read this man page on "lseek64()" and see if any of the "gotcha's" mentioned in it might be applicable to your system:
Like an idiot, I declared result as an int, instead of off64_t. Note that lseek returns the offset at which the file pointer was positioned, hence the program fails.
What I noticed was that
For filesize 4294967298 (4GB + 2), the program did not crash, but the result was being set to 2.
For filesize 4294967295 (4GB - 2), the result was being set to -1.
It crashed when filesize was exactly 4GB.
Since the int could hold a maximum value of 2147483647, any value over it was making the result a -ve number.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.