use common AutoDeleter types

Change-Id: I115e14b76f3ff049c5f7d9471efd3619a0038fcf
Reviewed-on: https://review.haiku-os.org/c/haiku/+/3483
Reviewed-by: Adrien Destugues <[email protected]>
Reviewed-by: Axel Dörfler <[email protected]>
This commit is contained in:
X512
2020-12-13 19:14:13 +00:00
committed by Adrien Destugues
parent 47404f12f2
commit cdccd323b5
19 changed files with 48 additions and 60 deletions
@@ -1,17 +0,0 @@
/*
* Copyright 2011, Oliver Tappe <[email protected]>
* Distributed under the terms of the MIT License.
*/
#ifndef UTILS_H
#define UTILS_H
#include <AutoDeleter.h>
#include <vfs.h>
typedef CObjectDeleter<vnode, void, vfs_put_vnode> VnodePutter;
#endif // UTILS_H
@@ -19,11 +19,11 @@
#include <vfs.h> #include <vfs.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterDrivers.h>
#include "DebugSupport.h" #include "DebugSupport.h"
#include "kernel_interface.h" #include "kernel_interface.h"
#include "Node.h" #include "Node.h"
#include "Utils.h"
// #pragma mark - Volume // #pragma mark - Volume
@@ -51,8 +51,7 @@ Volume::Mount(const char* parameterString)
{ {
const char* source = NULL; const char* source = NULL;
void* parameterHandle = parse_driver_settings_string(parameterString); void* parameterHandle = parse_driver_settings_string(parameterString);
CObjectDeleter<void, status_t, delete_driver_settings> DriverSettingsUnloader parameterDeleter(parameterHandle);
parameterDeleter(parameterHandle);
if (parameterHandle != NULL) if (parameterHandle != NULL)
source = get_driver_parameter(parameterHandle, "source", NULL, NULL); source = get_driver_parameter(parameterHandle, "source", NULL, NULL);
if (source == NULL || source[0] == '\0') { if (source == NULL || source[0] == '\0') {
@@ -12,12 +12,11 @@
#include <vfs.h> #include <vfs.h>
#include <AutoDeleter.h> #include <AutoDeleterDrivers.h>
#include "DebugSupport.h" #include "DebugSupport.h"
#include "kernel_interface.h" #include "kernel_interface.h"
#include "Node.h" #include "Node.h"
#include "Utils.h"
#include "Volume.h" #include "Volume.h"
@@ -6,7 +6,7 @@
#include "PackageFSRoot.h" #include "PackageFSRoot.h"
#include <AutoDeleter.h> #include <AutoDeleterDrivers.h>
#include <vfs.h> #include <vfs.h>
@@ -114,7 +114,7 @@ PackageFSRoot::RegisterVolume(Volume* volume)
relativeRootPath, strerror(error)); relativeRootPath, strerror(error));
RETURN_ERROR(error); RETURN_ERROR(error);
} }
CObjectDeleter<struct vnode, void, vfs_put_vnode> vnodePutter(vnode); VnodePutter vnodePutter(vnode);
// stat it // stat it
struct stat st; struct stat st;
@@ -8,7 +8,7 @@
#include <driver_settings.h> #include <driver_settings.h>
#include <AutoDeleter.h> #include <AutoDeleterDrivers.h>
#include <directories.h> #include <directories.h>
#include <fs/KPath.h> #include <fs/KPath.h>
#include <vfs.h> #include <vfs.h>
@@ -216,8 +216,7 @@ PackageSettings::Load(dev_t mountPointDeviceID, ino_t mountPointNodeID,
void* settingsHandle = load_driver_settings(path.Path()); void* settingsHandle = load_driver_settings(path.Path());
if (settingsHandle == NULL) if (settingsHandle == NULL)
return B_ENTRY_NOT_FOUND; return B_ENTRY_NOT_FOUND;
CObjectDeleter<void, status_t, unload_driver_settings> DriverSettingsUnloader settingsDeleter(settingsHandle);
settingsDeleter(settingsHandle);
const driver_settings* settings = get_driver_settings(settingsHandle); const driver_settings* settings = get_driver_settings(settingsHandle);
for (int i = 0; i < settings->parameter_count; i++) { for (int i = 0; i < settings->parameter_count; i++) {
@@ -22,6 +22,8 @@
#include <package/PackageInfoAttributes.h> #include <package/PackageInfoAttributes.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
#include <AutoDeleterDrivers.h>
#include <PackagesDirectoryDefs.h> #include <PackagesDirectoryDefs.h>
#include <vfs.h> #include <vfs.h>
@@ -329,8 +331,7 @@ Volume::Mount(const char* parameterString)
NULL); NULL);
} }
CObjectDeleter<void, status_t, delete_driver_settings> DriverSettingsUnloader parameterHandleDeleter(parameterHandle);
parameterHandleDeleter(parameterHandle);
if (packages != NULL && packages[0] == '\0') { if (packages != NULL && packages[0] == '\0') {
FATAL("invalid package folder ('packages' parameter)!\n"); FATAL("invalid package folder ('packages' parameter)!\n");
@@ -711,7 +712,7 @@ Volume::_LoadOldPackagesStates(const char* packagesState)
ERROR("Failed to open administrative directory: %s\n", strerror(errno)); ERROR("Failed to open administrative directory: %s\n", strerror(errno));
RETURN_ERROR(errno); RETURN_ERROR(errno);
} }
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
while (dirent* entry = readdir(dir)) { while (dirent* entry = readdir(dir)) {
if (strncmp(entry->d_name, "state_", 6) != 0 if (strncmp(entry->d_name, "state_", 6) != 0
@@ -903,7 +904,7 @@ Volume::_AddInitialPackagesFromDirectory()
fPackagesDirectory->Path(), strerror(errno)); fPackagesDirectory->Path(), strerror(errno));
RETURN_ERROR(errno); RETURN_ERROR(errno);
} }
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
while (dirent* entry = readdir(dir)) { while (dirent* entry = readdir(dir)) {
// skip "." and ".." // skip "." and ".."
@@ -1778,7 +1779,7 @@ Volume::_PublishShineThroughDirectories()
_RemoveNode(directory); _RemoveNode(directory);
continue; continue;
} }
CObjectDeleter<struct vnode, void, vfs_put_vnode> vnodePutter(vnode); VnodePutter vnodePutter(vnode);
// stat it // stat it
struct stat st; struct stat st;
+2 -1
View File
@@ -51,6 +51,7 @@
#include <AppMisc.h> #include <AppMisc.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
#include <cpu_type.h> #include <cpu_type.h>
#include <parsedate.h> #include <parsedate.h>
#include <system_revision.h> #include <system_revision.h>
@@ -1463,7 +1464,7 @@ AboutView::_AddCopyrightsFromAttribute()
close(attrFD); close(attrFD);
return; return;
} }
CObjectDeleter<FILE, int, fclose> _(attrFile); FileCloser _(attrFile);
// read and parse the copyrights // read and parse the copyrights
BMessage package; BMessage package;
+2 -2
View File
@@ -12,7 +12,7 @@
#include <termios.h> #include <termios.h>
#include <unistd.h> #include <unistd.h>
#include <AutoDeleter.h> #include <AutoDeleterPosix.h>
#include <user_group.h> #include <user_group.h>
@@ -38,7 +38,7 @@ read_password(const char* prompt, char* password, size_t bufferSize,
in = tty; in = tty;
out = tty; out = tty;
} }
CObjectDeleter<FILE, int, fclose> ttyCloser(tty); FileCloser ttyCloser(tty);
// disable echo // disable echo
int inFD = fileno(in); int inFD = fileno(in);
+2 -1
View File
@@ -14,6 +14,7 @@
#include <package/hpkg/HPKGDefs.h> #include <package/hpkg/HPKGDefs.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
status_t status_t
@@ -27,7 +28,7 @@ add_current_directory_entries(BPackageWriter& packageWriter,
strerror(errno)); strerror(errno));
return errno; return errno;
} }
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
while (dirent* entry = readdir(dir)) { while (dirent* entry = readdir(dir)) {
// skip "." and ".." // skip "." and ".."
+2 -1
View File
@@ -17,6 +17,7 @@
#include <String.h> #include <String.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
#include <StringForSize.h> #include <StringForSize.h>
#include <TextTable.h> #include <TextTable.h>
@@ -387,7 +388,7 @@ command_list(int argc, const char* const* argv)
strerror(errno)); strerror(errno));
return 1; return 1;
} }
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
TextTable table; TextTable table;
table.AddColumn("ID", B_ALIGN_RIGHT); table.AddColumn("ID", B_ALIGN_RIGHT);
+2 -2
View File
@@ -19,6 +19,7 @@
#include <Message.h> #include <Message.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterDrivers.h>
#include <package/PackageInfo.h> #include <package/PackageInfo.h>
@@ -360,8 +361,7 @@ BRepositoryInfo::_SetTo(const BEntry& entry)
void* settingsHandle = parse_driver_settings_string(configString.String()); void* settingsHandle = parse_driver_settings_string(configString.String());
if (settingsHandle == NULL) if (settingsHandle == NULL)
return B_BAD_DATA; return B_BAD_DATA;
CObjectDeleter<void, status_t, unload_driver_settings> DriverSettingsUnloader settingsHandleDeleter(settingsHandle);
settingsHandleDeleter(settingsHandle);
const char* name = get_driver_parameter(settingsHandle, "name", NULL, NULL); const char* name = get_driver_parameter(settingsHandle, "name", NULL, NULL);
const char* identifier = get_driver_parameter(settingsHandle, "identifier", NULL, NULL); const char* identifier = get_driver_parameter(settingsHandle, "identifier", NULL, NULL);
+5 -4
View File
@@ -33,6 +33,7 @@
#include <package/hpkg/PackageDataReader.h> #include <package/hpkg/PackageDataReader.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
#include <RangeArray.h> #include <RangeArray.h>
#include <package/hpkg/HPKGDefsPrivate.h> #include <package/hpkg/HPKGDefsPrivate.h>
@@ -1138,7 +1139,7 @@ PackageWriterImpl::_UpdateCheckEntryCollisions(Attribute* parentAttribute,
// first we check for colliding node attributes, though // first we check for colliding node attributes, though
if (DIR* attrDir = fs_fopen_attr_dir(fd)) { if (DIR* attrDir = fs_fopen_attr_dir(fd)) {
CObjectDeleter<DIR, int, fs_close_attr_dir> attrDirCloser(attrDir); AttrDirCloser attrDirCloser(attrDir);
while (dirent* entry = fs_read_attr_dir(attrDir)) { while (dirent* entry = fs_read_attr_dir(attrDir)) {
attr_info attrInfo; attr_info attrInfo;
@@ -1184,7 +1185,7 @@ PackageWriterImpl::_UpdateCheckEntryCollisions(Attribute* parentAttribute,
close(clonedFD); close(clonedFD);
throw status_t(errno); throw status_t(errno);
} }
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
while (dirent* entry = readdir(dir)) { while (dirent* entry = readdir(dir)) {
// skip "." and ".." // skip "." and ".."
@@ -1525,7 +1526,7 @@ PackageWriterImpl::_AddEntry(int dirFD, Entry* entry, const char* fileName,
// add attributes // add attributes
if (DIR* attrDir = fs_fopen_attr_dir(fd)) { if (DIR* attrDir = fs_fopen_attr_dir(fd)) {
CObjectDeleter<DIR, int, fs_close_attr_dir> attrDirCloser(attrDir); AttrDirCloser attrDirCloser(attrDir);
while (dirent* entry = fs_read_attr_dir(attrDir)) { while (dirent* entry = fs_read_attr_dir(attrDir)) {
attr_info attrInfo; attr_info attrInfo;
@@ -1586,7 +1587,7 @@ PackageWriterImpl::_AddDirectoryChildren(Entry* entry, int fd, char* pathBuffer)
close(clonedFD); close(clonedFD);
throw status_t(errno); throw status_t(errno);
} }
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
while (dirent* entry = readdir(dir)) { while (dirent* entry = readdir(dir)) {
// skip "." and ".." // skip "." and ".."
@@ -18,6 +18,7 @@
#include <Path.h> #include <Path.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
#include "PackageManagerUtils.h" #include "PackageManagerUtils.h"
@@ -210,7 +211,7 @@ BRepositoryBuilder::AddPackagesDirectory(const char* path)
DIR* dir = opendir(path); DIR* dir = opendir(path);
if (dir == NULL) if (dir == NULL)
DIE(errno, "failed to open package directory \"%s\"", path); DIE(errno, "failed to open package directory \"%s\"", path);
CObjectDeleter<DIR, int, closedir> dirCloser(dir); DirCloser dirCloser(dir);
// iterate through directory entries // iterate through directory entries
while (dirent* entry = readdir(dir)) { while (dirent* entry = readdir(dir)) {
+2 -2
View File
@@ -18,6 +18,7 @@
#include <string.h> #include <string.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterDrivers.h>
#include <File.h> #include <File.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <Path.h> #include <Path.h>
@@ -139,8 +140,7 @@ Settings::ReadSwapSettings()
void* settings = load_driver_settings(kVirtualMemorySettings); void* settings = load_driver_settings(kVirtualMemorySettings);
if (settings == NULL) if (settings == NULL)
return kErrorSettingsNotFound; return kErrorSettingsNotFound;
CObjectDeleter<void, status_t, unload_driver_settings> DriverSettingsUnloader settingDeleter(settings);
settingDeleter(settings);
const char* enabled = get_driver_parameter(settings, "vm", NULL, NULL); const char* enabled = get_driver_parameter(settings, "vm", NULL, NULL);
const char* automatic = get_driver_parameter(settings, "swap_auto", const char* automatic = get_driver_parameter(settings, "swap_auto",
@@ -20,6 +20,7 @@
#include <StringList.h> #include <StringList.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterPosix.h>
#include <LaunchRoster.h> #include <LaunchRoster.h>
#include <RegistrarDefs.h> #include <RegistrarDefs.h>
@@ -568,14 +569,14 @@ public:
debug_printf("REG: Failed to open passwd file \"%s\" for " debug_printf("REG: Failed to open passwd file \"%s\" for "
"writing: %s\n", kPasswdFile, strerror(errno)); "writing: %s\n", kPasswdFile, strerror(errno));
} }
CObjectDeleter<FILE, int, fclose> _1(passwdFile); FileCloser _1(passwdFile);
FILE* shadowFile = fopen(kShadowPwdFile, "w"); FILE* shadowFile = fopen(kShadowPwdFile, "w");
if (shadowFile == NULL) { if (shadowFile == NULL) {
debug_printf("REG: Failed to open shadow passwd file \"%s\" for " debug_printf("REG: Failed to open shadow passwd file \"%s\" for "
"writing: %s\n", kShadowPwdFile, strerror(errno)); "writing: %s\n", kShadowPwdFile, strerror(errno));
} }
CObjectDeleter<FILE, int, fclose> _2(shadowFile); FileCloser _2(shadowFile);
// write users // write users
for (map<uid_t, User*>::const_iterator it = fUsersByID.begin(); for (map<uid_t, User*>::const_iterator it = fUsersByID.begin();
@@ -694,7 +695,7 @@ public:
debug_printf("REG: Failed to open group file \"%s\" for " debug_printf("REG: Failed to open group file \"%s\" for "
"writing: %s\n", kGroupFile, strerror(errno)); "writing: %s\n", kGroupFile, strerror(errno));
} }
CObjectDeleter<FILE, int, fclose> _1(groupFile); FileCloser _1(groupFile);
// write groups // write groups
for (map<gid_t, Group*>::const_iterator it = fGroupsByID.begin(); for (map<gid_t, Group*>::const_iterator it = fGroupsByID.begin();
@@ -1245,7 +1246,7 @@ AuthenticationManager::_InitPasswdDB()
kPasswdFile, strerror(errno)); kPasswdFile, strerror(errno));
return errno; return errno;
} }
CObjectDeleter<FILE, int, fclose> _(file); FileCloser _(file);
char lineBuffer[LINE_MAX]; char lineBuffer[LINE_MAX];
while (char* line = fgets(lineBuffer, sizeof(lineBuffer), file)) { while (char* line = fgets(lineBuffer, sizeof(lineBuffer), file)) {
@@ -1294,7 +1295,7 @@ AuthenticationManager::_InitGroupDB()
kGroupFile, strerror(errno)); kGroupFile, strerror(errno));
return errno; return errno;
} }
CObjectDeleter<FILE, int, fclose> _(file); FileCloser _(file);
char lineBuffer[LINE_MAX]; char lineBuffer[LINE_MAX];
while (char* line = fgets(lineBuffer, sizeof(lineBuffer), file)) { while (char* line = fgets(lineBuffer, sizeof(lineBuffer), file)) {
@@ -1342,7 +1343,7 @@ AuthenticationManager::_InitShadowPwdDB()
kShadowPwdFile, strerror(errno)); kShadowPwdFile, strerror(errno));
return errno; return errno;
} }
CObjectDeleter<FILE, int, fclose> _(file); FileCloser _(file);
char lineBuffer[LINE_MAX]; char lineBuffer[LINE_MAX];
while (char* line = fgets(lineBuffer, sizeof(lineBuffer), file)) { while (char* line = fgets(lineBuffer, sizeof(lineBuffer), file)) {
@@ -8,7 +8,7 @@
#include <driver_settings.h> #include <driver_settings.h>
#include <AutoDeleter.h> #include <AutoDeleterDrivers.h>
#include <boot/vfs.h> #include <boot/vfs.h>
#include <system/directories.h> #include <system/directories.h>
@@ -54,8 +54,7 @@ PackageSettingsItem::Load(::Directory* systemDirectory, const char* name)
void* settingsHandle = load_driver_settings_file(fd); void* settingsHandle = load_driver_settings_file(fd);
if (settingsHandle == NULL) if (settingsHandle == NULL)
return NULL; return NULL;
CObjectDeleter<void, status_t, &unload_driver_settings> DriverSettingsUnloader settingsDeleter(settingsHandle);
settingsDeleter(settingsHandle);
const driver_settings* settings = get_driver_settings(settingsHandle); const driver_settings* settings = get_driver_settings(settingsHandle);
for (int i = 0; i < settings->parameter_count; i++) { for (int i = 0; i < settings->parameter_count; i++) {
+2 -1
View File
@@ -16,6 +16,7 @@
#include <OS.h> #include <OS.h>
#include <AutoDeleter.h> #include <AutoDeleter.h>
#include <AutoDeleterDrivers.h>
#include <BytePointer.h> #include <BytePointer.h>
#include <syscalls.h> #include <syscalls.h>
@@ -497,7 +498,7 @@ dup_foreign_fd(team_id fromTeam, int fd, bool kernel)
file_descriptor* descriptor = get_fd(fromContext, fd); file_descriptor* descriptor = get_fd(fromContext, fd);
if (descriptor == NULL) if (descriptor == NULL)
return B_FILE_ERROR; return B_FILE_ERROR;
CObjectDeleter<file_descriptor, void, put_fd> descriptorPutter(descriptor); DescriptorPutter descriptorPutter(descriptor);
// create a new FD in the target I/O context // create a new FD in the target I/O context
int result = new_fd(get_current_io_context(kernel), descriptor); int result = new_fd(get_current_io_context(kernel), descriptor);
+3 -2
View File
@@ -15,6 +15,7 @@
#include <heap.h> #include <heap.h>
#include <AutoDeleterDrivers.h>
// #pragma mark - AsyncIOCallback // #pragma mark - AsyncIOCallback
@@ -478,7 +479,7 @@ do_fd_io(int fd, io_request* request)
return B_FILE_ERROR; return B_FILE_ERROR;
} }
CObjectDeleter<file_descriptor, void, put_fd> descriptorPutter(descriptor); DescriptorPutter descriptorPutter(descriptor);
return vfs_vnode_io(vnode, descriptor->cookie, request); return vfs_vnode_io(vnode, descriptor->cookie, request);
} }
@@ -500,7 +501,7 @@ do_iterative_fd_io(int fd, io_request* request, iterative_io_get_vecs getVecs,
return B_FILE_ERROR; return B_FILE_ERROR;
} }
CObjectDeleter<file_descriptor, void, put_fd> descriptorPutter(descriptor); DescriptorPutter descriptorPutter(descriptor);
if (!HAS_FS_CALL(vnode, io)) { if (!HAS_FS_CALL(vnode, io)) {
// no io() call -- fall back to synchronous I/O // no io() call -- fall back to synchronous I/O
+2 -2
View File
@@ -21,7 +21,7 @@
#include <OS.h> #include <OS.h>
#include <KernelExport.h> #include <KernelExport.h>
#include <AutoDeleter.h> #include <AutoDeleterDrivers.h>
#include <symbol_versioning.h> #include <symbol_versioning.h>
@@ -2054,7 +2054,7 @@ _vm_map_file(team_id team, const char* name, void** _address,
status_t status = vfs_get_vnode_from_fd(fd, kernel, &vnode); status_t status = vfs_get_vnode_from_fd(fd, kernel, &vnode);
if (status < B_OK) if (status < B_OK)
return status; return status;
CObjectDeleter<struct vnode, void, vfs_put_vnode> vnodePutter(vnode); VnodePutter vnodePutter(vnode);
// If we're going to pre-map pages, we need to reserve the pages needed by // If we're going to pre-map pages, we need to reserve the pages needed by
// the mapping backend upfront. // the mapping backend upfront.