FileFunctions.C 34.1 KB
Newer Older
1 2
/*****************************************************************************
*
bonnell's avatar
bonnell committed
3
* Copyright (c) 2000 - 2015, Lawrence Livermore National Security, LLC
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR  IMPLIED WARRANTIES, INCLUDING,  BUT NOT  LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE
* ARE  DISCLAIMED. IN  NO EVENT  SHALL LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS BE  LIABLE  FOR  ANY
* DIRECT,  INDIRECT,   INCIDENTAL,   SPECIAL,   EXEMPLARY,  OR   CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT  LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR
* SERVICES; LOSS OF  USE, DATA, OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER
* CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN  CONTRACT,  STRICT
* LIABILITY, OR TORT  (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY  WAY
* OUT OF THE  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*****************************************************************************/
#include <FileFunctions.h>
39
#include <Utility.h>
40 41

#include <visitstream.h>
42 43 44 45
#include <vectortypes.h>

#include <cerrno>

46 47 48 49 50 51 52
#include <stdio.h>
#include <string.h>
#include <snprintf.h>

#if defined(_WIN32)
#include <windows.h>
#include <userenv.h> // for GetProfilesDirectory
53
#include <direct.h>
bonnell's avatar
bonnell committed
54
#include <Shlwapi.h> // PathIsRelative
55 56 57 58 59 60
#else
#include <unistd.h>
#include <dirent.h>
#include <pwd.h>
#endif

61 62 63 64

const int STATIC_BUF_SIZE = 4096;
static char StaticStringBuf[STATIC_BUF_SIZE];

65
// ****************************************************************************
66
// Method: FileFunctions::VisItStat
67 68 69 70
//
// Purpose: platform independent stat function that supports large files
// when possible
//
fogal1's avatar
fogal1 committed
71
// Programmer: Mark C. Miller
fogal1's avatar
fogal1 committed
72
// Creation:   March 23, 2006
73
//
74 75 76 77
// Modifications:
//   Kathleen Biagas, Wed Nov 24 16:25:13 MST 2015
//   Use _stat64 if running 64 bit windows version.
//
78 79 80
// ****************************************************************************

int
81
FileFunctions::VisItStat(const std::string &filename, VisItStat_t *buf)
82 83
{
#if defined(_WIN32)
84 85 86
  #if defined(_WIN64)
    return _stat64(filename.c_str(), buf);
  #else
87
   return _stat(filename.c_str(), buf);
88
  #endif
89 90 91
#else

#if SIZEOF_OFF64_T > 4
92
    return stat64(filename.c_str(), buf);
93
#else
94
    return stat(filename.c_str(), buf);
95 96 97 98 99 100
#endif

#endif
}

// ****************************************************************************
101
// Method: FileFunctions::VisItFStat
102 103 104 105 106 107 108
//
// Purpose: platform independent fstat function that supports large files
// when possible
//
// Programmer: Mark C. Miller 
// Creation:   March 23, 2006 
//
109 110 111 112
// Modifications:
//   Kathleen Biagas, Wed Nov 24 16:25:13 MST 2015
//   Use _fstat64 if running 64 bit windows version.
//
113 114 115
// ****************************************************************************

int
116
FileFunctions::VisItFstat(int fd, VisItStat_t *buf)
117 118
{
#if defined(_WIN32)
119 120 121 122 123
  #if defined(_WIN64)
    return _fstat64(fd, buf);
  #else
    return _fstat(fd, buf);
  #endif
124 125 126 127 128 129 130 131 132 133 134 135
#else

#if SIZEOF_OFF64_T > 4
    return fstat64(fd, buf);
#else
    return fstat(fd, buf);
#endif

#endif
}

// ****************************************************************************
136
// Function: FileFunctions::ReadAndProcessDirectory
137 138 139 140 141 142 143
//
// Purpose: 
//   Reads the list of files in the specified directory and calls a callback
//   function on each file.
//
// Arguments:
//   directory     : The directory to read.
144
//   processOneFile : Callback function to process one file.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
//   data          : Callback data.
//   checkAccess   : Whether or not to check the file permissions.
//
// Returns:    True if successful; false otherwise.
//
// Programmer: Brad Whitlock
// Creation:   Mon Jul 7 15:08:02 PST 2003
//
// Modifications:
//   Brad Whitlock, Fri Jul 11 14:18:21 PST 2003
//   Made it work on Windows.
//
//   Hank Childs, Thu Jun  8 16:13:20 PDT 2006
//   Fix warning regarding uninitialized variable.
//
160 161 162
//   Kathleen Bonnell, Thu Apr 22 17:23:43 MST 2010 
//   Add '.' to test for isDir on Windows.
//
163 164 165
// ****************************************************************************

