Merge pull request #1 from XeonDead/main

Fix handling for parameters and uncrustify code to keep readability
This commit is contained in:
Nitan Alexandru Marcel
2023-06-02 11:41:25 +03:00
committed by GitHub
5 changed files with 242 additions and 215 deletions
+38 -21
View File
@@ -11,25 +11,28 @@
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); dp = opendir(usbGadgetRoot);
if (dp != nullptr) if (dp != nullptr)
while ((entry = readdir(dp))) while ((entry = readdir(dp)))
{ {
if (entry->d_name[0] != '.') if (entry->d_name[0] != '.')
{ {
char *gadget = strjin(usbGadgetRoot, entry->d_name); char *gadget = strjin(usbGadgetRoot, entry->d_name);
if (sysfs_read(strjin(gadget, (char*)"/UDC")) != nullptr)
if (sysfs_read(strjin(gadget, (char *)"/UDC")) != nullptr)
{ {
gadgetRoot = gadget; gadgetRoot = gadget;
break; break;
@@ -39,19 +42,22 @@ char *get_gadget_root()
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) if (gadgetRoot == nullptr)
{ {
return nullptr; return nullptr;
} }
char *usbConfigRoot = strjin(gadgetRoot, (char*)"/configs/"); char *usbConfigRoot = strjin(gadgetRoot, (char *)"/configs/");
char *configRoot = nullptr; char *configRoot = nullptr;
struct dirent *entry = nullptr; struct dirent *entry = nullptr;
DIR *dp = nullptr; DIR *dp = nullptr;
dp = opendir(usbConfigRoot); dp = opendir(usbConfigRoot);
if (dp != nullptr) if (dp != nullptr)
while ((entry = readdir(dp))) while ((entry = readdir(dp)))
{ {
@@ -67,6 +73,7 @@ char *get_config_root()
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) if (gadgetRoot == nullptr)
{ {
printf("No active gadget found\n"); printf("No active gadget found\n");
@@ -74,25 +81,26 @@ void mount_iso(char *iso_path, char *cdrom, char *ro)
} }
char *configRoot = get_config_root(); char *configRoot = get_config_root();
char *udc = get_udc(); char *udc = get_udc();
char *functionRoot = strjin(gadgetRoot, (char*)"/functions"); char *functionRoot = strjin(gadgetRoot, (char *)"/functions");
char *massStorageRoot = strjin(functionRoot, (char*)"/mass_storage.0"); char *massStorageRoot = strjin(functionRoot, (char *)"/mass_storage.0");
char *lunRoot = strjin(massStorageRoot, (char*)"/lun.0"); char *lunRoot = strjin(massStorageRoot, (char *)"/lun.0");
char *stallFile = strjin(massStorageRoot, (char*)"/stall"); char *stallFile = strjin(massStorageRoot, (char *)"/stall");
char *udcFile = strjin(gadgetRoot, (char*)"/UDC"); char *udcFile = strjin(gadgetRoot, (char *)"/UDC");
char *lunFile = strjin(lunRoot, (char*)"/file"); char *lunFile = strjin(lunRoot, (char *)"/file");
char *lunCdRom = strjin(lunRoot, (char*)"/cdrom"); char *lunCdRom = strjin(lunRoot, (char *)"/cdrom");
char *lunRo = strjin(lunRoot, (char*)"/ro"); char *lunRo = strjin(lunRoot, (char *)"/ro");
set_udc((char*)"", gadgetRoot); set_udc((char *)"", gadgetRoot);
if (!isdir(massStorageRoot)) if (!isdir(massStorageRoot))
{ {
mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); mkdir(massStorageRoot, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
} }
if (!isdir(strjin(configRoot, (char*)"/mass_storage.0")))
if (!isdir(strjin(configRoot, (char *)"/mass_storage.0")))
{ {
symlink(massStorageRoot, strjin(configRoot, (char*)"/mass_storage.0")); symlink(massStorageRoot, strjin(configRoot, (char *)"/mass_storage.0"));
} }
sysfs_write(lunFile, iso_path); sysfs_write(lunFile, iso_path);
sysfs_write(lunCdRom, cdrom); sysfs_write(lunCdRom, cdrom);
@@ -101,19 +109,28 @@ void mount_iso(char *iso_path, char *cdrom, char *ro)
set_udc(udc, gadgetRoot); set_udc(udc, gadgetRoot);
} }
void umount_iso()
{
char *empty = (char *)"";
mount_iso(empty, empty, empty);
}
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"); char *udcFile = strjin(gadget_root, (char *)"/UDC");
return sysfs_read(udcFile); return sysfs_read(udcFile);
} }
+10 -6
View File
@@ -2,11 +2,15 @@
#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
+7 -6
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,
char *w2);
bool isdir(char *path); bool isdir(char *path);
char *sysfs_read(char *path); char* sysfs_read(char *path);
void sysfs_write(char *path, char *content); void sysfs_write(char *path,
char *content);
#endif
#endif // ifndef UTIL_H
+15 -18
View File
@@ -3,20 +3,21 @@
#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 *iso_target = argv[1];
char *cdrom = (char*)"0"; char *cdrom = (char *)"0";
char *ro = (char*)"1"; char *ro = (char *)"1";
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
if (strcmp(argv[i], "-rw") == 0) if (strcmp(argv[i], "-rw") == 0)
{ {
ro = (char*)"0"; ro = (char *)"0";
} }
else if (strcmp(argv[i], "-cdrom") == 0) else if (strcmp(argv[i], "-cdrom") == 0)
{ {
cdrom = (char*)"1"; cdrom = (char *)"1";
} }
else else
{ {
@@ -29,7 +30,8 @@ int main(int argc, char* argv[]){
printf("Usage:\n"); printf("Usage:\n");
printf("cdrom [FILE]... [OPTION]...\n"); printf("cdrom [FILE]... [OPTION]...\n");
printf("Mounts the given FILE as a bootable device using configfs.\n"); printf("Mounts the given FILE as a bootable device using configfs.\n");
printf("Run without any arguments to unmount any mounted files and display this help message.\n\n"); printf(
"Run without any arguments to unmount any mounted files and display this help message.\n\n");
printf("Optional arguments:\n"); printf("Optional arguments:\n");
printf("-rw\t Mounts the file in read write mode.\n"); printf("-rw\t Mounts the file in read write mode.\n");
@@ -46,26 +48,21 @@ int main(int argc, char* argv[]){
printf("Device does not support configfs usb gadget\n"); printf("Device does not support configfs usb gadget\n");
return 1; return 1;
} }
if (getuid() != 0) if (getuid() != 0)
{ {
printf("Permission denied\n"); printf("Permission denied\n");
return 1; return 1;
} }
if (argc == 1)
{
iso_target = (char*)"";
}
if (argc > 2) if (argc > 1)
{ {
cdrom = argv[2];
}
if (argc > 3)
{
ro = argv[3];
}
mount_iso(iso_target, cdrom, ro); mount_iso(iso_target, cdrom, ro);
}
else
{
umount_iso();
}
return 0; return 0;
} }
+15 -7
View File
@@ -11,13 +11,16 @@
#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; struct mntent *ent;
FILE *mounts; FILE *mounts;
char *mount_point = nullptr; char *mount_point = nullptr;
mounts = setmntent("/proc/mounts", "r"); mounts = setmntent("/proc/mounts", "r");
while (nullptr != (ent = getmntent(mounts))) {
while (nullptr != (ent = getmntent(mounts)))
{
if (strcmp(ent->mnt_fsname, filesystem_type) == 0) if (strcmp(ent->mnt_fsname, filesystem_type) == 0)
{ {
mount_point = ent->mnt_dir; mount_point = ent->mnt_dir;
@@ -30,17 +33,19 @@ char *fs_mount_point(char *filesystem_type) {
{ {
const char *alt_usb_gadget = "/config/usb_gadget"; const char *alt_usb_gadget = "/config/usb_gadget";
DIR *dir = opendir(alt_usb_gadget); DIR *dir = opendir(alt_usb_gadget);
if (dir) if (dir)
{ {
mount_point = (char*)"/config"; mount_point = (char *)"/config";
} }
} }
return mount_point; 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); strcpy(s, w1);
strcat(s, w2); strcat(s, w2);
return s; return s;
@@ -49,6 +54,7 @@ char *strjin(char *w1, char *w2)
bool isdir(char *path) bool isdir(char *path)
{ {
struct stat sb; struct stat sb;
if (stat(path, &sb) == 0) if (stat(path, &sb) == 0)
return true; return true;
else else
@@ -63,22 +69,24 @@ void sysfs_write(char *path, char *content)
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()) if (!sysfsFile.is_open())
{ {
return nullptr; return nullptr;
} }
sysfsFile >> value; sysfsFile >> value;
sysfsFile.close(); sysfsFile.close();
if (value.empty()) if (value.empty())
{ {
return nullptr; return nullptr;
} }
char *result = new char[value.length() + 1]; char *result = new char[value.length() + 1];
strcpy(result, value.c_str()); strcpy(result, value.c_str());
return result; return result;
} }