author | Olivier Brunel
<jjk@jjacky.com> 2015-02-27 17:44:40 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2015-04-04 12:47:35 UTC |
parent | 775b41c85353895254d6ca9c49ad7870438fb9ea |
doc/aa-mvlog.pod | +31 | -0 |
package/modes | +1 | -0 |
package/targets.mak | +2 | -0 |
src/include/anopa/copy_file.h | +7 | -0 |
src/libanopa/copy_file.c | +37 | -0 |
src/libanopa/deps-lib/anopa | +1 | -0 |
src/libanopa/enable_service.c | +2 | -34 |
src/utils/aa-mvlog.c | +70 | -0 |
src/utils/deps-exe/aa-mvlog | +2 | -0 |
diff --git a/doc/aa-mvlog.pod b/doc/aa-mvlog.pod new file mode 100644 index 0000000..da69fe9 --- /dev/null +++ b/doc/aa-mvlog.pod @@ -0,0 +1,31 @@ +=head1 NAME + +aa-mvlog - Helper to move & rename log file + +=head1 SYNOPSIS + +B<aa-mvlog> I<LOGFILE> I<DESTDIR> + +=head1 OPTIONS + +=over + +=item B<-h, --help> + +Show help screen and exit. + +=item B<-V, --version> + +Show version information and exit. + +=back + +=head1 DESCRIPTION + +B<aa-mvlog>(1) is a small helper to move I<LOGFILE> into I<DESTDIR>, renaming it +to the TAI timestamp read from its first line (i.e. the first 25 bytes of +I<LOGFILE>), and creating in I<DESTDIR> a symlink named I<current> pointing to +it. + +This is mostly aimed to be used at the end of stage 2, to move logs of the boot +process (in I</run/initramfs/boot.log>) into persistent storage. diff --git a/package/modes b/package/modes index 5c37527..5a9ad78 100644 --- a/package/modes +++ b/package/modes @@ -4,6 +4,7 @@ aa-enable 0755 aa-incmdline 0755 aa-kill 0755 aa-mount 0755 +aa-mvlog 0755 aa-pivot 0755 aa-reboot 0755 aa-start 0755 diff --git a/package/targets.mak b/package/targets.mak index 6511818..b7e39f5 100644 --- a/package/targets.mak +++ b/package/targets.mak @@ -9,6 +9,7 @@ aa-echo \ aa-incmdline \ aa-kill \ aa-mount \ +aa-mvlog \ aa-pivot \ aa-reboot \ aa-sync \ @@ -24,6 +25,7 @@ aa-enable.1 \ aa-incmdline.1 \ aa-kill.1 \ aa-mount.1 \ +aa-mvlog.1 \ aa-pivot.1 \ aa-reboot.1 \ aa-start.1 \ diff --git a/src/include/anopa/copy_file.h b/src/include/anopa/copy_file.h new file mode 100644 index 0000000..9210255 --- /dev/null +++ b/src/include/anopa/copy_file.h @@ -0,0 +1,7 @@ + +#ifndef AA_COPY_FILE_H +#define AA_COPY_FILE_H + +int aa_copy_file (const char *src, const char *dst, mode_t mode, int overwrite); + +#endif /* AA_COPY_FILE_H */ diff --git a/src/libanopa/copy_file.c b/src/libanopa/copy_file.c new file mode 100644 index 0000000..17275a7 --- /dev/null +++ b/src/libanopa/copy_file.c @@ -0,0 +1,37 @@ + +#include <errno.h> +#include <fcntl.h> +#include <skalibs/djbunix.h> + +int +aa_copy_file (const char *src, const char *dst, mode_t mode, int overwrite) +{ + int fd_src; + int fd_dst; + + fd_src = open_readb (src); + if (fd_src < 0) + return -1; + + fd_dst = open3 (dst, O_WRONLY | O_CREAT | ((overwrite) ? O_TRUNC : O_EXCL), mode); + if (fd_dst < 0) + { + int e = errno; + fd_close (fd_src); + errno = e; + return -1; + } + + if (fd_cat (fd_src, fd_dst) < 0) + { + int e = errno; + fd_close (fd_src); + fd_close (fd_dst); + errno = e; + return -1; + } + + fd_close (fd_src); + fd_close (fd_dst); + return 0; +} diff --git a/src/libanopa/deps-lib/anopa b/src/libanopa/deps-lib/anopa index aa59e80..db9c47f 100644 --- a/src/libanopa/deps-lib/anopa +++ b/src/libanopa/deps-lib/anopa @@ -1,3 +1,4 @@ +copy_file.o die_usage.o die_version.o enable_service.o diff --git a/src/libanopa/enable_service.c b/src/libanopa/enable_service.c index f468c76..9336831 100644 --- a/src/libanopa/enable_service.c +++ b/src/libanopa/enable_service.c @@ -11,6 +11,7 @@ #include <skalibs/direntry.h> #include <skalibs/skamisc.h> #include <anopa/enable_service.h> +#include <anopa/copy_file.h> #include <anopa/err.h> static int @@ -42,39 +43,6 @@ is_valid_service_name (const char *name, int len) return 1; } -static int -copy_file (const char *src, const char *dst, mode_t mode) -{ - int fd_src; - int fd_dst; - - fd_src = open_readb (src); - if (fd_src < 0) - return -1; - - fd_dst = open3 (dst, O_WRONLY | O_CREAT | O_TRUNC, mode); - if (fd_dst < 0) - { - int e = errno; - fd_close (fd_src); - errno = e; - return -1; - } - - if (fd_cat (fd_src, fd_dst) < 0) - { - int e = errno; - fd_close (fd_src); - fd_close (fd_dst); - errno = e; - return -1; - } - - fd_close (fd_src); - fd_close (fd_dst); - return 0; -} - static int copy_log (const char *name, const char *cfg, mode_t mode, aa_warn_fn warn_fn) { @@ -240,7 +208,7 @@ copy_dir (const char *src, if (depth == 1 && instance && (flags & _AA_FLAG_IS_1OF4) && satmp.s[i + len - 1] == '@') byte_copy (buf_dst + l_dst + 1 + len, l_inst + 1, instance); - r = copy_file (buf_src, buf_dst, st.st_mode); + r = aa_copy_file (buf_src, buf_dst, st.st_mode, 1); if (depth == 1 && r == 0 && ae_cb) { if ((flags & (AA_FLAG_AUTO_ENABLE_NEEDS | _AA_FLAG_IS_NEEDS)) diff --git a/src/utils/aa-mvlog.c b/src/utils/aa-mvlog.c new file mode 100644 index 0000000..cc55d53 --- /dev/null +++ b/src/utils/aa-mvlog.c @@ -0,0 +1,70 @@ + +#include <sys/stat.h> +#include <unistd.h> +#include <skalibs/djbunix.h> +#include <skalibs/bytestr.h> +#include <skalibs/strerr2.h> +#include <anopa/common.h> +#include <anopa/copy_file.h> + +const char *PROG; + +static void +dieusage (int rc) +{ + aa_die_usage (rc, "[OPTION] LOGFILE DESTDIR", + " -h, --help Show this help screen and exit\n" + " -V, --version Show version information and exit\n" + ); +} + +int +main (int argc, char * const argv[]) +{ + PROG = "aa-mvlog"; + struct stat st; + + if (argc == 2) + { + if (str_equal (argv[1], "-V") || str_equal (argv[1], "--version")) + aa_die_version (); + else if (str_equal (argv[1], "-h") || str_equal (argv[1], "--help")) + dieusage (0); + } + if (argc != 3) + dieusage (1); + + if (stat (argv[1], &st) < 0) + strerr_diefu2sys (2, "stat ", argv[1]); + else if (!S_ISREG (st.st_mode)) + strerr_dief2x (2, argv[1], ": not a file"); + + { + int l = strlen (argv[2]); + char newname[l + 27]; + char target[26]; + + byte_copy (newname, l, argv[2]); + newname[l] = '/'; + if (openreadnclose (argv[1], newname + l + 1, 25) != 25) + strerr_diefu2sys (2, "read new name from ", argv[1]); + if (newname[l + 1] != '@' + || byte_chr (newname + l + 1, 25, '/') < 25 + || byte_chr (newname + l + 1, 25, '\0') < 25) + strerr_dief2x (2, "invalid new name read from ", argv[1]); + newname[l + 26] = '\0'; + + if (aa_copy_file (argv[1], newname, st.st_mode, 0) < 0) + strerr_diefu4sys (2, "copy ", argv[1], " as ", newname); + + byte_copy (target, 26, newname + l + 1); + byte_copy (newname + l + 1, 8, "current"); + unlink (newname); + if (symlink (target, newname) < 0) + strerr_warnwu2sys ("create symlink ", newname); + if (unlink (argv[1]) < 0) + strerr_warnwu2sys ("remove source file ", argv[1]); + } + + return 0; +} diff --git a/src/utils/deps-exe/aa-mvlog b/src/utils/deps-exe/aa-mvlog new file mode 100644 index 0000000..30987b4 --- /dev/null +++ b/src/utils/deps-exe/aa-mvlog @@ -0,0 +1,2 @@ +${LIBANOPA} +-lskarnet