bool
166 167
FileFunctions::ReadAndProcessDirectory(const std::string &directory,
    FileFunctions::ProcessDirectoryCallback *processOneFile, void *data,
168 169
    bool checkAccess)
{
fogal1's avatar
fogal1 committed
170
    bool retval = false;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

#if defined(_WIN32)
    if(directory == "My Computer")
    {
        // Add the drives to the list.
        char buf[200];
        DWORD bufLen = 200;
        DWORD slen = GetLogicalDriveStrings(200, buf);

        if(slen > 0)
        {
            char *ptr = buf;
            while(*ptr != 0)
            {
                std::string drive(ptr);
                (*processOneFile)(data, drive, true, true, 0);
                ptr += (drive.size() + 1);
                retval = true;
            }
        }
    }
192
    else if (directory.size() > 0)
193 194 195 196 197 198 199 200 201
    {
        // Try and read the files in fullPath.
        std::string searchPath(directory + std::string("\\*"));
        WIN32_FIND_DATA fd;
        HANDLE dirHandle = FindFirstFile(searchPath.c_str(), &fd);
        if(dirHandle != INVALID_HANDLE_VALUE)
        {
            do
            {
fogal1's avatar
fogal1 committed
202
                bool isDir =
203 204 205
                    ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0) ||
                     (strcmp(fd.cFileName, "..") == 0) ||
                     (strcmp(fd.cFileName, ".") == 0) ;
206 207 208 209 210 211 212 213 214 215 216
                long sz = ((fd.nFileSizeHigh * MAXDWORD) + fd.nFileSizeLow);
                std::string fileName(directory);
                if(directory.substr(directory.size() - 1) != "\\")
                    fileName += "\\";
                fileName += fd.cFileName;
                (*processOneFile)(data, fileName, isDir, true, sz);
                retval = true;

            } while(FindNextFile(dirHandle, &fd));
            FindClose(dirHandle);
        }
217 218 219
    } else {
        //Directory string was empty, nothing to do
        retval = false;
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
    }
#else
    DIR     *dir;
    dirent  *ent;

    // If the directory cannot be opened, return an error code.
    dir = opendir(directory.c_str());
    if (dir)
    {
        // Get the userId and the groups for that user so we can check the
        // file permissions.
        gid_t gids[100];
        int ngids = 0;
        uid_t uid = 0;
        if(checkAccess)
        {
            uid = getuid();
            ngids = getgroups(100, gids);
        }

        // Process each directory entry.
        while ((ent = readdir(dir)) != NULL)
        {
            // Get information about the file.
            VisItStat_t s;
            std::string fileName(directory);
            if(directory.substr(directory.size() - 1, 1) != "/")
                fileName += "/";
            fileName += ent->d_name;
            VisItStat(fileName.c_str(), &s);

            mode_t mode = s.st_mode;
            bool isdir = S_ISDIR(mode);
   
            bool canaccess = checkAccess ? false : true;
            if(checkAccess)
            {
                bool isuser  = (s.st_uid == uid);
                bool isgroup = false;
                for (int i=0; i<ngids && !isgroup; i++)
                    if (s.st_gid == gids[i])
                        isgroup=true;
    
                if (isdir)
                {
                    if ((mode & S_IROTH) &&
                        (mode & S_IXOTH))
                        canaccess=true;
                    else if (isuser &&
                             (mode & S_IRUSR) &&
                             (mode & S_IXUSR))
                        canaccess=true;
                    else if (isgroup &&
                             (mode & S_IRGRP) &&
                             (mode & S_IXGRP))
                        canaccess=true;
                }
                else
                {
                    if (mode & S_IROTH)
                        canaccess=true;
                    else if (isuser &&
                             (mode & S_IRUSR))
                        canaccess=true;
                    else if (isgroup &&
                             (mode & S_IRGRP))
                        canaccess=true;
                }
            }

            (*processOneFile)(data, fileName, isdir, canaccess, (long)s.st_size);
            retval = true;
        }
 
        closedir(dir);
    }
