* Replaced one occurence of sprintf() with snprintf().

* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40224 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2011-01-12 18:42:48 +00:00
parent fd4998dce7
commit b16710b814
2 changed files with 70 additions and 65 deletions

View File

@ -1,9 +1,12 @@
/* /*
* Copyright 2006-2011, Axel Dörfler, axeld@pinc-software.de.
* Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de. * Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*/ */
#include "KDiskDevice.h"
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
@ -13,11 +16,11 @@
#include <Drivers.h> #include <Drivers.h>
#include "ddm_userland_interface.h" #include "ddm_userland_interface.h"
#include "KDiskDevice.h"
#include "KDiskDeviceUtils.h" #include "KDiskDeviceUtils.h"
#include "KPath.h" #include "KPath.h"
#include "UserDataWriter.h" #include "UserDataWriter.h"
// debugging // debugging
//#define DBG(x) //#define DBG(x)
#define DBG(x) x #define DBG(x) x
@ -46,7 +49,7 @@ KDiskDevice::~KDiskDevice()
status_t status_t
KDiskDevice::SetTo(const char *path) KDiskDevice::SetTo(const char* path)
{ {
// check initialization and parameter // check initialization and parameter
status_t error = InitCheck(); status_t error = InitCheck();
@ -156,7 +159,7 @@ KDiskDevice::PublishDevice()
return B_OK; return B_OK;
} }
// UnpublishDevice
status_t status_t
KDiskDevice::UnpublishDevice() KDiskDevice::UnpublishDevice()
{ {
@ -165,7 +168,7 @@ KDiskDevice::UnpublishDevice()
return B_OK; return B_OK;
} }
// RepublishDevice
status_t status_t
KDiskDevice::RepublishDevice() KDiskDevice::RepublishDevice()
{ {
@ -174,42 +177,42 @@ KDiskDevice::RepublishDevice()
return B_OK; return B_OK;
} }
// SetDeviceFlags
void void
KDiskDevice::SetDeviceFlags(uint32 flags) KDiskDevice::SetDeviceFlags(uint32 flags)
{ {
fDeviceData.flags = flags; fDeviceData.flags = flags;
} }
// DeviceFlags
uint32 uint32
KDiskDevice::DeviceFlags() const KDiskDevice::DeviceFlags() const
{ {
return fDeviceData.flags; return fDeviceData.flags;
} }
// IsReadOnlyMedia
bool bool
KDiskDevice::IsReadOnlyMedia() const KDiskDevice::IsReadOnlyMedia() const
{ {
return fDeviceData.geometry.read_only; return fDeviceData.geometry.read_only;
} }
// IsWriteOnce
bool bool
KDiskDevice::IsWriteOnce() const KDiskDevice::IsWriteOnce() const
{ {
return fDeviceData.geometry.write_once; return fDeviceData.geometry.write_once;
} }
// IsRemovable
bool bool
KDiskDevice::IsRemovable() const KDiskDevice::IsRemovable() const
{ {
return fDeviceData.geometry.removable; return fDeviceData.geometry.removable;
} }
// HasMedia
bool bool
KDiskDevice::HasMedia() const KDiskDevice::HasMedia() const
{ {
@ -254,15 +257,14 @@ KDiskDevice::UpdateGeometry()
} }
// SetPath
status_t status_t
KDiskDevice::SetPath(const char *path) KDiskDevice::SetPath(const char* path)
{ {
return set_string(fDeviceData.path, path); return set_string(fDeviceData.path, path);
} }
// Path
const char * const char*
KDiskDevice::Path() const KDiskDevice::Path() const
{ {
return fDeviceData.path; return fDeviceData.path;
@ -270,7 +272,7 @@ KDiskDevice::Path() const
status_t status_t
KDiskDevice::GetFileName(char *buffer, size_t size) const KDiskDevice::GetFileName(char* buffer, size_t size) const
{ {
if (strlcpy(buffer, "raw", size) >= size) if (strlcpy(buffer, "raw", size) >= size)
return B_NAME_TOO_LONG; return B_NAME_TOO_LONG;
@ -278,9 +280,8 @@ KDiskDevice::GetFileName(char *buffer, size_t size) const
} }
// GetPath
status_t status_t
KDiskDevice::GetPath(KPath *path) const KDiskDevice::GetPath(KPath* path) const
{ {
if (!path || path->InitCheck() != B_OK) if (!path || path->InitCheck() != B_OK)
return B_BAD_VALUE; return B_BAD_VALUE;
@ -289,51 +290,50 @@ KDiskDevice::GetPath(KPath *path) const
return path->SetPath(fDeviceData.path); return path->SetPath(fDeviceData.path);
} }
// SetFD
void void
KDiskDevice::SetFD(int fd) KDiskDevice::SetFD(int fd)
{ {
fFD = fd; fFD = fd;
} }
// FD
int int
KDiskDevice::FD() const KDiskDevice::FD() const
{ {
return fFD; return fFD;
} }
// DeviceData
disk_device_data * disk_device_data*
KDiskDevice::DeviceData() KDiskDevice::DeviceData()
{ {
return &fDeviceData; return &fDeviceData;
} }
// DeviceData
const disk_device_data * const disk_device_data*
KDiskDevice::DeviceData() const KDiskDevice::DeviceData() const
{ {
return &fDeviceData; return &fDeviceData;
} }
// WriteUserData
void void
KDiskDevice::WriteUserData(UserDataWriter &writer, user_partition_data *data) KDiskDevice::WriteUserData(UserDataWriter& writer, user_partition_data* data)
{ {
return KPartition::WriteUserData(writer, data); return KPartition::WriteUserData(writer, data);
} }
// WriteUserData
void void
KDiskDevice::WriteUserData(UserDataWriter &writer) KDiskDevice::WriteUserData(UserDataWriter& writer)
{ {
KPartition *partition = this; KPartition* partition = this;
user_disk_device_data *data user_disk_device_data* data
= writer.AllocateDeviceData(partition->CountChildren()); = writer.AllocateDeviceData(partition->CountChildren());
char *path = writer.PlaceString(Path()); char* path = writer.PlaceString(Path());
if (data) { if (data != NULL) {
data->device_flags = DeviceFlags(); data->device_flags = DeviceFlags();
data->path = path; data->path = path;
writer.AddRelocationEntry(&data->path); writer.AddRelocationEntry(&data->path);
@ -342,7 +342,7 @@ KDiskDevice::WriteUserData(UserDataWriter &writer)
partition->WriteUserData(writer, NULL); partition->WriteUserData(writer, NULL);
} }
// Dump
void void
KDiskDevice::Dump(bool deep, int32 level) KDiskDevice::Dump(bool deep, int32 level)
{ {
@ -353,9 +353,9 @@ KDiskDevice::Dump(bool deep, int32 level)
KPartition::Dump(deep, 0); KPartition::Dump(deep, 0);
} }
// GetMediaStatus
status_t status_t
KDiskDevice::GetMediaStatus(status_t *mediaStatus) KDiskDevice::GetMediaStatus(status_t* mediaStatus)
{ {
status_t error = B_OK; status_t error = B_OK;
if (ioctl(fFD, B_GET_MEDIA_STATUS, mediaStatus) != 0) if (ioctl(fFD, B_GET_MEDIA_STATUS, mediaStatus) != 0)
@ -376,16 +376,16 @@ KDiskDevice::GetMediaStatus(status_t *mediaStatus)
return error; return error;
} }
// GetGeometry
status_t status_t
KDiskDevice::GetGeometry(device_geometry *geometry) KDiskDevice::GetGeometry(device_geometry* geometry)
{ {
if (ioctl(fFD, B_GET_GEOMETRY, geometry) != 0) if (ioctl(fFD, B_GET_GEOMETRY, geometry) != 0)
return errno; return errno;
return B_OK; return B_OK;
} }
// _InitPartitionData
void void
KDiskDevice::_InitPartitionData() KDiskDevice::_InitPartitionData()
{ {
@ -429,4 +429,3 @@ KDiskDevice::_UpdateDeviceFlags()
if (fDeviceData.geometry.write_once) if (fDeviceData.geometry.write_once)
SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_WRITE_ONCE); SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_WRITE_ONCE);
} }

View File

@ -1,16 +1,21 @@
// KFileDiskDevice.cpp /*
* Copyright 2003-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
* Distributed under the terms of the MIT License.
*/
#include <KFileDiskDevice.h>
#include <errno.h> #include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <KernelExport.h>
#include <Drivers.h>
#include <devfs.h> #include <devfs.h>
#include <Drivers.h>
#include <KernelExport.h>
#include <KDiskDeviceUtils.h> #include <KDiskDeviceUtils.h>
#include <KFileDiskDevice.h>
#include <KPath.h> #include <KPath.h>
@ -19,26 +24,27 @@
#define DBG(x) x #define DBG(x) x
#define OUT dprintf #define OUT dprintf
static const char *kFileDevicesDir = "/dev/disk/virtual/files";
static const char* kFileDevicesDir = "/dev/disk/virtual/files";
// constructor
KFileDiskDevice::KFileDiskDevice(partition_id id) KFileDiskDevice::KFileDiskDevice(partition_id id)
: KDiskDevice(id), :
fFilePath(NULL) KDiskDevice(id),
fFilePath(NULL)
{ {
SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_IS_FILE); SetDeviceFlags(DeviceFlags() | B_DISK_DEVICE_IS_FILE);
} }
// destructor
KFileDiskDevice::~KFileDiskDevice() KFileDiskDevice::~KFileDiskDevice()
{ {
Unset(); Unset();
} }
// SetTo
status_t status_t
KFileDiskDevice::SetTo(const char *filePath, const char *devicePath) KFileDiskDevice::SetTo(const char* filePath, const char* devicePath)
{ {
// check params // check params
if (!filePath || strlen(filePath) > B_PATH_NAME_LENGTH if (!filePath || strlen(filePath) > B_PATH_NAME_LENGTH
@ -60,7 +66,7 @@ KFileDiskDevice::SetTo(const char *filePath, const char *devicePath)
return B_BAD_VALUE; return B_BAD_VALUE;
// create the device, if requested // create the device, if requested
KPath tmpDevicePath; KPath tmpDevicePath;
if (!devicePath) { if (devicePath == NULL) {
// no device path: we shall create a new device entry // no device path: we shall create a new device entry
if (tmpDevicePath.InitCheck() != B_OK) if (tmpDevicePath.InitCheck() != B_OK)
return tmpDevicePath.InitCheck(); return tmpDevicePath.InitCheck();
@ -101,7 +107,7 @@ KFileDiskDevice::SetTo(const char *filePath, const char *devicePath)
return B_OK; return B_OK;
} }
// Unset
void void
KFileDiskDevice::Unset() KFileDiskDevice::Unset()
{ {
@ -118,23 +124,23 @@ KFileDiskDevice::Unset()
fFilePath = NULL; fFilePath = NULL;
} }
// InitCheck
status_t status_t
KFileDiskDevice::InitCheck() const KFileDiskDevice::InitCheck() const
{ {
return KDiskDevice::InitCheck(); return KDiskDevice::InitCheck();
} }
// FilePath
const char * const char*
KFileDiskDevice::FilePath() const KFileDiskDevice::FilePath() const
{ {
return fFilePath; return fFilePath;
} }
// GetMediaStatus
status_t status_t
KFileDiskDevice::GetMediaStatus(status_t *mediaStatus) KFileDiskDevice::GetMediaStatus(status_t* mediaStatus)
{ {
// check the file // check the file
struct stat st; struct stat st;
@ -145,9 +151,9 @@ KFileDiskDevice::GetMediaStatus(status_t *mediaStatus)
return B_OK; return B_OK;
} }
// GetGeometry
status_t status_t
KFileDiskDevice::GetGeometry(device_geometry *geometry) KFileDiskDevice::GetGeometry(device_geometry* geometry)
{ {
// check the file // check the file
struct stat st; struct stat st;
@ -177,25 +183,25 @@ KFileDiskDevice::GetGeometry(device_geometry *geometry)
return B_OK; return B_OK;
} }
// _RegisterDevice
status_t status_t
KFileDiskDevice::_RegisterDevice(const char *file, const char *device) KFileDiskDevice::_RegisterDevice(const char* file, const char* device)
{ {
return devfs_publish_file_device(device + 5, file); return devfs_publish_file_device(device + 5, file);
// we need to remove the "/dev/" part from the path // we need to remove the "/dev/" part from the path
} }
// _UnregisterDevice
status_t status_t
KFileDiskDevice::_UnregisterDevice(const char *_device) KFileDiskDevice::_UnregisterDevice(const char* _device)
{ {
return devfs_unpublish_file_device(_device + 5); return devfs_unpublish_file_device(_device + 5);
// we need to remove the "/dev/" part from the path // we need to remove the "/dev/" part from the path
} }
// _GetDirectoryPath
status_t status_t
KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath *path) KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath* path)
{ {
if (path == NULL) if (path == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
@ -206,7 +212,7 @@ KFileDiskDevice::_GetDirectoryPath(partition_id id, KPath *path)
status_t error = path->SetPath(kFileDevicesDir); status_t error = path->SetPath(kFileDevicesDir);
if (error == B_OK) { if (error == B_OK) {
char idBuffer[12]; char idBuffer[12];
sprintf(idBuffer, "%ld", id); snprintf(idBuffer, sizeof(idBuffer), "%ld", id);
error = path->Append(idBuffer); error = path->Append(idBuffer);
} }
return error; return error;