From ff4a31402218e5283d93ae210ef46eeca9d7ad53 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 20 May 2020 13:46:03 +0200 Subject: [PATCH] use fstat when we can This avoids having things change between the stat and open. --- spa/plugins/v4l2/v4l2-utils.c | 27 +++++++++++++-------------- src/tools/midifile.c | 12 +++++------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/spa/plugins/v4l2/v4l2-utils.c b/spa/plugins/v4l2/v4l2-utils.c index 899c4d897..eac07064f 100644 --- a/spa/plugins/v4l2/v4l2-utils.c +++ b/spa/plugins/v4l2/v4l2-utils.c @@ -59,21 +59,7 @@ int spa_v4l2_open(struct spa_v4l2_device *dev, const char *path) spa_log_debug(dev->log, "v4l2: Playback device is '%s'", path); - if (stat(path, &st) < 0) { - err = errno; - spa_log_error(dev->log, "v4l2: Cannot identify '%s': %d, %s", - path, err, strerror(err)); - goto error; - } - - if (!S_ISCHR(st.st_mode)) { - spa_log_error(dev->log, "v4l2: %s is no device", path); - err = ENODEV; - goto error; - } - dev->fd = open(path, O_RDWR | O_NONBLOCK, 0); - if (dev->fd == -1) { err = errno; spa_log_error(dev->log, "v4l2: Cannot open '%s': %d, %s", @@ -81,6 +67,19 @@ int spa_v4l2_open(struct spa_v4l2_device *dev, const char *path) goto error; } + if (fstat(dev->fd, &st) < 0) { + err = errno; + spa_log_error(dev->log, "v4l2: Cannot identify '%s': %d, %s", + path, err, strerror(err)); + goto error_close; + } + + if (!S_ISCHR(st.st_mode)) { + spa_log_error(dev->log, "v4l2: %s is no device", path); + err = ENODEV; + goto error_close; + } + if (xioctl(dev->fd, VIDIOC_QUERYCAP, &dev->cap) < 0) { err = errno; spa_log_error(dev->log, "v4l2: '%s' QUERYCAP: %m", path); diff --git a/src/tools/midifile.c b/src/tools/midifile.c index 2c9f46fdc..b6e3d5998 100644 --- a/src/tools/midifile.c +++ b/src/tools/midifile.c @@ -139,17 +139,15 @@ static int open_read(struct midi_file *mf, const char *filename, struct midi_fil uint16_t i; struct stat st; - if (stat(filename, &st) < 0) { - res = -errno; - goto exit; - } - - mf->size = st.st_size; - if ((mf->fd = open(filename, O_RDONLY)) < 0) { res = -errno; goto exit; } + if (fstat(mf->fd, &st) < 0) { + res = -errno; + goto exit_close; + } + mf->size = st.st_size; mf->data = mmap(NULL, mf->size, PROT_READ, MAP_SHARED, mf->fd, 0); if (mf->data == MAP_FAILED) {