#endif

    return retval;
}

// ****************************************************************************
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
// Method: MDServerConnection::ReadCWD
//
// Purpose:
//   Gets the current directory.
//
// Programmer: Jeremy Meredith
// Creation:   Wed Aug 23 12:09:39 PDT 2000
//
// Modifications:
//
// ****************************************************************************

std::string
FileFunctions::GetCurrentWorkingDirectory()
{
    char tmpcwd[1024];
#if defined(_WIN32)
    _getcwd(tmpcwd,1023);
#else
    if(getcwd(tmpcwd,1023) == NULL)
        tmpcwd[0] = '\0';
#endif
    tmpcwd[1023]='\0';
    return std::string(tmpcwd);
}

// ****************************************************************************
// Function: FileFunctions::ExpandPath
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
//
// Purpose: 
//   Expands a path that contains ~ as its first character so it contains the
//   absolute path to the specified user's home directory.
//
// Arguments:
//   path : The path to expand.
//
// Returns:    If the first character is "~" then the path gets expanded,
//             otherwise the path is returned unmodified.
//
// Programmer: Brad Whitlock
// Creation:   Thu Feb 17 14:57:57 PST 2005
//
// Modifications:
//   Kathleen Bonnell, Thu Nov 6 11:04:15 PST 2008
//   Change how homeDir is determined on Windows, to allow for roaming profiles
//   (where it may not be on C:) and for Vista, where the default profiles
//   directory is C:\Users not C:\Documents and Settings.
//   
//   Kathleen Bonnell, Fri Nov 7 15:46:33 PST 2008
//   Forgot path separator between homeDir and username. 
//
// ****************************************************************************

std::string
356 357
FileFunctions::ExpandPath(const std::string &path, 
    const std::string &cwd)
358 359
{
    std::string newPath(path);
360 361 362 363 364 365 366
    std::string workingDir(cwd);

    if(workingDir.empty())
        workingDir = GetCurrentWorkingDirectory();

    if(path.empty())
        return workingDir;
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

    if(path[0] == '~')
    {
        char username[256];
        int  i;

        // Find the user name portion of the path, ie ~user
        for (i = 1; isalnum(path[i]); i++)
        {
            username[i - 1] = path[i];
        }
        username[i - 1] = '\0';

#if defined(_WIN32)
        if(i == 1)
        {
            // User just specified '~', get the current user name.
            DWORD s = 256;
            GetUserName(username, &s);
        }
        // Get 'home' directory
        char *profDir = new char[MAX_PATH];
        DWORD size = 256;
        GetProfilesDirectory(profDir, &size);
        std::string homeDir(profDir);
        delete [] profDir;    

        // Append the rest of the path to the home directory.
        std::string restOfPath(path.substr(i, path.length() - i + 1));
        newPath = homeDir + "\\" + std::string(username) + restOfPath;
#else
        // Check if the user specified '~' or '~name'.
        struct passwd *users_passwd_entry = NULL;
        if (i == 1)
        {
            // User just specified '~', get /etc/passwd entry
            users_passwd_entry = getpwuid(getuid());
        }
        else
        {
            // User specified '~name', get /etc/passwd entry
            users_passwd_entry = getpwnam(username);
        }

        // Now that we have a passwd entry, validate it.
        if (users_passwd_entry == NULL)
        {
            // Did not specify a valid user name.  Do nothing. 
            return newPath;
        }
        if (users_passwd_entry->pw_dir == NULL)
        {
            // Passwd entry is invalid.  Do nothing.
            return newPath;
        }

        // Append the rest of the path to the home directory.
        std::string restOfPath(path.substr(i, path.length() - i + 1));
        newPath = std::string(users_passwd_entry->pw_dir) + restOfPath;
#endif
    }
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
#if defined(_WIN32)
    else if(path.substr(0, 12) == "My Computer\\" && path[13] == ':')
    {
        // Filter out the "My Computer" part of the path.
        newPath = path.substr(12);
    }
    else if(path.size() > 1 && path[1] == ':')
    {
        // absolute path. do nothing
        newPath = path;
    }
    else if(path == "My Computer")
    {
        // special path. do nothing
        newPath = path;
    }
    else
    {
        // relative path:
        newPath = workingDir + "\\" + path;
    }
#else
    else if(path[0] != '/')
    {
        // relative path:
        newPath = workingDir + "/" + path;
    }
    else
    {
        // absolute path: do nothing
        newPath = path;
    }
#endif

    return FilteredPath(newPath);
}

