[PATCH] D39514: Use allocate_file in FileOutputBuffer to avoid possible bus error.

Rafael Avila de Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 6 16:30:55 PST 2017


Rui Ueyama via Phabricator <reviews at reviews.llvm.org> writes:

> +  case fs::file_type::status_error: {
> +    // Create new file in same directory but with random name.
> +    SmallString<128> TempPath;
> +    int FD;
> +    if (auto EC = fs::createUniqueFile(Path + ".tmp%%%%%%%", FD, TempPath, Mode))
> +      return EC;
> +
> +    sys::RemoveFileOnSignal(TempPath);
> +
> +#ifndef LLVM_ON_WIN32
> +    // If you mmap a sparse file for writing and the disk becomes full
> +    // when writing to an unallocated block of the file, you'll receive
> +    // a signal (which is usually SIGBUS). There's no reliable and
> +    // portable way to gracefully handle such disk full situation. So,
> +    // in order to avoid it, we preallocate all disk blocks by calling
> +    // fallocate(2) or equivalent.
> +    //
> +    // If an operating system or a filesystem don't support fallocate,
> +    // we use in-memory buffer so that we can catch disk full error on
> +    // commit().
> +    //
> +    // On Windows, CreateFileMapping (the mmap function on Windows)
> +    // automatically extends the underlying file. We don't need to
> +    // extend the file beforehand, and calling _chsize (which is slow)
> +    // beforehand is just a waste of time.
> +    if (auto EC = fs::allocate_file(FD, Size)) {
> +      if (EC == errc::function_not_supported)
> +        return createInMemoryBuffer(Path, Size, Mode);
> +      return EC;

If allocate_file file fails, this will keep the temporary file on disk,
no?

Cheers,
Rafael


More information about the llvm-commits mailing list