int32_t Mount::renameObject(const char* oldname, const char* newname) { trace("Mount::renameObject(%s, %s)\n", oldname, newname); char coldname[PATH_MAX], cnewname[PATH_MAX]; int32_t ret; ret = canonicalise(coldname, PATH_MAX, oldname); if (ret < 0) return ret; ret = canonicalise(cnewname, PATH_MAX, newname); if (ret < 0) return ret; return translateError(MoveFile(coldname, cnewname)); }
int32_t Mount::removeDir(const char* dirname) { trace("Mount::removeDir(%s)\n", dirname); char cname[PATH_MAX]; int ret = canonicalise(cname, PATH_MAX, dirname); if (ret < 0) return ret; return translateError(RemoveDirectory(cname)); }
int32_t Mount::getInfo(const char* name, MetaData* data) { trace("Mount::getInfo(%s)\n", name); char cname[PATH_MAX]; int32_t ret = canonicalise(cname, PATH_MAX, name); if (ret < 0) return ret; return data->populate(cname); }
int32_t Mount::removeFile(const char* filename) { trace("Mount::removeFile(%s)\n", filename); char cname[PATH_MAX]; int ret = canonicalise(cname, PATH_MAX, filename); if (ret < 0) return ret; return translateError(remove(cname)); }
int32_t Mount::createDir(const char* dirname) { trace("Mount::createDir(%s)\n", dirname); char cname[PATH_MAX]; int ret = canonicalise(cname, PATH_MAX, dirname); if (ret < 0) return ret; return translateError(mkdir(cname, 0777)); }
int32_t Mount::createFile(const char* filename) { trace("Mount::createFile(%s)\n", filename); char cname[PATH_MAX]; int ret = canonicalise(cname, PATH_MAX, filename); if (ret < 0) return ret; ret = open(cname, O_RDWR|O_CREAT|O_TRUNC, 0666); if (ret < 0) return translateError(ret); return translateError(close(ret)); }
int32_t Mount::createFile(const char* filename) { trace("Mount::createFile(%s)\n", filename); char cname[PATH_MAX]; int ret = canonicalise(cname, PATH_MAX, filename); if (ret < 0) return ret; HANDLE fh = CreateFile(cname, GENERIC_READ|GENERIC_WRITE, // access FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode 0, // securty CREATE_ALWAYS, // creation FILE_ATTRIBUTE_NORMAL, // flags NULL); // template if (fh == INVALID_HANDLE_VALUE) return translateError(); return translateError(CloseHandle(fh)); }
int32_t Mount::openDir(const char* dirname, DirIter*& iter) { trace("Mount:openDir(%s)\n", dirname); char cname[PATH_MAX]; int32_t ret; ret = canonicalise(cname, PATH_MAX, dirname); if (ret < 0) return ret; DirIter *di = new DirIter(this, cname); ret = di->open(); if (ret < 0) { delete di; return ret; } iter = di; return VFS_ERR_OK; }
int32_t Mount::openFile(const char* filename, uint32_t flags, File*& file) { trace("Mount::openFile(%s, 0x%x)\n", filename, flags); char cname[PATH_MAX]; int32_t ret; ret = canonicalise(cname, PATH_MAX, filename); if (ret < 0) return ret; File* f = new File(this, cname); ret = f->open(flags); if (ret < 0) { delete f; return ret; } file = f; return VFS_ERR_OK; }
int32_t Mount::removeFile(const char* filename) { trace("Mount::removeFile(%s)\n", filename); char cname[PATH_MAX]; int ret = canonicalise(cname, PATH_MAX, filename); if (ret < 0) return ret; // we also let this remove directories - might need a generic 'get attr' function WIN32_FIND_DATA finddata; HANDLE fh = FindFirstFile(cname, &finddata); if (fh == INVALID_HANDLE_VALUE) return translateError(FALSE); FindClose(fh); if (finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return translateError(RemoveDirectory(cname)); else return translateError(DeleteFile(cname)); }
void load_dhcp(char *file, char *suffix, time_t now, char *hostname) { struct crec *spares; time_t ttd, tts; #ifdef HAVE_FILE_SYSTEM struct in_addr host_address; char token[MAXTOK], *dot; FILE *fp = fopen (file, "r"); if (!fp) { syslog (LOG_ERR, "failed to load %s: %m", file); return; } syslog (LOG_INFO, "reading %s", file); #endif /* remove all existing DHCP cache entries onto temp. freelist */ spares = cache_clear_dhcp(); #ifdef HAVE_FILE_SYSTEM while ((next_token(token, MAXTOK, fp))) { if (strcmp(token, "lease") == 0) { hostname[0] = '\0'; ttd = tts = (time_t)(-1); if (next_token(token, MAXTOK, fp) && inet_pton(AF_INET, token, &host_address)) { if (next_token(token, MAXTOK, fp) && *token == '{') { while (next_token(token, MAXTOK, fp) && *token != '}') { if ((strcmp(token, "client-hostname") == 0) || (strcmp(token, "hostname") == 0)) { next_token(hostname, MAXDNAME, fp); canonicalise(hostname); } else if ((strcmp(token, "ends") == 0) || (strcmp(token, "starts") == 0)) { struct tm lease_time; int is_ends = (strcmp(token, "ends") == 0); if (next_token(token, MAXTOK, fp) && /* skip weekday */ next_token(token, MAXTOK, fp) && /* Get date from lease file */ sscanf (token, "%d/%d/%d", &lease_time.tm_year, &lease_time.tm_mon, &lease_time.tm_mday) == 3 && next_token(token, MAXTOK, fp) && sscanf (token, "%d:%d:%d:", &lease_time.tm_hour, &lease_time.tm_min, &lease_time.tm_sec) == 3) { /* There doesn't seem to be a universally available library function which converts broken-down _GMT_ time to seconds-in-epoch. The following was borrowed from ISC dhcpd sources, where it is noted that it might not be entirely accurate for odd seconds. Since we're trying to get the same answer as dhcpd, that's just fine here. */ static int months [11] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; time_t time = ((((((365 * (lease_time.tm_year - 1970) + /* Days in years since '70 */ (lease_time.tm_year - 1969) / 4 + /* Leap days since '70 */ (lease_time.tm_mon > 1 /* Days in months this year */ ? months [lease_time.tm_mon - 2] : 0) + (lease_time.tm_mon > 2 && /* Leap day this year */ !((lease_time.tm_year - 1972) & 3)) + lease_time.tm_mday - 1) * 24) + /* Day of month */ lease_time.tm_hour) * 60) + lease_time.tm_min) * 60) + lease_time.tm_sec; if (is_ends) ttd = time; else tts = time; } } } /* missing info? */ if (!*hostname) continue; if (ttd == (time_t)(-1)) continue; /* infinite lease to is represented by -1 */ /* This makes is to the lease file as start time one less than end time. */ /* We use -1 as infinite in ttd */ if ((tts != -1) && (ttd == tts - 1)) ttd = (time_t)(-1); else if (ttd < now) continue; dot = strchr(hostname, '.'); if (suffix) { if (dot) { /* suffix and lease has ending: must match */ if (strcmp(dot+1, suffix) != 0) syslog(LOG_WARNING, "Ignoring DHCP lease for %s because it has an illegal domain part", hostname); else process_lease(&spares, hostname, host_address, ttd, F_REVERSE); } else { /* suffix exists but lease has no ending - add lease and lease.suffix */ process_lease(&spares, hostname, host_address, ttd, 0); strncat(hostname, ".", MAXDNAME); strncat(hostname, suffix, MAXDNAME); hostname[MAXDNAME-1] = 0; /* in case strncat hit limit */ /* Make FQDN canonical for reverse lookups */ process_lease(&spares, hostname, host_address, ttd, F_REVERSE); } } else { /* no suffix */ if (dot) /* no lease ending allowed */ syslog(LOG_WARNING, "Ignoring DHCP lease for %s because it has a domain part", hostname); else process_lease(&spares, hostname, host_address, ttd, F_REVERSE); } } } } } fclose(fp); #else #endif /* free any still-unused cache structs */ while (spares) { struct crec *tmp = spares->next; safe_free(spares); spares = tmp; } }
TEST(filesystem, canonicalise) { EXPECT_TRUE(!canonicalise("test_cypress_util").empty()); EXPECT_EQ('/', canonicalise("test_cypress_util")[0]); }