isodrive: uncrustify code to google cpp writing style

This commit is contained in:
XeonDead
2023-06-01 13:34:22 +03:00
parent 49853f73e5
commit 56b8356b64
5 changed files with 230 additions and 202 deletions
+93 -82
View File
@@ -11,109 +11,120 @@
bool supported() bool supported()
{ {
return fs_mount_point((char*)"configfs") != nullptr; return fs_mount_point((char *)"configfs") != nullptr;
} }
char *get_gadget_root() char* get_gadget_root()
{ {
char *configFsRoot = fs_mount_point((char*)"configfs"); char *configFsRoot = fs_mount_point((char *)"configfs");
char *usbGadgetRoot = strjin(configFsRoot, (char*)"/usb_gadget/"); char *usbGadgetRoot = strjin(configFsRoot, (char *)"/usb_gadget/");
char *gadgetRoot = nullptr; char *gadgetRoot = nullptr;
struct dirent *entry = nullptr; struct dirent *entry = nullptr;
DIR *dp = nullptr; DIR *dp = nullptr;
dp = opendir(usbGadgetRoot);
if (dp != nullptr) dp = opendir(usbGadgetRoot);
while ((entry = readdir(dp)))
{ if (dp != nullptr)
if (entry->d_name[0] != '.') while ((entry = readdir(dp)))
{ {
char *gadget = strjin(usbGadgetRoot, entry->d_name); if (entry->d_name[0] != '.')
if (sysfs_read(strjin(gadget, (char*)"/UDC")) != nullptr) {
{ char *gadget = strjin(usbGadgetRoot, entry->d_name);
gadgetRoot = gadget;
break; if (sysfs_read(strjin(gadget, (char *)"/UDC")) != nullptr)
} {
} gadgetRoot = gadget;
} break;
return gadgetRoot; }
}
}
return gadgetRoot;
} }
char *get_config_root() char* get_config_root()
{ {
char *gadgetRoot = get_gadget_root(); char *gadgetRoot = get_gadget_root();
if (gadgetRoot == nullptr)
{
return nullptr;
}
char *usbConfigRoot = strjin(gadgetRoot, (char*)"/configs/");
char *configRoot = nullptr;
struct dirent *entry = nullptr; if (gadgetRoot == nullptr)
DIR *dp = nullptr; {
dp = opendir(usbConfigRoot); return nullptr;
if (dp != nullptr) }
while ((entry = readdir(dp))) char *usbConfigRoot = strjin(gadgetRoot, (char *)"/configs/");
{ char *configRoot = nullptr;
if (entry->d_name[0] != '.')
{ struct dirent *entry = nullptr;
configRoot = strjin(usbConfigRoot, entry->d_name); DIR *dp = nullptr;
break;
} dp = opendir(usbConfigRoot);
}
return configRoot; if (dp != nullptr)
while ((entry = readdir(dp)))
{
if (entry->d_name[0] != '.')
{
configRoot = strjin(usbConfigRoot, entry->d_name);
break;
}
}
return configRoot;
} }
void mount_iso(char *iso_path, char *cdrom, char *ro) void mount_iso(char *iso_path, char *cdrom, char *ro)
{ {
char *gadgetRoot = get_gadget_root(); char *gadgetRoot = get_gadget_root();
if (gadgetRoot == nullptr)
{
printf("No active gadget found\n");
return;
}
char *configRoot = get_config_root();
char *udc = get_udc();
char *functionRoot = strjin(gadgetRoot, (char*)"/functions");
char *massStorageRoot = strjin(functionRoot, (char*)"/mass_storage.0");
char *lunRoot = strjin(massStorageRoot, (char*)"/lun.0");
char *stallFile = strjin(massStorageRoot, (char*)"/stall"); if (gadgetRoot == nullptr)
char *udcFile = strjin(gadgetRoot, (char*)"/UDC"); {
char *lunFile = strjin(lunRoot, (char*)"/file"); printf("No active gadget found\n");
char *lunCdRom = strjin(lunRoot, (char*)"/cdrom"); return;
char *lunRo = strjin(lunRoot, (char*)"/ro"); }
char *configRoot = get_config_root();
char *udc = get_udc();
char *functionRoot = strjin(gadgetRoot, (char *)"/functions");
char *massStorageRoot = strjin(functionRoot, (char *)"/mass_storage.0");
char *lunRoot = strjin(massStorageRoot, (char *)"/lun.0");
set_udc((char*)"", gadgetRoot); char *stallFile = strjin(massStorageRoot, (char *)"/stall");
char *udcFile = strjin(gadgetRoot, (char *)"/UDC");
char *lunFile = strjin(lunRoot, (char *)"/file");
char *lunCdRom = strjin(lunRoot, (char *)"/cdrom");
char *lunRo = strjin(lunRoot, (char *)"/ro");
if (!isdir(massStorageRoot)) set_udc((char *)"", gadgetRoot);
{
mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
if (!isdir(strjin(configRoot, (char*)"/mass_storage.0")))
{
symlink(massStorageRoot, strjin(configRoot, (char*)"/mass_storage.0"));
}
sysfs_write(lunFile, iso_path);
sysfs_write(lunCdRom, cdrom);
sysfs_write(lunRo, ro);
set_udc(udc, gadgetRoot); if (!isdir(massStorageRoot))
{
mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
if (!isdir(strjin(configRoot, (char *)"/mass_storage.0")))
{
symlink(massStorageRoot, strjin(configRoot, (char *)"/mass_storage.0"));
}
sysfs_write(lunFile, iso_path);
sysfs_write(lunCdRom, cdrom);
sysfs_write(lunRo, ro);
set_udc(udc, gadgetRoot);
} }
void set_udc(char *udc, char *gadget) void set_udc(char *udc, char *gadget)
{ {
char *udcFile = strjin(gadget, (char*)"/UDC"); char *udcFile = strjin(gadget, (char *)"/UDC");
sysfs_write(udcFile, udc);
sysfs_write(udcFile, udc);
} }
char *get_udc() char* get_udc()
{ {
char *gadget_root = get_gadget_root(); char *gadget_root = get_gadget_root();
if (gadget_root == nullptr)
{ if (gadget_root == nullptr)
return nullptr; {
} return nullptr;
char *udcFile = strjin(gadget_root, (char*)"/UDC"); }
return sysfs_read(udcFile); char *udcFile = strjin(gadget_root, (char *)"/UDC");
return sysfs_read(udcFile);
} }
+11 -7
View File
@@ -1,12 +1,16 @@
#ifndef CONFIGFSISOMANAGER_H #ifndef CONFIGFSISOMANAGER_H
#define CONFIGFSISOMANAGER_H #define CONFIGFSISOMANAGER_H
bool supported(); bool supported();
char *get_gadget_root(); char* get_gadget_root();
char *get_config_root(); char* get_config_root();
void mount_iso(char *iso_path, char *cdrom, char *ro); void mount_iso(char *iso_path,
void set_udc(char *udc, char *gadget); char *cdrom,
char *get_udc(); char *ro);
void umount_iso();
void set_udc(char *udc,
char *gadget);
char* get_udc();
#endif #endif // ifndef CONFIGFSISOMANAGER_H
+8 -7
View File
@@ -1,11 +1,12 @@
#ifndef UTIL_H #ifndef UTIL_H
#define UTIL_H #define UTIL_H
char *fs_mount_point(char *filesystem_type); char* fs_mount_point(char *filesystem_type);
char *strjin(char *w1, char *w2); char* strjin(char *w1,
bool isdir(char *path); char *w2);
char *sysfs_read(char *path); bool isdir(char *path);
void sysfs_write(char *path, char *content); char* sysfs_read(char *path);
void sysfs_write(char *path,
#endif char *content);
#endif // ifndef UTIL_H
+50 -46
View File
@@ -3,55 +3,59 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int main(int argc, char* argv[]){ int main(int argc, char *argv[])
char *iso_target = argv[1]; {
char *cdrom = (char*)"0"; char *iso_target = argv[1];
char *ro = (char*)"1"; char *cdrom = (char *)"0";
for (int i = 1; i < argc; i++) char *ro = (char *)"1";
{
if (strcmp(argv[i], "-rw") == 0)
{
ro = (char*)"0";
}
else if (strcmp(argv[i], "-cdrom") == 0)
{
cdrom = (char*)"1";
}
else
{
iso_target = argv[i];
}
}
if (argc == 1) for (int i = 1; i < argc; i++)
{ {
printf("Usage:\n"); if (strcmp(argv[i], "-rw") == 0)
printf("cdrom [FILE]... [OPTION]...\n"); {
printf("Mounts the given FILE as a bootable device using configfs.\n"); ro = (char *)"0";
printf("Run without any arguments to unmount any mounted files and display this help message.\n\n"); }
else if (strcmp(argv[i], "-cdrom") == 0)
{
cdrom = (char *)"1";
}
else
{
iso_target = argv[i];
}
}
printf("Optional arguments:\n"); if (argc == 1)
printf("-rw\t Mounts the file in read write mode.\n"); {
printf("-cdrom\t Mounts the file as a cdrom.\n\n"); printf("Usage:\n");
printf("UMOUNT:\n"); printf("cdrom [FILE]... [OPTION]...\n");
} printf("Mounts the given FILE as a bootable device using configfs.\n");
else printf(
{ "Run without any arguments to unmount any mounted files and display this help message.\n\n");
printf("MOUNT:\n");
}
if (!supported()) printf("Optional arguments:\n");
{ printf("-rw\t Mounts the file in read write mode.\n");
printf("Device does not support configfs usb gadget\n"); printf("-cdrom\t Mounts the file as a cdrom.\n\n");
return 1; printf("UMOUNT:\n");
} }
if (getuid() != 0) else
{ {
printf("Permission denied\n"); printf("MOUNT:\n");
return 1; }
}
mount_iso(iso_target, cdrom, ro); if (!supported())
{
printf("Device does not support configfs usb gadget\n");
return 1;
}
return 0; if (getuid() != 0)
{
printf("Permission denied\n");
return 1;
}
mount_iso(iso_target, cdrom, ro);
return 0;
} }
+61 -53
View File
@@ -11,74 +11,82 @@
#include <cstdio> #include <cstdio>
#include <dirent.h> #include <dirent.h>
char *fs_mount_point(char *filesystem_type) { char* fs_mount_point(char *filesystem_type)
struct mntent *ent; {
FILE *mounts; struct mntent *ent;
char *mount_point = nullptr; FILE *mounts;
char *mount_point = nullptr;
mounts = setmntent("/proc/mounts", "r"); mounts = setmntent("/proc/mounts", "r");
while (nullptr != (ent = getmntent(mounts))) {
if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
{
mount_point = ent->mnt_dir;
break;
}
}
// Alternate search location on Android while (nullptr != (ent = getmntent(mounts)))
if (mount_point == nullptr) {
{ if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
const char *alt_usb_gadget = "/config/usb_gadget"; {
DIR *dir = opendir(alt_usb_gadget); mount_point = ent->mnt_dir;
if (dir) break;
{ }
mount_point = (char*)"/config"; }
}
} // Alternate search location on Android
return mount_point; if (mount_point == nullptr)
{
const char *alt_usb_gadget = "/config/usb_gadget";
DIR *dir = opendir(alt_usb_gadget);
if (dir)
{
mount_point = (char *)"/config";
}
}
return mount_point;
} }
char *strjin(char *w1, char *w2) char* strjin(char *w1, char *w2)
{ {
char *s = new char[strlen(w1)+strlen(w2)+1]; char *s = new char[strlen(w1) + strlen(w2) + 1];
strcpy(s, w1);
strcat(s, w2); strcpy(s, w1);
return s; strcat(s, w2);
return s;
} }
bool isdir(char *path) bool isdir(char *path)
{ {
struct stat sb; struct stat sb;
if (stat(path, &sb) == 0)
return true; if (stat(path, &sb) == 0)
else return true;
return false; else
return false;
} }
void sysfs_write(char *path, char *content) void sysfs_write(char *path, char *content)
{ {
printf("Write: %s -> %s\n", content, path); printf("Write: %s -> %s\n", content, path);
std::ofstream sysfsFile(path); std::ofstream sysfsFile(path);
sysfsFile << content << std::endl; sysfsFile << content << std::endl;
sysfsFile.close(); sysfsFile.close();
} }
char *sysfs_read(char *path) char* sysfs_read(char *path)
{ {
std::string value; std::string value;
std::ifstream sysfsFile(path); std::ifstream sysfsFile(path);
if (!sysfsFile.is_open())
{
return nullptr;
}
sysfsFile >> value;
sysfsFile.close();
if (value.empty())
{
return nullptr;
}
char *result = new char[value.length() + 1];
strcpy(result, value.c_str());
return result;
if (!sysfsFile.is_open())
{
return nullptr;
}
sysfsFile >> value;
sysfsFile.close();
if (value.empty())
{
return nullptr;
}
char *result = new char[value.length() + 1];
strcpy(result, value.c_str());
return result;
} }