How to use direct io write a small file with out change file size.
Assuming I have a file, file size is 1.1KB, I use direct io to read the file, and change some data, and write file......
see below
//open file with O_DIRECT
handle_ = open("1.html", O_RDWR | O_DIRECT | O_SYNC | O_LARGEFILE,0777))
//file size is 1.1KB (i read 4KB)
pread64(handle_,buffer,4096,0);
memcpy(buffer,"11111",5);
//error (512b Aligned)
pwrite64(handle_,buffer,1.1KB,0);
//OK,but file size has been extended to 4KB
pwrite64(handle_,buffer,4KB,0);
This is a real world problem because some software may use this file too, like webserver, it will get a wrong file size and response to client a bigger file.
(must be direct io, I have my reasons)