Code Monkey home page Code Monkey logo

Comments (4)

morrychan avatar morrychan commented on August 19, 2024

so if this package can not support small size direct write, is there any other solution?

from directio.

ncw avatar ncw commented on August 19, 2024

You can write less than a block but you need to do it from an aligned buffer.

when buf size less than blockSize, it reports error when write after calling AlignedBlock(). It should be noted down if not support this case.

Can you give a code example please?

from directio.

AlexNk avatar AlexNk commented on August 19, 2024

This is not just for smaller sizes.

When the buffer is not even to BlockSize, it will generate an error:

package main

import (
	"github.com/ncw/directio"
	"log"
	"os"
)

func main() {
	block1 := directio.AlignedBlock(directio.BlockSize + 1)
	for i := 0; i < len(block1); i++ {
		block1[i] = 'A'
	}

	// Write the file
	out, err := directio.OpenFile("O_DIRECT.test", os.O_CREATE|os.O_WRONLY, 0666)
	defer out.Close()
	if err != nil {
		log.Fatal("Failed to directio.OpenFile for write: ", err)
	}
	_, err = out.Write(block1)
	if err != nil {
		log.Fatal("Failed to write: ", err)
	}
}

from directio.

ncw avatar ncw commented on August 19, 2024

I think the alignment rules depend on the file system and the kernel.

From man 2 open

   O_DIRECT
       The O_DIRECT flag may impose alignment restrictions on the  length  and
       address  of  user-space  buffers and the file offset of I/Os.  In Linux
       alignment restrictions vary by filesystem and kernel version and  might
       be  absent entirely.  However there is currently no filesystem-indepen‐
       dent interface for an application to discover these restrictions for  a
       given  file  or  filesystem.  Some filesystems provide their own inter‐
       faces for doing so, for example the XFS_IOC_DIOINFO  operation  in  xf‐
       sctl(3).

So I think getting an error on writing a non block sized chunk is normal.

from directio.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.