adb_utils.cpp revision 84b0bf22644b35d6b3d3f7dc96311a484c3519b3
15830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes/* 25830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * Copyright (C) 2015 The Android Open Source Project 35830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * 45830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 55830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * you may not use this file except in compliance with the License. 65830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * You may obtain a copy of the License at 75830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * 85830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 95830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * 105830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * Unless required by applicable law or agreed to in writing, software 115830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 125830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 135830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * See the License for the specific language governing permissions and 145830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes * limitations under the License. 155830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes */ 165830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes 17e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes#define TRACE_TAG TRACE_ADB 18e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 195830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include "adb_utils.h" 205830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes 21a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes#include <stdlib.h> 225830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include <sys/stat.h> 235830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include <sys/types.h> 245830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes#include <unistd.h> 255830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes 26e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes#include <algorithm> 27e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 28e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes#include <base/stringprintf.h> 29e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 30e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes#include "adb_trace.h" 3153daee6a2b415caa0ff3e425c7fa613c834bca61Elliott Hughes#include "sysdeps.h" 3253daee6a2b415caa0ff3e425c7fa613c834bca61Elliott Hughes 33a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughesbool getcwd(std::string* s) { 34a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes char* cwd = getcwd(nullptr, 0); 35a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes if (cwd != nullptr) *s = cwd; 36a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes free(cwd); 37a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes return (cwd != nullptr); 38a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes} 39a7090b94c181f3efe5b53d2c8367b78d99074dfeElliott Hughes 405830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughesbool directory_exists(const std::string& path) { 415830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes struct stat sb; 425830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes return lstat(path.c_str(), &sb) != -1 && S_ISDIR(sb.st_mode); 435830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes} 445830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes 455830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughesstd::string escape_arg(const std::string& s) { 465498adefb00cd979137361b98fcbf8d51f72ebebElliott Hughes std::string result = s; 475830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes 4884b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes // Escape any ' in the string (before we single-quote the whole thing). 4984b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes // The correct way to do this for the shell is to replace ' with '\'' --- that is, 5084b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes // close the existing single-quoted string, escape a single single-quote, and start 5184b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes // a new single-quoted string. Like the C preprocessor, the shell will concatenate 5284b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes // these pieces into one string. 5384b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes for (size_t i = 0; i < s.size(); ++i) { 5484b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes if (s[i] == '\'') { 5584b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes result.insert(i, "'\\'"); 5684b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes i += 2; 5784b0bf22644b35d6b3d3f7dc96311a484c3519b3Elliott Hughes } 585830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes } 595498adefb00cd979137361b98fcbf8d51f72ebebElliott Hughes 605498adefb00cd979137361b98fcbf8d51f72ebebElliott Hughes // Prefix and suffix the whole string with '. 615498adefb00cd979137361b98fcbf8d51f72ebebElliott Hughes result.insert(result.begin(), '\''); 625498adefb00cd979137361b98fcbf8d51f72ebebElliott Hughes result.push_back('\''); 635830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes return result; 645830577bd82fdb7c39555da20a4cf585b8bb376aElliott Hughes} 65e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 66e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughesvoid dump_hex(const void* data, size_t byte_count) { 67e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes byte_count = std::min(byte_count, size_t(16)); 68e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 69e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes const uint8_t* p = reinterpret_cast<const uint8_t*>(data); 70e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 71e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes std::string line; 72e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes for (size_t i = 0; i < byte_count; ++i) { 73e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes android::base::StringAppendF(&line, "%02x", p[i]); 74e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes } 75e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes line.push_back(' '); 76e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 77e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes for (size_t i = 0; i < byte_count; ++i) { 78e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes int c = p[i]; 79e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes if (c < 32 || c > 127) { 80e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes c = '.'; 81e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes } 82e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes line.push_back(c); 83e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes } 84e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes 85e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes DR("%s\n", line.c_str()); 86e67f1f87d9b1188ec8617035db7006c37ee7b21eElliott Hughes} 87