// ****************************************************************************
// Method: FileFunctions::FilteredPath
//
// Purpose: 
//   Filters extra junk out of a path.
//
// Arguments:
//   path : The path string that we're filtering.
//
// Returns:    A filtered path string.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Wed Feb 13 14:08:01 PST 2002
//
// Modifications:
//   Brad Whitlock, Thu Mar 18 11:40:40 PDT 2004
//   I fixed the code so it filters out .. properly.
//
//   Brad Whitlock, Wed Apr 14 17:22:53 PST 2004
//   I fixed the .. filtering for Windows.
//
//   Brad Whitlock, Mon Sep 28 16:36:18 PDT 2009
//   Don't pop_back unless there's something to remove.
//
// ****************************************************************************

std::string
FileFunctions::FilteredPath(const std::string &path)
{
    // Remove multiple slashes in a row.
    size_t i = 0;
    size_t state = 0;
    std::string filteredPath;
    for(i = 0; i < path.length(); ++i)
    {
        if(state == 0)
        {
            filteredPath += path[i];
            if(path[i] == VISIT_SLASH_CHAR)
                state = 1;
        }
        else if(path[i] != VISIT_SLASH_CHAR)
        {
            filteredPath += path[i];
            state = 0;
        }
    }

    std::string path2(filteredPath);
    if(path2.length() > 0 && path2[path2.length() - 1] == VISIT_SLASH_CHAR)
    {
        filteredPath = path2.substr(0, path2.length() - 1);
    }

    if(filteredPath.size() == 0)
        filteredPath = VISIT_SLASH_STRING;
    else
    {
        // Filter out .. so we get the right path.
        stringVector tmpNames;
        std::string  tmp;
        state = 0;
        const char *str = filteredPath.c_str();
        for(i = 0; i < filteredPath.length() + 1; ++i)
        {
            if(str[i] == VISIT_SLASH_CHAR || str[i] == '\0')
            {
                if(tmp.size() > 0)
                {
                    if(tmp == "..")
                    {
                        if(tmpNames.size() > 0)
                            tmpNames.pop_back();
                    }
                    else
                        tmpNames.push_back(tmp);
                }
                tmp = "";
            }
            else
                tmp += str[i];
        }

        // Reassemble the path fragments.
        if(tmpNames.size() > 0)
        {
            filteredPath = "";
            for(i = 0; i < tmpNames.size(); ++i)
            { 
#if defined(_WIN32)
                if(i > 0)
                    filteredPath += VISIT_SLASH_STRING;
#else
                filteredPath += VISIT_SLASH_STRING;
#endif
                filteredPath += tmpNames[i];
            }
        }
    }

    return filteredPath;
}

