Skip to content

[FFI][full dart code, equal c code, output] open return-ing error, for direct and sync io #53526

Closed
@gintominto5329

Description

@gintominto5329

hello,

I have created a DBMS, which need to do DIRECT and SYNC, io ops, on LARGE files.

The open ffi function returning error 20.

SDK version

Dart SDK: 3.2.0-140.0.dev (Thu Sep 7 01:04:10 2023 -0700); "linux_x64", "debian-latest", "amd64"

Dart code (work, but ffi function returns -1(errno 20))

import "dart:convert" as convert;
import "dart:ffi" as ffi;
import "package:ffi/ffi.dart" as ffi;

void main() {
  const //
      O_CREAT = 0100,
      O_RDWR = 02,
      O_SYNC = 04010000,
      O_DIRECT = 040000,
      O_DSYNC = 010000,
      O_LARGEFILE = 0100000,
      S_IRUSR = 0400,
      S_IWUSR = 0200;

  final //
      path = (const convert.Utf8Encoder()).convert("/tmp/m.c"),
      len = path.length,
      memory = ffi.malloc.allocate<ffi.Uint8>(len + 1 /* for string de-limiter */
          );

  {
    final array = memory.asTypedList(len + 1);

    int i = 0;

    while (i < len) //
      array[i] = path[i++];

    array[len] = 0;
  }

  final //
      fd = ffi.DynamicLibrary.process().lookupFunction<
          ffi.Int32 Function(ffi.Pointer<ffi.Uint8> pathname, ffi.Int32 flags, ffi.Uint32 mode), //
          int Function(ffi.Pointer<ffi.Uint8> file__path, int flags, int mode)>(
        "open64",
      )(
        memory,
        (((O_DSYNC | O_LARGEFILE) | O_SYNC | O_DIRECT) | O_CREAT | O_RDWR),
        (S_IRUSR | S_IWUSR),
      ),
      errno = ffi.DynamicLibrary.process()
          .lookupFunction<
              ffi.Pointer<ffi.Int32> Function(), //
              ffi.Pointer<ffi.Int32> Function()>(
            "__errno_location",
          )()
          .value;

  print("open: $fd");
  print("errno: $errno");
}

Output of dart code

open: -1
errno: 20

Equivalent C code (working correct, fully)

#define _GNU_SOURCE 1
#define _FILE_OFFSET_BITS 64

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>

int main()
{
	int fd = open64(
      "/tmp/m.c",
      (((O_DSYNC | O_LARGEFILE) | O_SYNC | O_DIRECT) | O_CREAT | O_RDWR),
      (S_IRUSR | S_IWUSR)
    );

	printf("%d(fd)\n", fd);
}

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions