llvm.org GIT mirror llvm / 61d9d49
Reverting r297617 because it broke some bots: http://bb.pgr.jp/builders/cmake-llvm-x86_64-linux/builds/49970 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@297618 91177308-0d34-0410-b5e6-96231b3b80d8 Aaron Ballman 3 years ago
5 changed file(s) with 55 addition(s) and 304 deletion(s). Raw diff Collapse all Expand all
177177 Perms(Perms) {}
178178 #elif defined(LLVM_ON_WIN32)
179179 file_status() = default;
180
181 file_status(file_type Type) : Type(Type) {}
182
183 file_status(file_type Type, perms Perms, uint32_t LastAccessTimeHigh,
184 uint32_t LastAccessTimeLow, uint32_t LastWriteTimeHigh,
185 uint32_t LastWriteTimeLow, uint32_t VolumeSerialNumber,
186 uint32_t FileSizeHigh, uint32_t FileSizeLow,
180
181 file_status(file_type Type) : Type(Type) {}
182
183 file_status(file_type Type, uint32_t LastAccessTimeHigh,
184 uint32_t LastAccessTimeLow, uint32_t LastWriteTimeHigh,
185 uint32_t LastWriteTimeLow, uint32_t VolumeSerialNumber,
186 uint32_t FileSizeHigh, uint32_t FileSizeLow,
187187 uint32_t FileIndexHigh, uint32_t FileIndexLow)
188188 : LastAccessedTimeHigh(LastAccessTimeHigh), LastAccessedTimeLow(LastAccessTimeLow),
189189 LastWriteTimeHigh(LastWriteTimeHigh),
190 LastWriteTimeLow(LastWriteTimeLow),
191 VolumeSerialNumber(VolumeSerialNumber), FileSizeHigh(FileSizeHigh),
192 FileSizeLow(FileSizeLow), FileIndexHigh(FileIndexHigh),
193 FileIndexLow(FileIndexLow), Type(Type), Perms(Perms) {}
194 #endif
195
196 // getters
190 LastWriteTimeLow(LastWriteTimeLow),
191 VolumeSerialNumber(VolumeSerialNumber), FileSizeHigh(FileSizeHigh),
192 FileSizeLow(FileSizeLow), FileIndexHigh(FileIndexHigh),
193 FileIndexLow(FileIndexLow), Type(Type) {}
194 #endif
195
196 // getters
197197 file_type type() const { return Type; }
198198 perms permissions() const { return Perms; }
199199 TimePoint<> getLastAccessedTime() const;
601601 std::error_code status(const Twine &path, file_status &result,
602602 bool follow = true);
603603
604 /// @brief A version for when a file descriptor is already available.
605 std::error_code status(int FD, file_status &Result);
606
607 /// @brief Set file permissions.
608 ///
609 /// @param Path File to set permissions on.
610 /// @param Permissions New file permissions.
611 /// @returns errc::success if the permissions were successfully set, otherwise
612 /// a platform-specific error_code.
613 /// @note On Windows, all permissions except *_write are ignored. Using any of
614 /// owner_write, group_write, or all_write will make the file writable.
615 /// Otherwise, the file will be marked as read-only.
616 std::error_code setPermissions(const Twine &Path, perms Permissions);
617
618 /// @brief Get file permissions.
619 ///
620 /// @param Path File to get permissions from.
621 /// @returns the permissions if they were successfully retrieved, otherwise a
622 /// platform-specific error_code.
623 /// @note On Windows, if the file does not have the FILE_ATTRIBUTE_READONLY
624 /// attribute, all_all will be returned. Otherwise, all_read | all_exe
625 /// will be returned.
626 ErrorOr getPermissions(const Twine &Path);
627
628 /// @brief Get file size.
629 ///
630 /// @param Path Input path.
604 /// @brief A version for when a file descriptor is already available.
605 std::error_code status(int FD, file_status &Result);
606
607 /// @brief Get file size.
608 ///
609 /// @param Path Input path.
631610 /// @param Result Set to the size of the file in \a Path.
632611 /// @returns errc::success if result has been successfully set, otherwise a
633612 /// platform-specific error_code.
11881188 }
11891189
11901190 std::error_code directory_entry::status(file_status &result) const {
1191 return fs::status(Path, result, FollowSymlinks);
1192 }
1193
1194 ErrorOr getPermissions(const Twine &Path) {
1195 file_status Status;
1196 if (std::error_code EC = status(Path, Status))
1197 return EC;
1198
1199 return Status.permissions();
1200 }
1201
1202 } // end namespace fs
1203 } // end namespace sys
1204 } // end namespace llvm
1191 return fs::status(Path, result, FollowSymlinks);
1192 }
1193
1194 } // end namespace fs
1195 } // end namespace sys
1196 } // end namespace llvm
12051197
12061198 // Include the truly platform-specific parts.
12071199 #if defined(LLVM_ON_UNIX)
567567 std::error_code status(int FD, file_status &Result) {
568568 struct stat Status;
569569 int StatRet = ::fstat(FD, &Status);
570 return fillStatus(StatRet, Status, Result);
571 }
572
573 std::error_code setPermissions(const Twine &Path, perms Permissions) {
574 SmallString<128> PathStorage;
575 StringRef P = Path.toNullTerminatedStringRef(PathStorage);
576
577 if (::chmod(P.begin(), Permissions))
578 return std::error_code(errno, std::generic_category());
579 return std::error_code();
580 }
581
582 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
583 #if defined(HAVE_FUTIMENS)
584 timespec Times[2];
570 return fillStatus(StatRet, Status, Result);
571 }
572
573 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
574 #if defined(HAVE_FUTIMENS)
575 timespec Times[2];
585576 Times[0] = Times[1] = sys::toTimeSpec(Time);
586577 if (::futimens(FD, Times))
587578 return std::error_code(errno, std::generic_category());
526526 goto handle_status_error;
527527
528528 {
529 file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
530 ? file_type::directory_file
531 : file_type::regular_file;
532 perms Permissions = (Info.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
533 ? (all_read | all_exe)
534 : all_all;
535 Result = file_status(
536 Type, Permissions, Info.ftLastAccessTime.dwHighDateTime,
537 Info.ftLastAccessTime.dwLowDateTime,
538 Info.ftLastWriteTime.dwHighDateTime, Info.ftLastWriteTime.dwLowDateTime,
539 Info.dwVolumeSerialNumber, Info.nFileSizeHigh, Info.nFileSizeLow,
540 Info.nFileIndexHigh, Info.nFileIndexLow);
541 return std::error_code();
542 }
543
529 file_type Type = (Info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
530 ? file_type::directory_file
531 : file_type::regular_file;
532 Result =
533 file_status(Type, Info.ftLastAccessTime.dwHighDateTime,
534 Info.ftLastAccessTime.dwLowDateTime,
535 Info.ftLastWriteTime.dwHighDateTime,
536 Info.ftLastWriteTime.dwLowDateTime,
537 Info.dwVolumeSerialNumber, Info.nFileSizeHigh,
538 Info.nFileSizeLow, Info.nFileIndexHigh, Info.nFileIndexLow);
539 return std::error_code();
540 }
541
544542 handle_status_error:
545543 DWORD LastError = ::GetLastError();
546544 if (LastError == ERROR_FILE_NOT_FOUND ||
587585
588586 std::error_code status(int FD, file_status &Result) {
589587 HANDLE FileHandle = reinterpret_cast(_get_osfhandle(FD));
590 return getStatus(FileHandle, Result);
591 }
592
593 std::error_code setPermissions(const Twine &Path, perms Permissions) {
594 SmallVector PathUTF16;
595 if (std::error_code EC = widenPath(Path, PathUTF16))
596 return EC;
597
598 DWORD Attributes = ::GetFileAttributesW(PathUTF16.begin());
599 if (Attributes == INVALID_FILE_ATTRIBUTES)
600 return mapWindowsError(GetLastError());
601
602 // There are many Windows file attributes that are not to do with the file
603 // permissions (e.g. FILE_ATTRIBUTE_HIDDEN). We need to be careful to preserve
604 // them.
605 if (Permissions & all_write) {
606 Attributes &= ~FILE_ATTRIBUTE_READONLY;
607 if (Attributes == 0)
608 // FILE_ATTRIBUTE_NORMAL indicates no other attributes are set.
609 Attributes |= FILE_ATTRIBUTE_NORMAL;
610 }
611 else {
612 Attributes |= FILE_ATTRIBUTE_READONLY;
613 // FILE_ATTRIBUTE_NORMAL is not compatible with any other attributes, so
614 // remove it, if it is present.
615 Attributes &= ~FILE_ATTRIBUTE_NORMAL;
616 }
617
618 if (!::SetFileAttributesW(PathUTF16.begin(), Attributes))
619 return mapWindowsError(GetLastError());
620
621 return std::error_code();
622 }
623
624 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
625 FILETIME FT = toFILETIME(Time);
626 HANDLE FileHandle = reinterpret_cast(_get_osfhandle(FD));
588 return getStatus(FileHandle, Result);
589 }
590
591 std::error_code setLastModificationAndAccessTime(int FD, TimePoint<> Time) {
592 FILETIME FT = toFILETIME(Time);
593 HANDLE FileHandle = reinterpret_cast(_get_osfhandle(FD));
627594 if (!SetFileTime(FileHandle, NULL, &FT, &FT))
628595 return mapWindowsError(::GetLastError());
629596 return std::error_code();
12521252 fs::UniqueID D1, D2;
12531253 ASSERT_NO_ERROR(fs::getUniqueID(TestDirectory, D1));
12541254 ASSERT_NO_ERROR(fs::getUniqueID(path, D2));
1255 ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;
1256 }
1257
1258 TEST_F(FileSystemTest, permissions) {
1259 int FD;
1260 SmallString<64> TempPath;
1261 ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath));
1262 FileRemover Cleanup(TempPath);
1263
1264 // Make sure it exists.
1265 ASSERT_TRUE(fs::exists(Twine(TempPath)));
1266
1267 auto CheckPermissions = [&](fs::perms Expected) {
1268 ErrorOr Actual = fs::getPermissions(TempPath);
1269 return Actual && *Actual == Expected;
1270 };
1271
1272 std::error_code NoError;
1273 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all), NoError);
1274 EXPECT_TRUE(CheckPermissions(fs::all_all));
1275
1276 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::all_exe), NoError);
1277 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::all_exe));
1278
1279 #if defined(LLVM_ON_WIN32)
1280 fs::perms ReadOnly = fs::all_read | fs::all_exe;
1281 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
1282 EXPECT_TRUE(CheckPermissions(ReadOnly));
1283
1284 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
1285 EXPECT_TRUE(CheckPermissions(ReadOnly));
1286
1287 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
1288 EXPECT_TRUE(CheckPermissions(fs::all_all));
1289
1290 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
1291 EXPECT_TRUE(CheckPermissions(ReadOnly));
1292
1293 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
1294 EXPECT_TRUE(CheckPermissions(fs::all_all));
1295
1296 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
1297 EXPECT_TRUE(CheckPermissions(ReadOnly));
1298
1299 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
1300 EXPECT_TRUE(CheckPermissions(fs::all_all));
1301
1302 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
1303 EXPECT_TRUE(CheckPermissions(ReadOnly));
1304
1305 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
1306 EXPECT_TRUE(CheckPermissions(fs::all_all));
1307
1308 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
1309 EXPECT_TRUE(CheckPermissions(ReadOnly));
1310
1311 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
1312 EXPECT_TRUE(CheckPermissions(fs::all_all));
1313
1314 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
1315 EXPECT_TRUE(CheckPermissions(ReadOnly));
1316
1317 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
1318 EXPECT_TRUE(CheckPermissions(fs::all_all));
1319
1320 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
1321 EXPECT_TRUE(CheckPermissions(ReadOnly));
1322
1323 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
1324 EXPECT_TRUE(CheckPermissions(fs::all_all));
1325
1326 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
1327 EXPECT_TRUE(CheckPermissions(ReadOnly));
1328
1329 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
1330 EXPECT_TRUE(CheckPermissions(ReadOnly));
1331
1332 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
1333 EXPECT_TRUE(CheckPermissions(ReadOnly));
1334
1335 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
1336 EXPECT_TRUE(CheckPermissions(ReadOnly));
1337
1338 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
1339 fs::set_gid_on_exe |
1340 fs::sticky_bit),
1341 NoError);
1342 EXPECT_TRUE(CheckPermissions(ReadOnly));
1343
1344 EXPECT_EQ(fs::setPermissions(TempPath, ReadOnly | fs::set_uid_on_exe |
1345 fs::set_gid_on_exe |
1346 fs::sticky_bit),
1347 NoError);
1348 EXPECT_TRUE(CheckPermissions(ReadOnly));
1349
1350 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all | fs::set_uid_on_exe |
1351 fs::set_gid_on_exe |
1352 fs::sticky_bit),
1353 NoError);
1354 EXPECT_TRUE(CheckPermissions(fs::all_all));
1355 #else
1356 EXPECT_EQ(fs::setPermissions(TempPath, fs::no_perms), NoError);
1357 EXPECT_TRUE(CheckPermissions(fs::no_perms));
1358
1359 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_read), NoError);
1360 EXPECT_TRUE(CheckPermissions(fs::owner_read));
1361
1362 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_write), NoError);
1363 EXPECT_TRUE(CheckPermissions(fs::owner_write));
1364
1365 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_exe), NoError);
1366 EXPECT_TRUE(CheckPermissions(fs::owner_exe));
1367
1368 EXPECT_EQ(fs::setPermissions(TempPath, fs::owner_all), NoError);
1369 EXPECT_TRUE(CheckPermissions(fs::owner_all));
1370
1371 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_read), NoError);
1372 EXPECT_TRUE(CheckPermissions(fs::group_read));
1373
1374 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_write), NoError);
1375 EXPECT_TRUE(CheckPermissions(fs::group_write));
1376
1377 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_exe), NoError);
1378 EXPECT_TRUE(CheckPermissions(fs::group_exe));
1379
1380 EXPECT_EQ(fs::setPermissions(TempPath, fs::group_all), NoError);
1381 EXPECT_TRUE(CheckPermissions(fs::group_all));
1382
1383 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_read), NoError);
1384 EXPECT_TRUE(CheckPermissions(fs::others_read));
1385
1386 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_write), NoError);
1387 EXPECT_TRUE(CheckPermissions(fs::others_write));
1388
1389 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_exe), NoError);
1390 EXPECT_TRUE(CheckPermissions(fs::others_exe));
1391
1392 EXPECT_EQ(fs::setPermissions(TempPath, fs::others_all), NoError);
1393 EXPECT_TRUE(CheckPermissions(fs::others_all));
1394
1395 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read), NoError);
1396 EXPECT_TRUE(CheckPermissions(fs::all_read));
1397
1398 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_write), NoError);
1399 EXPECT_TRUE(CheckPermissions(fs::all_write));
1400
1401 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_exe), NoError);
1402 EXPECT_TRUE(CheckPermissions(fs::all_exe));
1403
1404 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe), NoError);
1405 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe));
1406
1407 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_gid_on_exe), NoError);
1408 EXPECT_TRUE(CheckPermissions(fs::set_gid_on_exe));
1409
1410 EXPECT_EQ(fs::setPermissions(TempPath, fs::sticky_bit), NoError);
1411 EXPECT_TRUE(CheckPermissions(fs::sticky_bit));
1412
1413 EXPECT_EQ(fs::setPermissions(TempPath, fs::set_uid_on_exe |
1414 fs::set_gid_on_exe |
1415 fs::sticky_bit),
1416 NoError);
1417 EXPECT_TRUE(CheckPermissions(fs::set_uid_on_exe | fs::set_gid_on_exe |
1418 fs::sticky_bit));
1419
1420 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_read | fs::set_uid_on_exe |
1421 fs::set_gid_on_exe |
1422 fs::sticky_bit),
1423 NoError);
1424 EXPECT_TRUE(CheckPermissions(fs::all_read | fs::set_uid_on_exe |
1425 fs::set_gid_on_exe | fs::sticky_bit));
1426
1427 EXPECT_EQ(fs::setPermissions(TempPath, fs::all_all | fs::set_uid_on_exe |
1428 fs::set_gid_on_exe |
1429 fs::sticky_bit),
1430 NoError);
1431 EXPECT_TRUE(CheckPermissions(fs::all_all | fs::set_uid_on_exe |
1432 fs::set_gid_on_exe | fs::sticky_bit));
1433 #endif
1434 }
1435
1436 } // anonymous namespace
1255 ASSERT_EQ(D1, D2) << "D1: " << TestDirectory << "\nD2: " << path;
1256 }
1257
1258 } // anonymous namespace