/****************************************************************************** * TMTools.cpp - description * ------------------- * begin : 2005/05/25 * copyright : (C) 2005 by Harald K. Strack * copyright : (C) 2005 by Trymedia Inc. * email : hstrack@trymedia.com ****************************************************************************** * changes name date comment * hks 2005/06/26 Helper functions for directory support added. */ /* TMTools: Singleton which holds all functions which are usefull in more than * one hierachy of classes. Like global namespace but nicer. * FIXME: Not yet fully used through the full application. * * */ #include <iostream> #include "TMTools.h" using namespace std; //Singleton TMTools * TMTools::_self = NULL; TMTools::TMTools () { } TMTools::~TMTools () { } /* Factory method for singleton. */ TMTools * TMTools::Instance () { if (_self == 0) { _self = new TMTools (); } return _self; } /* Sets an error message to Error * e. * Behaves like printf(). */ void TMTools::SetErrorMsg (Error * e, ErrorSeverity s, const char *fmt, ...) { char buf[1024]; va_list ap; va_start (ap, fmt); vsprintf (buf, fmt, ap); va_end (ap); cerr << buf << endl; e->Set (s, buf); //FIXME: closeme when last revision is closed.. } /* Read Attributes from file */ struct stat * TMTools::GetAttr (char *file, Error * e) { struct stat *attributes = (struct stat *) malloc (sizeof (struct stat)); errno = 0; if (lstat (file, attributes) != 0) { int tmp = errno; errno = 0; if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: GetAttr: lstat: %s - %s\n", GetDescription ().Text (), file, strerror (tmp)); } else { cerr << "Fatal: " << GetDescription ().Text () << " GetAttr: lstat: " << file << " - " << strerror (tmp) << endl; } return NULL; } return attributes; } /* Do chmod */ void TMTools::SetMode (char *file, mode_t st_mode, Error * e) { /* A mode change of a link changes the original file */ if (S_ISLNK (st_mode)) { return; } errno = 0; if ((chmod (file, st_mode)) != 0) { int tmp = errno; errno = 0; /* special handling for dead links */ if (tmp == ENOENT) { if (S_ISLNK (st_mode)) { return; } } if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: chmod: %s - %s\n", GetDescription ().Text (), file, strerror (tmp)); } else { cerr << "Fatal: " << GetDescription ().Text () << " chmod: " << file << " - " << strerror (tmp) << endl; } } } /* Open or create a file = touch a file */ void TMTools::TouchFile (char *file, Error * e) { errno = 0; int fd; if ((fd = open (file, O_CREAT | O_RDWR | O_TRUNC, 00444)) < 0) { int tmp = errno; errno = 0; if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: open: %s - %s\n", GetDescription ().Text (), file, strerror (tmp)); } else { cerr << "Fatal: " << GetDescription ().Text () << " open: " << file << " - " << strerror (tmp) << endl; } } close (fd); } /* Create a directory */ void TMTools::Mkdir (char *path, Error * e) { errno = 0; if (mkdir (path, 0755) < 0) { int tmp = errno; errno = 0; if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: mkdir: %s - %s\n", GetDescription ().Text (), path, strerror (tmp)); } else { cerr << "Fatal: " << GetDescription ().Text () << " mkdir: " << path << " - " << strerror (tmp) << endl; } } } /* Unlink a file */ void TMTools::UnlinkFile (char *file, Error * e) { errno = 0; if (unlink (file) < 0) { int tmp = errno; errno = 0; if (tmp == ENOENT) return; if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: unlink: %s - %s\n", GetDescription ().Text (), file, strerror (tmp)); } else { cerr << "Fatal: " << GetDescription ().Text () << " unlink: " << file << " - " << strerror (tmp) << endl; } } } bool TMTools::IsDirectory (const StrPtr & path, Error * e) { struct stat attributes; errno = 0; if (lstat (path.Text (), &attributes) != 0) { int tmp = errno; errno = 0; if (tmp == ENOENT) { return 0; } if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: IsDirectory: lstat: %s - %s\n", GetDescription ().Text (), path.Text (), strerror (tmp)); } else { cerr << "Fatal: " << GetDescription (). Text () << "IsDirectory: lstat: " << path. Text () << " - " << strerror (tmp) << endl; } return 0; } if (S_ISDIR (attributes.st_mode)) { return 1; } else { return 0; } } bool TMTools::FileExists (const char *path, Error * e) { struct stat attributes; errno = 0; if (lstat (path, &attributes) != 0) { int tmp = errno; errno = 0; if (tmp == ENOENT) { return 0; } if (e != NULL) { SetErrorMsg (e, E_FATAL, "Fatal: %s: FileExists: lstat: %s - %s\n", GetDescription ().Text (), path, strerror (tmp)); } else { cerr << "Fatal: " << GetDescription (). Text () << " FileExists:lstat: " << path << " - " << strerror (tmp) << endl; } return 0; } return 1; } bool TMTools::IsNullFile (const StrPtr & path, Error * e) { struct stat *attributes = GetAttr (path.Text (), e); if (attributes != NULL && !attributes->st_size) { return 1; } else { return 0; } } /* String for errormessages */ StrBuf & TMTools::GetDescription () { StrBuf * buf = new StrBuf; buf->Set ("TRYMEDIA TOOLS"); return *buf; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#4 | 5056 | harald_strack |
Directory support. Serious bug concerned to not unzipped binaries removed. Some other small bugfixes. |
||
#3 | 4980 | harald_strack |
Since I am maintaining the stuff in a local repository, I did some copy errors in the last revisions. Fixed. |
||
#2 | 4978 | harald_strack |
ap4 integrate implemented. Octal format is now used to save permissions. NOT backwards compatible anymore!!! |
||
#1 | 4975 | harald_strack |
Permissions are now masked to 555, so no write is possible. If you edit the Makefile and give the parameter -DFULL_PERMS, you have the old behaviour. Bugfix in ap4 revert: Edited and changed files were not reverted correctly. |