void flush(void) { // TODO: flush() can later return some info about the flushed data, e.g. size if (!this->headersSet) { this->flushHeaders(); } xbuf_cat(this->httpOut, (char *) this->payload.c_str()); u32toa(this->httpOut->ptr + this->ptrHeaderSize, this->httpOut->len - this->headersSize); }
/** * If the specified inode entry is a dummy directory, this function moves * all its children to the lost+found directory. * * @param inode_entry The parent inode to test and empty. * * @return 0 on success; nonzero on failure. */ static int nffs_restore_migrate_orphan_children(struct nffs_inode_entry *inode_entry) { struct nffs_inode_entry *lost_found_sub; struct nffs_inode_entry *child_entry; char buf[32]; int rc; if (!nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) { /* Not a directory. */ return 0; } if (inode_entry->nie_refcnt != 0) { /* Not a dummy. */ return 0; } if (SLIST_EMPTY(&inode_entry->nie_child_list)) { /* No children to migrate. */ return 0; } /* Create a directory in lost+found to hold the dummy directory's * contents. */ strcpy(buf, "/lost+found/"); u32toa(&buf[strlen(buf)], inode_entry->nie_hash_entry.nhe_id); rc = nffs_path_new_dir(buf, &lost_found_sub); if (rc != 0 && rc != FS_EEXIST) { return rc; } /* Move each child into the new subdirectory. */ while ((child_entry = SLIST_FIRST(&inode_entry->nie_child_list)) != NULL) { rc = nffs_inode_rename(child_entry, lost_found_sub, NULL); if (rc != 0) { return rc; } } return 0; }
// ---------------------------------------------------------------------------- // imported functions: // get_reply(): get a pointer on the 'reply' dynamic buffer from the server // getus(): get current time in microseconds (1 millisecond = 1,000 us) // get_env(): get connection's 'environment' variables from the server // xbuf_cat(): like strcat(), but it works in the specified dynamic buffer // gif_build(): build an in-memory GIF image from a bitmap and palette // ---------------------------------------------------------------------------- int main(int argc, char *argv[]) { // ------------------------------------------------------------------------- // get a pointer on the server reply // ------------------------------------------------------------------------- xbuf_t *reply = get_reply(argv); // ------------------------------------------------------------------------- // allocate memory for a raw bitmap // ------------------------------------------------------------------------- int w = 800, h = 800, nbcolors = 256, wXh = w * h; u8 *bmp = (u8*)malloc(wXh); if(!bmp) return 503; // service unavailable // ------------------------------------------------------------------------- // render the Mandelbrot set in our bitmap // ------------------------------------------------------------------------- fractals(bmp, w, h, nbcolors); // ------------------------------------------------------------------------- // display the palette (useful when playing with 'tabcol[]' values) // ------------------------------------------------------------------------- { #define ROUND(a) ((a) > 0 ? (int)((a)+0.5) : -(int)(0.5-(a))) u8 *p = bmp; int i = h, wd20 = w / 20; float color = 0, col = (float)nbcolors / (float)h; while(i--) { color += col; memset(p, ROUND(color) & 255, wd20); p += w; } } // ------------------------------------------------------------------------- // build a smooth multi-gradient color palette from the fixed values below // ------------------------------------------------------------------------- static rgb_t tabcol[]={ { 0, 0, 128}, // Med. Blue { 0, 100, 200}, // Light Blue {100, 160, 160}, // Cyan { 0, 220, 100}, // Green {255, 255, 0}, // Yellow {255, 128, 0}, // Orange {128, 0, 0}, // Med. Red { 64, 0, 0}, // Dark Red {128, 0, 0}, // Med. Red {255, 128, 0}, // Orange {255, 255, 0}, // Yellow { 0, 220, 100}, // Green {100, 160, 160}, // Cyan { 0, 100, 200}, // Light Blue { 0, 0, 128}, // Med. Blue { 64, 0, 0}, // Dark Red {128, 0, 0}, // Med. Red {255, 128, 0}, // Orange {255, 255, 0}, // Yellow { 0, 220, 100}, // Green {100, 160, 160}, // Cyan { 0, 100, 200}, // Light Blue { 0, 0, 128}, // Med. Blue { 0, 0, 64}, // Dark Blue }, *tab = tabcol; // ------------------------------------------------------------------------- // just for fun, select different colors for each call // ------------------------------------------------------------------------- static u32 call = 0; u32 ncols = sizeof(tabcol) / sizeof(rgb_t); switch(call) { case 0: tab = tabcol; ncols = 10; break; // blue case 1: tab = &tabcol[ 4]; ncols = 10; break; // yellow case 2: tab = &tabcol[ 7]; ncols = 7; break; // dark red case 3: tab = &tabcol[ 1]; ncols = 16; break; // rainbow - } call = (call + 1) & 3; // generate the palette with our defined gradient steps u8 pal[768]; dr_gradient(pal, nbcolors, tab, ncols); // nice palete but we want a black body to delimit the mandelbrot set memset(pal + ((nbcolors - (nbcolors / 16)) * 3), 0, (nbcolors / 16) * 3); // ------------------------------------------------------------------------- // create custom HTTP response headers to send a GIF file // ------------------------------------------------------------------------- // (G-WAN automatically generates headers if none are provided but it can't // guess all MIME types so this automatic feature is for 'text/html' only // ...unless you explicitly specify the reply buffer MIME type) #ifdef BUILD_CUSTOM_HEADERS // old way of doing things // get the current HTTP date (like "Wed, 02 Jun 2010 06:49:37 GMT") u8 *date = (u8*)get_env(argv, SERVER_DATE); xbuf_xcat(reply, "HTTP/1.1 200 OK\r\n" "Date: %s\r\n" "Last-Modified: %s\r\n" "Content-type: image/gif\r\n" "Content-Length: \r\n" // make room for the for GIF length "Connection: close\r\n\r\n", date, date); // ------------------------------------------------------------------------- // make sure that we have enough space in the 'reply' buffer // (we are going to fill it directly from gif_build(), not via xbuf_xxx) // ------------------------------------------------------------------------- // (if we have not enough memory, we will get a 'graceful' crash) if(reply->allocated < (wXh / 10)) // very gross approximation { if(!xbuf_growto(reply, wXh / 10)) // resize reply { xbuf_init(reply); xbuf_ncat(reply, " ", 1); reply->len = 0; // discart pointless data, keep allocated memory return 503; // error: we could not allocate enough memory } } // ------------------------------------------------------------------------- // save the place where to patch the void 'Content-Length' HTTP Header // ------------------------------------------------------------------------- char *p = reply->ptr + reply->len - (sizeof("\r\nConnection: close\r\n\r\n") - 1); // ------------------------------------------------------------------------- // append a GIF image (-1:no transparency, 0: no comment) to 'reply' // ------------------------------------------------------------------------- int len = gif_build((u8*)(reply->ptr + reply->len), bmp, w, h, pal, nbcolors, -1, 0); if(len < 0) len = 0; // (len == -1) if gif_build() failed reply->len += len; // add the GIF size to the 'reply' buffer length free(bmp); // ------------------------------------------------------------------------- // store the GIF size in the empty space of the 'Content-Length' header // ------------------------------------------------------------------------- u32toa(p, len); #else // #ifdef BUILD_CUSTOM_HEADERS // works with any supported MIME type // ------------------------------------------------------------------------- // specify a MIME type so we don't have to build custom HTTP headers // ------------------------------------------------------------------------- char *mime = (char*)get_env(argv, REPLY_MIME_TYPE); // note that we setup the FILE EXTENTION, not the MIME type: mime[0] = '.'; mime[1] = 'g'; mime[2] = 'i'; mime[3] = 'f'; mime[4] = 0; // ------------------------------------------------------------------------- // make sure that we have enough space in the 'reply' buffer // (we are going to fill it directly from gif_build(), not via xbuf_xxx) // ------------------------------------------------------------------------- // (if we have not enough memory, we will get a 'graceful' crash) if(reply->allocated < (wXh / 10)) // very gross approximation { if(!xbuf_growto(reply, wXh / 10)) // resize reply { xbuf_init(reply); xbuf_ncat(reply, " ", 1); reply->len = 0; // discart pointless data, keep allocated memory return 503; // error: we could not allocate enough memory } } // ------------------------------------------------------------------------- // append a GIF image (-1:no transparency, 0: no comment) to 'reply' // ------------------------------------------------------------------------- int len = gif_build((u8*)(reply->ptr + reply->len), bmp, w, h, pal, nbcolors, -1, 0); if(len < 0) len = 0; // (len == -1) if gif_build() failed reply->len += len; // add the GIF size to the 'reply' buffer length free(bmp); #endif // #else #ifdef BUILD_CUSTOM_HEADERS return 200; // return an HTTP code (200:'OK') }