Open
Description
What happened:
I had an example:
// example.c
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
void print_error(const char* op) {
printf("%s failed: %s (errno: %d)\n", op, strerror(errno), errno);
}
int main() {
const char* filename = "test.file";
int fd;
struct stat st;
// 创建测试文件
fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, 0600);
if (fd == -1) {
print_error("open");
return 1;
}
printf("File created with fd: %d\n", fd);
// 写入一些初始数据
if (write(fd, "test data", 9) == -1) {
print_error("write");
close(fd);
return 1;
}
// 获取并打印初始文件状态
if (fstat(fd, &st) == 0) {
printf("Initial file size: %ld\n", st.st_size);
}
// 执行 unlink
printf("Unlinking file...\n");
if (unlink(filename) == -1) {
print_error("unlink");
close(fd);
return 1;
}
printf("File unlinked but still open\n");
// unlink 后尝试用 ftruncate 改变文件大小
printf("Attempting to resize file after unlink...\n");
if (ftruncate(fd, 16384) == -1) {
print_error("ftruncate");
} else {
printf("Successfully resized file to 16384 bytes\n");
}
// 再次获取文件状态
if (fstat(fd, &st) == 0) {
printf("Final file size: %ld\n", st.st_size);
}
// 关闭文件
close(fd);
printf("File closed\n");
return 0;
}
When I run it on local filesystem, it runs well:
$gcc example.c
[root@xx~/tonybai/setattr-after-unlink]# ./a.out
File created with fd: 3
Initial file size: 9
Unlinking file...
File unlinked but still open
Attempting to resize file after unlink...
Successfully resized file to 16384 bytes
Final file size: 16384
File closed
but when I run it on directory mounted by juicefs, I got error below:
# /mnt/jfs-debug/tonybai ./a.out
File created with fd: 3
Initial file size: 9
Unlinking file...
File unlinked but still open
Attempting to resize file after unlink...
ftruncate failed: Operation not permitted (errno: 1) --- here!!!!
Final file size: 9
File closed
What you expected to happen:
The results of above two runs should be the same!
How to reproduce it (as minimally and precisely as possible):
Compiile and Run the example program on linux /ext4 file system
Anything else we need to know?
Environment:
- JuiceFS version (use
juicefs --version
) or Hadoop Java SDK version:
juicefs --version
juicefs version 1.2.1+2024-08-30.cd871d1
- Cloud provider or hardware configuration running JuiceFS:
- OS (e.g
cat /etc/os-release
):
NAME="BigCloud Enterprise Linux"
VERSION="22.10U1 LTS"
ID="bclinux"
VERSION_ID="22.10U1"
PRETTY_NAME="BigCloud Enterprise Linux For Euler 22.10U1 LTS"
ANSI_COLOR="0;31"
- Kernel (e.g.
uname -a
): linux 5.10+ - Object storage (cloud provider and region, or self maintained): ecloud eos
- Metadata engine info (version, cloud provider managed or self maintained): pg
- Network connectivity (JuiceFS to metadata engine, JuiceFS to object storage):
- Others:
Activity