// ****************************************************************************
//  Function: FileFunctions::Basename
//
//  Purpose: Find the basename of a file path string
//
//  Programmer: Mark C. Miller
//  Creation:   Unknown
//
//  Modifications:
//    Jeremy Meredith, Wed May 20 13:46:39 EDT 2009
//    Should default to "0" for start, and only use "-1" for
//    the "all-slash-string" case.
//
//    Kathleen Biagas, Thu Jul 28 09:41:27 PDT 2011
//    When searching the string, look for either type of slash char, but still
//    use the sys-dependent VISIT_SLASH_STRING when setting in the empty buf.
//
587 588 589
//    Mark C. Miller, Tue Sep 15 20:18:22 PDT 2015
//    Added arg and logic to support an optional suffix string just like
//    Unix basename command.
590
// ****************************************************************************
591 592
static char const *
basename(char const *path, int& start, char const *suffix=0)
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
{
   start = 0;

   if (path == 0)
   {
       strcpy(StaticStringBuf, ".");
       return StaticStringBuf;
   }
   else if (*path == '\0')
   {
       strcpy(StaticStringBuf, ".");
       return StaticStringBuf;
   }
   else
   {
       // find end of path string
       int n = 0;
       while ((path[n] != '\0') && (n < STATIC_BUF_SIZE))
           n++;

       // deal with string too large
       if (n == STATIC_BUF_SIZE)
       {
           strcpy(StaticStringBuf, ".");
           return StaticStringBuf;
       }

       // backup, skipping over all trailing slash chars
       int j = n-1;
       while ((j >= 0) && (path[j] == '/' || path[j] == '\\'))
           j--;

       // deal with string consisting of all slash chars
       if (j == -1)
       {
           start = -1;
           strcpy(StaticStringBuf, VISIT_SLASH_STRING);
           return StaticStringBuf;
       }

       // backup to just after next slash char
       int i = j-1;
       while ((i >= 0) && (path[i] != '/' && path[i] != '\\'))
           i--;
       i++;
       start = i;

640
       // build the candidate return string
641 642 643 644
       int k;
       for (k = 0; k < j - i + 1; k++)
           StaticStringBuf[k] = path[i+k];
       StaticStringBuf[k] = '\0';
645 646 647 648 649 650 651 652 653 654

       // Handle optional suffix but only if its not equal to
       // remaining string (as per man pages for dirname)
       if (suffix)
       {
           int n = strlen(suffix);
           if (n < k && !strncmp(&StaticStringBuf[k-n],suffix,n))
               StaticStringBuf[k-n] = '\0';
       }

655 656 657 658
       return StaticStringBuf;
   }
}

659 660
char const *
FileFunctions::Basename(char const *path, char const *suffix)
661 662
{
   int dummy1;
663
   return basename(path, dummy1, suffix);
664 665 666
}

std::string
667
FileFunctions::Basename(const std::string &path, const std::string &suffix)
668
{
669
    return Basename(path.c_str(), suffix.c_str());
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
}

// ****************************************************************************
//  Function: Dirname
//
//  Purpose: Find the dirname of a file path string
//
//  Programmer: Mark C. Miller
//  Creation:   Unknown
//
//  Modifications:
//    Jeremy Meredith, Wed May 20 13:46:39 EDT 2009
//    Special cases were unnecessary; they fall out of the start position
//    returned from the above implementation of basename naturally.  Fixed
//    a couple of the special cases as well.
//
//    Kathleen Biagas, Thu Jul 28 09:41:27 PDT 2011
//    When searching the string, look for either type of slash char, but still
//    use the sys-dependent VISIT_SLASH_STRING when setting in the empty buf.
//
//    Mark C. Miller, Wed Jul 11 20:03:16 PDT 2012
//    Fixed the special case where the only part of the string left after
//    eliminating the basename part is a single slash char at index zero.
// ****************************************************************************
const char *
FileFunctions::Dirname(const char *path)
{
    int start;

    // ok, figure out the basename
    basename(path, start);

    if (start == -1)
    {
        strcpy(StaticStringBuf, VISIT_SLASH_STRING);
        return StaticStringBuf;
    }
    else if (start == 0)
    {
        strcpy(StaticStringBuf, ".");
        return StaticStringBuf;
    }
    else
    {
        int i;
        for (i = 0; i < start; i++)
            StaticStringBuf[i] = path[i];
        if (i > 1 && (StaticStringBuf[i-1] == '/' ||
                      StaticStringBuf[i-1] == '\\'))
            StaticStringBuf[i-1] = '\0';
        else
            StaticStringBuf[i] = '\0';
        return StaticStringBuf;
    }
}

std::string
FileFunctions::Dirname(const std::string &path)
{
    return Dirname(path.c_str());
}

// ****************************************************************************
//  Function: Normalize
//
//  Purpose: Normalize a pathname; removing all embedded './' and/or '../' or
//  '//', and any trailing '/'. Note, however, that he code is written to use
//  whatever the VISIT_SLASH_STRING is so it should work on Windows as well
//  with the exception of a leading drive letter and colon.
//
//  Programmer: Mark C. Miller, Mon Jul 16 21:56:03 PDT 2012
//
//  Modifications:
//    Kathleen Biagas, Thu June 6 09:39:25 PDT 2013
//    Added pathSep argument that defaults to platform-specific 
//    VISIT_SLASH_STRING.  Use of non-platform specific case my be needed if
//    parsing internal database path-names.
//
// ****************************************************************************

