You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
automount/disk.c

115 lines
2.8 KiB

/*
* MIT License
*
* Copyright (c) 2020 Alessandro Mauri
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#include <limits.h>
#include "disk.h"
#include "util.h"
#define SYS_DEV_BLOCK_PATH "/sys/dev/block/"
struct disk_list *
disk_get_by_name (struct disk_list *head, const char *s)
{
for (; head; head = head->next) {
if (!strcmp(s, head->name))
break;
}
return head;
}
void
disk_rm_by_name (struct disk_list **head, const char *s)
{
struct disk_list *prev = NULL, *tmp = NULL;
for (tmp = *head; tmp; prev = tmp, tmp = tmp->next) {
if (!strcmp(s, tmp->name)) {
if (prev)
prev->next = tmp->next;
else
*head = tmp->next;
free(tmp);
break;
}
}
return;
}
void
disk_list_free (struct disk_list **head)
{
for (struct disk_list *tmp = *head; tmp;) {
*head = (*head)->next;
free(tmp);
tmp = *head;
}
return;
}
/* return true if the path is the main disk (sda, sdb, etc not sda2, sdb1) */
int path_is_disk(const char *path)
{
struct stat stbuf = {0};
char buf[PATH_MAX] = {0};
printf("%s\n", path);
if (stat(path, &stbuf) < 0 || !S_ISBLK(stbuf.st_mode))
return 0;
if (snprintf(buf, PATH_MAX, SYS_DEV_BLOCK_PATH "%d:%d",
major(stbuf.st_rdev), minor(stbuf.st_rdev)) >= PATH_MAX)
return -1;
// FIXME: possible race condition here
snprintf(buf, PATH_MAX, "%s", get_path(buf));
strcat(buf, "/partition");
if (file_exists(buf) > 0)
return 1;
else
return 0;
}
/* The do-all function */
/*int disk_add_disk (struct disk_list *head, const char *path)
{
struct disk_list dt = {0};
// verify if it is a disk
if (!path_is_disk(path))
return 0;
// get info (real name, etc)
// get partitions
// add partitions
disk_list_add(head, &dt);
return 1;
}
*/