From 2aba22668d4f61ddad7dff2a3e872458bf64e67a Mon Sep 17 00:00:00 2001 From: Oliver Kurth Date: Tue, 24 Oct 2017 14:07:33 -0700 Subject: [PATCH] lib/file: Enhance File_EnsureDirectoryEx to do more checking When creating a directory, distinguish whether an EEXIST error is because the directory already exists or because there is a file with that path name. Return the appropriate error. --- open-vm-tools/lib/file/file.c | 33 ++++++++++++++++++++++++-------- open-vm-tools/lib/include/file.h | 9 ++++++--- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/open-vm-tools/lib/file/file.c b/open-vm-tools/lib/file/file.c index 275614853..1ad13fdcc 100644 --- a/open-vm-tools/lib/file/file.c +++ b/open-vm-tools/lib/file/file.c @@ -365,9 +365,9 @@ File_UnlinkRetry(const char *pathName, // IN: Bool File_CreateDirectoryEx(const char *pathName, // IN: - int mask) // IN: + int mode) // IN: { - int err = FileCreateDirectory(pathName, mask); + int err = FileCreateDirectory(pathName, mode); return err == 0; } @@ -385,7 +385,7 @@ File_CreateDirectoryEx(const char *pathName, // IN: * Results: * TRUE Directory was created * FALSE Directory creation failed. - * See File_EnsureDirectoryEx for dealing with directories that + * See File_EnsureDirectory for dealing with directories that * may exist. * * Side effects: @@ -424,11 +424,28 @@ File_CreateDirectory(const char *pathName) // IN: Bool File_EnsureDirectoryEx(const char *pathName, // IN: - int mask) // IN: + int mode) // IN: { - int err = FileCreateDirectory(pathName, mask); + int err = FileCreateDirectory(pathName, mode); + + if (err == EEXIST) { + FileData fileData; + + err = FileAttributes(pathName, &fileData); + + if (err == 0) { + if (fileData.fileType != FILE_TYPE_DIRECTORY) { + err = ENOTDIR; + errno = ENOTDIR; + +#if defined(_WIN32) + SetLastError(ERROR_DIRECTORY); +#endif + } + } + } - return ((err == 0) || (err == EEXIST)); + return err == 0; } @@ -1679,7 +1696,7 @@ FileFirstSlashIndex(const char *pathName, // IN: Bool File_CreateDirectoryHierarchyEx(const char *pathName, // IN: - int mask, // IN: + int mode, // IN: char **topmostCreated) // OUT/OPT: { char *volume; @@ -1734,7 +1751,7 @@ File_CreateDirectoryHierarchyEx(const char *pathName, // IN: * confusing. We avoid this by attempting to create the directory before * checking the type. */ - err = FileCreateDirectory(temp, mask); + err = FileCreateDirectory(temp, mode); if (err == 0) { if (topmostCreated != NULL && *topmostCreated == NULL) { diff --git a/open-vm-tools/lib/include/file.h b/open-vm-tools/lib/include/file.h index 7cf44eea0..d0bbe6d93 100644 --- a/open-vm-tools/lib/include/file.h +++ b/open-vm-tools/lib/include/file.h @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998-2016 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2017 VMware, Inc. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -151,17 +151,20 @@ char *File_PathJoin(const char *dirName, Bool File_CreateDirectory(const char *pathName); Bool File_CreateDirectoryEx(const char *pathName, - int mask); + int mode); Bool File_EnsureDirectory(const char *pathName); +Bool File_EnsureDirectoryEx(const char *pathName, + int mode); + Bool File_DeleteEmptyDirectory(const char *pathName); Bool File_CreateDirectoryHierarchy(const char *pathName, char **topmostCreated); Bool File_CreateDirectoryHierarchyEx(const char *pathName, - int mask, + int mode, char **topmostCreated); Bool File_DeleteDirectoryContent(const char *pathName); -- 2.47.3