const char *
FileFunctions::Normalize(const char *path, const char *pathSep)
{
    std::string retval(path);

    // First, remove any double slashes
    std::string dbl_slash = std::string(pathSep) + std::string(pathSep);
    size_t dbl_slash_idx = retval.rfind(dbl_slash);
    while (dbl_slash_idx != std::string::npos)
    {
        retval.erase(dbl_slash_idx, 1);
        dbl_slash_idx = retval.rfind(dbl_slash);
    }

    // Remove any terms of the form "./". These have no effect
    std::string dot_slash = std::string(".") + std::string(pathSep);
    size_t dot_slash_idx = retval.rfind(dot_slash);
    while (dot_slash_idx != std::string::npos)
    {
        if ((dot_slash_idx > 0 && retval[dot_slash_idx-1] != '.') ||
             dot_slash_idx == 0)
        {
            retval.erase(dot_slash_idx, 2);
            dot_slash_idx = retval.rfind(dot_slash,dot_slash_idx-1);
        }
        else
        {
            if (dot_slash_idx > 0)
                dot_slash_idx = retval.rfind(dot_slash,dot_slash_idx-1);
            else
                dot_slash_idx = std::string::npos;
        }
    }

    // Remove any trailing slash if one exists
    if (retval[retval.size()-1] == pathSep[0])
        retval.erase(retval.size()-1);

    // At this point we have a string that begins with a slash
    // and has only <path> terms or "../" terms. We need to
    // resolve any "../" terms by backing up through the <path>
    // terms that precede them.
    std::string slash_dot_dot = std::string(pathSep) + std::string("..");
    size_t slash_dot_dot_idx = retval.find(slash_dot_dot);
    bool noCharsRemainingToBackup = false;
    while (slash_dot_dot_idx != std::string::npos)
    {
        size_t preceding_slash_idx = retval.rfind(pathSep, slash_dot_dot_idx-1);
        if (preceding_slash_idx == std::string::npos)
        {
            size_t nchars = slash_dot_dot_idx + 3;
            retval.erase(0, nchars);
            slash_dot_dot_idx = retval.find(slash_dot_dot);
            if (slash_dot_dot_idx == 0)
            {
                retval = "";
                noCharsRemainingToBackup = true;
                break;
            }
        }
        else
        {
            size_t nchars = slash_dot_dot_idx - preceding_slash_idx + 3;
            retval.erase(preceding_slash_idx+1, nchars);
            slash_dot_dot_idx = retval.find(slash_dot_dot);
        }
    }

    // Remove any trailing slash if one exists
    if (retval.size() && retval[retval.size()-1] == pathSep[0])
        retval.erase(retval.size()-1);

    if (retval == "" && !noCharsRemainingToBackup) retval = ".";

    StaticStringBuf[0] = '\0';
    strcat(StaticStringBuf, retval.c_str());
    return StaticStringBuf;
}

std::string
FileFunctions::Normalize(const std::string &path, const std::string &pathSep)
{
    return Normalize(path.c_str(), pathSep.c_str());
}

// ****************************************************************************
//  Function: Absname
//
//  Purpose: Compute absolute path name based on cwd and a path relative to
//  the cwd.
//
//  Programmer: Mark C. Miller, Mon Jul 16 21:56:03 PDT 2012
//
//  Modifications:
//    Kathleen Biagas, Thu June 6 09:39:25 PDT 2013
//    Added pathSep argument that defaults to platform-specific 
//    VISIT_SLASH_STRING.  Use of non-platform specific case my be needed if
//    parsing internal database path-names.
//
bonnell's avatar
bonnell committed
849 850 851
//    Kathleen Biagas, Thu Jan 29 15:53:12 MST 2015
//    Some tweaks on windows to hanlde cwd_context of '.'.
//
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
// ****************************************************************************

