grub/include/grub/osdep/hostfile_unix.h
Peter Jones 03e72830ab grub-editenv: Add grub_util_readlink()
Currently grub-editenv and related tools are not able to follow symbolic
links when finding their config file. For example the grub-editenv create
command will wrongly overwrite a symlink in /boot/grub2/grubenv with a new
regular file, instead of creating a file in the path the symlink points to.

A following patch will change that and add support in grub-editenv to
follow symbolic links when finding the grub environment variables file.

Add a grub_util_readlink() helper function that is just a wrapper around
the platform specific function to read the value of a symbolic link. This
helper function will be used by the following patch for grub-editenv.

The helper function is not added for Windows, since this operating system
doesn't have a primitive to read the contents of a symbolic link.

Signed-off-by: Peter Jones <pjones@redhat.com>
Reviewed-by: Adam Jackson <ajax@redhat.com>
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
2020-02-18 15:14:13 +01:00

118 lines
2.8 KiB
C

/*
* GRUB -- GRand Unified Bootloader
* Copyright (C) 2010,2011,2012,2013 Free Software Foundation, Inc.
*
* GRUB is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* GRUB is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GRUB_EMU_HOSTFILE_H
#define GRUB_EMU_HOSTFILE_H 1
#include <config.h>
#include <stdarg.h>
#include <grub/symbol.h>
#include <grub/types.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <unistd.h>
#include <stdio.h>
typedef struct dirent *grub_util_fd_dirent_t;
typedef DIR *grub_util_fd_dir_t;
static inline grub_util_fd_dir_t
grub_util_fd_opendir (const char *name)
{
return opendir (name);
}
static inline void
grub_util_fd_closedir (grub_util_fd_dir_t dirp)
{
closedir (dirp);
}
static inline grub_util_fd_dirent_t
grub_util_fd_readdir (grub_util_fd_dir_t dirp)
{
return readdir (dirp);
}
static inline int
grub_util_unlink (const char *pathname)
{
return unlink (pathname);
}
static inline int
grub_util_rmdir (const char *pathname)
{
return rmdir (pathname);
}
static inline int
grub_util_rename (const char *from, const char *to)
{
return rename (from, to);
}
static inline ssize_t
grub_util_readlink (const char *name, char *buf, size_t bufsize)
{
return readlink(name, buf, bufsize);
}
#define grub_util_mkdir(a) mkdir ((a), 0755)
#if defined (__NetBSD__)
/* NetBSD uses /boot for its boot block. */
# define DEFAULT_DIRECTORY "/"GRUB_DIR_NAME
#else
# define DEFAULT_DIRECTORY "/"GRUB_BOOT_DIR_NAME"/"GRUB_DIR_NAME
#endif
enum grub_util_fd_open_flags_t
{
GRUB_UTIL_FD_O_RDONLY = O_RDONLY,
GRUB_UTIL_FD_O_WRONLY = O_WRONLY,
GRUB_UTIL_FD_O_RDWR = O_RDWR,
GRUB_UTIL_FD_O_CREATTRUNC = O_CREAT | O_TRUNC,
GRUB_UTIL_FD_O_SYNC = (0
#ifdef O_SYNC
| O_SYNC
#endif
#ifdef O_FSYNC
| O_FSYNC
#endif
)
};
#define DEFAULT_DEVICE_MAP DEFAULT_DIRECTORY "/device.map"
typedef int grub_util_fd_t;
#define GRUB_UTIL_FD_INVALID -1
#define GRUB_UTIL_FD_IS_VALID(x) ((x) >= 0)
#define GRUB_UTIL_FD_STAT_IS_FUNCTIONAL 1
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__APPLE__) || defined(__NetBSD__) || defined (__sun__) || defined(__OpenBSD__) || defined(__HAIKU__)
#define GRUB_DISK_DEVS_ARE_CHAR 1
#else
#define GRUB_DISK_DEVS_ARE_CHAR 0
#endif
#endif