Truncate file at front

前端 未结 7 849
耶瑟儿~
耶瑟儿~ 2020-12-09 09:48

A problem I was working on recently got me to wishing that I could lop off the front of a file. Kind of like a “truncate at front,” if you will. Truncating a file at the bac

相关标签:
7条回答
  • 2020-12-09 10:15

    On file systems that support sparse files "punching" a hole and removing data at an arbitrary file position is very easy. The operating system just has to mark the corresponding blocks as "not allocated". Removing data from the beginning of a file is just a special case of this operation. The main thing that is required is a system call that will implement such an operation: ftruncate2(int fd, off_t offset, size_t count).

    On Linux systems this is actually implemented with the fallocate system call by specifying the FALLOC_FL_PUNCH_HOLE flag to zero-out a range and the FALLOC_FL_COLLAPSE_RANGE flag to completely remove the data in that range. Note that there are restrictions on what ranges can be specified and that not all filesystems support these operations.

    0 讨论(0)
  • 2020-12-09 10:20

    Actually there are record base file systems - IBM have one and I believe DEC VMS also had this facility. I seem to remember both allowed (allow? I guess they are still around) deleting and inserting at random positions in a file.

    0 讨论(0)
  • 2020-12-09 10:20

    There is also a unix command called head -- so you could do this via:

    head -n1000 file > file_truncated
    
    0 讨论(0)
  • 2020-12-09 10:22

    NTFS can do something like this with it's sparse file support but it's generaly not that useful.

    0 讨论(0)
  • 2020-12-09 10:30

    Truncate files at front seems not too hard to implement at system level.

    But there are issues.

    • The first one is at programming level. When opening file in random access the current paradigm is to use offset from the beginning of the file to point out different places in the file. If we truncate at beginning of file (or perform insertion or removal from the middle of the file) that is not any more a stable property. (While appendind or truncating from the end is not a problem).

    In other words truncating the beginning would change the only reference point and that is bad.

    • At a system level uses exist as you pointed out, but are quite rare. I believe most uses of files are of the write once read many kind, so even truncate is not a critical feature and we could probably do without it (well some things would become more difficult, but nothing would become impossible).

    If we want more complex accesses (and there are indeed needs) we open files in random mode and add some internal data structure. Theses informations can also be shared between several files. This leads us to the last issue I see, probably the most important.

    • In a sense when we using random access files with some internal structure... we are still using files but we are not any more using files paradigm. Typical such cases are the databases where we want to perform insertion or removal of records without caring at all about their physical place. Databases can use files as low level implementation but for optimisation purposes some database editors choose to completely bypass filesystem (think about Oracle partitions).

    I see no technical reason why we couldn't do everything that is currently done in an operating system with files using a database as data storage layer. I even heard that NTFS has many common points with databases in it's internals. An operating system can (and probably will in some not so far future) use another paradigm than files one.

    Summarily i believe that's not a technical problem at all, just a change of paradigm and that removing the beginning is definitely not part of the current "files paradigm", but not a big and useful enough change to compell changing anything at all.

    0 讨论(0)
  • 2020-12-09 10:31

    may can achieve this goal in two steps

    long fileLength; //file total length
    long reserveLength; //reserve length until the file ending
    int fd; //file open for read & write
    
    sendfile(fd, fd, fileLength-reserveLength, reserveLength);
    ftruncate(fd, reserveLength);
    
    0 讨论(0)
提交回复
热议问题