const char *
FileFunctions::Absname(const char *cwd_context, const char *path, 
    const char *pathSep)
{
    // Clear our temporary array for handling char * return values.
    StaticStringBuf[0] = '\0';

    // cwd_context is null or empty string
    if (!cwd_context || cwd_context[0] == '\0')
    {
        if (!path) return StaticStringBuf;
        if (path[0] != pathSep[0]) return StaticStringBuf;

        std::string npath(Normalize(path, pathSep));
        strcpy(StaticStringBuf, npath.c_str());
        return StaticStringBuf;
    }

    // path is null or empty string
    if (!path || path[0] == '\0')
    {
        if (!cwd_context) return StaticStringBuf;
        if (cwd_context[0] != pathSep[0]) return StaticStringBuf;

        std::string ncwd(Normalize(cwd_context, pathSep));
        strcpy(StaticStringBuf, ncwd.c_str());
        return StaticStringBuf;
    }

    if (path[0] == pathSep[0])
    {
        std::string npath(Normalize(path, pathSep));
        strcpy(StaticStringBuf, npath.c_str());
        return StaticStringBuf;
    }

bonnell's avatar
bonnell committed
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
#ifndef _WIN32
    if (cwd_context[0] != pathSep[0])
    {
        return StaticStringBuf;
    }
#else
    if(cwd_context[0] == '.')
    {
        if (PathIsRelative(path))
        {
            if(_fullpath(StaticStringBuf, ".\\", _MAX_PATH) != NULL)
                return StaticStringBuf;
            else
                return path;
        }
        return path;
    }
#endif
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142

    // Catenate path to cwd_context and then Normalize the result
    std::string path2 = std::string(cwd_context) + std::string(pathSep) + std::string(path);
    std::string npath = Normalize(path2.c_str(), pathSep);
    strcpy(StaticStringBuf, npath.c_str());
    return StaticStringBuf;
}

std::string
FileFunctions::Absname(const std::string &cwd_context, 
                       const std::string &path, 
                       const std::string &pathSep)
{
    return Absname(cwd_context.c_str(), path.c_str(), pathSep.c_str());
}

// ****************************************************************************
//  Function: CheckPermissions
//
//  Purpose:
//      Makes sure that we have read permission to the file.
//
//  Arguments:
//      filename   The fully qualified (wrt to path) name of the file.
//
//  Programmer: Hank Childs
//  Creation:   November 7, 2001
//
//  Modifications:
//    Brad Whitlock, Tue Apr 2 14:21:07 PST 2002
//    Added a Windows implementation.
//
//    Hank Childs, Mon May  6 16:03:04 PDT 2002
//    Don't read uninitialized memory if the stat failed.
//
//    Brad Whitlock, Fri Jul 26 11:33:07 PDT 2002
//    I changed the code so it gives a different kind of exception if the
//    desired file does not exist.
//
//    Mark C. Miller, Thu Mar 30 16:45:35 PST 2006
//    Made it use VisItStat instead of stat
//
//    Tom Fogal, Sun May  3 15:33:55 MDT 2009
//    Marked the functions static.
//
//    Kathleen Biagas, Thu Feb 6 13:24:01 PST 2014
//    Test for file's existence on Windows. Throwing the exception here can
//    prevent engine crashing elsewhere. (conn_cmfe test with bad_file.silo).
//
// ****************************************************************************

#if defined(_WIN32)
FileFunctions::PermissionsResult
FileFunctions::CheckPermissions(const std::string &filename)
{
    FileFunctions::PermissionsResult retval = FileFunctions::PERMISSION_RESULT_READABLE;
    FileFunctions::VisItStat_t s;
    int result = FileFunctions::VisItStat(filename, &s);
    if (result < 0)
    {
        retval = FileFunctions::PERMISSION_RESULT_NOFILE;
    }
    return retval;
}

#else
static bool    setUpUserInfo = false;
static uid_t   uid;
static gid_t   gids[100];
static int     ngids;

static void
SetUpUserInfo(void)
{
    setUpUserInfo = true;
    uid = getuid();
    ngids = getgroups(100, gids);
}

FileFunctions::PermissionsResult
FileFunctions::CheckPermissions(const std::string &filename)
{
    if (!setUpUserInfo)
    {
        SetUpUserInfo();
    }

    VisItStat_t s;
    int rv = VisItStat(filename, &s);
    if (rv < 0)
    {
        if(errno == ENOENT || errno == ENOTDIR)
        {
            return PERMISSION_RESULT_NOFILE;
        }
        else
        {
            return PERMISSION_RESULT_NONREADABLE;
        }
    }
    mode_t mode = s.st_mode;

    //
    // If other has permission, then we are set.
    //
    if (mode & S_IROTH)
    {
        return PERMISSION_RESULT_READABLE;
    }

    //
    // If we are the user and the user has permission, then we are set.
    //
    bool isuser =  (s.st_uid == uid);
    if (isuser && (mode & S_IRUSR))
    {
        return PERMISSION_RESULT_READABLE;
    }

    //
    // If we are in the group and the group has permission, then we are set.
    //
    bool isgroup = false;
    for (int i = 0 ; i < ngids ; i++)
    {
        if (gids[i] == s.st_gid)
        {
            isgroup = true;
        }
    }
    if (isgroup && (mode & S_IRGRP))
    {
        return PERMISSION_RESULT_READABLE;
    }

    return PERMISSION_RESULT_NONREADABLE;
}
#endif

