| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-21 02:51:55 +02:00
										 |  |  | #include <AK/OSError.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-21 13:29:36 +02:00
										 |  |  | #include <AK/Result.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-06 15:34:26 +02:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 15:04:03 +01:00
										 |  |  | #include <LibCore/IODevice.h>
 | 
					
						
							| 
									
										
										
										
											2021-02-21 02:51:55 +02:00
										 |  |  | #include <sys/stat.h>
 | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class File final : public IODevice { | 
					
						
							|  |  |  |     C_OBJECT(File) | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     virtual ~File() override; | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-17 00:48:31 +02:00
										 |  |  |     static Result<NonnullRefPtr<File>, String> open(String filename, IODevice::OpenMode, mode_t = 0644); | 
					
						
							| 
									
										
										
										
											2020-03-30 12:08:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  |     String filename() const { return m_filename; } | 
					
						
							| 
									
										
										
										
											2021-04-17 00:48:31 +02:00
										 |  |  |     void set_filename(const String filename) { m_filename = move(filename); } | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-09 11:50:18 +01:00
										 |  |  |     bool is_directory() const; | 
					
						
							|  |  |  |     static bool is_directory(const String& filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-30 00:36:19 +03:00
										 |  |  |     bool is_device() const; | 
					
						
							|  |  |  |     static bool is_device(const String& filename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-12 21:17:00 +01:00
										 |  |  |     static bool exists(const String& filename); | 
					
						
							| 
									
										
										
										
											2021-02-21 02:51:55 +02:00
										 |  |  |     static bool ensure_parent_directories(const String& path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class RecursionMode { | 
					
						
							|  |  |  |         Allowed, | 
					
						
							|  |  |  |         Disallowed | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class LinkMode { | 
					
						
							|  |  |  |         Allowed, | 
					
						
							|  |  |  |         Disallowed | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     enum class AddDuplicateFileMarker { | 
					
						
							|  |  |  |         Yes, | 
					
						
							|  |  |  |         No, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct CopyError { | 
					
						
							|  |  |  |         OSError error_code; | 
					
						
							|  |  |  |         bool tried_recursing; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static Result<void, CopyError> copy_file(const String& dst_path, const struct stat& src_stat, File& source); | 
					
						
							|  |  |  |     static Result<void, CopyError> copy_directory(const String& dst_path, const String& src_path, const struct stat& src_stat, LinkMode = LinkMode::Disallowed); | 
					
						
							|  |  |  |     static Result<void, CopyError> copy_file_or_directory(const String& dst_path, const String& src_path, RecursionMode = RecursionMode::Allowed, LinkMode = LinkMode::Disallowed, AddDuplicateFileMarker = AddDuplicateFileMarker::Yes); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-12 21:29:01 +02:00
										 |  |  |     static String real_path_for(const String& filename); | 
					
						
							| 
									
										
										
										
											2021-04-17 00:48:31 +02:00
										 |  |  |     static String read_link(String const& link_path); | 
					
						
							| 
									
										
										
										
											2021-02-21 02:51:55 +02:00
										 |  |  |     static Result<void, OSError> link_file(const String& dst_path, const String& src_path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     struct RemoveError { | 
					
						
							|  |  |  |         String file; | 
					
						
							|  |  |  |         OSError error_code; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     static Result<void, RemoveError> remove(const String& path, RecursionMode, bool force); | 
					
						
							| 
									
										
										
										
											2020-02-12 21:17:00 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     virtual bool open(IODevice::OpenMode) override; | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 11:31:27 +00:00
										 |  |  |     enum class ShouldCloseFileDescriptor { | 
					
						
							| 
									
										
										
										
											2019-05-28 11:53:16 +02:00
										 |  |  |         No = 0, | 
					
						
							|  |  |  |         Yes | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2020-10-25 11:31:27 +00:00
										 |  |  |     bool open(int fd, IODevice::OpenMode, ShouldCloseFileDescriptor); | 
					
						
							| 
									
										
										
										
											2019-04-26 02:22:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-22 19:37:11 -03:00
										 |  |  |     static NonnullRefPtr<File> standard_input(); | 
					
						
							|  |  |  |     static NonnullRefPtr<File> standard_output(); | 
					
						
							|  |  |  |     static NonnullRefPtr<File> standard_error(); | 
					
						
							| 
									
										
										
										
											2020-08-17 15:58:07 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     File(Object* parent = nullptr) | 
					
						
							|  |  |  |         : IODevice(parent) | 
					
						
							| 
									
										
										
										
											2019-09-21 20:50:06 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-17 00:48:31 +02:00
										 |  |  |     explicit File(String filename, Object* parent = nullptr); | 
					
						
							| 
									
										
										
										
											2019-09-21 20:50:06 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-30 12:08:25 +02:00
										 |  |  |     bool open_impl(IODevice::OpenMode, mode_t); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  |     String m_filename; | 
					
						
							| 
									
										
										
										
											2020-10-25 11:31:27 +00:00
										 |  |  |     ShouldCloseFileDescriptor m_should_close_file_descriptor { ShouldCloseFileDescriptor::Yes }; | 
					
						
							| 
									
										
										
										
											2019-04-10 20:22:23 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |