Lines Matching full:file
3 // found in the LICENSE file.
5 #include "base/files/file.h"
18 using base::File;
27 // Don't create a File at all. in TEST()
28 File file; in TEST() local
29 EXPECT_FALSE(file.IsValid()); in TEST()
30 EXPECT_EQ(base::File::FILE_ERROR_FAILED, file.error_details()); in TEST()
32 File file2(base::File::FILE_ERROR_TOO_MANY_OPENED); in TEST()
34 EXPECT_EQ(base::File::FILE_ERROR_TOO_MANY_OPENED, file2.error_details()); in TEST()
38 // Open a file that doesn't exist. in TEST()
39 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); in TEST() local
40 EXPECT_FALSE(file.IsValid()); in TEST()
41 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, file.error_details()); in TEST()
42 EXPECT_EQ(base::File::FILE_ERROR_NOT_FOUND, base::File::GetLastFileError()); in TEST()
46 // Open or create a file. in TEST()
47 File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ); in TEST() local
48 EXPECT_TRUE(file.IsValid()); in TEST()
49 EXPECT_TRUE(file.created()); in TEST()
50 EXPECT_EQ(base::File::FILE_OK, file.error_details()); in TEST()
54 // Open an existing file. in TEST()
55 File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); in TEST() local
56 EXPECT_TRUE(file.IsValid()); in TEST()
57 EXPECT_FALSE(file.created()); in TEST()
58 EXPECT_EQ(base::File::FILE_OK, file.error_details()); in TEST()
60 // This time verify closing the file. in TEST()
61 file.Close(); in TEST()
62 EXPECT_FALSE(file.IsValid()); in TEST()
66 // Open an existing file through Initialize in TEST()
67 File file; in TEST() local
68 file.Initialize(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); in TEST()
69 EXPECT_TRUE(file.IsValid()); in TEST()
70 EXPECT_FALSE(file.created()); in TEST()
71 EXPECT_EQ(base::File::FILE_OK, file.error_details()); in TEST()
73 // This time verify closing the file. in TEST()
74 file.Close(); in TEST()
75 EXPECT_FALSE(file.IsValid()); in TEST()
79 // Create a file that exists. in TEST()
80 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_READ); in TEST() local
81 EXPECT_FALSE(file.IsValid()); in TEST()
82 EXPECT_FALSE(file.created()); in TEST()
83 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, file.error_details()); in TEST()
84 EXPECT_EQ(base::File::FILE_ERROR_EXISTS, base::File::GetLastFileError()); in TEST()
88 // Create or overwrite a file. in TEST()
89 File file(file_path, in TEST() local
90 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); in TEST()
91 EXPECT_TRUE(file.IsValid()); in TEST()
92 EXPECT_TRUE(file.created()); in TEST()
93 EXPECT_EQ(base::File::FILE_OK, file.error_details()); in TEST()
97 // Create a delete-on-close file. in TEST()
99 File file(file_path, in TEST() local
100 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | in TEST()
101 base::File::FLAG_DELETE_ON_CLOSE); in TEST()
102 EXPECT_TRUE(file.IsValid()); in TEST()
103 EXPECT_TRUE(file.created()); in TEST()
104 EXPECT_EQ(base::File::FILE_OK, file.error_details()); in TEST()
114 File file(file_path, in TEST() local
115 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_DELETE_ON_CLOSE); in TEST()
116 std::swap(file, file); in TEST()
117 EXPECT_TRUE(file.IsValid()); in TEST()
126 File file(file_path, base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_ASYNC); in TEST() local
127 EXPECT_TRUE(file.IsValid()); in TEST()
128 EXPECT_TRUE(file.async()); in TEST()
132 File file(file_path, base::File::FLAG_OPEN_ALWAYS); in TEST() local
133 EXPECT_TRUE(file.IsValid()); in TEST()
134 EXPECT_FALSE(file.async()); in TEST()
143 // Create a file. in TEST()
144 File file(file_path, in TEST() local
145 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_READ | in TEST()
146 base::File::FLAG_SHARE_DELETE); in TEST()
147 EXPECT_TRUE(file.IsValid()); in TEST()
148 EXPECT_TRUE(file.created()); in TEST()
149 EXPECT_EQ(base::File::FILE_OK, file.error_details()); in TEST()
151 // Open an existing file and mark it as delete on close. in TEST()
152 File same_file(file_path, in TEST()
153 base::File::FLAG_OPEN | base::File::FLAG_DELETE_ON_CLOSE | in TEST()
154 base::File::FLAG_READ); in TEST()
155 EXPECT_TRUE(file.IsValid()); in TEST()
157 EXPECT_EQ(base::File::FILE_OK, same_file.error_details()); in TEST()
159 // Close both handles and check that the file is gone. in TEST()
160 file.Close(); in TEST()
169 File file(file_path, in TEST() local
170 base::File::FLAG_CREATE | base::File::FLAG_READ | in TEST()
171 base::File::FLAG_WRITE); in TEST()
172 ASSERT_TRUE(file.IsValid()); in TEST()
177 // Write 0 bytes to the file. in TEST()
178 int bytes_written = file.Write(0, data_to_write, 0); in TEST()
182 bytes_written = file.Write(0, nullptr, 0); in TEST()
185 // Write "test" to the file. in TEST()
186 bytes_written = file.Write(0, data_to_write, kTestDataSize); in TEST()
191 int bytes_read = file.Read(kTestDataSize, data_read_1, kTestDataSize); in TEST()
194 // Read from somewhere in the middle of the file. in TEST()
196 bytes_read = file.Read(kPartialReadOffset, data_read_1, kTestDataSize); in TEST()
202 bytes_read = file.Read(0, data_read_1, 0); in TEST()
205 // Read the entire file. in TEST()
206 bytes_read = file.Read(0, data_read_1, kTestDataSize); in TEST()
212 bytes_read = file.ReadNoBestEffort(0, data_read_1, kTestDataSize); in TEST()
217 // Write past the end of the file. in TEST()
220 bytes_written = file.Write(kOffsetBeyondEndOfFile, in TEST()
224 // Make sure the file was extended. in TEST()
229 // Make sure the file was zero-padded. in TEST()
231 bytes_read = file.Read(0, data_read_2, static_cast<int>(file_size)); in TEST()
247 EXPECT_EQ(File::FILE_ERROR_ACCESS_DENIED, File::GetLastFileError()); in TEST()
253 File file(nonexistent_path, File::FLAG_OPEN | File::FLAG_READ); in TEST() local
254 File::Error last_error = File::GetLastFileError(); in TEST()
255 EXPECT_FALSE(file.IsValid()); in TEST()
256 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, file.error_details()); in TEST()
257 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, last_error); in TEST()
264 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_APPEND); in TEST() local
265 ASSERT_TRUE(file.IsValid()); in TEST()
270 // Write 0 bytes to the file. in TEST()
271 int bytes_written = file.Write(0, data_to_write, 0); in TEST()
275 bytes_written = file.Write(0, nullptr, 0); in TEST()
278 // Write "test" to the file. in TEST()
279 bytes_written = file.Write(0, data_to_write, kTestDataSize); in TEST()
282 file.Close(); in TEST()
283 File file2(file_path, in TEST()
284 base::File::FLAG_OPEN | base::File::FLAG_READ | in TEST()
285 base::File::FLAG_APPEND); in TEST()
288 // Test passing the file around. in TEST()
289 file = std::move(file2); in TEST()
291 ASSERT_TRUE(file.IsValid()); in TEST()
296 // Append "78" to the file. in TEST()
297 bytes_written = file.Write(0, append_data_to_write, kAppendDataSize); in TEST()
300 // Read the entire file. in TEST()
302 int bytes_read = file.Read(0, data_read_1, in TEST()
316 File file(file_path, in TEST() local
317 base::File::FLAG_CREATE | base::File::FLAG_READ | in TEST()
318 base::File::FLAG_WRITE); in TEST()
319 ASSERT_TRUE(file.IsValid()); in TEST()
320 EXPECT_EQ(0, file.GetLength()); in TEST()
322 // Write "test" to the file. in TEST()
325 int bytes_written = file.Write(0, data_to_write, kTestDataSize); in TEST()
328 // Extend the file. in TEST()
331 EXPECT_TRUE(file.SetLength(kExtendedFileLength)); in TEST()
332 EXPECT_EQ(kExtendedFileLength, file.GetLength()); in TEST()
336 // Make sure the file was zero-padded. in TEST()
338 int bytes_read = file.Read(0, data_read, static_cast<int>(file_size)); in TEST()
345 // Truncate the file. in TEST()
347 EXPECT_TRUE(file.SetLength(kTruncatedFileLength)); in TEST()
348 EXPECT_EQ(kTruncatedFileLength, file.GetLength()); in TEST()
352 // Make sure the file was truncated. in TEST()
353 bytes_read = file.Read(0, data_read, kTestDataSize); in TEST()
358 // Close the file and reopen with base::File::FLAG_CREATE_ALWAYS, and make in TEST()
359 // sure the file is empty (old file was overridden). in TEST()
360 file.Close(); in TEST()
361 file.Initialize(file_path, in TEST()
362 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); in TEST()
363 EXPECT_EQ(0, file.GetLength()); in TEST()
374 File file(temp_dir.GetPath().AppendASCII("touch_get_info_file"), local
375 base::File::FLAG_CREATE | base::File::FLAG_WRITE |
376 base::File::FLAG_WRITE_ATTRIBUTES);
377 ASSERT_TRUE(file.IsValid());
379 // Get info for a newly created file.
380 base::File::Info info;
381 EXPECT_TRUE(file.GetInfo(&info));
394 // Write "test" to the file.
397 int bytes_written = file.Write(0, data, kTestDataSize);
409 EXPECT_TRUE(file.SetTimes(new_last_accessed, new_last_modified));
411 // Make sure the file info was updated accordingly.
412 EXPECT_TRUE(file.GetInfo(&info));
439 File file(file_path, local
440 base::File::FLAG_CREATE | base::File::FLAG_READ |
441 base::File::FLAG_WRITE);
442 EXPECT_TRUE(file.IsValid());
446 EXPECT_EQ(kDataSize, file.Write(0, kData, kDataSize));
448 EXPECT_EQ(0, file.Seek(base::File::FROM_BEGIN, 0));
452 EXPECT_EQ(first_chunk_size, file.ReadAtCurrentPos(buffer, first_chunk_size));
454 file.ReadAtCurrentPos(buffer + first_chunk_size,
464 File file(file_path, local
465 base::File::FLAG_CREATE | base::File::FLAG_READ |
466 base::File::FLAG_WRITE);
467 EXPECT_TRUE(file.IsValid());
473 EXPECT_EQ(first_chunk_size, file.WriteAtCurrentPos(kData, first_chunk_size));
475 file.WriteAtCurrentPos(kData + first_chunk_size,
479 EXPECT_EQ(kDataSize, file.Read(0, buffer, kDataSize));
487 File file(file_path, local
488 base::File::FLAG_CREATE | base::File::FLAG_READ |
489 base::File::FLAG_WRITE);
490 ASSERT_TRUE(file.IsValid());
493 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_BEGIN, kOffset));
494 EXPECT_EQ(2 * kOffset, file.Seek(base::File::FROM_CURRENT, kOffset));
495 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_CURRENT, -kOffset));
496 EXPECT_TRUE(file.SetLength(kOffset * 2));
497 EXPECT_EQ(kOffset, file.Seek(base::File::FROM_END, -kOffset));
503 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
504 File file(file_path,(base::File::FLAG_CREATE | local
505 base::File::FLAG_READ |
506 base::File::FLAG_WRITE));
507 ASSERT_TRUE(file.IsValid());
509 File file2(file.Duplicate());
516 ASSERT_EQ(0, file.Seek(base::File::FROM_CURRENT, 0));
517 ASSERT_EQ(0, file2.Seek(base::File::FROM_CURRENT, 0));
518 ASSERT_EQ(kDataLen, file.WriteAtCurrentPos(kData, kDataLen));
519 ASSERT_EQ(kDataLen, file.Seek(base::File::FROM_CURRENT, 0));
520 ASSERT_EQ(kDataLen, file2.Seek(base::File::FROM_CURRENT, 0));
521 file.Close();
530 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
531 File file(file_path,(base::File::FLAG_CREATE | local
532 base::File::FLAG_READ |
533 base::File::FLAG_WRITE |
534 base::File::FLAG_DELETE_ON_CLOSE));
535 ASSERT_TRUE(file.IsValid());
536 File file2(file.Duplicate());
538 file.Close();
552 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
553 File file(file_path, local
554 (base::File::FLAG_CREATE | base::File::FLAG_READ |
555 base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE));
556 ASSERT_TRUE(file.IsValid());
558 const char kData[] = "this file is sparse.";
562 // If the file fails to write, it is probably we are running out of disk space
563 // and the file system doesn't support sparse file.
564 if (file.Write(kLargeFileOffset - kDataLen - 1, kData, kDataLen) < 0)
567 ASSERT_EQ(kDataLen, file.Write(kLargeFileOffset + 1, kData, kDataLen));
578 base::File dir(
588 base::File::Info info;
598 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
600 // Creating and closing a file with DELETE perms should do nothing special.
601 File file(file_path, local
602 (base::File::FLAG_CREATE | base::File::FLAG_READ |
603 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
604 ASSERT_TRUE(file.IsValid());
605 file.Close();
612 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
614 // Creating a file with DELETE and then marking for delete on close should
616 File file(file_path, local
617 (base::File::FLAG_CREATE | base::File::FLAG_READ |
618 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
619 ASSERT_TRUE(file.IsValid());
620 ASSERT_TRUE(file.DeleteOnClose(true));
621 file.Close();
628 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
630 // Creating a file with DELETE, marking it for delete, then clearing delete on
632 File file(file_path, local
633 (base::File::FLAG_CREATE | base::File::FLAG_READ |
634 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
635 ASSERT_TRUE(file.IsValid());
636 ASSERT_TRUE(file.DeleteOnClose(true));
637 ASSERT_TRUE(file.DeleteOnClose(false));
638 file.Close();
645 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
648 File file( local
650 (base::File::FLAG_CREATE | base::File::FLAG_READ |
651 base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE |
652 base::File::FLAG_SHARE_DELETE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
653 ASSERT_TRUE(file.IsValid());
659 file.DeleteOnClose(false);
660 file.Close();
667 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
670 File file( local
672 (base::File::FLAG_CREATE | base::File::FLAG_READ |
673 base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE |
674 base::File::FLAG_SHARE_DELETE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
675 ASSERT_TRUE(file.IsValid());
677 File file2(
679 (base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE |
680 base::File::FLAG_SHARE_DELETE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
686 file.Close();
693 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
695 // It should not be possible to mark a file for deletion when it was not
697 File file(file_path, (base::File::FLAG_CREATE | base::File::FLAG_READ | local
698 base::File::FLAG_WRITE));
699 ASSERT_TRUE(file.IsValid());
700 ASSERT_FALSE(file.DeleteOnClose(true));
701 file.Close();
708 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
712 File file(file_path, (base::File::FLAG_CREATE | base::File::FLAG_READ | local
713 base::File::FLAG_WRITE));
714 ASSERT_TRUE(file.IsValid());
715 File file2(
717 (base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE |
718 base::File::FLAG_DELETE_ON_CLOSE | base::File::FLAG_SHARE_DELETE));
721 file.Close();
728 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
730 // Mapping a file into memory blocks DeleteOnClose.
731 File file(file_path, local
732 (base::File::FLAG_CREATE | base::File::FLAG_READ |
733 base::File::FLAG_WRITE | base::File::FLAG_CAN_DELETE_ON_CLOSE));
734 ASSERT_TRUE(file.IsValid());
735 ASSERT_EQ(5, file.WriteAtCurrentPos("12345", 5));
739 ASSERT_TRUE(mapping.Initialize(file.Duplicate()));
742 EXPECT_FALSE(file.DeleteOnClose(true));
745 file.Close();
753 FilePath file_path = temp_dir.GetPath().AppendASCII("file");
755 File file(file_path, base::File::FLAG_CREATE | base::File::FLAG_WRITE | local
756 base::File::FLAG_ASYNC);
757 File lying_file(file.TakePlatformFile(), false /* async */);