[gnome-terminal] server: Stop using chdir and readlink
- From: Christian Persch <chpe src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-terminal] server: Stop using chdir and readlink
- Date: Tue, 5 Jun 2012 13:09:27 +0000 (UTC)
commit cf3cad8764d770c24a180fd86e2b2375fe62b9be
Author: Christian Persch <chpe gnome org>
Date: Mon Jun 4 23:56:58 2012 +0200
server: Stop using chdir and readlink
They really don't work, and may hang the process (see bug #622180).
Instead, rely on the shell to keep the terminal updated about the
current directory via vte's new current-directory-uri property that
can be updated via the OSC 7 ; URI BEL escape sequence.
Vte helpfully installs a script to /etc/profile.d that works via the
shell prompt. Just put this in your ~/.bashrc:
export PS1='\[$(__vte_ps1)\]'$PS1
https://bugzilla.gnome.org/show_bug.cgi?id=622180
https://bugzilla.gnome.org/show_bug.cgi?id=675987
configure.ac | 2 +-
src/terminal-screen.c | 97 ++++---------------------------------------------
src/terminal-screen.h | 1 -
src/terminal-window.c | 4 +-
4 files changed, 10 insertions(+), 94 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index dbb5e01..b8cbf02 100644
--- a/configure.ac
+++ b/configure.ac
@@ -54,7 +54,7 @@ case "$with_gtk" in
3.0) GTK_API_VERSION=3.0
GTK_REQUIRED=3.5.3
VTE_PC_VERSION=-2.90
- VTE_REQUIRED=0.32.1
+ VTE_REQUIRED=0.33.0
;;
esac
diff --git a/src/terminal-screen.c b/src/terminal-screen.c
index befd075..bd5f915 100644
--- a/src/terminal-screen.c
+++ b/src/terminal-screen.c
@@ -188,59 +188,6 @@ static guint n_url_regexes;
G_DEFINE_TYPE (TerminalScreen, terminal_screen, VTE_TYPE_TERMINAL)
-static char *
-cwd_of_pid (int pid)
-{
- static const char patterns[][18] = {
- "/proc/%d/cwd", /* Linux */
- "/proc/%d/path/cwd", /* Solaris >= 10 */
- };
- guint i;
-
- if (pid == -1)
- return NULL;
-
- /* Try to get the working directory using various OS-specific mechanisms */
- for (i = 0; i < G_N_ELEMENTS (patterns); ++i)
- {
- char cwd_file[64];
- char buf[PATH_MAX + 1];
- int len;
-
- g_snprintf (cwd_file, sizeof (cwd_file), patterns[i], pid);
- len = readlink (cwd_file, buf, sizeof (buf) - 1);
-
- if (len > 0 && buf[0] == '/')
- return g_strndup (buf, len);
-
- /* If that didn't do it, try this hack */
- if (len <= 0)
- {
- char *cwd, *working_dir = NULL;
-
- cwd = g_get_current_dir ();
- if (cwd != NULL)
- {
- /* On Solaris, readlink returns an empty string, but the
- * link can be used as a directory, including as a target
- * of chdir().
- */
- if (chdir (cwd_file) == 0)
- {
- working_dir = g_get_current_dir ();
- (void) chdir (cwd);
- }
- g_free (cwd);
- }
-
- if (working_dir)
- return working_dir;
- }
- }
-
- return NULL;
-}
-
static void
free_tag_data (TagData *tagdata)
{
@@ -1781,51 +1728,21 @@ terminal_screen_get_dynamic_icon_title (TerminalScreen *screen)
* Returns: a newly allocated string containing the current working directory,
* or %NULL on failure
*/
-char*
+char *
terminal_screen_get_current_dir (TerminalScreen *screen)
{
- TerminalScreenPrivate *priv = screen->priv;
- char *cwd;
+ const char *uri;
- if (priv->pty_fd != -1) {
-#if 0
- /* Get the foreground process ID */
- cwd = cwd_of_pid (tcgetpgrp (priv->pty_fd));
- if (cwd != NULL)
- return cwd;
-#endif
+ uri = vte_terminal_get_current_directory_uri (VTE_TERMINAL (screen));
+ if (uri != NULL)
+ return g_filename_from_uri (uri, NULL, NULL);
- /* If that didn't work, try falling back to the primary child. See bug #575184. */
- cwd = cwd_of_pid (priv->child_pid);
- if (cwd != NULL)
- return cwd;
- }
+ if (screen->priv->initial_working_directory)
+ return g_strdup (screen->priv->initial_working_directory);
return NULL;
}
-/**
- * terminal_screen_get_current_dir_with_fallback:
- * @screen:
- *
- * Like terminal_screen_get_current_dir(), but falls back to returning
- * @screen's initial working directory, with a further fallback to the
- * user's home directory.
- *
- * Returns: a newly allocated string containing the current working directory,
- * or %NULL on failure
- */
-char*
-terminal_screen_get_current_dir_with_fallback (TerminalScreen *screen)
-{
- TerminalScreenPrivate *priv = screen->priv;
-
- if (priv->pty_fd == -1)
- return g_strdup (priv->initial_working_directory);
-
- return terminal_screen_get_current_dir (screen);
-}
-
void
terminal_screen_set_font_scale (TerminalScreen *screen,
double factor)
diff --git a/src/terminal-screen.h b/src/terminal-screen.h
index fd2b56f..47c8c75 100644
--- a/src/terminal-screen.h
+++ b/src/terminal-screen.h
@@ -118,7 +118,6 @@ const char *terminal_screen_get_dynamic_title (TerminalScreen *screen);
const char *terminal_screen_get_dynamic_icon_title (TerminalScreen *screen);
char *terminal_screen_get_current_dir (TerminalScreen *screen);
-char *terminal_screen_get_current_dir_with_fallback (TerminalScreen *screen);
void terminal_screen_set_font (TerminalScreen *screen);
void terminal_screen_set_font_scale (TerminalScreen *screen,
diff --git a/src/terminal-window.c b/src/terminal-window.c
index 90a6b79..bd7c615 100644
--- a/src/terminal-window.c
+++ b/src/terminal-window.c
@@ -2702,7 +2702,7 @@ file_new_window_callback (GtkAction *action,
new_window = terminal_app_new_window (app, gtk_widget_get_screen (GTK_WIDGET (window)));
- new_working_directory = terminal_screen_get_current_dir_with_fallback (priv->active_screen);
+ new_working_directory = terminal_screen_get_current_dir (priv->active_screen);
terminal_app_new_terminal (app, new_window, profile,
NULL, NULL,
new_working_directory,
@@ -2729,7 +2729,7 @@ file_new_tab_callback (GtkAction *action,
if (!profile)
return;
- new_working_directory = terminal_screen_get_current_dir_with_fallback (priv->active_screen);
+ new_working_directory = terminal_screen_get_current_dir (priv->active_screen);
terminal_app_new_terminal (app, window, profile,
NULL, NULL,
new_working_directory,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]