Wait, if (flags and O_SYNC) is wrong?

I needed to convert file status flags between operating systems yesterday. They are the values you pass as the second argument to openO_NONBLOCK, O_NOATIME, O_SYNC, O_DSYNC, and so on:

int open(const char *path, int flags, /* mode_t mode */ );

So naturally I wrote it like this:

int dst_flags = 0;
if (src_flags & SRC_O_NONBLOCK) dst_flags |= DST_O_NONBLOCK;
if (src_flags & SRC_O_NOATIME) dst_flags |= DST_O_NOATIME;
if (src_flags & SRC_O_SYNC) dst_flags |= DST_O_SYNC;
if (src_flags & SRC_O_DSYNC) dst_flags |= DST_O_DSYNC;
...

It turns out this code has a subtle bug.

添加评论
点赞收藏
点踩分享查看原文
评论
?
参与讨论