Skip to content

Commit 9b1ec10

Browse files
calebsanderigaw
authored andcommitted
util: use cleanup functions
Use cleanup attributes from cleanup.h to avoid boilerplate cleanup code. Signed-off-by: Caleb Sander <[email protected]>
1 parent ea0bccb commit 9b1ec10

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

src/nvme/util.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <ccan/endian/endian.h>
2525

26+
#include "cleanup.h"
2627
#include "private.h"
2728
#include "util.h"
2829
#include "log.h"
@@ -427,7 +428,7 @@ void nvme_init_copy_range_f1(struct nvme_copy_range_f1 *copy, __u16 *nlbs,
427428
copy[i].elbatm = cpu_to_le16(elbatms[i]);
428429
copy[i].elbat = cpu_to_le16(elbats[i]);
429430
nvme_init_copy_range_elbt(copy[i].elbt, eilbrts[i]);
430-
}
431+
}
431432
}
432433

433434
void nvme_init_copy_range_f2(struct nvme_copy_range_f2 *copy, __u32 *snsids,
@@ -756,15 +757,14 @@ char *kv_keymatch(const char *kv, const char *key)
756757
static size_t read_file(const char * fname, char *buffer, size_t *bufsz)
757758
{
758759
char *p;
759-
FILE *file;
760+
_cleanup_file_ FILE *file;
760761
size_t len;
761762

762763
file = fopen(fname, "re");
763764
if (!file)
764765
return 0;
765766

766767
p = fgets(buffer, *bufsz, file);
767-
fclose(file);
768768

769769
if (!p)
770770
return 0;
@@ -806,7 +806,7 @@ size_t get_entity_name(char *buffer, size_t bufsz)
806806

807807
size_t get_entity_version(char *buffer, size_t bufsz)
808808
{
809-
FILE *file;
809+
_cleanup_file_ FILE *file;
810810
size_t num_bytes = 0;
811811

812812
/* /proc/sys/kernel/ostype typically contains the string "Linux" */
@@ -856,7 +856,6 @@ size_t get_entity_version(char *buffer, size_t bufsz)
856856
if (s)
857857
ver_id_len = copy_value(ver_id, sizeof(ver_id), s);
858858
}
859-
fclose(file);
860859

861860
if (name_len) {
862861
/* Append a space */
@@ -929,14 +928,13 @@ int nvme_uuid_from_string(const char *str, unsigned char uuid[NVME_UUID_LEN])
929928

930929
int nvme_uuid_random(unsigned char uuid[NVME_UUID_LEN])
931930
{
932-
int f;
931+
_cleanup_fd_ int f;
933932
ssize_t n;
934933

935934
f = open("/dev/urandom", O_RDONLY);
936935
if (f < 0)
937936
return -errno;
938937
n = read(f, uuid, NVME_UUID_LEN);
939-
close(f);
940938
if (n < 0)
941939
return -errno;
942940
else if (n != NVME_UUID_LEN)

0 commit comments

Comments
 (0)