Skip to content
Snippets Groups Projects
Commit 993d95a6 authored by toole1's avatar toole1
Browse files

jack_update

git-svn-id: https://subversion.cs.illinois.edu/svn/cs225@4360 6fbd10e7-183d-0410-a318-cb416676e4f2
parent acf83487
No related branches found
No related tags found
No related merge requests found
monad identification file
This file is used for monad directory identification
monad Build Date: Mon Aug 29 23:31:01 CDT 2011
Built by toole1 on linux3.ews.illinois.edu
Build Date: Wed Aug 31 15:03:31 CDT 2011
No preview for this file type
......@@ -186,7 +186,6 @@ bool exists(const string & path)
// Try stat-ing it
struct stat st;
if (stat(path.c_str(), &st) != 0) return false;
// Check for read permission
if ((st.st_mode & S_IRUSR) == 0) return false;
......@@ -204,7 +203,6 @@ mode_t permissions(const string & path)
// Try stat-ing it
struct stat st;
if (stat(path.c_str(), &st) != 0) return -1;
// Check for read permission
if ((st.st_mode & S_IRUSR) == 0) return -1;
......@@ -296,8 +294,10 @@ void protectDir(const string & dir)
// Originally from Simon Biber
// http://bytes.com/topic/c/answers/545614-list-files-current-directory
vector<string> getFilesInDir(const string & dir)
vector<string> get_files_in_dir(const string & dir)
{
EXIT_IF_ERROR(dir == "" || dir[dir.length()-1] != '/', "Directory name must end in a '/'");
vector<string> files;
DIR * dirp = opendir(dir.c_str());
if (dirp == NULL) return files;
......@@ -305,7 +305,9 @@ vector<string> getFilesInDir(const string & dir)
struct dirent * ent = readdir(dirp);
while (ent != NULL)
{
// TODO (toole1) finish this
string file = ent->d_name;
if (file != "." && file != "..")
files.push_back(dir + file);
ent = readdir(dirp);
}
......@@ -313,6 +315,28 @@ vector<string> getFilesInDir(const string & dir)
return files;
}
bool is_symlink(const string & file)
{
// Try lstat-ing it
struct stat st;
if (lstat(file.c_str(), &st) != 0) return -1;
// Check for read permission
if ((st.st_mode & S_IRUSR) == 0) return false;
// & with symlink bit
return (S_ISLNK(st.st_mode)) != 0;
}
string get_symlink_target(const string & symlink)
{
const size_t buf_size = 4096;
char buf[buf_size+1]; // TODO (toole1): hack-y value
ssize_t len = readlink(symlink.c_str(), buf, buf_size);
EXIT_IF_ERROR(len < 0 || static_cast<size_t>(len) == buf_size, "Error getting target of symlink " + symlink);
buf[len] = '\0';
return string(buf);
}
void linkDirs(const string & sourceFolder, const string & destFolder, const vector<string> & dirs)
{
assertExists(destFolder);
......
#ifndef UTIL_H
#define UTIL_H
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -90,7 +91,9 @@ void copyFiles(const string & sourceFolder, const string & destFolder, const vec
void protectFiles(const string & folder, const vector<string> & files);
void protectDir(const string & dir);
void linkDirs(const string & sourceFolder, const string & destFolder, const vector<string> & dirs);
vector<string> getFilesInDir(const string & dir);
vector<string> get_files_in_dir(const string & dir);
bool is_symlink(const string & file);
string get_symlink_target(const string & symlink);
// STRING REPLACEMENT
bool replaceFirst(string & str, const string & toreplace, const string & with);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment