- Timestamp:
- Oct 26, 2010 8:07:21 AM (14 years ago)
- Location:
- trunk/src/VBox
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxManage/VBoxManageGuestCtrl.cpp
r33414 r33446 70 70 typedef struct DIRECTORYENTRY 71 71 { 72 char *pszPath; 72 char *pszSourcePath; 73 char *pszDestPath; 73 74 RTLISTNODE Node; 74 75 } DIRECTORYENTRY, *PDIRECTORYENTRY; … … 92 93 " <source on host> <destination on guest>\n" 93 94 " --username <name> --password <password>\n" 94 " [-- recursive] [--verbose] [--flags <flags>]\n"95 " [--dryrun] [--recursive] [--verbose] [--flags <flags>]\n" 95 96 "\n"); 96 97 } … … 626 627 } 627 628 629 630 int ctrlCopyDirectoryEntryAppend(const char *pszFileSource, const char *pszFileDest, 631 PRTLISTNODE pList) 632 { 633 AssertPtrReturn(pszFileSource, VERR_INVALID_POINTER); 634 AssertPtrReturn(pszFileDest, VERR_INVALID_POINTER); 635 AssertPtrReturn(pList, VERR_INVALID_POINTER); 636 637 PDIRECTORYENTRY pNode = (PDIRECTORYENTRY)RTMemAlloc(sizeof(DIRECTORYENTRY)); 638 if (pNode == NULL) 639 return VERR_NO_MEMORY; 640 641 pNode->pszSourcePath = NULL; 642 pNode->pszDestPath = NULL; 643 if (RT_SUCCESS(RTStrAAppend(&pNode->pszSourcePath, pszFileSource))) 644 { 645 if (RT_SUCCESS(RTStrAAppend(&pNode->pszDestPath, pszFileDest))) 646 { 647 pNode->Node.pPrev = NULL; 648 pNode->Node.pNext = NULL; 649 RTListAppend(pList, &pNode->Node); 650 return VINF_SUCCESS; 651 } 652 return VERR_NO_MEMORY; 653 } 654 return VERR_NO_MEMORY; 655 } 656 628 657 /** 629 658 * TODO … … 631 660 * @return IPRT status code. 632 661 * @return int 633 * @param pszPath 662 * @param pszRootDir 663 * @param pszSubDir 664 * @param pszFilter 665 * @param uFlags 666 * @param pcObjects 634 667 * @param pList 635 668 */ 636 int ctrlCopyDirectoryEntryAppend(const char *pszPath, PRTLISTNODE pList) 669 int ctrlCopyDirectoryRead(const char *pszRootDir, const char *pszSubDir, const char *pszFilter, 670 uint32_t uFlags, uint32_t *pcObjects, PRTLISTNODE pList) 637 671 { 638 AssertPtrReturn(pszPath, VERR_INVALID_POINTER); 672 AssertPtrReturn(pszRootDir, VERR_INVALID_POINTER); 673 /* Sub directory is optional. */ 674 /* Filter directory is optional. */ 675 AssertPtrReturn(pcObjects, VERR_INVALID_POINTER); 639 676 AssertPtrReturn(pList, VERR_INVALID_POINTER); 640 677 641 LogFlowFunc(("Appending to pList=%p: %s\n", pList, pszPath)); 642 643 PDIRECTORYENTRY pNode = (PDIRECTORYENTRY)RTMemAlloc(sizeof(DIRECTORYENTRY)); 644 if (pNode == NULL) 645 return VERR_NO_MEMORY; 646 647 pNode->pszPath = NULL; 648 if (RT_SUCCESS(RTStrAAppend(&pNode->pszPath, pszPath))) 649 { 650 pNode->Node.pPrev = NULL; 651 pNode->Node.pNext = NULL; 652 RTListAppend(pList, &pNode->Node); 653 return VINF_SUCCESS; 654 } 655 return VERR_NO_MEMORY; 678 PRTDIR pDir = NULL; 679 680 int rc = VINF_SUCCESS; 681 char szCurDir[RTPATH_MAX]; 682 if (RTStrPrintf(szCurDir, sizeof(szCurDir), pszRootDir)) 683 { 684 if (pszSubDir != NULL) 685 rc = RTPathAppend(szCurDir, sizeof(szCurDir), pszSubDir); 686 if (RT_SUCCESS(rc) && pszFilter != NULL) 687 rc = RTPathAppend(szCurDir, sizeof(szCurDir), pszFilter); 688 } 689 else 690 rc = VERR_NO_MEMORY; 691 692 if (pszFilter) 693 rc = RTDirOpenFiltered(&pDir, szCurDir, 694 #ifdef RT_OS_WINDOWS 695 RTDIRFILTER_WINNT); 696 #else 697 RTDIRFILTER_UNIX); 698 #endif 699 else 700 rc = RTDirOpen(&pDir, szCurDir); 701 702 if (RT_SUCCESS(rc)) 703 { 704 for (;;) 705 { 706 RTDIRENTRY DirEntry; 707 rc = RTDirRead(pDir, &DirEntry, NULL); 708 if (RT_FAILURE(rc)) 709 { 710 if (rc == VERR_NO_MORE_FILES) 711 rc = VINF_SUCCESS; 712 break; 713 } 714 switch (DirEntry.enmType) 715 { 716 case RTDIRENTRYTYPE_DIRECTORY: 717 /* Skip "." and ".." entrires. */ 718 if ( !strcmp(DirEntry.szName, ".") 719 || !strcmp(DirEntry.szName, "..")) 720 { 721 break; 722 } 723 if (uFlags & CopyFileFlag_Recursive) 724 { 725 char *pszNewSub = NULL; 726 if (pszSubDir) 727 RTStrAPrintf(&pszNewSub, "%s%s/", pszSubDir, DirEntry.szName); 728 else 729 RTStrAPrintf(&pszNewSub, "%s/", DirEntry.szName); 730 731 if (pszNewSub) 732 { 733 rc = ctrlCopyDirectoryRead(pszRootDir, pszNewSub, pszFilter, 734 uFlags, pcObjects, pList); 735 RTStrFree(pszNewSub); 736 } 737 else 738 rc = VERR_NO_MEMORY; 739 } 740 break; 741 742 case RTDIRENTRYTYPE_FILE: 743 { 744 char *pszFileSource = NULL; 745 char *pszFileDest = NULL; 746 if (RTStrAPrintf(&pszFileSource, "%s%s%s", 747 pszRootDir, pszSubDir ? pszSubDir : "", 748 DirEntry.szName)) 749 { 750 if (!RTStrAPrintf(&pszFileDest, "%s%s", 751 pszSubDir ? pszSubDir : "", 752 DirEntry.szName)) 753 { 754 rc = VERR_NO_MEMORY; 755 } 756 } 757 else 758 rc = VERR_NO_MEMORY; 759 760 if (RT_SUCCESS(rc)) 761 { 762 rc = ctrlCopyDirectoryEntryAppend(pszFileSource, pszFileDest, pList); 763 if (RT_SUCCESS(rc)) 764 *pcObjects = *pcObjects + 1; 765 } 766 767 if (pszFileSource) 768 RTStrFree(pszFileSource); 769 if (pszFileDest) 770 RTStrFree(pszFileDest); 771 break; 772 } 773 774 case RTDIRENTRYTYPE_SYMLINK: 775 if ( (uFlags & CopyFileFlag_Recursive) 776 && (uFlags & CopyFileFlag_FollowLinks)) 777 { 778 /* TODO */ 779 } 780 break; 781 782 default: 783 break; 784 } 785 if (RT_FAILURE(rc)) 786 break; 787 } 788 } 789 790 if (pDir) 791 RTDirClose(pDir); 792 return rc; 656 793 } 657 794 … … 661 798 * @return IPRT status code. 662 799 * @return int 663 * @param psz Directory664 * @param psz Filter800 * @param pszSource 801 * @param pszDest 665 802 * @param uFlags 666 803 * @param pcObjects 667 804 * @param pList 668 */669 int ctrlCopyDirectoryRead(const char *pszDirectory, const char *pszFilter,670 uint32_t uFlags, uint32_t *pcObjects, PRTLISTNODE pList)671 {672 AssertPtrReturn(pszDirectory, VERR_INVALID_POINTER);673 /* Filter is optional. */674 AssertPtrReturn(pcObjects, VERR_INVALID_POINTER);675 AssertPtrReturn(pList, VERR_INVALID_POINTER);676 677 LogFlowFunc(("Reading directory: %s, filter: %s\n",678 pszDirectory, pszFilter ? pszFilter : "<None>"));679 680 PRTDIR pDir = NULL;681 int rc = RTDirOpenFiltered(&pDir, pszDirectory,682 #ifdef RT_OS_WINDOWS683 RTDIRFILTER_WINNT);684 #else685 RTDIRFILTER_UNIX);686 #endif687 char *pszDirectoryStrip = RTStrDup(pszDirectory);688 if (!pszDirectoryStrip)689 rc = VERR_NO_MEMORY;690 691 if (RT_SUCCESS(rc))692 {693 RTPathStripFilename(pszDirectoryStrip);694 for (;;)695 {696 RTDIRENTRY DirEntry;697 rc = RTDirRead(pDir, &DirEntry, NULL);698 if (RT_FAILURE(rc))699 {700 if (rc == VERR_NO_MORE_FILES)701 rc = VINF_SUCCESS;702 break;703 }704 switch (DirEntry.enmType)705 {706 case RTDIRENTRYTYPE_DIRECTORY:707 /* Skip "." and ".." entrires. */708 if ( !strcmp(DirEntry.szName, ".")709 || !strcmp(DirEntry.szName, ".."))710 {711 break;712 }713 if (uFlags & CopyFileFlag_Recursive)714 rc = ctrlCopyDirectoryRead(DirEntry.szName, pszFilter,715 uFlags, pcObjects, pList);716 break;717 718 case RTDIRENTRYTYPE_FILE:719 {720 char *pszFile;721 if (RTStrAPrintf(&pszFile, "%s%c%s",722 pszDirectoryStrip, RTPATH_SLASH, DirEntry.szName))723 {724 rc = ctrlCopyDirectoryEntryAppend(pszFile, pList);725 if (RT_SUCCESS(rc))726 *pcObjects = *pcObjects + 1;727 RTStrFree(pszFile);728 }729 break;730 }731 732 case RTDIRENTRYTYPE_SYMLINK:733 if ( (uFlags & CopyFileFlag_Recursive)734 && (uFlags & CopyFileFlag_FollowLinks))735 {736 rc = ctrlCopyDirectoryRead(DirEntry.szName, pszFilter,737 uFlags, pcObjects, pList);738 }739 break;740 741 default:742 break;743 }744 if (RT_FAILURE(rc))745 break;746 }747 RTStrFree(pszDirectoryStrip);748 }749 750 if (pDir)751 RTDirClose(pDir);752 return rc;753 }754 755 /**756 * TODO757 *758 * @return IPRT status code.759 * @param pszSource760 * @param pszDest761 * @param uFlags762 805 */ 763 806 int ctrlCopyInit(const char *pszSource, const char *pszDest, uint32_t uFlags, … … 769 812 AssertPtrReturn(pList, VERR_INVALID_PARAMETER); 770 813 771 int rc ;814 int rc = VINF_SUCCESS; 772 815 char *pszSourceAbs = RTPathAbsDup(pszSource); 773 816 if (pszSourceAbs) … … 777 820 { 778 821 RTListInit(pList); 779 rc = ctrlCopyDirectoryEntryAppend(pszSourceAbs, p List);822 rc = ctrlCopyDirectoryEntryAppend(pszSourceAbs, pszDest, pList); 780 823 *pcObjects = 1; 781 824 } 782 825 else /* ... or a directory. */ 783 826 { 784 RTListInit(pList); 785 rc = ctrlCopyDirectoryRead(pszSourceAbs, NULL /* Filter */, 786 uFlags, pcObjects, pList); 787 if (*pcObjects == 0) 788 rc = VERR_NOT_FOUND; 827 /* Append trailing slash to absolute directory. */ 828 if (RTDirExists(pszSourceAbs)) 829 RTStrAAppend(&pszSourceAbs, RTPATH_SLASH_STR); 830 831 /* Extract directory filter (e.g. "*.exe"). */ 832 char *pszFilter = RTPathFilename(pszSourceAbs); 833 char *pszSourceAbsRoot = RTStrDup(pszSourceAbs); 834 if (pszSourceAbsRoot) 835 { 836 if (pszFilter) 837 { 838 RTPathStripFilename(pszSourceAbsRoot); 839 RTStrAAppend(&pszSourceAbsRoot, RTPATH_SLASH_STR); 840 } 841 else 842 { 843 /* 844 * If we have more than one file to copy, make sure that we have 845 * a trailing slash so that we can construct a full path name 846 * (e.g. "foo.txt" -> "c:/foo/temp.txt") as destination. 847 */ 848 size_t cch = strlen(pszSourceAbsRoot); 849 if ( cch > 1 850 && !RTPATH_IS_SLASH(pszSourceAbsRoot[cch - 1]) 851 && !RTPATH_IS_SLASH(pszSourceAbsRoot[cch - 2])) 852 { 853 rc = RTStrAAppend(&pszSourceAbsRoot, RTPATH_SLASH_STR); 854 } 855 } 856 857 if (RT_SUCCESS(rc)) 858 { 859 RTListInit(pList); 860 rc = ctrlCopyDirectoryRead(pszSourceAbsRoot, NULL /* Sub directory */, 861 pszFilter, 862 uFlags, pcObjects, pList); 863 if (*pcObjects == 0) 864 rc = VERR_NOT_FOUND; 865 } 866 867 RTStrFree(pszSourceAbsRoot); 868 } 869 else 870 rc = VERR_NO_MEMORY; 789 871 } 790 872 RTStrFree(pszSourceAbs); … … 811 893 bool fLast = RTListNodeIsLast(pList, &pNode->Node); 812 894 813 if (pNode->pszPath) 814 RTStrFree(pNode->pszPath); 895 if (pNode->pszSourcePath) 896 RTStrFree(pNode->pszSourcePath); 897 if (pNode->pszDestPath) 898 RTStrFree(pNode->pszDestPath); 815 899 RTListNodeRemove(&pNode->Node); 816 900 RTMemFree(pNode); … … 968 1052 bool fVerbose = false; 969 1053 bool fCopyRecursive = false; 1054 bool fDryRun = false; 970 1055 971 1056 /* Iterate through all possible commands (if available). */ … … 994 1079 ++i; 995 1080 } 1081 } 1082 else if (!strcmp(a->argv[i], "--dryrun")) 1083 { 1084 fDryRun = true; 996 1085 } 997 1086 else if (!strcmp(a->argv[i], "--flags")) … … 1070 1159 ULONG uPID = 0; 1071 1160 1161 if (fVerbose) 1162 { 1163 if (fDryRun) 1164 RTPrintf("Dry run - no files copied!\n"); 1165 RTPrintf("Gathering file information ...\n"); 1166 } 1167 1072 1168 RTLISTNODE listToCopy; 1073 1169 uint32_t cObjects = 0; … … 1093 1189 else 1094 1190 { 1095 if ( fVerbose)1191 if (RT_SUCCESS(vrc) && fVerbose) 1096 1192 { 1097 1193 if (fCopyRecursive) … … 1110 1206 { 1111 1207 if (fVerbose) 1112 RTPrintf("Copying \"%s\" (%u of %u) ...\n", 1113 pNode->pszPath, uCurObject, cObjects); 1114 vrc = ctrlCopyFile(guest, pNode->pszPath, Utf8Dest.c_str(), 1115 Utf8UserName.c_str(), Utf8Password.c_str(), uFlags); 1208 RTPrintf("Copying \"%s\" (%u/%u) ...\n", 1209 pNode->pszSourcePath, uCurObject, cObjects); 1210 if (!fDryRun) 1211 vrc = ctrlCopyFile(guest, pNode->pszSourcePath, pNode->pszDestPath, 1212 Utf8UserName.c_str(), Utf8Password.c_str(), uFlags); 1116 1213 if (RT_FAILURE(vrc)) 1117 1214 break; -
trunk/src/VBox/Main/GuestImpl.cpp
r33412 r33446 1891 1891 * Prepare tool command line. 1892 1892 */ 1893 args.push_back(Bstr("vbox_cat").raw()); /* The actual (internal) tool to use (as argv[0]). */ 1894 args.push_back(Bstr("--output").raw()); /* We want to write a file ... */ 1895 args.push_back(Bstr(Utf8Dest).raw()); /* ... which is specified here. */ 1896 1897 /* 1898 * Okay, since we gathered all stuff we need until now to start the 1899 * actual copying, start the guest part now. 1900 */ 1893 char szOutput[RTPATH_MAX]; 1894 if (RTStrPrintf(szOutput, sizeof(szOutput), "--output=%s", Utf8Dest.c_str())) 1895 { 1896 args.push_back(Bstr("vbox_cat").raw()); /* The actual (internal) tool to use (as argv[0]). */ 1897 args.push_back(Bstr(szOutput).raw()); /* We want to write a file ... */ 1898 } 1899 else 1900 rc = setError(VBOX_E_IPRT_ERROR, tr("Error preparing command line")); 1901 1902 ComPtr<IProgress> execProgress; 1901 1903 ULONG uPID; 1902 ComPtr<IProgress> execProgress; 1903 rc = ExecuteProcess(Bstr("vbox_cat").raw(), 0 /* Flags */, 1904 ComSafeArrayAsInParam(args), 1905 ComSafeArrayAsInParam(env), 1906 Bstr(Utf8UserName).raw(), 1907 Bstr(Utf8Password).raw(), 0 /* Timeout */, 1908 &uPID, execProgress.asOutParam()); 1904 if (SUCCEEDED(rc)) 1905 { 1906 LogRel(("Copying file \"%s\" to guest \"%s\" ...\n", 1907 Utf8Source.c_str(), Utf8Dest.c_str())); 1908 1909 /* 1910 * Okay, since we gathered all stuff we need until now to start the 1911 * actual copying, start the guest part now. 1912 */ 1913 rc = ExecuteProcess(Bstr("vbox_cat").raw(), 0 /* Flags */, 1914 ComSafeArrayAsInParam(args), 1915 ComSafeArrayAsInParam(env), 1916 Bstr(Utf8UserName).raw(), 1917 Bstr(Utf8Password).raw(), 0 /* Timeout */, 1918 &uPID, execProgress.asOutParam()); 1919 } 1920 1909 1921 if (SUCCEEDED(rc)) 1910 1922 {
Note:
See TracChangeset
for help on using the changeset viewer.