// ****************************************************************************
// Method: FileFunctions::SplitHostDatabase
//
// Purpose: 
//   Splits a hostDB into host and database names.
//
// Arguments:
//   hostDB : The entire host and db name.
//   host   : The host that was split out of the hostDB.
//   db     : The db that was split out of the hostDB.
//
// Programmer: Brad Whitlock
// Creation:   Thu Mar 25 15:40:48 PST 2004
//
// Modifications:
//   
// ****************************************************************************

void
FileFunctions::SplitHostDatabase(const std::string &hostDB, 
    std::string &host, std::string &db)
{
    std::string hdb(hostDB);
    //
    // If we found what looks like drive punctuation (on Windows), replace
    // the : in the drive with something else so we can still check
    // for a host without getting it wrong.
    //
    bool foundDrive = false;
    std::string::size_type driveColon = hdb.find(":\\");
    if(driveColon != std::string::npos)
    {
        hdb.replace(driveColon, 1, "]");
        foundDrive = true;
    }

    // Look for the host colon.
    std::string::size_type hostColon = hdb.find(':');

    //
    // Now that the host colon was looked for, replace the drive if
    // we found one previously.
    //
    if(foundDrive)
        hdb.replace(driveColon, 1, ":");

    //
    // If the database string doesn't have a ':' in it then assume that
    // the host name is "localhost" and the database name is the entire
    // string.
    //
    if (hostColon == std::string::npos)
    {
        host = "localhost";
        db = hdb;
    }
    else
    {
        //
        // If the database string does have a ':' in it then the part before
        // it is the host name and the part after it is the database name.
        //
        host = hdb.substr(0, hostColon);
        db = hdb.substr(hostColon + 1);
    }
}

// ****************************************************************************
// Method: FileFunctions::ComposeDatabaseName
//
// Purpose: 
//   Composes a database name from host and file names.
//
// Arguments:
//   host : The name of the host where the database is stored.
//   db   : The name of the database.
//
// Returns:    The host + database name.
//
// Note:       
//
// Programmer: Brad Whitlock
// Creation:   Thu Mar 25 15:05:40 PST 2004
//
// Modifications:
//   
// ****************************************************************************

std::string
FileFunctions::ComposeDatabaseName(const std::string &host,
    const std::string &db)
{
    std::string h(host);

    if(h == "")
        h = "localhost";
1143

1144
    return h + ":" + db;
1145
}
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188

// ****************************************************************************
//  Method: FileMatchesPatternCB
//
//  Purpose:
//    This function is a callback to the method ReadAndProcessDirectory,
//    located in Utility.h.  It is called for each file in a given directory.
//    Once it receives a file, it feeds that file to caller which then
//    determines if the filename matches the requested pattern.
//
//  Programmer: Kathleen Biagas
//  Creation:   Jun 26, 2013
//
//  Modifications:
//    Kathleen Biagas, Fri Jun 26 12:13:39 PDT 2015
//    Moved from NetworkManager, and added the 'returnFullpath' callback data
//    item.
//
// ****************************************************************************


void
FileFunctions::FileMatchesPatternCB(void *cbdata, const std::string &filename, bool isDir, bool canAccess, long size)
{
    if (!isDir)
    {
        void **arr = (void **)cbdata;
        std::vector< std::string > *fl = (std::vector< std::string > *)arr[0];
        std::string *pattern = (std::string*)arr[1];
        int *returnFullPath = (int*)arr[2];
        std::string name(filename);
        size_t index  = filename.rfind(VISIT_SLASH_CHAR);
        if(index != std::string::npos)
            name = name.substr(index+1);
        if (WildcardStringMatch(*pattern, name))
        {
           if (*returnFullPath)
               fl->push_back(filename);
           else 
               fl->push_back(name);
        }
    }
}