Обсуждение: Add remove duplicate slashes to canonicalize_path()

Поиск
Список
Период
Сортировка

Add remove duplicate slashes to canonicalize_path()

От
Bruce Momjian
Дата:
The following applied patch removes duplicate slashes from the path in
canonicalize_path().  It preserve double leading slashes on Win32.

e.g.    ////a////b => /a/b

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/port/path.c
===================================================================
RCS file: /cvsroot/pgsql/src/port/path.c,v
retrieving revision 1.44
diff -c -c -r1.44 path.c
*** src/port/path.c    6 Nov 2004 21:39:45 -0000    1.44
--- src/port/path.c    7 Nov 2004 01:53:58 -0000
***************
*** 203,223 ****
   *        o  make Win32 path use Unix slashes
   *        o  remove trailing quote on Win32
   *        o  remove trailing slash
   *        o  remove trailing '.'
   *        o  process trailing '..' ourselves
   */
  void
  canonicalize_path(char *path)
  {
! #ifdef WIN32

      /*
       * The Windows command processor will accept suitably quoted paths
       * with forward slashes, but barfs badly with mixed forward and back
       * slashes.
       */
-     char       *p;
-
      for (p = path; *p; p++)
      {
          if (*p == '\\')
--- 203,224 ----
   *        o  make Win32 path use Unix slashes
   *        o  remove trailing quote on Win32
   *        o  remove trailing slash
+  *        o  remove duplicate adjacent separators
   *        o  remove trailing '.'
   *        o  process trailing '..' ourselves
   */
  void
  canonicalize_path(char *path)
  {
!     char       *p, *to_p;
!     bool        was_sep = false;

+ #ifdef WIN32
      /*
       * The Windows command processor will accept suitably quoted paths
       * with forward slashes, but barfs badly with mixed forward and back
       * slashes.
       */
      for (p = path; *p; p++)
      {
          if (*p == '\\')
***************
*** 226,232 ****

      /*
       * In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass
!      * \c\d" as argv[2].
       */
      if (p > path && *(p - 1) == '"')
          *(p - 1) = '/';
--- 227,233 ----

      /*
       * In Win32, if you do: prog.exe "a b" "\c\d\" the system will pass
!      * \c\d" as argv[2], so trim off trailing quote.
       */
      if (p > path && *(p - 1) == '"')
          *(p - 1) = '/';
***************
*** 240,245 ****
--- 241,267 ----
      trim_trailing_separator(path);

      /*
+      *    Remove duplicate adjacent separators
+      */
+     p = path;
+ #ifdef WIN32
+     /* Don't remove leading double-slash on Win32 */
+     if (*p)
+         p++;
+ #endif
+     to_p = p;
+     for (; *p; p++, to_p++)
+     {
+         /* Handle many adjacent slashes, like "/a///b" */
+         while (*p == '/' && was_sep)
+             p++;
+         if (to_p != p)
+             *to_p = *p;
+         was_sep = (*p == '/');
+     }
+     *to_p = '\0';
+
+     /*
       * Remove any trailing uses of "." and process ".." ourselves
       */
      for (;;)
***************
*** 247,255 ****
          int            len = strlen(path);

          if (len > 2 && strcmp(path + len - 2, "/.") == 0)
-         {
              trim_directory(path);
-         }
          else if (len > 3 && strcmp(path + len - 3, "/..") == 0)
          {
              trim_directory(path);
--- 269,275 ----

Re: Add remove duplicate slashes to canonicalize_path()

От
Andrew Dunstan
Дата:

Bruce Momjian wrote:

>*** 247,255 ****
>          int            len = strlen(path);
>
>          if (len > 2 && strcmp(path + len - 2, "/.") == 0)
>-         {
>              trim_directory(path);
>-         }
>          else if (len > 3 && strcmp(path + len - 3, "/..") == 0)
>          {
>              trim_directory(path);
>
>
>

As a matter of style I hate this. Stripping the braces from a branch of
an if statement on the ground that it only has one statement, when other
branches might be multi-statement blocks is just downright ugly.  I
thought the outcome of a recent discussion was that we would stop doing
that, at least automatically from pgindent, but I don't see any reason
to do it manually either.

cheers

andrew

Re: Add remove duplicate slashes to canonicalize_path()

От
Bruce Momjian
Дата:
Andrew Dunstan wrote:
>
>
> Bruce Momjian wrote:
>
> >*** 247,255 ****
> >          int            len = strlen(path);
> >
> >          if (len > 2 && strcmp(path + len - 2, "/.") == 0)
> >-         {
> >              trim_directory(path);
> >-         }
> >          else if (len > 3 && strcmp(path + len - 3, "/..") == 0)
> >          {
> >              trim_directory(path);
> >
> >
> >
>
> As a matter of style I hate this. Stripping the braces from a branch of
> an if statement on the ground that it only has one statement, when other
> branches might be multi-statement blocks is just downright ugly.  I
> thought the outcome of a recent discussion was that we would stop doing
> that, at least automatically from pgindent, but I don't see any reason
> to do it manually either.

Right, we aren't doing it automatically.  Manually it seemed OK.  What
is our consensus on this?

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073

Re: Add remove duplicate slashes to canonicalize_path()

От
Reini Urban
Дата:
Bruce Momjian schrieb:
> The following applied patch removes duplicate slashes from the path in
> canonicalize_path().  It preserve double leading slashes on Win32.
>
> e.g.    ////a////b => /a/b
snip
>    *        o  make Win32 path use Unix slashes
>    *        o  remove trailing quote on Win32
>    *        o  remove trailing slash
>    *        o  remove trailing '.'
>    *        o  process trailing '..' ourselves

At the first glance I thought this will break Win95 (via cygwin), but it
really is ok, 'cause all the paths go through cygwin.
--
Reini Urban
http://xarch.tu-graz.ac.at/home/rurban/