2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
20 /***************************************************************************
21 escape.cpp - escaping of strings
23 begin : Sun Nov 14 1999
24 copyright : (C) 1999 by Intra2net AG
25 ***************************************************************************/
34 #include <sys/types.h>
43 #include <boost/scoped_array.hpp>
44 #include "filefunc.hxx"
45 #include "stringfunc.hxx"
53 ** implementation of Stat
63 Stat::Stat(const std::string& path, bool follow_links)
65 stat(path,follow_links);
66 } // eo Stat::Stat(const std::string&,bool)
75 * @brief updates the internal data.
77 * In other words: stat()'s the file again.
83 // pass a copy of Path: otherwise clear() would leave an empty reference
84 stat(string(Path), FollowLinks);
86 } // eo Stat::recheck()
90 * @brief calls stat() or lstat() to get the information for the given path
91 * and stores that information.
92 * @param path the path which should be checked
93 * @param follow_links determine if (symbalic) links should be followed.
95 void Stat::stat(const std::string& path, bool follow_links)
99 FollowLinks= follow_links;
100 struct stat stat_info;
102 res = ( follow_links ? ::stat(path.c_str(), &stat_info) : ::lstat(path.c_str(), &stat_info) );
105 Device = stat_info.st_dev;
106 Inode = stat_info.st_ino;
107 Mode = (stat_info.st_mode & ~(S_IFMT));
108 NumLinks = stat_info.st_nlink;
109 Uid = stat_info.st_uid;
110 Gid = stat_info.st_gid;
111 DeviceType = stat_info.st_rdev;
112 Size = stat_info.st_size;
113 Atime = stat_info.st_atime;
114 Mtime = stat_info.st_mtime;
115 Ctime = stat_info.st_atime;
117 IsLink= S_ISLNK( stat_info.st_mode );
118 IsRegular= S_ISREG( stat_info.st_mode );
119 IsDirectory= S_ISDIR( stat_info.st_mode );
120 IsCharacterDevice= S_ISCHR( stat_info.st_mode );
121 IsBlockDevice= S_ISBLK( stat_info.st_mode );
122 IsFifo= S_ISFIFO( stat_info.st_mode );
123 IsSocket= S_ISSOCK( stat_info.st_mode );
126 } // eo Stat::stat(const std::string&,bool)
130 * @brief clears the internal data.
152 IsCharacterDevice= false;
153 IsBlockDevice= false;
156 } // eo Stat::clear()
160 * @brief checks if another instance describes the same file.
161 * @param rhs the other instance.
162 * @return @a true iff the other instance describes the same file.
164 * The "same file" means that the files are located on the same device and use the same inode.
165 * They might still have two different directory entries (different paths)!
167 bool Stat::is_same_as(const Stat& rhs)
169 return Valid and rhs.Valid
170 and ( Device == rhs.Device)
171 and ( Inode == rhs.Inode);
172 } // eo Stat::is_same_as(const Stat& rhs);
176 * @brief checks if this and the other instance describe the same device.
177 * @param rhs the other instance.
178 * @return @a true if we and the other instance describe a device and the same device.
180 * "Same device" means that the devices have the same type and the same major and minor id.
182 bool Stat::is_same_device_as(const Stat& rhs)
184 return is_device() and rhs.is_device()
185 and ( IsBlockDevice == rhs.IsBlockDevice )
186 and ( IsCharacterDevice == rhs.IsCharacterDevice )
187 and ( DeviceType == rhs.DeviceType);
188 } // eo Stat::is_same_device_as(const Stat&)
191 * @brief check existence of a path.
192 * @param path path which should be tested.
193 * @return @a true iff path exists.
195 bool path_exists(const std::string& path)
197 struct stat stat_info;
198 int res = ::stat(path.c_str(), &stat_info);
199 if (res) return false;
201 } // eo path_exists(const std::string&)
204 * @brief check existence of a regular file.
205 * @param path path which should be tested.
206 * @return @a true if path exists and is a regular file (or a link pointing to a regular file).
207 * @note this checks for regular files; not for the pure exitsnace of a path; use pathExists() for that.
210 bool file_exists(const std::string& path)
212 struct stat stat_info;
213 int res = ::stat(path.c_str(), &stat_info);
214 if (res) return false;
215 return S_ISREG(stat_info.st_mode);
216 } // eo file_exists(const std::string&)
218 // TODO: Use Stat class
221 * @param name filename to get size for
222 * @return file size or -1 if file does not exist
224 long file_size (const string &name)
228 struct stat statbuff;
230 if (lstat(name.c_str(), &statbuff) < 0)
233 if (!S_ISREG(statbuff.st_mode))
236 iReturn=statbuff.st_size;
242 * @brief tests the last modification time stamp of a path.
243 * @param path path which should be tested.
244 * @return the last modification time or 0 if the path doen't exist.
246 time_t file_mtime(const std::string& path)
248 struct stat stat_info;
249 int res = ::stat(path.c_str(), &stat_info);
251 return stat_info.st_mtime;
252 } // eo file_mtime(const std::string&)
256 * @brief reads the contents of a directory.
257 * @param path the path to the directory whose contents should be read.
258 * @param[out] result the resulting list of names.
259 * @param include_dot_names determines if dot-files should be included in the list.
260 * @return @a true if reading the directory was succesful, @a false on error.
263 const std::string& path,
264 std::vector< std::string >& result,
265 bool include_dot_names )
267 DIR* dir = ::opendir( path.c_str());
272 struct dirent store, *entry = NULL;
273 while (readdir_r(dir, &store, &entry) == 0 && entry != NULL)
275 std::string name( entry->d_name );
276 if (! include_dot_names && (name[0] == '.') )
280 result.push_back( name );
284 } // eo get_dir(const std::string&,std::vector< std::string >&,bool)
288 * @brief reads the contents of a directory
289 * @param path the path to the directory whose contents should be read.
290 * @param include_dot_names determines if dot-files should be included in the list.
291 * @return the list of names (empty on error).
293 std::vector< std::string > get_dir(const std::string& path, bool include_dot_names )
295 std::vector< std::string > result;
296 get_dir(path,result,include_dot_names);
298 } // eo get_dir(const std::string&,bool)
303 * @brief removes a file from a filesystem.
304 * @param path path to the file.
305 * @return @a true if the unlink was successful.
307 bool unlink( const std::string& path )
309 int res = ::unlink( path.c_str() );
311 } // eo unlink(const std::string&)
316 * @brief creates a symbolic link named @a link_name to @a target.
317 * @param target the target the link should point to.
318 * @param link_name the name of the link.
319 * @param force if @a true, the (file or link) @a link_name is removed if it exists.
320 * @return @a true iff the symlink was successfully created.
322 bool symlink(const std::string& target, const std::string& link_name, bool force)
325 if (target.empty() or link_name.empty() or target == link_name)
327 // no, we don't do this!
330 std::string n_target;
331 if (target[0] == '/') // absolute target?
335 else // relative target
337 // for stat'ing: prepend dir of link_name:
338 n_target= dirname(link_name)+"/"+ target;
340 Stat target_stat(n_target, false);
341 Stat link_name_stat(link_name, false);
342 if (target_stat.exists() && link_name_stat.exists())
344 if (link_name_stat.is_same_as(target_stat)
345 or link_name_stat.is_same_device_as(target_stat) )
349 //TODO: more consistency checks?!
351 if (link_name_stat.exists())
353 // the link name already exists.
356 // "force" as given, so try to remove the link_name:
359 link_name_stat.recheck();
362 if (link_name_stat.exists())
364 // well, if the link_name still exists; we cannot create that link:
368 res= ::symlink(target.c_str(), link_name.c_str());
370 } // eo symlink(const std::string&,const std::string&,bool)
375 * @brief reads the target of a symbolic link.
376 * @param path path to the symbolic link
377 * @return the target of the link or an empty string on error.
379 std::string read_link(const std::string& path)
382 Stat stat(path,false);
383 if (!stat || !stat.is_link())
385 return std::string();
387 int buffer_size= PATH_MAX+1 + 128;
388 boost::scoped_array<char> buffer_ptr( new char[buffer_size] );
389 int res= ::readlink( path.c_str(), buffer_ptr.get(), buffer_size-1 );
392 return std::string( buffer_ptr.get(), res );
394 return std::string();
395 } // eo read_link(const std::string&)
400 * @brief returns content of a file as string.
402 * A simple (q'n'd) function for retrieving content of a file as string.<br>
403 * Also able to read special (but regular) files which don't provide a size when stat'ed
404 * (like files in the /proc filesystem).
406 * @param path path to the file.
407 * @return the content of the file as string (empty if file could be opened).
409 std::string read_file(const std::string& path)
414 return std::string();
416 std::ifstream f( path.c_str(), std::ios::in | std::ios::binary );
420 // NOTE: there are cases where we don't know the correct size (/proc files...)
421 // therefore we only use the size for reserving space if we know it, but don't
422 // use it when reading the file!
425 // if we know the size, we reserve enough space.
426 result.reserve( stat.size() );
431 f.read(buffer, sizeof(buffer));
432 result.append(buffer, f.gcount());
436 } // eo read_file(const std::string&)
440 * @brief writes a string to a file.
441 * @param path path to the file
442 * @param data the data which should be written into the file
443 * @return @a true if the data was written to the file.
445 * A simple (q'n'd) function for writing a string to a file.
447 bool write_file(const std::string& path, const std::string& data)
449 std::ofstream f( path.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
452 f.write( data.data(), data.size() );
459 } // eo write_file(const std::string&,const std::string&)
463 * Copy file in 4k blocks from source to target.
464 * Overwrites the target if it already exists.
466 * On error the target file gets removed.
468 * @param src source file
469 * @param dest target file
470 * @return true if all is ok, false on error
472 bool copy_file(const std::string& src, const std::string& dest)
474 std::ifstream input( src.c_str(), std::ios::in | std::ios::binary );
478 std::ofstream output( dest.c_str(), std::ios::out | std::ios::binary | std::ios::trunc);
482 // Out of disc space?
483 if (!copy_stream(input,output))
494 * Copy streams in 4k blocks.
496 * @param is source stream
497 * @param os target stream
498 * @return true if all is ok, false on error
500 bool copy_stream(std::istream& is, std::ostream& os)
511 is.read(buffer, sizeof(buffer));
512 os.write(buffer, is.gcount());
523 * @brief returns the filename part of a path (last component)
524 * @param path the path.
525 * @return the last component of the path.
527 std::string basename(const std::string& path)
529 std::string::size_type pos= path.rfind('/');
530 if (pos != std::string::npos)
532 return path.substr(pos+1);
535 } // eo basename(const std::string&)
539 * @brief returns the directory part of a path.
540 * @param path the path.
541 * @return the directory part of the path.
543 std::string dirname(const std::string& path)
545 std::string::size_type pos= path.rfind('/');
546 if (pos != std::string::npos)
548 std::string result(path,0,pos);
556 } // eo dirname(const std::string&)
560 * @brief normalizes a path.
562 * This method removes empty and "." elements.
563 * It also resolves ".." parts by removing previous path elements if possible.
564 * Leading ".." elements are preserved when a relative path was given; else they are removed.
565 * Trailing slashes are removed.
567 * @param path the path which should be normalized.
568 * @return the normalized path.
571 std::string normalize_path(const std::string& path)
575 return std::string();
577 // remember if the given path was absolute since this information vanishes when
578 // we split the path (since we split with omitting empty parts...)
579 bool is_absolute= (path[0]=='/');
580 std::list< std::string > parts;
581 std::list< std::string > result_parts;
583 split_string(path,parts,"/",true);
585 for(std::list< std::string >::const_iterator it_parts= parts.begin();
586 it_parts != parts.end();
589 std::string part(*it_parts); //convenience..
590 if (part == std::string(".") )
592 // single dot is "current path"; ignore!
595 if (part == std::string("..") )
597 // double dot is "one part back"
598 if (result_parts.empty())
602 // ignore since we cannot move behind / on absolute paths...
606 // on relative path, we need to store the "..":
607 result_parts.push_back(part);
610 else if (result_parts.back() == std::string("..") )
612 // if last element was already "..", we need to store the new one again...
613 // (PS: no need for "absolute" check; this can only be the case on relative path)
614 result_parts.push_back(part);
618 // remove last element.
619 result_parts.pop_back();
623 result_parts.push_back(part);
630 result+= join_string(result_parts,"/");
632 } // eo normalize_path(const std::string&)
636 * @brief calls fsync on a given directory to sync all it's metadata
637 * @param path the path of the directory.
638 * @return true if successful
640 bool dirsync(const std::string& path)
642 // sync the directory the file is in
643 DIR* dir=opendir(path.c_str());
647 int ret=fsync(dirfd(dir));
655 * @brief changes the file(/path) mode.
656 * @param path the path to change the mode for.
657 * @param mode the new file mode.
658 * @return @a true iff the file mode was sucessfully changed.
660 bool chmod(const std::string& path, int mode)
662 int res= ::chmod(path.c_str(), mode);
664 } // eo chmod(const std::string&,int)
668 * @brief changed the owner of a file(/path)
669 * @param path the path to change the owner for.
670 * @param user the new file owner.
671 * @param group the new file group.
672 * @return @a true iff the file owner was succesfully changed.
675 * the validity of user and group within the system is not checked.
676 * This is intentional since this way we can use id's which are not assigned.
678 bool chown(const std::string& path, const I2n::User& user, const I2n::Group& group)
681 if (uid<0) return false;
682 gid_t gid= group.Gid;
683 if (gid<0) gid= user.Gid;
684 if (gid<0) return false;
685 int res= ::chown( path.c_str(), uid, gid);
687 } // eo chown(const std::string&,const User&,const Group&)
690 * Recursive delete of files and directories
691 * @param path File or directory to delete
692 * @param error Will contain the error if the return value is false [optional]
693 * @return true on success, false otherwise
695 bool recursive_delete(const std::string &path, std::string *error)
701 if (stat(path.c_str(), &my_stat) != 0) {
702 throw runtime_error("can't stat " + path);
705 if (S_ISDIR(my_stat.st_mode)) {
706 DIR *dir = opendir(path.c_str());
708 throw runtime_error("can't open directory " + path);
711 struct dirent store, *entry = NULL;
712 while (readdir_r(dir, &store, &entry) == 0 && entry != NULL)
714 string filename = entry->d_name;
715 if (filename == "." || filename == "..") {
719 // Delete subdir or file.
720 rtn = recursive_delete(path + "/" + filename, error);
728 throw runtime_error("can't remove directory " + path);
732 throw runtime_error("can't unlink " + path);
735 } catch (exception &e) {
738 out << e.what() << " (" << strerror(errno) << ")";
745 out << "unknown error (" << strerror(errno) << ")";
752 } // eo recursive_delete(const std::string&,std::string*)
755 Create a unique temporary directory from path_template.
756 @param Path template. The last six characters must be XXXXXX.
757 @param error Will contain the error if the return value is false [optional]
758 @return Name of new directory or empty string on error.
760 std::string mkdtemp(const std::string &path_template, std::string *error)
762 boost::scoped_array<char> buf( new char[path_template.size()+1] );
763 path_template.copy(buf.get(), path_template.size());
764 buf[path_template.size()]=0;
766 char *unique_dir = ::mkdtemp(buf.get());
770 *error = strerror(errno);
774 // Scoped pointer is still valid
775 return std::string(unique_dir);
780 @param path Path to create
781 @param error Will contain the error if the return value is false [optional]
782 @return True on success, false on error
784 bool mkdir(const std::string &path, const mode_t &mode, std::string *error)
786 if ( ::mkdir(path.c_str(), mode) == 0)
790 *error = strerror(errno);
796 @param path Path to removed
797 @param error Will contain the error if the return value is false [optional]
798 @return True on successs, false otherwise
800 bool rmdir(const std::string &path, std::string *error)
802 if ( ::rmdir(path.c_str() ) == 0)
806 *error = strerror(errno);
810 /// Small helper class for scoped free
814 scoped_C_free(void *ptr)
815 : pointer_to_free(ptr)
821 free (pointer_to_free);
822 pointer_to_free = NULL;
826 void *pointer_to_free;
830 Get current working directory
831 @return Current working directory. Empty string on error.
835 char *cwd = ::getcwd(NULL, 0);
839 // Make deallocation of cwd exception safe
840 scoped_C_free holder(cwd);
842 string current_dir(cwd);
847 Change current working directory
848 @param path Path to change to
849 @param error Will contain the error if the return value is false [optional]
850 @return True on successs, false otherwise
852 bool chdir(const std::string &path, std::string *error)
854 if ( ::chdir(path.c_str() ) == 0)
858 *error = strerror(errno);
863 Set file mode creation mask
864 @param mask Creation mask
865 @return Previous creation mask (function call always succeeds)
867 mode_t umask(mode_t mask)
869 return ::umask(mask);
872 } // eo namespace I2n