Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fbf72d2b authored by Josh Gao's avatar Josh Gao Committed by android-build-merger
Browse files

adb: fix stat on Windows.

am: 29e7e3ed

Change-Id: If4e69a43806625956152d3f8fd0358d29f2352e1
parents e1dfdec6 29e7e3ed
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ LIBADB_TEST_SRCS := \
    fdevent_test.cpp \
    socket_test.cpp \
    sysdeps_test.cpp \
    sysdeps/stat_test.cpp \
    transport_test.cpp \

LIBADB_CFLAGS := \
@@ -89,6 +90,7 @@ LIBADB_linux_SRC_FILES := \

LIBADB_windows_SRC_FILES := \
    sysdeps_win32.cpp \
    sysdeps/win32/stat.cpp \
    usb_windows.cpp \

LIBADB_TEST_windows_SRCS := \
+10 −4
Original line number Diff line number Diff line
@@ -123,14 +123,20 @@ TEST(adb_utils, adb_dirname) {

void test_mkdirs(const std::string basepath) {
  // Test creating a directory hierarchy.
  EXPECT_TRUE(mkdirs(basepath));
  ASSERT_TRUE(mkdirs(basepath));
  // Test finding an existing directory hierarchy.
  EXPECT_TRUE(mkdirs(basepath));
  ASSERT_TRUE(mkdirs(basepath));
  // Test mkdirs on an existing hierarchy with a trailing slash.
  ASSERT_TRUE(mkdirs(basepath + '/'));
#if defined(_WIN32)
  ASSERT_TRUE(mkdirs(basepath + '\\'));
#endif

  const std::string filepath = basepath + "/file";
  // Verify that the hierarchy was created by trying to create a file in it.
  EXPECT_NE(-1, adb_creat(filepath.c_str(), 0600));
  ASSERT_NE(-1, adb_creat(filepath.c_str(), 0600));
  // If a file exists where we want a directory, the operation should fail.
  EXPECT_FALSE(mkdirs(filepath));
  ASSERT_FALSE(mkdirs(filepath));
}

TEST(adb_utils, mkdirs) {
+2 −23
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@
#include <android-base/unique_fd.h>
#include <android-base/utf8.h>

#include "sysdeps/stat.h"

/*
 * TEMP_FAILURE_RETRY is defined by some, but not all, versions of
 * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's
@@ -199,8 +201,6 @@ static __inline__ void close_on_exec(int fd)
    /* nothing really */
}

#define  lstat    stat   /* no symlinks on Win32 */

#define  S_ISLNK(m)   0   /* no symlinks on Win32 */

extern int  adb_unlink(const char*  path);
@@ -307,27 +307,6 @@ static __inline__ int adb_is_absolute_host_path(const char* path) {
    return isalpha(path[0]) && path[1] == ':' && path[2] == '\\';
}

// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
//   struct stat s;
//   stat(filename, &s);
// To turn into the following:
//   struct adb_stat s;
//   adb_stat(filename, &s);
// To get this to work, we need to make 'struct adb_stat' the same as
// 'struct stat'. Note that this definition of 'struct adb_stat' uses the
// *current* macro definition of stat, so it may actually be inheriting from
// struct _stat32i64 (or some other remapping).
struct adb_stat : public stat {};

static_assert(sizeof(struct adb_stat) == sizeof(struct stat),
    "structures should be the same");

extern int adb_stat(const char* f, struct adb_stat* s);

// stat is already a macro, undefine it so we can redefine it.
#undef stat
#define stat adb_stat

// UTF-8 versions of POSIX APIs.
extern DIR* adb_opendir(const char* dirname);
extern struct dirent* adb_readdir(DIR* dir);

adb/sysdeps/stat.h

0 → 100644
+46 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#if defined(_WIN32)
// stat is broken on Win32: stat on a path with a trailing slash or backslash will always fail with
// ENOENT.
int adb_stat(const char* path, struct adb_stat* buf);

// We later define a macro mapping 'stat' to 'adb_stat'. This causes:
//   struct stat s;
//   stat(filename, &s);
// To turn into the following:
//   struct adb_stat s;
//   adb_stat(filename, &s);
// To get this to work, we need to make 'struct adb_stat' the same as
// 'struct stat'. Note that this definition of 'struct adb_stat' uses the
// *current* macro definition of stat, so it may actually be inheriting from
// struct _stat32i64 (or some other remapping).
struct adb_stat : public stat {};

#undef stat
#define stat adb_stat

// Windows doesn't have lstat.
#define lstat adb_stat

#endif
+65 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>

#include <android-base/test_utils.h>
#include <gtest/gtest.h>

#include "adb_utils.h"
#include "sysdeps.h"

TEST(sysdeps, stat) {
    TemporaryDir td;
    TemporaryFile tf;

    struct stat st;
    ASSERT_EQ(0, stat(td.path, &st));
    ASSERT_FALSE(S_ISREG(st.st_mode));
    ASSERT_TRUE(S_ISDIR(st.st_mode));

    ASSERT_EQ(0, stat((std::string(td.path) + '/').c_str(), &st));
    ASSERT_TRUE(S_ISDIR(st.st_mode));

#if defined(_WIN32)
    ASSERT_EQ(0, stat((std::string(td.path) + '\\').c_str(), &st));
    ASSERT_TRUE(S_ISDIR(st.st_mode));
#endif

    std::string nonexistent_path = std::string(td.path) + "/nonexistent";
    ASSERT_EQ(-1, stat(nonexistent_path.c_str(), &st));
    ASSERT_EQ(ENOENT, errno);

    ASSERT_EQ(-1, stat((nonexistent_path + "/").c_str(), &st));
    ASSERT_EQ(ENOENT, errno);

#if defined(_WIN32)
    ASSERT_EQ(-1, stat((nonexistent_path + "\\").c_str(), &st));
    ASSERT_EQ(ENOENT, errno);
#endif

    ASSERT_EQ(0, stat(tf.path, &st));
    ASSERT_TRUE(S_ISREG(st.st_mode));
    ASSERT_FALSE(S_ISDIR(st.st_mode));

    ASSERT_EQ(-1, stat((std::string(tf.path) + '/').c_str(), &st));
    ASSERT_EQ(ENOTDIR, errno);

#if defined(_WIN32)
    ASSERT_EQ(-1, stat((std::string(tf.path) + '\\').c_str(), &st));
    ASSERT_EQ(ENOTDIR, errno);
#endif
}
Loading