int python_initialize() { PyObject *ekg, *ekg_config; /* PyImport_ImportModule spodziewa siê nazwy modu³u, który znajduje * siê w $PYTHONPATH, wiêc dodajemy tam katalog ~/.gg/scripts. mo¿na * to zrobiæ w bardziej elegancki sposób, ale po co komplikowaæ sobie * ¿ycie? * * Argument putenv() nie jest zwalniany xfree(), bo powoduje to * problemy na systemach, w których putenv() jest zgodne z SUSv2 (np * niektóre SunOS). */ if (getenv("PYTHONPATH")) { char *tmp = saprintf("%s:%s", getenv("PYTHONPATH"), prepare_path("scripts", 0)); #ifdef HAVE_SETENV setenv("PYTHONPATH", tmp, 1); #else { char *s = saprintf("PYTHONPATH=%s", tmp); putenv(s); } #endif xfree(tmp); } else { #ifdef HAVE_SETENV setenv("PYTHONPATH", prepare_path("scripts", 0), 1); #else { char *s = saprintf("PYTHONPATH=%s", prepare_path("scripts", 0)); putenv(s); } #endif } Py_Initialize(); PyImport_AddModule("ekg"); if (!(ekg = Py_InitModule("ekg", ekg_methods))) return -1; PyModule_AddStringConstant(ekg, "version", VERSION); ekg_config = PyObject_NEW(PyObject, &ekg_config_type); PyModule_AddObject(ekg, "config", ekg_config); return 0; }
static int prepare_data(PyObject *obj, xorn_obtype_t *type_return, const void **data_return) { if (PyObject_TypeCheck(obj, &ArcType)) prepare_arc((Arc *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &BoxType)) prepare_box((Box *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &CircleType)) prepare_circle((Circle *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &ComponentType)) prepare_component((Component *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &LineType)) prepare_line((Line *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &NetType)) prepare_net((Net *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &PathType)) prepare_path((Path *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &PictureType)) prepare_picture((Picture *)obj, type_return, data_return); else if (PyObject_TypeCheck(obj, &TextType)) prepare_text((Text *)obj, type_return, data_return); else return -1; return 0; }
int open(const unsigned char *path, int modeflags) { unsigned char path2[MAX_PATH]; FSInterface *fs=NULL; BlockDeviceIF *device=NULL; prepare_path(path, (unsigned char*)path2, sizeof(path2)); fs_vfs_get((char*) path2, &fs, &device); FSFile *file = kmalloc(sizeof(FSFile)); memset(file, 0, sizeof(*file)); _fs_file_init(file); file->fs = fs; file->device = device; file->access = modeflags; file->internalfd = file->fs->open(file->fs, file->device, path2, strlen(path2), modeflags); if (file->internalfd < 0) { kfree(file); return -1; } LinkNode *node = kmalloc(sizeof(LinkNode)); node->data = file; Process *p = process_get_current(0); klist_append(&p->open_files, node); return (int)file; }
int stat(const char *path, struct stat *out) { unsigned char path2[MAX_PATH]; FSInterface *fs=NULL; BlockDeviceIF *device=NULL; if (!out || !path) return -1; prepare_path(path, (unsigned char*)path2, sizeof(path2)); fs_vfs_get((char*) path2, &fs, &device); klock_lock(&device->lock); klock_lock(&fs->lock); int inode = fs->path_to_inode(fs, device, path2, strlen(path2)); //e9printf("PATH: %s, PATH2: %s, INODE: %d\n", path, path2, inode); if (inode < 0) { klock_unlock(&fs->lock); klock_unlock(&device->lock); return -1; } int ret = fs->stat(fs, device, inode, out); //e9printf("RET: %d, st_mode: %d\n", ret, out->st_mode); klock_unlock(&fs->lock); klock_unlock(&device->lock); return ret; }
/* * emoticon_read() * * ³aduje do listy wszystkie makra z pliku ~/.gg/emoticons * format tego pliku w dokumentacji. * * 0/-1 */ int emoticon_read() { const char *filename; char *buf, **emot; FILE *f; if (!(filename = prepare_path("emoticons", 0))) return -1; if (!(f = fopen(filename, "r"))) return -1; while ((buf = read_file(f))) { if (buf[0] == '#') { xfree(buf); continue; } emot = array_make(buf, "\t", 2, 1, 1); if (array_count(emot) == 2) emoticon_add(emot[0], emot[1]); array_free(emot); xfree(buf); } fclose(f); return 0; }
static DIR_ITER* smb_diropen_r(struct _reent *re, DIR_ITER *dirState, const char *dirname) { FHANDLE *ret = NULL; FHANDLE *fh = (FHANDLE*) (dirState->dirStruct); register int r = 0; PathInformation_t info; memset(fh, 0, sizeof (FHANDLE)); if (!dirname) { re->_errno = ENOENT; return NULL; } if ((UID == -1) || (TID == -1)) { re->_errno = EINVAL; return NULL; } char *path = prepare_path((char *) dirname, smb_curpath, 4096); smb_io_lock(); // test if the dir exists r = smb_QueryPathInformation(UID, TID, (PathInformation_t *) & info, path); if (r < 0) { ret = NULL; re->_errno = EIO; goto io_unlock; } if (!(info.FileAttributes & EXT_ATTR_DIRECTORY)) { ret = NULL; re->_errno = ENOTDIR; goto io_unlock; } if (fh) { fh->mode = O_DIROPEN; fh->filesize = 0; fh->position = 0; fh->smb_fid = -1; strncpy(fh->name, path, 255); if (fh->name[strlen(fh->name) - 1] != '\\') strcat(fh->name, "\\"); strcat(fh->name, "*"); ret = fh; } else { ret = NULL; re->_errno = EMFILE; } io_unlock: smb_io_unlock(); return (DIR_ITER*) ret; }
ErrnoError copy_file(const std::string& pathFrom, const std::string& pathTo) { if (pathFrom.empty()) { return make_error_perror("copy_file", EINVAL); } if (pathTo.empty()) { return make_error_perror("copy_file", EINVAL); } std::string pr_from = prepare_path(pathFrom); std::string pr_to = prepare_path(pathTo); int cpRes = cp(pr_from.c_str(), pr_to.c_str()); bool result = cpRes != ERROR_RESULT_VALUE; if (!result) { return make_error_perror("cp", errno); } return ErrnoError(); }
/* * msg_queue_write() * * zapisuje niedostarczone wiadomo¶ci na dysku. * * 0/-1 */ int msg_queue_write() { const char *path; list_t l; int num = 0; if (!msg_queue) return -1; path = prepare_path("queue", 1); if (mkdir(path, 0700) && errno != EEXIST) return -1; for (l = msg_queue; l; l = l->next) { struct msg_queue *m = l->data; char *fn; FILE *f; int i; /* nie zapisujemy wiadomo¶ci, które za³apa³y siê do wysy³ki */ if (m->msg_seq != -1) continue; fn = saprintf("%s/%ld.%d", path, (long) m->time, num++); if (!(f = fopen(fn, "w"))) { xfree(fn); continue; } fprintf(f, "%d\n%d\n%d\n", m->msg_class, m->msg_seq, m->uin_count); for (i = 0; i < m->uin_count; i++) fprintf(f, "%d\n", m->uins[i]); fprintf(f, "%d\n%ld\n%d\n", m->secure, (long) m->time, m->formatlen); if (m->formatlen) { for (i = 0; i < m->formatlen; i++) fprintf(f, "%c", m->format[i]); fprintf(f, "\n"); } fprintf(f, "%s", m->msg); fclose(f); chmod(fn, 0600); xfree(fn); } return 0; }
static int smb_open_r(struct _reent *re, void *fileStruct, const char *filename, int mode, int flags) { FHANDLE *fh = (FHANDLE*) fileStruct; register int r = -1; s64 filesize; if (!filename) { re->_errno = ENOENT; return -1; } if ((UID == -1) || (TID == -1)) { re->_errno = EINVAL; return -1; } char *path = prepare_path((char *) filename, smb_curpath, 4096); smb_io_lock(); if (fh) { r = smb_OpenAndX(UID, TID, path, &filesize, mode); if (r < 0) { if (r == -1) { re->_errno = EIO; } else if (r == -2) { re->_errno = EPERM; } else if (r == -3) { re->_errno = ENOENT; } else { re->_errno = EIO; } r = -1; } else { fh->smb_fid = r; fh->mode = mode; fh->filesize = filesize; fh->position = 0; if (fh->mode & O_TRUNC) fh->filesize = 0; else if (fh->mode & O_APPEND) fh->position = filesize; strncpy(fh->name, path, 256); r = 0; } } else { re->_errno = EMFILE; r = -1; } smb_io_unlock(); return (r == 0) ? (int) fh : -1; }
DIR *opendir(const unsigned char *path) { if (!path) return NULL; unsigned char path2[MAX_PATH]; FSInterface *fs=NULL; BlockDeviceIF *device=NULL; prepare_path(path, (unsigned char*)path2, sizeof(path2)); fs_vfs_get((char*) path2, &fs, &device); if (bad_fsdev(fs, device)) { kprintf("File system driver corruption! 1\n"); return NULL; } if (!fs->opendir_inode) { kprintf("opendir not supported"); return NULL; } klock_lock(&device->lock); klock_lock(&fs->lock); int inode = fs->path_to_inode(fs, device, path2, strlen(path2)); if (inode < 0) { klock_unlock(&fs->lock); klock_unlock(&device->lock); return NULL; } //allocate dirent and DIR in a single block DIR *ret = NULL; uintptr_t addr = (uintptr_t) kmalloc(sizeof(DIR) + sizeof(dirent)); addr += sizeof(dirent); ret = (DIR*)addr; memset(ret, 0, sizeof(ret)); fs->opendir_inode(fs, device, ret, inode); klock_unlock(&fs->lock); klock_unlock(&device->lock); FSFile *dirfile = kmalloc(sizeof(FSFile)); memset(dirfile, 0, sizeof(*dirfile)); dirfile->fs = fs; dirfile->device = device; ret->dirfd = (int)dirfile; return ret; }
ErrnoError move_file(const std::string& pathFrom, const std::string& pathTo) { if (pathFrom.empty()) { return make_error_perror("move_file", EINVAL); } if (pathTo.empty()) { return make_error_perror("move_file", EINVAL); } std::string pr_from = prepare_path(pathFrom); std::string pr_to = prepare_path(pathTo); #ifdef OS_WIN WINBOOL res = MoveFileExA(pr_from.c_str(), pr_to.c_str(), MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING); bool result = res != 0; #else int res = rename(pr_from.c_str(), pr_to.c_str()); bool result = res != ERROR_RESULT_VALUE; #endif if (!result) { return make_error_perror("rename", errno); } return ErrnoError(); }
void python_autorun() { const char *path = prepare_path("scripts/autorun", 0); struct dirent *d; struct stat st; char *tmp; DIR *dir; if (!(dir = opendir(path))) return; /* nale¿y utworzyæ plik ~/.gg/scripts/autorun/__init__.py, inaczej * python nie bêdzie mo¿na ³adowaæ skryptów przez ,,autorun.nazwa'' */ tmp = saprintf("%s/__init__.py", path); if (stat(tmp, &st)) { FILE *f = fopen(tmp, "w"); if (f) fclose(f); } xfree(tmp); while ((d = readdir(dir))) { tmp = saprintf("%s/%s", path, d->d_name); if (stat(tmp, &st) || S_ISDIR(st.st_mode)) { xfree(tmp); continue; } xfree(tmp); if (!strcmp(d->d_name, "__init__.py")) continue; if (strlen(d->d_name) < 3 || strcmp(d->d_name + strlen(d->d_name) - 3, ".py")) continue; tmp = saprintf("autorun.%s", d->d_name); tmp[strlen(tmp) - 3] = 0; python_load(tmp, 0); xfree(tmp); } closedir(dir); }
ErrnoError remove_file(const std::string& file_path) { if (file_path.empty()) { return make_error_perror("remove_file", EINVAL); } std::string pr_to = prepare_path(file_path); int res = remove(pr_to.c_str()); bool result = res != ERROR_RESULT_VALUE; if (!result && errno != ENOENT) { return make_error_perror("remove", errno); } return ErrnoError(); }
ErrnoError remove_directory(const std::string& path, bool is_recursive) { if (path.empty()) { return make_error_perror("remove_directory", EINVAL); } std::string pr_path = prepare_path(path); if (pr_path[pr_path.length() - 1] == get_separator<char>()) { pr_path[pr_path.length() - 1] = 0; } const char* pr_path_ptr = pr_path.c_str(); if (is_recursive) { DIR* dirp = opendir(pr_path_ptr); if (!dirp) { return ErrnoError(); } struct dirent* p; while ((p = readdir(dirp)) != nullptr) { /* Skip the names "." and ".." as we don't want to recurse on them. */ if (!strcmp(p->d_name, ".") || !strcmp(p->d_name, "..")) { continue; } char pathBuffer[PATH_MAX] = {0}; SNPrintf(pathBuffer, sizeof(pathBuffer), "%s/%s", path, p->d_name); struct stat statbuf; if (!::stat(pathBuffer, &statbuf)) { if (S_ISDIR(statbuf.st_mode)) { ErrnoError err = remove_directory(pathBuffer, is_recursive); if (err) { closedir(dirp); return err; } } else { ErrnoError err = remove_file(pathBuffer); if (err) { closedir(dirp); return err; } } } } closedir(dirp); } return do_rmdir_directory(pr_path_ptr); }
ErrnoError node_access(const std::string& node) { if (node.empty()) { return make_error_perror("node_access", EINVAL); } std::string pr_node = prepare_path(node); if (pr_node.empty()) { return make_error_perror("node_access", EINVAL); } int res = access(pr_node.c_str(), W_OK); if (res == ERROR_RESULT_VALUE) { return make_error_perror("access", errno); } return ErrnoError(); }
static int smb_stat_r(struct _reent *re, const char *filename, struct stat *st) { register int r; PathInformation_t info; if (!filename) { re->_errno = ENOENT; return -1; } if ((UID == -1) || (TID == -1)) { re->_errno = EINVAL; return -1; } char *path = prepare_path((char *) filename, smb_curpath, 4096); smb_io_lock(); memset((void *) st, 0, sizeof (struct stat)); r = smb_QueryPathInformation(UID, TID, (PathInformation_t *) & info, path); if (r < 0) { re->_errno = EIO; r = -1; goto io_unlock; } // 64 bit :s st->st_ctime = info.Created; st->st_atime = info.LastAccess; st->st_mtime = info.Change; st->st_size = (int) (info.EndOfFile & 0xffffffff); //stat->st_size = (int) ((info.EndOfFile >> 32) & 0xffffffff); if (info.FileAttributes & EXT_ATTR_DIRECTORY) st->st_mode |= S_IFDIR; else st->st_mode |= S_IFREG; r = 0; io_unlock: smb_io_unlock(); return r; }
mm_file_io_c::mm_file_io_c(const std::string &path, const open_mode mode) : m_file_name(path) , m_file(NULL) , m_eof(false) { DWORD access_mode, share_mode, disposition; switch (mode) { case MODE_READ: access_mode = GENERIC_READ; share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE; disposition = OPEN_EXISTING; break; case MODE_WRITE: access_mode = GENERIC_WRITE | GENERIC_READ; share_mode = FILE_SHARE_READ; disposition = OPEN_EXISTING; break; case MODE_SAFE: access_mode = GENERIC_WRITE | GENERIC_READ; share_mode = FILE_SHARE_READ; disposition = OPEN_ALWAYS; break; case MODE_CREATE: access_mode = GENERIC_WRITE; share_mode = FILE_SHARE_READ; disposition = CREATE_ALWAYS; break; default: throw mtx::invalid_parameter_x(); } if ((MODE_WRITE == mode) || (MODE_CREATE == mode)) prepare_path(path); m_file = (void *)CreateFileUtf8(path.c_str(), access_mode, share_mode, NULL, disposition, 0, NULL); if ((HANDLE)m_file == (HANDLE)0xFFFFFFFF) throw mtx::mm_io::open_x(); m_dos_style_newlines = true; }
ErrnoError create_directory(const std::string& path, bool is_recursive) { if (path.empty()) { return make_error_perror("create_directory", EINVAL); } std::string pr_path = prepare_path(path); if (pr_path.empty()) { return make_error_perror("create_directory", EINVAL); } pr_path = stable_dir_path(pr_path); if (pr_path.empty()) { return make_error_perror("create_directory", EINVAL); } const char* pr_path_ptr = pr_path.c_str(); if (is_recursive) { char* p = nullptr; #ifdef OS_WIN uint8_t shift = 3; #else uint8_t shift = 1; #endif for (p = const_cast<char*>(pr_path_ptr + shift); *p; p++) { if (*p == get_separator<char>()) { *p = 0; const char* path = pr_path_ptr; if (!is_directory_exist(path)) { ErrnoError err = do_create_directory(path); if (err) { return err; } } *p = get_separator<char>(); } } } return do_create_directory(pr_path_ptr); }
mm_file_io_c::mm_file_io_c(const std::string &path, const open_mode mode) : m_file_name(path) , m_file(NULL) { const char *cmode; switch (mode) { case MODE_READ: cmode = "rb"; break; case MODE_WRITE: cmode = "r+b"; break; case MODE_CREATE: cmode = "w+b"; break; case MODE_SAFE: cmode = "rb"; break; default: throw mtx::invalid_parameter_x(); } if ((MODE_WRITE == mode) || (MODE_CREATE == mode)) prepare_path(path); std::string local_path = g_cc_local_utf8->native(path); struct stat st; if ((0 == stat(local_path.c_str(), &st)) && S_ISDIR(st.st_mode)) throw mtx::mm_io::open_x(); m_file = (FILE *)fopen(local_path.c_str(), cmode); if (NULL == m_file) throw mtx::mm_io::open_x(); }
/* * python_load() * * ³aduje skrypt pythona o podanej nazwie z ~/.gg/scripts * * - name - nazwa skryptu, * - quiet. * * 0/-1 */ int python_load(const char *name, int quiet) { PyObject *module, *init; struct module m; char *name2; if (!name) { printq("python_need_name"); return -1; } if (strchr(name, '/')) { printq("python_wrong_location", prepare_path("scripts", 0)); return -1; } name2 = xstrdup(name); if (strlen(name2) > 3 && !strcasecmp(name2 + strlen(name2) - 3, ".py")) name2[strlen(name2) - 3] = 0; module = PyImport_ImportModule(name2); if (!module) { printq("python_not_found", name2); PyErr_Print(); xfree(name2); return -1; } if ((init = PyObject_GetAttrString(module, "init"))) { if (PyCallable_Check(init)) { PyObject *result = PyObject_CallFunction(init, "()"); if (result) { int resulti = PyInt_AsLong(result); if (!resulti) { } Py_XDECREF(result); } } Py_XDECREF(init); } memset(&m, 0, sizeof(m)); m.name = xstrdup(name2); m.module = module; m.deinit = python_get_func(module, "deinit"); m.handle_msg = python_get_func(module, "handle_msg"); m.handle_msg_own = python_get_func(module, "handle_msg_own"); m.handle_connect = python_get_func(module, "handle_connect"); m.handle_disconnect = python_get_func(module, "handle_disconnect"); m.handle_status = python_get_func(module, "handle_status"); m.handle_status_own = python_get_func(module, "handle_status_own"); m.handle_redraw_header = python_get_func(module, "handle_redraw_header"); m.handle_redraw_statusbar = python_get_func(module, "handle_redraw_statusbar"); m.handle_keypress = python_get_func(module, "handle_keypress"); m.handle_command_line = python_get_func(module, "handle_command_line"); PyErr_Clear(); list_add(&modules, &m, sizeof(m)); xfree(name2); return 0; }
/* * msg_queue_read() * * wczytuje kolejkê niewys³anych wiadomo¶ci z dysku. * * 0/-1 */ int msg_queue_read() { const char *path; struct dirent *d; DIR *dir; path = prepare_path("queue", 0); if (!(dir = opendir(path))) return -1; while ((d = readdir(dir))) { struct msg_queue m; struct stat st; string_t msg; char *fn, *buf; FILE *f; int i; fn = saprintf("%s/%s", path, d->d_name); if (stat(fn, &st) || !S_ISREG(st.st_mode)) { xfree(fn); continue; } if (!(f = fopen(fn, "r"))) { xfree(fn); continue; } memset(&m, 0, sizeof(m)); fscanf(f, "%d\n", &m.msg_class); fscanf(f, "%d\n", &m.msg_seq); fscanf(f, "%d\n", &m.uin_count); /* jaki¶ zdrowy limit */ if (m.uin_count < 1 || m.uin_count > 100) { fclose(f); xfree(fn); continue; } m.uins = xcalloc(m.uin_count, sizeof(uin_t)); for (i = 0; i < m.uin_count; i++) fscanf(f, "%d\n", &m.uins[i]); fscanf(f, "%d\n", &m.secure); fscanf(f, "%ld\n", (long *) &m.time); fscanf(f, "%d\n", &m.formatlen); /* dziwny plik? */ if (!m.time || !m.msg_seq || !m.msg_class) { fclose(f); xfree(fn); xfree(m.uins); continue; } if (m.formatlen) { m.format = xcalloc(m.formatlen, sizeof(unsigned char)); for (i = 0; i < m.formatlen; i++) fscanf(f, "%c", &m.format[i]); fscanf(f, "%*c"); } else m.format = NULL; msg = string_init(NULL); buf = read_file(f); while (buf) { string_append(msg, buf); xfree(buf); buf = read_file(f); if (buf) string_append(msg, "\r\n"); } m.msg = msg->str; string_free(msg, 0); fclose(f); list_add(&msg_queue, &m, sizeof(m)); unlink(fn); xfree(fn); } closedir(dir); return 0; }