diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-10 23:12:02 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2011-05-11 00:28:52 +0300 |
commit | db81e378576dcc5510032c72060e48e562f208c9 (patch) | |
tree | 914c5f277a5fad050527a5c5da815f521e8023dc /src | |
parent | 84db86f0151385a8f05483bf691fde86b4e4e153 (diff) | |
download | wydawca-db81e378576dcc5510032c72060e48e562f208c9.tar.gz wydawca-db81e378576dcc5510032c72060e48e562f208c9.tar.bz2 |
Remove save-cwd.
* src/pushd.c: New file.
* src/Makefile.am: Add pushd.c
* src/wydawca.h (push_dir, pop_dir): New functions.
* src/diskio.c: Use push_dir/pop_dir.
* src/exec.c: Likewise.
* src/gpg.c: Likewise.
* gnulib.modules: Remove save-cwd.
* tests/pushck.c: New file.
* tests/pushdir.at: New file.
* tests/testsuite.at: Add pushdir.at
* tests/Makefile.am: Likewise.
* tests/.gitignore: Add pushck.
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/diskio.c | 24 | ||||
-rw-r--r-- | src/exec.c | 1 | ||||
-rw-r--r-- | src/gpg.c | 6 | ||||
-rw-r--r-- | src/pushd.c | 162 | ||||
-rw-r--r-- | src/wydawca.h | 4 |
6 files changed, 176 insertions, 22 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index b79e0ec..8fd7d94 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,2 +34,3 @@ wydawca_SOURCES=\ process.c\ + pushd.c\ sql.c\ diff --git a/src/diskio.c b/src/diskio.c index ed16e35..9addd9b 100644 --- a/src/diskio.c +++ b/src/diskio.c @@ -17,3 +17,2 @@ #include "wydawca.h" -#include "save-cwd.h" @@ -531,3 +530,2 @@ dir_symlink_file (struct file_triplet *trp, int rc = 0; - struct saved_cwd cwd; const struct spool *spool = trp->spool; @@ -539,9 +537,2 @@ dir_symlink_file (struct file_triplet *trp, - if (save_cwd (&cwd)) - { - logmsg (LOG_ERR, _("cannot save current directory: %s"), - strerror (errno)); - return 1; - } - src = safe_file_name_alloc (wanted_src); @@ -587,3 +578,3 @@ dir_symlink_file (struct file_triplet *trp, { - if (chdir (dst_dir)) + if (push_dir (dst_dir)) logmsg (LOG_ERR, _("cannot change to %s: %s"), @@ -627,2 +618,8 @@ dir_symlink_file (struct file_triplet *trp, } + if (pop_dir ()) + { + logmsg (LOG_EMERG, _("cannot restore current directory: %s"), + strerror (errno)); + exit (EX_SOFTWARE); + } } @@ -631,9 +628,2 @@ dir_symlink_file (struct file_triplet *trp, - if (restore_cwd (&cwd)) - { - logmsg (LOG_EMERG, _("cannot restore current directory: %s"), - strerror (errno)); - exit (EX_SOFTWARE); - } - free (src); @@ -17,3 +17,2 @@ #include "wydawca.h" -#include "save-cwd.h" #include <gpgme.h> @@ -19,3 +19,2 @@ #include "wydawca.h" -#include "save-cwd.h" #include <gpgme.h> @@ -93,5 +92,4 @@ rmdir_r (const char *name) int rc; - struct saved_cwd cwd; - if (save_cwd (&cwd)) + if (push_dir (NULL)) { @@ -102,3 +100,3 @@ rmdir_r (const char *name) rc = recursive_rmdir (name); - if (restore_cwd (&cwd)) + if (pop_dir ()) { diff --git a/src/pushd.c b/src/pushd.c new file mode 100644 index 0000000..6de10a6 --- /dev/null +++ b/src/pushd.c @@ -0,0 +1,162 @@ +/* wydawca - automatic release submission daemon + Copyright (C) 2011 Sergey Poznyakoff + + Wydawca 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. + + Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */ + +#include "wydawca.h" + +struct saved_dir +{ + struct saved_dir *prev; + int fd; + char *name; +}; + +struct saved_dir *dir_stack; + +#ifndef O_SEARCH +# define O_SEARCH 0 +#endif + +char * +getcwd_alloc (void) +{ + size_t size = 0; + char *buf = NULL; + char *p; + + do + { + if (!buf) + { + size = 64; + buf = malloc (size); + } + else + { + char *np; + size_t ns = 2 * size; + if (ns < size) + { + free (buf); + errno = ENOMEM; + return NULL; + } + size = ns; + np = realloc (buf, size); + if (!np) + free (buf); + buf = np; + } + if (!buf) + return NULL; + } + while ((p = getcwd (buf, size)) == NULL && errno == ERANGE); + if (!p) + { + int ec = errno; + free (buf); + buf = NULL; + errno = ec; + } + return buf; +} + +static int +_save_dir (struct saved_dir *sd) +{ + int fd; + +#ifdef HAVE_FCHDIR + fd = open (".", O_SEARCH); +#else + fd = -1; +#endif + if (fd == -1) + { + sd->name = getcwd_alloc (); + if (!sd->name) + return errno; + } + else + sd->name = NULL; + sd->fd = fd; + return 0; +} + +static void +_drop_dir (struct saved_dir *sd) +{ + if (sd->fd != -1) + close (sd->fd); + free (sd->name); + free (sd); +} + +static int +_push_dir (struct saved_dir *sd, const char *dirname) +{ + int rc = _save_dir (sd); + if (rc) + return rc; + if (dirname && chdir (dirname)) + { + rc = errno; + _drop_dir (sd); + return rc; + } + return 0; +} + +int +push_dir (const char *dirname) +{ + int rc; + struct saved_dir *sd = malloc (sizeof (*sd)); + + if (!sd) + return errno; + rc = _push_dir (sd, dirname); + if (rc) + { + errno = rc; + return -1; + } + sd->prev = dir_stack; + dir_stack = sd; + return 0; +} + +int +pop_dir () +{ + struct saved_dir *sd = dir_stack; + int rc; + + if (!sd) + { + errno = ENOENT; + return -1; + } + dir_stack = sd->prev; + if (sd->fd != -1) + rc = fchdir (sd->fd); + else + rc = chdir (sd->name); + if (rc) + rc = errno; + _drop_dir (sd); + errno = rc; + return rc; +} diff --git a/src/wydawca.h b/src/wydawca.h index 7b9875f..8d5dd34 100644 --- a/src/wydawca.h +++ b/src/wydawca.h @@ -556 +556,5 @@ int tcpwrap_access(int fd); int wydawca_userprivs (uid_t uid, gid_t gid, gid_t *grplist, size_t ngrp); + +int push_dir (const char *dirname); +int pop_dir (void); +char *getcwd_alloc (void); |