onepub-dev / dart_posix

MIT License
12 stars 4 forks source link

mode not initialized in Stat class #1

Closed ab36245 closed 2 years ago

ab36245 commented 2 years ago

I'm trying to use the mode property of the Stat class, but as soon as I access it I get

LateInitializationError: Field 'mode' has not been initialized.

Looking at stat.dart it seems like mode is the only late initialized property:

class Stat {
  Stat._fromNativeStat(_stat_struct _stat)
      : deviceId = _stat.st_dev,
        inode = _stat.st_ino,
        nlink = _stat.st_nlink,
        uid = _stat.st_uid,
        gid = _stat.st_gid,
        rdev = _stat.st_rdev,
        size = _stat.st_size,
        blockSize = _stat.st_blksize,
        blocks = _stat.st_blocks,
        lastAccess =
            DateTime.fromMillisecondsSinceEpoch(_stat.st_atim.tv_sec * 1000),
        lastModified =
            DateTime.fromMillisecondsSinceEpoch(_stat.st_mtim.tv_sec * 1000),
        lastStatusChange =
            DateTime.fromMillisecondsSinceEpoch(_stat.st_ctim.tv_sec * 1000);

  /// st_dev - ID of device containing filesystem entity
  final int deviceId;

  /// st_ino - Inode number of filesystem entity
  final int inode;

  /// st_mode - File type and mode
  late final Mode mode;
...

The _fromNativeStat constructor looks like it just omits the mode initialization.

Is adding initialization to the constructor a nice quick fix?

bsutton commented 2 years ago

So a couple of issues.

I've been working in the nullsafety branch. I've now merged it into master so masters is once again master.

The merged code correctly initialises mode.

The code however has a second problem in that I'm having trouble with the mode bits. This could be because I'm doing the bit manipulation incorrectly or the struct mapping is wrong.

I'm bogged down with some other projects at the moment so If you have the time to look at the problem that would be great.

If you run the stat_test.dart unit test the problem shows itself.

I've pushed the latest code.

bsutton commented 2 years ago

I decided to have a quick look at the stat function and I found the problem.

I've just released 2.2.0 so try with the latest and let me know how you go.

ab36245 commented 2 years ago

Thanks for the very fast turnaround @bsutton. I'll test it tomorrow and let you know

ab36245 commented 2 years ago

Thanks @bsutton. I can confirm that the mode field is set correctly after a stat() call in 2.2.0.

There is a problem, however, with the mode bits as you say. I'll raise a separate issue for that.

bsutton commented 2 years ago

thanks for the confirmation.